Exemple #1
0
        public void ByteStreamShouldReturnUponCancellation()
#endif
        {
            ISocket        socket        = A.Fake <ISocket>();
            INetworkStream networkStream = A.Fake <INetworkStream>();

            A.CallTo(() => socket.GetStream()).Returns(networkStream);
            A.CallTo(() => socket.Connected).Returns(true);
            A.CallTo(() => socket.Available).Returns(1);
            TcpByteStream tcpByteStream = new TcpByteStream(socket);

            A.CallTo(() => networkStream.ReadByte()).Returns(142);
            tcpByteStream.Connected.Should().BeTrue();
            CancellationTokenSource cancellationToken = new CancellationTokenSource();

            Stopwatch         stopwatch = new Stopwatch();
            ByteStreamHandler sut       = new ByteStreamHandler(tcpByteStream, cancellationToken);

#if ASYNC
            cancellationToken.CancelAfter(100);
            await sut.ReadAsync(TimeSpan.FromMilliseconds(1000));
#else
            Thread t = new Thread(new ThreadStart(() =>
            {
                AutoResetEvent are = new AutoResetEvent(false);
                are.WaitOne(100);
                cancellationToken.Cancel();
            }));
            t.Start();
            sut.Read(TimeSpan.FromMilliseconds(1000));
#endif

            stopwatch.ElapsedMilliseconds.Should().BeLessThan(500);
        }
Exemple #2
0
        public void GivenAFakedSocketACallToWriteShouldBeRelayed()
        {
            string         writtenString = Guid.NewGuid().ToString();
            ISocket        socket        = A.Fake <ISocket>();
            INetworkStream stream        = A.Fake <INetworkStream>();

            A.CallTo(() => socket.GetStream()).Returns(stream);
            TcpByteStream sut = new TcpByteStream(socket);

            sut.Write(writtenString);

            A.CallTo(() => stream.Write(A <byte[]> .Ignored, 0, writtenString.Length)).MustHaveHappened();
        }