public virtual void OpenTest()
        {
            PersistentTcpConnection conn = CreateConnection("test");

            try
            {
                GetConnectionAccessor(conn).Socket = new ClientSocketMock();
                conn.Open();
                Assert.IsTrue(conn.IsConnected);
                conn.Close();
                Assert.IsFalse(conn.IsConnected);
            }
            finally
            {
                conn.Close();
            }
        }
예제 #2
0
 public virtual void ConnectToSphinx()
 {
     using (var connection = new PersistentTcpConnection(TestSettings.Default.Host, TestSettings.Default.Port))
     {
         connection.Open();
         Assert.IsTrue(connection.IsConnected);
         connection.Close();
         Assert.IsFalse(connection.IsConnected);
     }
 }
        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 DataStreamTest()
        {
            PersistentTcpConnection conn = CreateConnection("test");

            try
            {
                PersistentTcpConnection_Accessor accessor = GetConnectionAccessor(conn);
                accessor.Socket = new ClientSocketMock();
                Assert.IsNull(accessor.DataStream);
                conn.Open();
                Assert.IsNotNull(accessor.DataStream);
            }
            finally
            {
                conn.Close();
            }
        }
        public virtual void ProtectedDisposeTest()
        {
            PersistentTcpConnection conn = CreateConnection("test");

            try
            {
                PersistentTcpConnection_Accessor accessor = GetConnectionAccessor(conn);
                accessor.Socket = new ClientSocketMock();
                conn.Open();
                Assert.IsTrue(conn.IsConnected);
                accessor.Dispose(false);
                Assert.IsTrue(conn.IsConnected);
                accessor.Dispose(true);
                Assert.IsFalse(conn.IsConnected);
            }
            finally
            {
                conn.Close();
            }
        }
        public virtual void ConnectionTimeoutTest()
        {
            PersistentTcpConnection conn = CreateConnection("test");

            try
            {
                PersistentTcpConnection_Accessor accessor = GetConnectionAccessor(conn);
                accessor.Socket = new ClientSocketMock();
                const int timeout = 100;
                conn.ConnectionTimeout = timeout;
                Assert.AreEqual(timeout, conn.ConnectionTimeout);
                conn.Open();
                // wait "timeout" time and give timer little time to set new property value
                Thread.Sleep(timeout + 100);
                Assert.IsFalse(conn.IsConnected);
            }
            finally
            {
                conn.Close();
            }
        }
        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();
            }
        }