public async Task TransferLargeArray()
        {
            using (var client1 = new TcpStreamClient())
            {
                _clientReceivedEvent.Reset();
                _receivedBytes = null;
                _expectedCount = 100000;
                await client1.ConnectAsync("127.0.0.1", _service.ListeningEndPoint.Port);
                client1.Start(OnLargeArrayReceivedByClient);

                var writer = new BinaryWriter(client1.OutputStream);

                var sent = 0;
                while (sent < _expectedCount)
                {
                    int n = System.Math.Min(sent + 1, _expectedCount - sent);
                    var buf = new byte[n];
                    for (int i = 0; i < n; i++)
                    {
                        buf[i] = (byte)sent++;
                    }

                    writer.Write(buf);
                    client1.OutputStream.Flush(); // synchronous
                    Thread.Sleep(10);
                }

                Assert.IsTrue(_clientReceivedEvent.Wait(NormalTimeout));
                Assert.IsNotNull(_receivedBytes);
                Assert.AreEqual(_expectedCount, _receivedBytes.Length);
                for (int i = 0; i < _expectedCount; i++)
                {
                    if (_receivedBytes[i] != (byte)i)
                    {
                        Assert.Fail("received {0} at index {1}", _receivedBytes[i], i);
                    }
                }
            }
        }
 public async Task ConnectClientUnsuccessful()
 {
     var client1 = new TcpStreamClient();
     try
     {
         Assert.IsNull(client1.RemoteEndPoint);
         _serviceConnectedEvent.Reset();
         try
         {
             await client1.ConnectAsync("127.0.0.1", 9000);
         }
         catch (IOException)
         {
             Assert.IsNotNull(client1.RemoteEndPoint);
             Assert.IsFalse(client1.IsConnected);
             _serviceConnectedEvent.Wait(100);
             Assert.IsNull(_stub1);
             Assert.IsNull(_stub2);
             return;
         }
         Assert.Fail("expected IOException");
     }
     finally
     {
         client1.Dispose();
     }
 }
 public async Task TransferSendFast()
 {
     using (var client1 = new TcpStreamClient())
     {
         _clientReceivedEvent.Reset();
         _receiveDelay = 50;
         await client1.ConnectAsync("127.0.0.1", _service.ListeningEndPoint.Port, OnDataReceivedByClient, OnClientChannelDisconnected);
         var writer = new BinaryWriter(client1.OutputStream);
         var sent = 1234567890.987654321m;
         writer.Write(sent);
         client1.OutputStream.Flush(); // synchronous
         Assert.IsTrue(_clientReceivedEvent.Wait(NormalTimeout));
         Assert.AreEqual(sent, _receivedDec);
     }
 }
        public async Task ConnectClientsSuccessful()
        {
            var client1 = new TcpStreamClient();
            var client2 = new TcpStreamClient();
            try
            {
                Assert.IsFalse(client1.IsConnected);
                Assert.IsFalse(client2.IsConnected);
                Assert.IsNull(_stub1);
                Assert.IsNull(_stub2);

                _serviceConnectedEvent.Reset();
                await client1.ConnectAsync("127.0.0.1", _service.ListeningEndPoint.Port);
                Assert.IsTrue(client1.IsConnected);
                _serviceConnectedEvent.Wait(NormalTimeout);
                Assert.IsTrue(_stub1.IsConnected);
                Assert.IsNull(_stub2);

                _serviceConnectedEvent.Reset();
                await client2.ConnectAsync("127.0.0.1", _service.ListeningEndPoint.Port);
                Assert.IsTrue(client1.IsConnected);
                Assert.IsTrue(client2.IsConnected);
                Assert.IsTrue(_stub1.IsConnected);
                _serviceConnectedEvent.Wait(NormalTimeout);
                Assert.IsTrue(_stub2.IsConnected);
            }
            finally
            {
                client1.Dispose();
                client2.Dispose();
            }
        }