Example #1
0
        public void TimeoutTest()
        {
            var pipe = new Pipe { OutputStream = { ReadTimeout = 0 } };
            UnitTestHelpers.AssertThrows<TimeoutException>(() => pipe.OutputStream.ReadByte());

            pipe.WriteText(new string('a', 2048));
            pipe.ReadTextAsync(2048).Result.ShouldEqual(new string('a', 2048));
        }
Example #2
0
 public void TestWriteTimeout()
 {
     var pipe = new Pipe { InputStream = { WriteTimeout = 0 } };
     pipe.SetFixedLength();
     pipe.WriteText(new string('a', 2 * Constants.ByteBufferSize));
     var asyncWrite = pipe.InputStream.WriteAsync(new byte[1], 0, 1);
     asyncWrite.ContinueWith(_ => { }).Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(true);
     asyncWrite.IsFaulted.ShouldEqual(true);
     Assert.IsInstanceOfType(asyncWrite.Exception.InnerException, typeof(TimeoutException));
 }
Example #3
0
        public void SimpleTest()
        {
            var pipe = new Pipe();

            pipe.WriteText("abc");
            pipe.ReadTextAsync(3).Result.ShouldEqual("abc");

            pipe.WriteText("1");
            pipe.WriteText("2");
            pipe.WriteText("3");
            pipe.ReadTextAsync(3).Result.ShouldEqual("123");

            var asyncRead = pipe.ReadTextAsync(100);
            asyncRead.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false, asyncRead.IsCompleted ? "Found: " + (asyncRead.Result ?? "null") : "not complete");
            pipe.WriteText("x");
            asyncRead.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false, asyncRead.IsCompleted ? "Found: " + (asyncRead.Result ?? "null") : "not complete");
            pipe.WriteText(new string('y', 100));
            asyncRead.Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true, asyncRead.IsCompleted ? "Found: " + (asyncRead.Result ?? "null") : "not complete");
            asyncRead.Result.ShouldEqual("x" + new string('y', 99));
        }
Example #4
0
        public void TestCancel()
        {
            var pipe = new Pipe();
            var cancellationTokenSource = new CancellationTokenSource();

            var asyncRead = pipe.ReadTextAsync(1, cancellationTokenSource.Token);
            asyncRead.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false);
            cancellationTokenSource.Cancel();
            asyncRead.ContinueWith(_ => { }).Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
            asyncRead.IsCanceled.ShouldEqual(true);

            pipe.WriteText("aaa");
            pipe.ReadTextAsync(2).Result.ShouldEqual("aa");

            asyncRead = pipe.ReadTextAsync(1, cancellationTokenSource.Token);
            asyncRead.ContinueWith(_ => { }).Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
            asyncRead.IsCanceled.ShouldEqual(true);
        }
Example #5
0
        public void TestCloseReadSide()
        {
            var pipe = new Pipe();
            pipe.WriteText("abc");
            pipe.ReadTextAsync(2).Result.ShouldEqual("ab");
            pipe.OutputStream.Close();
            UnitTestHelpers.AssertThrows<ObjectDisposedException>(() => pipe.OutputStream.ReadByte());

            var largeBytes = new byte[10 * 1024];
            var initialMemory = GC.GetTotalMemory(forceFullCollection: true);
            for (var i = 0; i < int.MaxValue / 1024; ++i)
            {
                pipe.InputStream.Write(largeBytes, 0, largeBytes.Length);
            }
            var finalMemory = GC.GetTotalMemory(forceFullCollection: true);

            Assert.IsTrue(finalMemory - initialMemory < 10 * largeBytes.Length, "final = " + finalMemory + " initial = " + initialMemory);

            UnitTestHelpers.AssertThrows<ObjectDisposedException>(() => pipe.OutputStream.ReadByte());

            pipe.InputStream.Close();
        }
Example #6
0
        public void TestCloseWriteSide()
        {
            var pipe = new Pipe();
            pipe.WriteText("123456");
            pipe.InputStream.Close();
            UnitTestHelpers.AssertThrows<ObjectDisposedException>(() => pipe.InputStream.WriteByte(1));

            pipe.ReadTextAsync(5).Result.ShouldEqual("12345");
            pipe.ReadTextAsync(2).Result.ShouldEqual(null);

            pipe = new Pipe();
            var asyncRead = pipe.ReadTextAsync(1);
            asyncRead.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false);
            pipe.InputStream.Close();
            asyncRead.Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
        }
Example #7
0
        public void TestPartialWriteDoesNotCancel()
        {
            var pipe = new Pipe();
            var cancellationTokenSource = new CancellationTokenSource();

            pipe.WriteText(new string('a', 2 * Constants.ByteBufferSize));

            var asyncRead = pipe.ReadTextAsync(1);
            asyncRead.Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
            asyncRead.Result.ShouldEqual("a");
            
            pipe.SetFixedLength();

            var bytes = Enumerable.Repeat((byte)'b', Constants.ByteBufferSize).ToArray();
            var asyncWrite = pipe.InputStream.WriteAsync(bytes, 0, bytes.Length, cancellationTokenSource.Token);
            asyncWrite.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false);
            cancellationTokenSource.Cancel();
            asyncWrite.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false);

            asyncRead = pipe.ReadTextAsync((3 * Constants.ByteBufferSize) - 1);
            asyncRead.Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
            asyncRead.Result.ShouldEqual(new string('a', (2 * Constants.ByteBufferSize) - 1) + new string('b', Constants.ByteBufferSize));
        }
Example #8
0
        public void TestCancelWrite()
        {
            var pipe = new Pipe();
            var cancellationTokenSource = new CancellationTokenSource();

            pipe.WriteText(new string('a', 2 * Constants.ByteBufferSize));

            pipe.SetFixedLength();

            var bytes = Enumerable.Repeat((byte)'b', Constants.ByteBufferSize).ToArray();
            var asyncWrite = pipe.InputStream.WriteAsync(bytes, 0, bytes.Length, cancellationTokenSource.Token);
            asyncWrite.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false);
            cancellationTokenSource.Cancel();
            asyncWrite.ContinueWith(_ => { }).Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
            asyncWrite.IsCanceled.ShouldEqual(true);
        }