Пример #1
0
        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);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }