Ejemplo n.º 1
0
        public void Client_with_ipv6_should_connect_to_server_and_signal_appropriate_callbacks()
        {
            var connected1 = new ManualResetEventSlim(false);
            var connected2 = new ManualResetEventSlim(false);

            var ex     = ServerHelpers.CreateExecutor();
            var server = ServerHelpers.CreateServerIPv6();

            server.Connected.Subscribe(client =>
            {
                connected1.Set();
            });

            server.Started.Subscribe(u =>
            {
                var client = new SocketClient(ex, true);
                client.Connected.Subscribe(_ =>
                {
                    connected2.Set();
                });
                client.Disconnected.Subscribe(exn =>
                {
                    throw exn;
                });
                client.Connect(new IPEndPoint(IPAddress.IPv6Loopback, server.BindEndPoint.Port));
            });

            server.Start();

            connected1.AssertWaitFor(5000);
            connected2.AssertWaitFor(5000);

            server.StopAndAssertStopped();
        }
Ejemplo n.º 2
0
            public void Starting_two_times_should_throw()
            {
                var server = ServerHelpers.CreateServer();

                server.Start();

                Assert.Throws(typeof(InvalidOperationException), () =>
                {
                    server.Start();
                });
            }
Ejemplo n.º 3
0
            public void Starting_should_call_started_callback()
            {
                var started = new ManualResetEventSlim();
                var server  = ServerHelpers.CreateServer();

                server.Started.Subscribe(u => { started.Set(); });
                server.Start();

                started.AssertWaitFor(2000);

                server.StopAndAssertStopped();
            }
Ejemplo n.º 4
0
        public void Connected_clients_should_have_proper_end_points()
        {
            SocketServer server;
            SocketClient c1, c2;

            ServerHelpers.CreateServerAndConnectedClient(out server, out c1, out c2);

            Assert.Equal(c1.LocalEndPoint, c2.RemoteEndPoint);
            Assert.Equal(c1.RemoteEndPoint, c2.LocalEndPoint);

            server.StopAndAssertStopped();
        }
Ejemplo n.º 5
0
        public void Send_packets_should_be_received()
        {
            SocketServer server;
            SocketClient c1, c2;
            var          buffer = DataHelpers.CreateRandomBuffer(204800);

            ServerHelpers.CreateServerAndConnectedClient(out server, out c1, out c2);

            var recvBuffer  = c2.ReceiveData(204800, 2000, () => c1.Send(buffer));
            var recvBuffer2 = c1.ReceiveData(204800, 2000, () => c2.Send(buffer));

            Assert.Equal(buffer, recvBuffer);
            Assert.Equal(buffer, recvBuffer2);
        }
Ejemplo n.º 6
0
        public static void CreateServerAndConnectedClient(out SocketServer server,
                                                          out SslClient client1, out SslClient client2)
        {
            var connected1 = new ManualResetEventSlim(false);
            var connected2 = new ManualResetEventSlim(false);

            var ex = ServerHelpers.CreateExecutor();
            var s  = ServerHelpers.CreateServer();

            SslClient lClient = null;
            SslClient sClient = null;

            var certBytesStream = Assembly.GetExecutingAssembly()
                                  .GetManifestResourceStream("Stacks.Tests.StacksTest.pfx");
            var certBytes = new BinaryReader(certBytesStream).ReadBytes((int)certBytesStream.Length);

            s.Connected.Subscribe(c =>
            {
                sClient = new SslClient(c, new X509Certificate2(certBytes));

                sClient.Connected.Subscribe(_ =>
                {
                    connected2.Set();
                });

                sClient.EstablishSsl();
            });

            s.Started.Subscribe(_ =>
            {
                lClient = new SslClient(new SocketClient(ex), "Stacks Test", true);

                lClient.Connected.Subscribe(__ =>
                {
                    connected1.Set();
                });

                lClient.Connect(new IPEndPoint(IPAddress.Loopback, s.BindEndPoint.Port));
            });

            s.Start();

            connected1.AssertWaitFor(300000);
            connected2.AssertWaitFor(300000);

            server  = s;
            client1 = lClient;
            client2 = sClient;
        }
Ejemplo n.º 7
0
            public void Starting_and_stopping_should_call_both_callbacks()
            {
                var started = new ManualResetEventSlim();
                var stopped = new ManualResetEventSlim();
                var server  = ServerHelpers.CreateServer();

                server.Started.Subscribe(_ => { started.Set(); });
                server.Stopped.Subscribe(_ => { stopped.Set(); });

                server.Start();
                server.Stop();

                started.AssertWaitFor(2000);
                stopped.AssertWaitFor(2000);
            }
Ejemplo n.º 8
0
            public void When_exception_is_thrown_inside_callback_executor_should_signal_error()
            {
                var errOccured = new ManualResetEventSlim();

                var exec   = new ActionBlockExecutor(null, new ActionBlockExecutorSettings());
                var server = ServerHelpers.CreateServer(exec);

                exec.Error += exc => { Assert.Equal("abcdef", exc.Message); errOccured.Set(); };
                server.Started.Subscribe(_ => { throw new Exception("abcdef"); });

                server.Start();

                errOccured.AssertWaitFor(2000);

                server.Stop();
            }
Ejemplo n.º 9
0
        public void If_client_cannot_connect_if_should_raise_disconnect_event_with_connection_refused_error()
        {
            var disconnectedCalled = new ManualResetEventSlim();

            var executor = ServerHelpers.CreateExecutor();
            var client   = new SocketClient(executor);

            client.Disconnected.Subscribe(exn =>
            {
                Assert.IsType(typeof(SocketException), exn);
                Assert.Equal((int)SocketError.ConnectionRefused, ((SocketException)exn).ErrorCode);
                disconnectedCalled.Set();
            });

            client.Connect(new IPEndPoint(IPAddress.Loopback, 45232));
            disconnectedCalled.AssertWaitFor();
        }
Ejemplo n.º 10
0
        public void Calling_connect_twice_should_throw_an_exception()
        {
            var hasConnected = new ManualResetEventSlim();
            var executor     = ServerHelpers.CreateExecutor();
            var server       = ServerHelpers.CreateServer(executor);

            server.Started.Subscribe(_ =>
            {
                Assert.Throws(typeof(InvalidOperationException),
                              () =>
                {
                    var client = new SocketClient(executor);
                    client.Connect(server.BindEndPoint);
                    hasConnected.Set();
                    client.Connect(server.BindEndPoint);
                });
            });

            server.Start();

            hasConnected.AssertWaitFor();
        }
Ejemplo n.º 11
0
        public static void CreateServerAndConnectedClient(out SocketServer server,
                                                          out SocketClient client1, out SocketClient client2)
        {
            var connected1 = new ManualResetEventSlim(false);
            var connected2 = new ManualResetEventSlim(false);

            var ex = ServerHelpers.CreateExecutor();
            var s  = ServerHelpers.CreateServer();

            SocketClient lClient = null;
            SocketClient sClient = null;

            s.Connected.Subscribe(c =>
            {
                sClient = c;
                connected2.Set();
            });

            s.Started.Subscribe(_ =>
            {
                var c = new SocketClient(ex);
                c.Connected.Subscribe(u =>
                {
                    lClient = c;
                    connected1.Set();
                });
                c.Connect(new IPEndPoint(IPAddress.Loopback, s.BindEndPoint.Port));
            });

            s.Start();

            connected1.AssertWaitFor(3000);
            connected2.AssertWaitFor(3000);

            server  = s;
            client1 = lClient;
            client2 = sClient;
        }
Ejemplo n.º 12
0
        public void If_server_closes_then_client_should_raise_disconnect_event()
        {
            var disconnectedCalled = new ManualResetEventSlim();

            SocketServer server;
            SocketClient serverClient, remoteClient;

            ServerHelpers.CreateServerAndConnectedClient(out server, out serverClient, out remoteClient);

            remoteClient.Disconnected.Subscribe(exn =>
            {
                Assert.IsType(typeof(SocketException), exn);
                Assert.Equal((int)SocketError.Disconnecting, ((SocketException)exn).ErrorCode);
                disconnectedCalled.Set();
            });

            serverClient.Disconnected.Subscribe(exn =>
            {
            });
            server.Stop();
            serverClient.Close();

            disconnectedCalled.AssertWaitFor(5000);
        }