public virtual void PerformCommandTest() { PersistentTcpConnection conn = CreateConnection("test"); try { PersistentTcpConnection_Accessor accessor = GetConnectionAccessor(conn); // assign mock socket accessor.Socket = new ClientSocketMock(); accessor.Socket.Open(); XmlFormatterFactoryMock factory = new XmlFormatterFactoryMock(); accessor.FormatterFactory = factory; IBinaryWriter writer = factory.CreateWriter(accessor.DataStream); // command id and version const ServerCommand id = ServerCommand.Status; const short ver = 1; MockCommand command = new MockCommand(conn, id, ver); accessor.PerformCommand(command); StreamAdapter_Accessor streamAccessor = GetStreamAccessor((StreamAdapter)accessor.DataStream); streamAccessor.Stream.Seek(0, SeekOrigin.Begin); IBinaryReader reader = factory.CreateReader(accessor.DataStream); // read & check data sent by mock command code ServerCommand actualId = (ServerCommand)reader.ReadInt16(); Assert.AreEqual(id, actualId); short actualVer = reader.ReadInt16(); Assert.AreEqual(ver, actualVer); } finally { conn.Close(); } }
public virtual void PerformCommandTest() { ConnectionBase conn = CreateConnection("test"); try { TcpConnection_Accessor accessor = GetConnectionAccessor(conn); // assign mock socket accessor.Socket = new ClientSocketMock(); accessor.Socket.Open(); XmlFormatterFactoryMock factory = new XmlFormatterFactoryMock(); accessor.FormatterFactory = factory; IBinaryWriter writer = factory.CreateWriter(accessor.DataStream); // preserialize server protocol version and rewind to start pos. (emulate first stage of handshake procedure) int protocolVersion = TcpConnection_Accessor.MAJOR_PROTOCOL_VERSION; writer.Write(protocolVersion); StreamAdapter_Accessor streamAccessor = GetStreamAccessor((StreamAdapter)accessor.DataStream); long pos = streamAccessor.Stream.Position; streamAccessor.Stream.Seek(0, SeekOrigin.Begin); // command id and version const ServerCommand id = ServerCommand.Status; const short ver = 1; MockCommand command = new MockCommand(conn, id, ver); accessor.PerformCommand(command); accessor.Socket.Open(); streamAccessor.Stream.Seek(pos, SeekOrigin.Begin); IBinaryReader reader = factory.CreateReader(accessor.DataStream); // skip protocol version sent by hadshake method int clientProtocol = reader.ReadInt32(); // read & check data sent by mock command code ServerCommand actualId = (ServerCommand)reader.ReadInt16(); Assert.AreEqual(id, actualId); short actualVer = reader.ReadInt16(); Assert.AreEqual(ver, actualVer); } finally { conn.Close(); } }
public virtual void SendHandshakeTest() { PersistentTcpConnection conn = CreateConnection("test"); try { PersistentTcpConnection_Accessor accessor = GetConnectionAccessor(conn); // assign mock socket and open fake stream accessor.Socket = new ClientSocketMock(); // underlying socket sholud be already opened accessor.Socket.Open(); // assign mock binary formatter factory XmlFormatterFactoryMock factory = new XmlFormatterFactoryMock(); accessor.FormatterFactory = factory; IBinaryWriter writer = factory.CreateWriter(accessor.DataStream); // preserialize server protocol version and rewind to start pos. (emulate first stage of handshake procedure) int protocolVersion = TcpConnection_Accessor.MAJOR_PROTOCOL_VERSION; writer.Write(protocolVersion); StreamAdapter_Accessor streamAccessor = GetStreamAccessor((StreamAdapter)accessor.DataStream); long pos = streamAccessor.Stream.Position; streamAccessor.Stream.Seek(0, SeekOrigin.Begin); accessor.SendHandshake(); // restore stream pos streamAccessor.Stream.Seek(pos, SeekOrigin.Begin); IBinaryReader reader = factory.CreateReader(accessor.DataStream); // read client response int actual = reader.ReadInt32(); Assert.AreEqual(actual, protocolVersion); // read persistent connection command id and version ServerCommand id = (ServerCommand)reader.ReadInt16(); Assert.AreEqual(ServerCommand.Persist, id); short ver = reader.ReadInt16(); Assert.AreEqual(PersistentTcpConnection_Accessor.PERSIST_COMMAND_VERSION, ver); // read command length & body int length = reader.ReadInt32(); Assert.AreEqual(sizeof(int), length); int body = reader.ReadInt32(); Assert.AreEqual(PersistentTcpConnection_Accessor.PERSIST_COMMAND_BODY, body); } finally { conn.Close(); } }
public virtual void SendHandshakeTest() { ConnectionBase conn = CreateConnection("test"); try { TcpConnection_Accessor accessor = GetConnectionAccessor(conn); // assign mock socket and open fake stream accessor.Socket = new ClientSocketMock(); // underlying socket sholud be already opened accessor.Socket.Open(); // assign mock binary formatter factory XmlFormatterFactoryMock factory = new XmlFormatterFactoryMock(); accessor._formatterFactory = factory; IBinaryWriter writer = factory.CreateWriter(accessor.DataStream); // preserialize server reponse (protocol version) and rewind to start pos. (emulate first stage of handshake procedure) int protocolVersion = TcpConnection_Accessor.MAJOR_PROTOCOL_VERSION; writer.Write(protocolVersion); StreamAdapter_Accessor streamAccessor = GetStreamAccessor((StreamAdapter)accessor.DataStream); long pos = streamAccessor.Stream.Position; streamAccessor.Stream.Seek(0, SeekOrigin.Begin); accessor.SendHandshake(); // restore stream pos streamAccessor.Stream.Seek(pos, SeekOrigin.Begin); IBinaryReader reader = factory.CreateReader(accessor.DataStream); // read client response int actual = reader.ReadInt32(); Assert.AreEqual(actual, protocolVersion); } finally { conn.Close(); } }