Ejemplo n.º 1
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));
 }
Ejemplo n.º 2
0
        public void TestPartialWriteDoesNotTimeout()
        {
            var pipe = new Pipe { InputStream = { WriteTimeout = 0 }, OutputStream = { ReadTimeout = 1000 } };
            pipe.SetFixedLength();
            var text = Enumerable.Repeat((byte)'t', 3 * Constants.ByteBufferSize).ToArray();
            var asyncWrite = pipe.InputStream.WriteAsync(text, 0, text.Length);
            asyncWrite.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false);

            pipe.ReadTextAsync(text.Length).Result.ShouldEqual(new string(text.Select(b => (char)b).ToArray()));
        }
Ejemplo n.º 3
0
        public void TestLargeStreamWithFixedLength()
        {
            var pipe = new Pipe();
            pipe.SetFixedLength();

            var bytes = Enumerable.Range(0, 5 * Constants.ByteBufferSize)
                .Select(b => (byte)(b % 256))
                .ToArray();
            var asyncWrite = pipe.InputStream.WriteAsync(bytes, 0, bytes.Length)
                .ContinueWith(_ => pipe.InputStream.Close());
            var memoryStream = new MemoryStream();
            var buffer = new byte[777];
            int bytesRead;
            while ((bytesRead = pipe.OutputStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                memoryStream.Write(buffer, 0, bytesRead);
            }
            asyncWrite.Wait(TimeSpan.FromSeconds(1)).ShouldEqual(true);
            memoryStream.ToArray().SequenceEqual(bytes).ShouldEqual(true);
        }
Ejemplo n.º 4
0
        public void FuzzTest()
        {
            const int ByteCount = 100000;

            var pipe = new Pipe();
            var writeTask = Task.Run(async () =>
            {
                var memoryStream = new MemoryStream();
                var random = new Random(1234);
                var bytesWritten = 0;
                while (bytesWritten < ByteCount)
                {
                    //Console.WriteLine("Writing " + memoryStream.Length);
                    switch (random.Next(10))
                    {
                        case 1:
                            //Console.WriteLine("SETTING FIXED LENGTH");
                            pipe.SetFixedLength();
                            break;
                        case 2:
                        case 3:
                            await Task.Delay(1);
                            break;
                        default:
                            var bufferLength = random.Next(0, 5000);
                            var offset = random.Next(0, bufferLength + 1);
                            var count = random.Next(0, bufferLength - offset + 1);
                            var buffer = new byte[bufferLength];
                            random.NextBytes(buffer);
                            memoryStream.Write(buffer, offset, count);
                            //Console.WriteLine("WRITE START");
                            await pipe.InputStream.WriteAsync(buffer, offset, count);
                            //Console.WriteLine("WRITE END");
                            bytesWritten += count;
                            break;
                    }
                }
                //Console.WriteLine("WRITER ALL DONE");
                pipe.InputStream.Close();
                return memoryStream;
            });

            var readTask = Task.Run(async () =>
            {
                var memoryStream = new MemoryStream();
                var random = new Random(5678);
                while (true)
                {
                    //Console.WriteLine("Reading " + memoryStream.Length);
                    if (random.Next(10) == 1)
                    {
                        await Task.Delay(1);
                    }
                    var bufferLength = random.Next(0, 5000);
                    var offset = random.Next(0, bufferLength + 1);
                    var count = random.Next(0, bufferLength - offset + 1);
                    var buffer = new byte[bufferLength];
                    //Console.WriteLine("READ START");
                    var bytesRead = await pipe.OutputStream.ReadAsync(buffer, offset, count);
                    //Console.WriteLine("READ END");
                    if (bytesRead == 0 && count > 0)
                    {
                        // if we tried to read more than 1 byte and we got 0, the pipe is done
                        //Console.WriteLine("READER ALL DONE");
                        return memoryStream;
                    }
                    memoryStream.Write(buffer, offset, bytesRead);
                }
            });

            Task.WhenAll(writeTask, readTask).Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);

            CollectionAssert.AreEqual(writeTask.Result.ToArray(), readTask.Result.ToArray());
        }
Ejemplo n.º 5
0
        public void TestConcurrentWrites()
        {
            var pipe = new Pipe();
            pipe.SetFixedLength();

            var longText = new string('x', (2 * Constants.ByteBufferSize) + 1);
            var asyncWrite = pipe.WriteTextAsync(longText);
            asyncWrite.Wait(TimeSpan.FromSeconds(.01)).ShouldEqual(false);
            UnitTestHelpers.AssertThrows<InvalidOperationException>(() => pipe.InputStream.WriteByte(101));
            pipe.OutputStream.Close();
            asyncWrite.Wait(TimeSpan.FromSeconds(5)).ShouldEqual(true);
        }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 7
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);
        }