Beispiel #1
0
        public void TestEventLoopSendAndClose(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);

            int calledCount = 0;

            // This OnNetworkAction asks for more data, creating an event loop
            testLocalSocketState.OnNetworkAction = (x) =>
            {
                if (x.ErrorOccured)
                {
                    return;
                }
                calledCount++;
                Networking.GetData(x);
            };

            Networking.SendAndClose(testRemoteSocketState.TheSocket, "a");
            Assert.IsFalse(Networking.SendAndClose(testRemoteSocketState.TheSocket, "a"));
            Assert.IsFalse(testRemoteSocketState.TheSocket.Connected);
            Networking.GetData(testLocalSocketState);
            Assert.AreEqual("a", testLocalSocketState.GetData());
            NetworkTestHelper.WaitForOrTimeout(() => calledCount == 1, NetworkTestHelper.timeout);

            Networking.SendAndClose(testLocalSocketState.TheSocket, "a");
            NetworkTestHelper.WaitForOrTimeout(() => calledCount == 2, NetworkTestHelper.timeout);

            Assert.AreEqual(1, calledCount);
        }
Beispiel #2
0
        public void TestCloseThenSendAndClose(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState, 2151);

            testLocalSocketState.TheSocket.Close();

            Assert.IsFalse(Networking.SendAndClose(testLocalSocketState.TheSocket, "a"));
        }
Beispiel #3
0
        public void TestSendAndClose(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState, 2151);

            Assert.IsTrue(Networking.SendAndClose(testLocalSocketState.TheSocket, "a"));
            // No assertions, but the following should not result in an unhandled exception
            Assert.IsFalse(Networking.Send(testLocalSocketState.TheSocket, "a"));
        }
Beispiel #4
0
        public void testSendAndClose(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);

            // Set the action to do nothing
            testLocalSocketState.OnNetworkAction  = x => { };
            testRemoteSocketState.OnNetworkAction = x => { };

            Networking.SendAndClose(testLocalSocketState.TheSocket, "a");
            Assert.IsFalse(testLocalSocketState.TheSocket.Connected);
        }
Beispiel #5
0
        public void SendAndClose_SendFails_ShouldCloseSocket(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);
            testLocalSocketState.OnNetworkAction  = x => { };
            testRemoteSocketState.OnNetworkAction = x => { };

            string nullInjection     = null;
            bool   sendWasSuccessful = Networking.SendAndClose(testLocalSocketState.TheSocket, nullInjection);

            Networking.GetData(testRemoteSocketState);
            NetworkTestHelper.WaitForOrTimeout(() => testRemoteSocketState.GetData().Length > 0, NetworkTestHelper.timeout);

            Assert.IsFalse(sendWasSuccessful);
            Assert.AreEqual("", testRemoteSocketState.GetData());
            Assert.IsFalse(testLocalSocketState.TheSocket.Connected);
        }
Beispiel #6
0
        public void SendAndClose_SendTinyMessage_ShouldPutDataInSocketStateAndCloseSocket(bool clientSide)
        {
            // assemble
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);
            testLocalSocketState.OnNetworkAction  = x => { };
            testRemoteSocketState.OnNetworkAction = x => { };
            bool sendWasSuccessful;

            // act
            sendWasSuccessful = Networking.SendAndClose(testLocalSocketState.TheSocket, "abc");
            Networking.GetData(testRemoteSocketState);
            NetworkTestHelper.WaitForOrTimeout(() => testRemoteSocketState.GetData().Length > 0, NetworkTestHelper.timeout);

            // assert
            Assert.IsTrue(sendWasSuccessful);
            Assert.AreEqual("abc", testRemoteSocketState.GetData());
            Assert.IsFalse(testLocalSocketState.TheSocket.Connected);
        }
Beispiel #7
0
        public void SendAndClose_SocketIsAlreadyClosed_ShouldNotAttemptToSend(bool clientSide)
        {
            SetupTestConnections(clientSide, out testListener, out testLocalSocketState, out testRemoteSocketState);
            testLocalSocketState.OnNetworkAction  = x => { };
            testRemoteSocketState.OnNetworkAction = x => { };
            bool sendWasSuccessful = true;

            testLocalSocketState.TheSocket.Shutdown(SocketShutdown.Both);
            testLocalSocketState.TheSocket.Close();
            sendWasSuccessful = Networking.SendAndClose(testLocalSocketState.TheSocket, "abc");
            Networking.GetData(testRemoteSocketState);
            NetworkTestHelper.WaitForOrTimeout(() => testRemoteSocketState.GetData().Length > 0, NetworkTestHelper.timeout);

            Assert.IsFalse(sendWasSuccessful);
            Assert.AreEqual("", testRemoteSocketState.GetData());
            Assert.IsFalse(testLocalSocketState.TheSocket.Connected);
            // TODO is this check helpful?
            Assert.ThrowsException <ObjectDisposedException>(() => testLocalSocketState.TheSocket.Available);
        }
Beispiel #8
0
        public void TestMyOwn()
        {
            // List to represents connections
            List <SocketState> server = new List <SocketState>();

            void SaveServer(SocketState s)
            {
                lock (this)
                {
                    server.Add(s);
                }
            }

            //Two clients
            SocketState client_1 = null;

            void SaveClient_1(SocketState s)
            {
                client_1 = s;
            }

            SocketState client_2 = null;

            void SaveClient_2(SocketState s)
            {
                client_2 = s;
            }

            // Starting a server
            TcpListener listener = Networking.StartServer(SaveServer, 2112);

            // Client1 Connect to the server
            Networking.ConnectToServer(SaveClient_1, "localhost", 2112);

            // Client2 Connect to the server
            Networking.ConnectToServer(SaveClient_2, "localhost", 2112);

            // Make sure this line is what you want to it behave
            NetworkTestHelper.WaitForOrTimeout(() => (client_2 != null) && (client_1 != null) && (server.Count == 2), NetworkTestHelper.timeout);

            // Set the action to do nothing
            foreach (SocketState state in server)
            {
                state.OnNetworkAction = x => { };
            }

            client_1.OnNetworkAction = x => { };
            client_2.OnNetworkAction = x => { };

            foreach (SocketState state in server)
            {
                Networking.SendAndClose(state.TheSocket, "a");
            }

            Networking.GetData(client_1);
            Networking.GetData(client_2);


            // Note that waiting for data like this is *NOT* how the networking library is
            // intended to be used. This is only for testing purposes.
            // Normally, you would provide an OnNetworkAction that handles the data.
            NetworkTestHelper.WaitForOrTimeout(() => client_1.GetData().Length > 0 && client_2.GetData().Length > 0, NetworkTestHelper.timeout);

            Assert.AreEqual("a", client_1.GetData());
            Assert.AreEqual("a", client_2.GetData());

            //Make sure everything close and stop properly
            listener.Stop();
            foreach (SocketState state in server)
            {
                state.TheSocket.Close();
            }
            client_1.TheSocket.Close();
            client_2.TheSocket.Close();
        }