Example #1
0
        public void Client_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.CreateServer();

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

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

            server.Start();

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

            server.StopAndAssertStopped();
        }
Example #2
0
        public void Client_should_signal_disconnection_when_server_is_closed()
        {
            var disconnected = new ManualResetEventSlim();
            Utils.CreateServerAndClientProxy<TestActor, ITestActor>(out server, out clientProxy);

            clientProxy.Disconnected.Subscribe(exn => { disconnected.Set(); });
            
            server.Stop();

            disconnected.AssertWaitFor();
        }
Example #3
0
            public void Starting_should_call_started_callback()
            {
                var started = new ManualResetEventSlim();
                var server = ServerHelpers.CreateServer();

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

                started.AssertWaitFor(2000);

                server.StopAndAssertStopped();
            }
Example #4
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);
            }
Example #5
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 += () => { throw new Exception("abcdef"); };

                server.Start();

                errOccured.AssertWaitFor(2000);

                server.Stop();
            }
Example #6
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();
        }
Example #7
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);
        }
Example #8
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();
        }
Example #9
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;
        }
Example #10
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;
        }
Example #11
0
 public static void StopAndAssertStopped(this SocketServer server)
 {
     var stopped = new ManualResetEventSlim(false);
     server.Stopped.Subscribe(_ =>
     {
         stopped.Set();
     });
     server.Stop();
     stopped.AssertWaitFor(3000);
 }