コード例 #1
0
        public void Client_connects_disconnects_and_reconnects()
        {
            Server.Config.RequireAuthentication = false;
            int connectedTimes = 0;


            Client.Ready += (sender, args) =>
            {
                if (!args.Session.Authenticated)
                {
                    LastException = new Exception("Client ready event called while authentication failed.");
                }

                if (++connectedTimes == 5)
                {
                    TestStatus.Set();
                }
                else
                {
                    Client.Close();
                }
            };

            Client.Closed += (sender, args) =>
            {
                if (!TestStatus.IsSet)
                {
                    Client.Connect();
                }
            };

            StartAndWait();
        }
コード例 #2
0
        public void Client_does_not_send_empty_message()
        {
            var messageSource = GenerateRandomMessage(2, 50);

            Client.Connected += (sender, args) =>
            {
                Client.Send(new MqMessage());
                Client.Send(messageSource);
            };

            Server.IncomingMessage += (sender, args) =>
            {
                MqMessage message;

                while (args.Messages.Count > 0)
                {
                    message = args.Messages.Dequeue();
                    if (message.Count != 2)
                    {
                        LastException = new Exception("Server received an empty message.");
                    }
                    TestStatus.Set();
                }
            };

            StartAndWait();
        }
コード例 #3
0
        public void Client_times_out_while_connecting_for_too_long()
        {
            Config.ConnectionTimeout = 100;
            Client = new MqClient <SimpleMqSession, MqConfig>(Config);

            Server.Connected += (sender, args) => { args.Session.Socket.Close(); };


            Client.Closed += (sender, args) =>
            {
                if (args.CloseReason == SocketCloseReason.TimeOut)
                {
                    TestStatus.Set();
                }
                else
                {
                    LastException = new Exception("Client closed for reason other than timeout.");
                }
            };

            StartAndWait(false, 10000, false);

            if (TestStatus.IsSet == false)
            {
                throw new Exception("Socket did not timeout.");
            }
        }
コード例 #4
0
        public void Server_verifies_authentication()
        {
            var authData = new byte[] { 1, 2, 3, 4, 5 };

            Server.Config.RequireAuthentication = true;

            Server.SessionSetup +=
                (sender, args) => { args.Session.AddService <ICalculatorService>(new CalculatorService()); };

            Server.Authenticate += (sender, e) =>
            {
                try
                {
                    Assert.Equal(authData, e.AuthData);
                }
                catch (Exception ex)
                {
                    LastException = ex;
                }
                finally
                {
                    TestStatus.Set();
                }
            };

            Client.Authenticate += (sender, e) => { e.AuthData = authData; };


            StartAndWait();
        }
コード例 #5
0
        public void Server_does_not_request_authentication()
        {
            Server.Config.RequireAuthentication = false;

            Server.SessionSetup +=
                (sender, args) => { args.Session.AddService <ICalculatorService>(new CalculatorService()); };

            Client.Authenticate += (sender, e) => { };


            Client.Ready += (sender, e) =>
            {
                e.Session.AddProxy <ICalculatorService>("CalculatorService");
                var service = Client.Session.GetProxy <ICalculatorService>();

                var result = service.Add(100, 200);

                if (result != 300)
                {
                    LastException = new Exception("Client authenticated.");
                }
                TestStatus.Set();
            };


            StartAndWait();
        }
コード例 #6
0
        public void Client_prevents_times_out()
        {
            var clientConfig = new MqConfig
            {
                Ip            = Config.Ip,
                Port          = Config.Port,
                PingFrequency = 100
            };


            Client = new MqClient <SimpleMqSession, MqConfig>(clientConfig);

            Config.PingTimeout = 200;
            Server             = new MqServer <SimpleMqSession, MqConfig>(Config);


            Client.Closed += (sender, args) =>
            {
                if (args.CloseReason == SocketCloseReason.TimeOut)
                {
                    TestStatus.Set();
                }
                else
                {
                    LastException = new Exception("Client closed for reason other than timeout.");
                }
            };

            StartAndWait(false, 1500);

            if (TestStatus.IsSet)
            {
                throw new Exception("Client timed out.");
            }
        }
コード例 #7
0
        public void Client_closes_self()
        {
            Client.Connected += (sender, args) => Client.Close();

            Client.Closed += (sender, args) => TestStatus.Set();

            StartAndWait();
        }
コード例 #8
0
        public void Client_notifies_server_closing_session()
        {
            Client.Connected += (sender, args) => Client.Close();

            Server.Closed += (sender, args) => TestStatus.Set();

            StartAndWait();
        }
コード例 #9
0
        public void Client_disconects_from_server()
        {
            Client.Connected += (sender, args) => { Client.Close(); };

            Client.Closed += (sender, args) => TestStatus.Set();

            StartAndWait(true, 500000);
        }
コード例 #10
0
        public void Server_detects_client_disconnect()
        {
            Client.Connected += (sender, args) => { Client.Close(); };

            Server.Closed += (session, value) => { TestStatus.Set(); };

            StartAndWait();
        }
コード例 #11
0
        public void Client_notified_server_stopping()
        {
            Server.Connected += (sender, session) => Server.Stop();

            Client.Closed += (sender, args) => TestStatus.Set();

            StartAndWait();
        }
コード例 #12
0
        public void Server_requests_authentication()
        {
            Server.Config.RequireAuthentication = true;


            Client.Authenticate += (sender, e) => { TestStatus.Set(); };


            StartAndWait();
        }
コード例 #13
0
        public void Client_does_not_notify_on_command_frame()
        {
            var commandFrame = new MqFrame(new byte[21], MqFrameType.Command, Config);

            Client.Connected += (sender, args) => { Client.Send(commandFrame); };

            Server.IncomingMessage += (sender, args) => { TestStatus.Set(); };

            StartAndWait(false, 500);

            if (TestStatus.IsSet)
            {
                throw new Exception("Server read command frame.");
            }
        }
コード例 #14
0
        public void Client_times_out_on_long_auth()
        {
            Server.Config.RequireAuthentication = true;
            Client.Config.ConnectionTimeout     = 100;

            Client.Closed += (sender, e) =>
            {
                if (e.CloseReason != SocketCloseReason.TimeOut)
                {
                    LastException = new Exception("Client was not notified that the authentication failed.");
                }
                TestStatus.Set();
            };

            Server.Authenticate += (sender, e) => { Thread.Sleep(500); };

            StartAndWait(true, 5000, true);
        }
コード例 #15
0
        public void Client_notified_server_session_closed()
        {
            Server.Connected += (sender, session) =>
            {
                //Thread.Sleep(1000);
                //session.Session.Send(new MqMessage(new MqFrame(new byte[24], MqFrameType.Last)));
                session.Session.Close(SocketCloseReason.ApplicationError);
            };

            Client.Closed += (sender, args) =>
            {
                if (args.CloseReason != SocketCloseReason.ApplicationError)
                {
                    LastException = new InvalidOperationException("Server did not return proper close reason.");
                }
                TestStatus.Set();
            };

            StartAndWait();
        }
コード例 #16
0
        public void Client_disconnectes_from_failed_authentication()
        {
            Server.Config.RequireAuthentication = true;

            Server.SessionSetup +=
                (sender, args) => { args.Session.AddService <ICalculatorService>(new CalculatorService()); };

            Server.Authenticate += (sender, e) => { e.Authenticated = false; };

            Client.Closed += (sender, e) =>
            {
                if (e.CloseReason != SocketCloseReason.AuthenticationFailure)
                {
                    LastException = new Exception("Server closed session for invalid reason");
                }
                TestStatus.Set();
            };

            Client.Authenticate += (sender, e) => { e.AuthData = new byte[] { 5, 4, 3, 2, 1 }; };

            StartAndWait();
        }
コード例 #17
0
        public void Client_notified_of_authentication_success()
        {
            Server.Config.RequireAuthentication = true;

            Server.SessionSetup +=
                (sender, args) => { args.Session.AddService <ICalculatorService>(new CalculatorService()); };

            Server.Authenticate += (sender, e) => { e.Authenticated = true; };

            Client.Ready += (sender, e) =>
            {
                if (e.Session.Authenticated == false)
                {
                    LastException = new Exception("Client notified of authentication wrongly.");
                }
                TestStatus.Set();
            };

            Client.Authenticate += (sender, e) => { e.AuthData = new byte[] { 5, 4, 3, 2, 1 }; };

            StartAndWait();
        }
コード例 #18
0
        public void Server_should_send_data_to_client(int number, bool validate)
        {
            var messageSource = GenerateRandomMessage(4, 50);

            Server.Connected += (sender, session) =>
            {
                for (int i = 0; i < number; i++)
                {
                    session.Session.Send(messageSource);
                }
            };

            int clientMessageCount = 0;

            Client.IncomingMessage += (sender, args) =>
            {
                MqMessage message;

                clientMessageCount += args.Messages.Count;

                while (args.Messages.Count > 0)
                {
                    message = args.Messages.Dequeue();

                    if (validate)
                    {
                        CompareMessages(messageSource, message);
                    }
                }

                if (clientMessageCount == number)
                {
                    TestStatus.Set();
                }
            };

            StartAndWait();
        }
コード例 #19
0
        public void Client_calls_proxy_method()
        {
            Server.Ready += (sender, args) => { args.Session.AddService(new CalculatorService()); };

            Client.Authenticate += (sender, args) => { };


            Client.Ready += (sender, args) =>
            {
                args.Session.AddProxy <ICalculatorService>("CalculatorService");
                var service = Client.Session.GetProxy <ICalculatorService>();
                var result  = service.Add(100, 200);

                if (result != 300)
                {
                    LastException = new Exception("Service returned wrong result.");
                }

                TestStatus.Set();
            };

            StartAndWait();
        }
コード例 #20
0
        public void Client_calls_proxy_method_sequential()
        {
            Server.SessionSetup += (sender, args) => { args.Session.AddService(new CalculatorService()); };


            Client.Ready += (sender, args) =>
            {
                args.Session.AddProxy <ICalculatorService>("CalculatorService");
                var       service   = Client.Session.GetProxy <ICalculatorService>();
                Stopwatch stopwatch = Stopwatch.StartNew();

                int addedInt = 0;
                for (int i = 0; i < 10; i++)
                {
                    addedInt = service.Add(addedInt, 1);
                }

                Output.WriteLine($"{stopwatch.ElapsedMilliseconds}");
                TestStatus.Set();
            };

            StartAndWait();
        }
コード例 #21
0
        public void Client_does_not_ready_before_server_authenticates()
        {
            Server.Config.RequireAuthentication = true;

            Server.Authenticate += (sender, args) =>
            {
                args.Authenticated = true;
                Thread.Sleep(200);
            };

            Client.Ready += (sender, args) =>
            {
                if (!args.Session.Authenticated)
                {
                    LastException = new Exception("Client ready event called while authentication failed.");
                }
                TestStatus.Set();
            };

            Client.Authenticate += (sender, e) => { e.AuthData = new byte[] { 5, 4, 3, 2, 1 }; };

            StartAndWait();
        }
コード例 #22
0
        public void Client_calls_proxy_method_and_canceles()
        {
            Server.SessionSetup += (sender, args) =>
            {
                var service = new CalculatorService();
                args.Session.AddService <ICalculatorService>(service);

                service.LongRunningTaskCanceled += (o, eventArgs) => { TestStatus.Set(); };
            };


            Client.Ready += (sender, args) =>
            {
                args.Session.AddProxy <ICalculatorService>("CalculatorService");
                var service     = Client.Session.GetProxy <ICalculatorService>();
                var tokenSource = new CancellationTokenSource();


                bool threw = false;
                try
                {
                    tokenSource.CancelAfter(500);
                    service.LongRunningTask(1, 2, tokenSource.Token);
                }
                catch (OperationCanceledException)
                {
                    threw = true;
                }

                if (threw != true)
                {
                    LastException = new Exception("Operation did not cancel.");
                }
            };

            StartAndWait();
        }
コード例 #23
0
        public void Client_should_send_data_to_server(int number, bool validate)
        {
            var messageSource    = GenerateRandomMessage(4, 50);
            int receivedMessages = 0;

            Client.Connected += (sender, args) =>
            {
                for (int i = 0; i < number; i++)
                {
                    Client.Send(messageSource);
                }
            };

            Server.IncomingMessage += (sender, args) =>
            {
                MqMessage message;

                while (args.Messages.Count > 0)
                {
                    message = args.Messages.Dequeue();
                    Interlocked.Increment(ref receivedMessages);
                    if (validate)
                    {
                        CompareMessages(messageSource, message);
                    }
                }


                if (receivedMessages == number)
                {
                    TestStatus.Set();
                }
            };

            StartAndWait();
        }
コード例 #24
0
        public void Server_accepts_new_connection()
        {
            Server.Connected += (sender, session) => { TestStatus.Set(); };

            StartAndWait();
        }
コード例 #25
0
        public void Client_connects_to_server()
        {
            Client.Connected += (sender, args) => TestStatus.Set();

            StartAndWait();
        }