예제 #1
0
        public void TcpConnectFailureTest()
        {
            var sender = new MyTcpNetworkSender("tcp://hostname:123", AddressFamily.Unspecified)
            {
                ConnectFailure = 1,
                Async          = true,
            };

            sender.Initialize();
            byte[] buffer = Encoding.UTF8.GetBytes("quick brown fox jumps over the lazy dog");

            var exceptions = new List <Exception>();
            var allSent    = new ManualResetEvent(false);

            for (int i = 1; i < 8; i++)
            {
                sender.Send(
                    buffer, 0, i, ex =>
                {
                    lock (exceptions)
                    {
                        exceptions.Add(ex);
                        if (exceptions.Count == 7)
                        {
                            allSent.Set();
                        }
                    }
                });
            }

#if SILVERLIGHT
            Assert.IsTrue(allSent.WaitOne(3000));
#else
            Assert.IsTrue(allSent.WaitOne(3000, false));
#endif

            var mre = new ManualResetEvent(false);
            sender.FlushAsync(ex => mre.Set());
#if SILVERLIGHT
            mre.WaitOne(3000);
#else
            mre.WaitOne(3000, false);
#endif

            var actual = sender.Log.ToString();

            Assert.IsTrue(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1);
            Assert.IsTrue(actual.IndexOf("create socket 10000 Stream Tcp") != -1);
            Assert.IsTrue(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1);
            Assert.IsTrue(actual.IndexOf("failed") != -1);

            foreach (var ex in exceptions)
            {
                Assert.IsNotNull(ex);
            }
        }
예제 #2
0
        public void TcpHappyPathTest()
        {
            foreach (bool async in new[] { false, true })
            {
                var sender = new MyTcpNetworkSender("tcp://hostname:123", AddressFamily.Unspecified)
                {
                    Async = async,
                };

                sender.Initialize();
                byte[] buffer = Encoding.UTF8.GetBytes("quick brown fox jumps over the lazy dog");

                var exceptions = new List <Exception>();

                for (int i = 1; i < 8; i *= 2)
                {
                    sender.Send(
                        buffer, 0, i, ex =>
                    {
                        lock (exceptions) exceptions.Add(ex);
                    });
                }

                var mre = new ManualResetEvent(false);

                sender.FlushAsync(ex =>
                {
                    lock (exceptions)
                    {
                        exceptions.Add(ex);
                    }

                    mre.Set();
                });

                mre.WaitOne();

                var actual = sender.Log.ToString();
                Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1);
                Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1);
                Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1);
                Assert.True(actual.IndexOf("send async 0 1 'q'") != -1);
                Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1);
                Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1);

                mre.Reset();
                for (int i = 1; i < 8; i *= 2)
                {
                    sender.Send(
                        buffer, 0, i, ex =>
                    {
                        lock (exceptions) exceptions.Add(ex);
                    });
                }

                sender.Close(ex =>
                {
                    lock (exceptions)
                    {
                        exceptions.Add(ex);
                    }

                    mre.Set();
                });

                mre.WaitOne();

                actual = sender.Log.ToString();

                Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1);
                Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1);
                Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1);
                Assert.True(actual.IndexOf("send async 0 1 'q'") != -1);
                Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1);
                Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1);
                Assert.True(actual.IndexOf("send async 0 1 'q'") != -1);
                Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1);
                Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1);
                Assert.True(actual.IndexOf("close") != -1);

                foreach (var ex in exceptions)
                {
                    Assert.Null(ex);
                }
            }
        }
예제 #3
0
 public MockSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, MyTcpNetworkSender sender)
 {
     this.sender = sender;
     log         = sender.Log;
     log.WriteLine("create socket {0} {1} {2}", addressFamily, socketType, protocolType);
 }
예제 #4
0
        public void TcpSendFailureTest()
        {
            var sender = new MyTcpNetworkSender("tcp://hostname:123", AddressFamily.Unspecified)
            {
                SendFailureIn = 3, // will cause failure on 3rd send
                Async         = true,
            };

            sender.Initialize();
            byte[] buffer = Encoding.UTF8.GetBytes("quick brown fox jumps over the lazy dog");

            var exceptions = new Exception[9];

            var writeFinished = new ManualResetEvent(false);
            int remaining     = exceptions.Length;

            for (int i = 1; i < 10; i++)
            {
                int pos = i - 1;

                sender.Send(
                    buffer, 0, i, ex =>
                {
                    lock (exceptions)
                    {
                        exceptions[pos] = ex;
                        if (--remaining == 0)
                        {
                            writeFinished.Set();
                        }
                    }
                });
            }

            var mre = new ManualResetEvent(false);

            writeFinished.WaitOne();
            sender.Close(ex => mre.Set());
            mre.WaitOne();

            var actual = sender.Log.ToString();

            Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1);
            Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1);
            Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1);
            Assert.True(actual.IndexOf("send async 0 1 'q'") != -1);
            Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1);
            Assert.True(actual.IndexOf("send async 0 3 'qui'") != -1);
            Assert.True(actual.IndexOf("failed") != -1);
            Assert.True(actual.IndexOf("close") != -1);

            for (int i = 0; i < exceptions.Length; ++i)
            {
                if (i < 2)
                {
                    Assert.Null(exceptions[i]);
                }
                else
                {
                    Assert.NotNull(exceptions[i]);
                }
            }
        }
예제 #5
0
        public void TcpHappyPathTest()
        {
            foreach (bool async in new[] { false, true })
            {
                var sender = new MyTcpNetworkSender("tcp://hostname:123", AddressFamily.Unspecified)
                {
                    Async = async,
                };

                sender.Initialize();
                byte[] buffer = Encoding.UTF8.GetBytes("quick brown fox jumps over the lazy dog");

                var exceptions = new List<Exception>();

                for (int i = 1; i < 8; i *= 2)
                {
                    sender.Send(
                        buffer, 0, i, ex =>
                        {
                            lock (exceptions) exceptions.Add(ex);
                        });
                }

                var mre = new ManualResetEvent(false);

                sender.FlushAsync(ex =>
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }

                        mre.Set();
                    });

                mre.WaitOne();

                var actual = sender.Log.ToString();
                Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1);
                Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1);
                Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1);
                Assert.True(actual.IndexOf("send async 0 1 'q'") != -1);
                Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1);
                Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1);

                mre.Reset();
                for (int i = 1; i < 8; i *= 2)
                {
                    sender.Send(
                        buffer, 0, i, ex =>
                        {
                            lock (exceptions) exceptions.Add(ex);
                        });
                }

                sender.Close(ex =>
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }

                        mre.Set();
                    });

                mre.WaitOne();

                actual = sender.Log.ToString();
                
                Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1);
                Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1);
                Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1);
                Assert.True(actual.IndexOf("send async 0 1 'q'") != -1);
                Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1);
                Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1);
                Assert.True(actual.IndexOf("send async 0 1 'q'") != -1);
                Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1);
                Assert.True(actual.IndexOf("send async 0 4 'quic'") != -1);
                Assert.True(actual.IndexOf("close") != -1);

                foreach (var ex in exceptions)
                {
                    Assert.Null(ex);
                }
            }
        }
예제 #6
0
 public MockSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, MyTcpNetworkSender sender)
 {
     this.sender = sender;
     this.log = sender.Log;
     this.log.WriteLine("create socket {0} {1} {2}", addressFamily, socketType, protocolType);
 }
예제 #7
0
        public void TcpSendFailureTest()
        {
            var sender = new MyTcpNetworkSender("tcp://hostname:123", AddressFamily.Unspecified)
            {
                SendFailureIn = 3, // will cause failure on 3rd send
                Async = true,
            };

            sender.Initialize();
            byte[] buffer = Encoding.UTF8.GetBytes("quick brown fox jumps over the lazy dog");

            var exceptions = new Exception[9];

            var writeFinished = new ManualResetEvent(false);
            int remaining = exceptions.Length;

            for (int i = 1; i < 10; i++)
            {
                int pos = i - 1;

                sender.Send(
                    buffer, 0, i, ex =>
                    {
                        lock (exceptions)
                        {
                            exceptions[pos] = ex;
                            if (--remaining == 0)
                            {
                                writeFinished.Set();
                            }
                        }
                    });
            }

            var mre = new ManualResetEvent(false);
            writeFinished.WaitOne();
            sender.Close(ex => mre.Set());
            mre.WaitOne();

            var actual = sender.Log.ToString();
            Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1);
            Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1);
            Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1);
            Assert.True(actual.IndexOf("send async 0 1 'q'") != -1);
            Assert.True(actual.IndexOf("send async 0 2 'qu'") != -1);
            Assert.True(actual.IndexOf("send async 0 3 'qui'") != -1);
            Assert.True(actual.IndexOf("failed") != -1);
            Assert.True(actual.IndexOf("close") != -1);

            for (int i = 0; i < exceptions.Length; ++i)
            {
                if (i < 2)
                {
                    Assert.Null(exceptions[i]);
                }
                else
                {
                    Assert.NotNull(exceptions[i]);
                }
            }
        }
예제 #8
0
        public void TcpConnectFailureTest()
        {
            var sender = new MyTcpNetworkSender("tcp://hostname:123", AddressFamily.Unspecified)
            {
                ConnectFailure = 1,
                Async = true,
            };

            sender.Initialize();
            byte[] buffer = Encoding.UTF8.GetBytes("quick brown fox jumps over the lazy dog");

            var exceptions = new List<Exception>();
            var allSent = new ManualResetEvent(false);

            for (int i = 1; i < 8; i++)
            {
                sender.Send(
                    buffer, 0, i, ex =>
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                            if (exceptions.Count == 7)
                            {
                                allSent.Set();
                            }
                        }
                    });
            }

            Assert.True(allSent.WaitOne(3000, false));

            var mre = new ManualResetEvent(false);
            sender.FlushAsync(ex => mre.Set());
            mre.WaitOne(3000, false);

            var actual = sender.Log.ToString();

            Assert.True(actual.IndexOf("Parse endpoint address tcp://hostname:123/ Unspecified") != -1);
            Assert.True(actual.IndexOf("create socket 10000 Stream Tcp") != -1);
            Assert.True(actual.IndexOf("connect async to {mock end point: tcp://hostname:123/}") != -1);
            Assert.True(actual.IndexOf("failed") != -1);

            foreach (var ex in exceptions)
            {
                Assert.NotNull(ex);
            }
        }