Beispiel #1
0
        public void IsConnected()
        {
            var test = new AsyncTest(passTimes: 2);

            bool first = true;

            client.PropertyChanged += (o, e) => {
                if (e.PropertyName != "IsConnected")
                {
                    return;
                }

                if (first)
                {
                    Assert.IsTrue(client.IsConnected);
                    first = false;

                    client.DisconnectAsync();
                }
                else
                {
                    Assert.IsFalse(client.IsConnected);
                }

                test.PassHandler(null, EventArgs.Empty);
            };

            client.ConnectAsync(new Target(Target.AnyIP, 0));

            test.Assert(10000);
        }
Beispiel #2
0
        public void DisconnectWhileReceiving()
        {
            var test = new AsyncTest <ClientDisconnectedEventArgs> (e =>
            {
                Assert.AreEqual(ConnectionResult.Custom, e.Reason);
                Assert.IsTrue(e.Requested);
            });

            bool send = true;

            client.Connected += (sender, e) =>
            {
                new Thread(() =>
                {
                    MockMessage m = new MockMessage {
                        Content = "asdf"
                    };
                    for (int i = 0; i < 10000 && send; ++i)
                    {
                        connection.Receive(new MessageEventArgs(connection, m));
                    }
                }).Start();

                Thread.Sleep(50);
                client.DisconnectAsync();
            };

            client.Disconnected += test.PassHandler;

            client.ConnectAsync(new Target(Target.AnyIP, 0));

            test.Assert(10000);
            send = false;
        }
        public void DisconnectFromClientOnServer()
        {
            var test = new AsyncTest(2);

            var c = GetNewClientConnection();

            c.Disconnected += (s, e) => {
                Assert.IsFalse(c.IsConnected, "IsConnected was still true");
                test.PassHandler(s, e);
            };;

            var wait = new AutoResetEvent(false);

            this.provider.Start(MessageTypes);
            this.provider.ConnectionMade += (s, e) =>
            {
                e.Connection.Disconnected += (ds, de) => {
                    Assert.IsFalse(e.Connection.IsConnected, "IsConnected was still true");
                    test.PassHandler(ds, de);
                };
                wait.Set();
            };

            c.ConnectAsync(Target, MessageTypes);

            if (!wait.WaitOne(10000))
            {
                Assert.Fail("Failed to connect");
            }

            c.DisconnectAsync();

            test.Assert(10000);
        }
        public void StressConcurrentSends()
        {
            var c = GetNewClientConnection();

            const int messages = 10000;
            var       test     = new AsyncTest(e =>
            {
                var me = (e as MessageEventArgs);
                Assert.IsNotNull(me);
                Assert.AreSame(c, me.Connection);

                int n;
                Assert.IsTrue(Int32.TryParse(((MockMessage)me.Message).Content, out n));
            }, messages);

            const int threads = 4;
            ParameterizedThreadStart thread = s =>
            {
                IConnection cn = (IConnection)s;
                try
                {
                    for (int i = 0; i < (messages / threads); ++i)
                    {
                        if (i > Int32.MaxValue)
                        {
                            System.Diagnostics.Debugger.Break();
                        }

                        if (!cn.IsConnected)
                        {
                            return;
                        }

                        cn.SendAsync(new MockMessage {
                            Content = i.ToString()
                        });
                        Thread.Sleep(1);
                    }
                }
                catch (Exception ex)
                {
                    test.FailWith(ex);
                }
            };

            this.provider.Start(MessageTypes);
            this.provider.ConnectionMade += (sender, e) =>
            {
                for (int i = 0; i < threads; ++i)
                {
                    new Thread(thread).Start(e.Connection);
                }
            };

            c.Disconnected    += test.FailHandler;
            c.MessageReceived += test.PassHandler;
            c.ConnectAsync(Target, MessageTypes);

            test.Assert(600000);
        }
Beispiel #5
0
        public async Task ProcessesMessagesAfterInternalReconnect()
        {
            var target = new Target(Target.AnyIP, 0);

            var result = await this.client.ConnectAsync(target);

            Assume.That(result.Result, Is.EqualTo(ConnectionResult.Success));

            await this.connection.DisconnectAsync();

            Assume.That(this.client.IsConnected, Is.False);

            await this.connection.ConnectAsync(target, MessageTypes.All);

            Assume.That(this.client.IsConnected, Is.True);

            const string content = "hi";

            var test = new AsyncTest(e => {
                var me = (MessageEventArgs <MockMessage>)e;
                Assert.AreSame(this.connection, me.Connection);
                Assert.AreEqual(content, me.Message.Content);
            });

            bool received = false;
            Action <MessageEventArgs <MockMessage> > handler = e => test.PassHandler(test, e);

            this.client.RegisterMessageHandler(handler);

            this.connection.Receive(new MessageEventArgs(this.connection, new MockMessage {
                Content = content
            }));

            test.Assert(10000);
        }
        public void ServerSendMessageAsync()
        {
            const string content = "Oh, hello there.";

            var c = GetNewClientConnection();

            var test = new AsyncTest(e =>
            {
                var me = (e as MessageEventArgs);
                Assert.IsNotNull(me);
                Assert.AreSame(c, me.Connection);

                var msg = (me.Message as MockMessage);
                Assert.IsNotNull(msg);
                Assert.AreEqual(content, msg.Content);
            });

            this.provider.Start(MessageTypes);
            this.provider.ConnectionMade += (sender, e) => e.Connection.SendAsync(new MockMessage {
                Content = content
            });

            c.Disconnected    += test.FailHandler;
            c.MessageReceived += test.PassHandler;
            c.ConnectAsync(Target, MessageTypes);

            test.Assert(10000);
        }
        public void SendLongMessageAsync()
        {
            string content = TestHelpers.GetLongString(random, MaxPayloadSize - sizeof(int));

            var c = GetNewClientConnection();

            var test = new AsyncTest(e =>
            {
                var me = (e as MessageEventArgs);
                Assert.IsNotNull(me);
                Assert.AreSame(c, me.Connection);

                var msg = (me.Message as MockMessage);
                Assert.IsNotNull(msg);
                Assert.AreEqual(content, msg.Content);
            });

            this.provider.Start(MessageTypes);
            this.provider.ConnectionMade += (sender, e) => e.Connection.SendAsync(new MockMessage {
                Content = content
            });

            c.Disconnected    += test.FailHandler;
            c.MessageReceived += test.PassHandler;
            c.ConnectAsync(Target, MessageTypes);

            test.Assert(30000);
        }
        public void ClientMessageSent()
        {
            const string content = "Oh, hello there.";

            var c = GetNewClientConnection();

            var test = new AsyncTest();

            this.provider.Start(MessageTypes);

            c.Disconnected += test.FailHandler;
            c.Connected    += (sender, e) =>
            {
                Task <bool> task = c.SendAsync(new MockMessage {
                    Content = content
                });
                task.Wait();

                if (task.Result)
                {
                    test.PassHandler(null, EventArgs.Empty);
                }
                else
                {
                    test.FailHandler(null, EventArgs.Empty);
                }
            };
            c.ConnectAsync(Target, MessageTypes);

            test.Assert(10000);
        }
        public void ClientSendMessageAsync()
        {
            const string content = "Oh, hello there.";

            var c = GetNewClientConnection();

            IServerConnection connection = null;

            var test = new AsyncTest(e =>
            {
                var me = (e as MessageEventArgs);
                Assert.IsNotNull(me);
                Assert.AreSame(me.Connection, connection);

                var msg = (me.Message as MockMessage);
                Assert.IsNotNull(msg);
                Assert.AreEqual(content, msg.Content);
            });

            this.provider.Start(MessageTypes);
            this.provider.ConnectionMade += (sender, e) =>
            {
                connection = e.Connection;
                e.Connection.MessageReceived += test.PassHandler;
            };

            c.Connected += (sender, e) => ThreadPool.QueueUserWorkItem(o => c.SendAsync(new MockMessage {
                Content = content
            }));
            c.ConnectAsync(Target, MessageTypes);

            test.Assert(10000);
        }
        public void ConnectedWithKey()
        {
            this.provider.Start(MessageTypes);

            RSAAsymmetricKey key = null;

            var test = new AsyncTest(e =>
            {
                var ce = (ConnectionMadeEventArgs)e;

                Assert.IsNotNull(ce.ClientPublicKey);
                Assert.AreEqual(key, ce.ClientPublicKey);
            });

            this.provider.ConnectionMade += test.PassHandler;

            var c = GetNewClientConnection(out key);

            if (key == null)
            {
                Assert.Ignore();
            }

            c.ConnectAsync(Target, MessageTypes);

            test.Assert(10000);
        }
        public void DisconnectFromServerOnServer()
        {
            var test = new AsyncTest(2);

            var c = GetNewClientConnection();

            c.Disconnected += test.PassHandler;
            IServerConnection sc = null;

            var wait = new AutoResetEvent(false);

            this.provider.Start(MessageTypes);
            this.provider.ConnectionMade += (sender, e) =>
            {
                e.Connection.Disconnected += test.PassHandler;
                sc = e.Connection;
                wait.Set();
            };

            c.ConnectAsync(Target, MessageTypes);

            if (!wait.WaitOne(10000) || sc == null)
            {
                Assert.Fail("Failed to connect");
            }

            sc.DisconnectAsync();

            test.Assert(10000);
        }
        public void GenericMessageHandlingGlobalOrder()
        {
            IServerConnection connection = null;

            var test = new AsyncTest(e =>
            {
                var me = (MessageEventArgs <MockMessage>)e;
                Assert.AreSame(connection, me.Connection);
                Assert.AreEqual("hi", me.Message.Content);
            });

            var server = new TempestServer(MessageTypes.Reliable);

            server.AddConnectionProvider(provider, ExecutionMode.GlobalOrder);
            server.Start();

            Action <MessageEventArgs <MockMessage> > handler = e => test.PassHandler(test, e);

            server.RegisterMessageHandler(handler);

            provider.ConnectionMade += (sender, e) => connection = e.Connection;

            var c = provider.GetClientConnection(protocol);

            c.ConnectAsync(new Target(Target.AnyIP, 0), MessageTypes.Reliable);
            c.SendAsync(new MockMessage {
                Content = "hi"
            });

            test.Assert(10000);
        }
Beispiel #13
0
        public void ConnectionLimitRestartListening()
        {
            IServerConnection connection = null;

            provider.ConnectionMade += (sender, e) =>
            {
                if (connection == null)
                {
                    connection = e.Connection;
                }
            };

            ConnectionLimit();

            Assert.IsNotNull(connection);

            connection.DisconnectAsync();

            AsyncTest test = new AsyncTest();

            provider.ConnectionMade += test.PassHandler;

            IClientConnection client = GetNewClientConnection();

            client.Disconnected += test.FailHandler;
            client.ConnectAsync(Target, MessageTypes);

            test.Assert(10000);
        }
        public void MessageHandling()
        {
            IServerConnection connection = null;

            var test = new AsyncTest(e =>
            {
                var me = (MessageEventArgs)e;
                Assert.AreSame(connection, me.Connection);
                Assert.IsTrue(me.Message is MockMessage);
                Assert.AreEqual("hi", ((MockMessage)me.Message).Content);
            });

            var server = new TempestServer(provider, MessageTypes.Reliable);

            server.Start();

            Action <MessageEventArgs> handler = e => test.PassHandler(test, e);

            ((IContext)server).RegisterMessageHandler(MockProtocol.Instance, 1, handler);

            provider.ConnectionMade += (sender, e) => connection = e.Connection;

            var c = provider.GetClientConnection(protocol);

            c.ConnectAsync(new Target(Target.AnyIP, 0), MessageTypes.Reliable);
            c.SendAsync(new MockMessage()
            {
                Content = "hi"
            });

            test.Assert(10000);
        }
Beispiel #15
0
        public void ISerializable()
        {
            byte[] buffer = new byte[20480];
            var    writer = new BufferValueWriter(buffer);

            SerializableTester tester = new SerializableTester
            {
                Name    = "MONKEY!",
                Numbers = new[] { 1, 2, 4, 8, 16, 32 }
            };

            var test = new AsyncTest();

            tester.SerializeCalled += test.PassHandler;

            writer.Write(context, tester);
            writer.Flush();

            var reader     = new BufferValueReader(buffer);
            var serialized = SerializerExtensions.Read <SerializableTester> (reader, context);

            Assert.IsNotNull(serialized);
            Assert.AreEqual(tester.Name, serialized.Name);
            Assert.IsTrue(tester.Numbers.SequenceEqual(serialized.Numbers), "Numbers does not match");

            test.Assert(1000);
        }
Beispiel #16
0
        public void PingPong()
        {
            AsyncTest test = new AsyncTest();

            IServerConnection connection = null;

            provider.ConnectionMade += (sender, e) =>
            {
                if (connection == null)
                {
                    connection = e.Connection;
                }
            };

            provider.Start(MessageTypes);

            ((NetworkConnectionProvider)provider).PingFrequency = 1000;
            var client = GetNewClientConnection();

            client.Disconnected += test.FailHandler;
            client.ConnectAsync(Target, MessageTypes);

            test.Assert(30000, false);
            Assert.IsNotNull(connection);
            Assert.IsTrue(connection.IsConnected);
            Assert.IsTrue(client.IsConnected);
        }
Beispiel #17
0
        public void TryGetBufferBlocksAndExisting()
        {
            var buffer = new BufferPool(128, 1);

            SocketAsyncEventArgs e;

            buffer.TryGetBuffer(out e);

            DateTime now = DateTime.Now;

            SocketAsyncEventArgs second = null;
            var test = new AsyncTest(args => {
                Assert.That(DateTime.Now - now, Is.GreaterThan(TimeSpan.FromSeconds(1)));
                Assert.That(second, Is.SameAs(e));
            });

            Task.Run(() => {
                Assert.IsTrue(buffer.TryGetBuffer(out second));
            }).ContinueWith(t => {
                test.PassHandler(null, EventArgs.Empty);
            });

            Task.Delay(1000).ContinueWith(t => {
                buffer.PushBuffer(e);
            });

            test.Assert(2000);
        }
Beispiel #18
0
        public void Multiple_Passes_FailIfNotPassed()
        {
            var test = new AsyncTest(multiple: true);

            test.PassHandler(this, EventArgs.Empty);
            // failIfNotPassed refers to explicit Assert.Pass() calls only, not PassHandler calls.
            Assert.Throws <AssertionException> (() => test.Assert(10, failIfNotPassed: true));
        }
Beispiel #19
0
        public void Multiple_Passes_NoFailures()
        {
            var test = new AsyncTest(multiple: true);

            test.PassHandler(this, EventArgs.Empty);
            test.PassHandler(this, EventArgs.Empty);

            test.Assert(10, failIfNotPassed: false);
        }
Beispiel #20
0
        public void Multiple_Passes_Failure()
        {
            var test = new AsyncTest(multiple: true);

            test.PassHandler(this, EventArgs.Empty);
            test.PassHandler(this, EventArgs.Empty);
            test.FailHandler(this, EventArgs.Empty);

            Assert.Throws <AssertionException> (() => test.Assert(10, failIfNotPassed: false));
        }
Beispiel #21
0
        public void Connected()
        {
            var test = new AsyncTest();

            client.Connected    += test.PassHandler;
            client.Disconnected += test.FailHandler;

            client.ConnectAsync(new Target(Target.AnyIP, 0));

            test.Assert(10000);
        }
        public void ConnectionMade()
        {
            this.provider.Start(MessageTypes);

            var test = new AsyncTest();

            this.provider.ConnectionMade += test.PassHandler;
            var c = GetNewClientConnection();

            c.ConnectAsync(Target, MessageTypes);

            test.Assert(10000);
        }
        public void StressAuthenticatedAndEncrypted()
        {
            var c = GetNewClientConnection();

            const int messages = 1000;
            int       number   = 0;

            var test = new AsyncTest(e =>
            {
                var me = (e as MessageEventArgs);
                Assert.IsNotNull(me);
                Assert.AreSame(c, me.Connection);

                Assert.AreEqual(number, ((AuthenticatedAndEncryptedMessage)me.Message).Number);
                Assert.AreEqual(number++.ToString(), ((AuthenticatedAndEncryptedMessage)me.Message).Message);
            }, messages);

            this.provider.Start(MessageTypes);
            this.provider.ConnectionMade += (sender, e) => (new Thread(() =>
            {
                try
                {
                    for (int i = 0; i < messages; ++i)
                    {
                        if (i > Int32.MaxValue)
                        {
                            System.Diagnostics.Debugger.Break();
                        }

                        if (!e.Connection.IsConnected)
                        {
                            return;
                        }

                        e.Connection.SendAsync(new AuthenticatedAndEncryptedMessage {
                            Number = i, Message = i.ToString()
                        });
                    }
                }
                catch (Exception ex)
                {
                    test.FailWith(ex);
                }
            })).Start();

            c.Disconnected    += test.FailHandler;
            c.MessageReceived += test.PassHandler;
            c.ConnectAsync(Target, MessageTypes);

            test.Assert(80000);
        }
        public void EncryptedAndAuthenticatedLongMessage()
        {
            string message = TestHelpers.GetLongString(random, MaxPayloadSize - sizeof(int) * 2);

            var cmessage = new AuthenticatedAndEncryptedMessage
            {
                Message = message,
                Number  = 42
            };

            var c = GetNewClientConnection();

            var test = new AsyncTest(e =>
            {
                var me = (e as MessageEventArgs);
                Assert.IsNotNull(me);

                var msg = (me.Message as AuthenticatedAndEncryptedMessage);
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.Encrypted);
                Assert.IsTrue(msg.Authenticated);
                Assert.AreEqual(cmessage.Message, msg.Message);
                Assert.AreEqual(cmessage.Number, msg.Number);
            });

            IConnection      sc;
            ManualResetEvent wait = new ManualResetEvent(false);

            this.provider.ConnectionMade += (s, e) =>
            {
                sc = e.Connection;
                sc.MessageReceived += test.PassHandler;
                sc.Disconnected    += test.FailHandler;
                wait.Set();
            };

            this.provider.Start(MessageTypes);

            c.Disconnected += test.FailHandler;
            c.Connected    += (sender, e) => c.SendAsync(cmessage);
            c.ConnectAsync(Target, MessageTypes);

            if (!wait.WaitOne(10000))
            {
                Assert.Fail("Failed to connect");
            }

            test.Assert(10000);
        }
        public void ConnectionRejected()
        {
            var test = new AsyncTest();

            var c = GetNewClientConnection();

            c.Disconnected += test.PassHandler;

            this.provider.Start(MessageTypes);
            this.provider.ConnectionMade += (sender, e) => e.Rejected = true;

            c.ConnectAsync(Target, MessageTypes);

            test.Assert(10000);
        }
Beispiel #26
0
        public void Disconnect()
        {
            var test = new AsyncTest <ClientDisconnectedEventArgs> (e =>
            {
                Assert.AreEqual(ConnectionResult.Custom, e.Reason);
                Assert.IsTrue(e.Requested);
            });

            client.Connected    += (sender, e) => client.DisconnectAsync();
            client.Disconnected += test.PassHandler;

            client.ConnectAsync(new Target(Target.AnyIP, 0));

            test.Assert(10000);
        }
Beispiel #27
0
        public void DisconnectedFromConnection()
        {
            var test = new AsyncTest <ClientDisconnectedEventArgs> (e =>
            {
                Assert.IsFalse(e.Requested);
                Assert.AreEqual(ConnectionResult.EncryptionMismatch, e.Reason);
            });

            client.Connected    += (s, e) => connection.Disconnect(ConnectionResult.EncryptionMismatch);
            client.Disconnected += test.PassHandler;

            client.ConnectAsync(new Target(Target.AnyIP, 0));

            test.Assert(1000);
        }
        public void StressRandomLongAuthenticatedMessage()
        {
            var c = GetNewClientConnection();

            const int messages = 1000;
            int       number   = 0;

            var test = new AsyncTest(e =>
            {
                var me = (e as MessageEventArgs);
                Assert.IsNotNull(me);
                Assert.AreSame(c, me.Connection);

                Assert.AreEqual(number++, ((AuthenticatedMessage)me.Message).Number);
                Assert.IsTrue((((AuthenticatedMessage)me.Message).Message.Length >= 7500));
            }, messages);

            this.provider.Start(MessageTypes);
            this.provider.ConnectionMade += (sender, e) => (new Thread(() =>
            {
                try
                {
                    for (int i = 0; i < messages; ++i)
                    {
                        if (!e.Connection.IsConnected)
                        {
                            return;
                        }

                        e.Connection.SendAsync(new AuthenticatedMessage
                        {
                            Number = i,
                            Message = TestHelpers.GetLongString(random, MaxPayloadSize - sizeof(int) * 2)
                        });
                    }
                }
                catch (Exception ex)
                {
                    test.FailWith(ex);
                }
            })).Start();

            c.Disconnected    += test.FailHandler;
            c.MessageReceived += test.PassHandler;
            c.ConnectAsync(Target, MessageTypes);

            test.Assert(900000);
        }
        public void ConnectionMade()
        {
            var server = new TempestServer(provider, MessageTypes.Reliable);

            server.Start();

            var test = new AsyncTest();

            server.ConnectionMade += test.PassHandler;

            var client = provider.GetClientConnection(protocol);

            client.ConnectAsync(new Target(Target.AnyIP, 0), MessageTypes.Reliable);

            test.Assert(5000);
        }
Beispiel #30
0
        public void ServerConnectionConnected()
        {
            var provider = new MockConnectionProvider(MockProtocol.Instance);

            provider.Start(MessageTypes.Reliable);

            var test = new AsyncTest <ConnectionMadeEventArgs> (e => Assert.AreEqual(true, e.Connection.IsConnected));

            provider.ConnectionMade += test.PassHandler;

            var client = provider.GetClientConnection(MockProtocol.Instance);

            client.ConnectAsync(new Target(Target.AnyIP, 0), MessageTypes.Reliable);

            test.Assert(5000);
        }