Ejemplo n.º 1
0
        public async Task DoesntRetryNonRetryableExceptions()
        {
            var stream1 = new MockReadStream(100, throwAfter: 50, canSeek: true);
            var stream2 = new MockReadStream(50, offset: 50, throwAfter: 0, exceptionType: typeof(InvalidOperationException));

            MockTransport mockTransport = CreateMockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            },
                new MockResponse(200)
            {
                ContentStream = stream2
            }
                );
            var pipeline = new HttpPipeline(mockTransport);

            Stream reliableStream = await CreateAsync(
                offset => SendTestRequest(pipeline, offset),
                offset => SendTestRequestAsync(pipeline, offset),
                ResponseClassifier.Shared,
                maxRetries : 5);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 0, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(25, reliableStream.Position);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 25, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(50, reliableStream.Position);

            Assert.ThrowsAsync <InvalidOperationException>(() => reliableStream.ReadAsync(_buffer, 50, 50));

            AssertReads(_buffer, 50);
        }
        public async Task DoesntCallLengthPrematurely()
        {
            var stream1 = new NoLengthStream();
            var stream2 = new MockReadStream(50);

            var mockTransport = new MockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            },
                new MockResponse(200)
            {
                ContentStream = stream2
            }
                );
            var pipeline = new HttpPipeline(mockTransport);

            var reliableStream = RetriableStream.Create(await SendTestRequestAsync(pipeline, 0), offset => SendTestRequestAsync(pipeline, offset), new ResponseClassifier(), maxRetries: 5);

            Assert.AreEqual(50, await reliableStream.ReadAsync(_buffer, 0, 50));
            Assert.AreEqual(50, reliableStream.Position);

            Assert.AreEqual(0, await reliableStream.ReadAsync(_buffer, 0, 50));

            Assert.Throws <NotSupportedException>(() => _ = reliableStream.Length);

            AssertReads(_buffer, 50);
        }
        public async Task DoesntRetryNonRetryableExceptions()
        {
            var stream1 = new MockReadStream(100, throwAfter: 50);
            var stream2 = new MockReadStream(50, offset: 50, throwAfter: 0, throwIOException: false);

            var mockTransport = new MockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            },
                new MockResponse(200)
            {
                ContentStream = stream2
            }
                );
            var pipeline = new HttpPipeline(mockTransport);

            var reliableStream = await RetriableStream.Create(offset => SendTestRequestAsync(pipeline, offset), new ResponseClassifier(), maxRetries : 5);

            Assert.AreEqual(25, await reliableStream.ReadAsync(_buffer, 0, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(25, reliableStream.Position);

            Assert.AreEqual(25, await reliableStream.ReadAsync(_buffer, 25, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(50, reliableStream.Position);

            Assert.ThrowsAsync <InvalidOperationException>(() => reliableStream.ReadAsync(_buffer, 50, 50));

            AssertReads(_buffer, 50);
        }
        public async Task ThrowsCorrectExceptionOnCustomerCancellationTokens(Type initial, Type translated)
        {
            // not supported on sync
            if (!IsAsync)
            {
                Assert.Ignore();
            }

            var stream1 = new MockReadStream(100, throwAfter: 25, canSeek: true, exceptionType: initial);

            MockTransport mockTransport = CreateMockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            });
            var pipeline = new HttpPipeline(mockTransport);

            Stream reliableStream = await CreateAsync(
                offset => SendTestRequest(pipeline, offset),
                offset => SendTestRequestAsync(pipeline, offset),
                ResponseClassifier.Shared,
                maxRetries : 5);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 0, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(25, reliableStream.Position);

            var exception = await AsyncAssert.ThrowsAsync <Exception>(
                async() => await ReadAsync(reliableStream, _buffer, 25, 25, new CancellationToken(true)));

            Assert.IsInstanceOf(translated, exception);

            AssertReads(_buffer, 25);
        }
        public async Task ThrowsIfSendingRetryRequestThrows()
        {
            var stream1       = new MockReadStream(100, throwAfter: 50);
            var mockTransport = new MockTransport(new MockResponse(200)
            {
                ContentStream = stream1
            });

            var pipeline = new HttpPipeline(mockTransport);

            var reliableStream = await RetriableStream.Create(
                async offset =>
            {
                if (offset == 0)
                {
                    return(await SendTestRequestAsync(pipeline, offset));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }, new ResponseClassifier(), maxRetries : 5);

            await reliableStream.ReadAsync(_buffer, 0, 25);

            await reliableStream.ReadAsync(_buffer, 25, 25);

            AssertReads(_buffer, 50);
            Assert.ThrowsAsync <InvalidOperationException>(() => reliableStream.ReadAsync(_buffer, 50, 50));
        }
        public async Task MaintainsGlobalLengthAndPosition()
        {
            var stream1 = new MockReadStream(100, throwAfter: 50);
            var stream2 = new MockReadStream(50, offset: 50, throwIOException: false);

            var mockTransport = new MockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            },
                new MockResponse(200)
            {
                ContentStream = stream2
            }
                );
            var pipeline = new HttpPipeline(mockTransport);

            var reliableStream = await RetriableStream.Create(offset => SendTestRequestAsync(pipeline, offset), new ResponseClassifier(), maxRetries : 5);

            Assert.AreEqual(25, await reliableStream.ReadAsync(_buffer, 0, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(25, reliableStream.Position);

            Assert.AreEqual(25, await reliableStream.ReadAsync(_buffer, 25, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(50, reliableStream.Position);

            Assert.AreEqual(50, await reliableStream.ReadAsync(_buffer, 50, 50));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(100, reliableStream.Position);

            Assert.AreEqual(0, await reliableStream.ReadAsync(_buffer, 0, 50));
            AssertReads(_buffer, 100);
        }
        public async Task DoesntRetryCustomerCancellationTokens()
        {
            // not supported on sync
            if (!IsAsync)
            {
                Assert.Ignore();
            }

            var stream1 = new MockReadStream(100);

            MockTransport mockTransport = CreateMockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            });
            var pipeline = new HttpPipeline(mockTransport);

            Stream reliableStream = await CreateAsync(
                offset => SendTestRequest(pipeline, offset),
                offset => SendTestRequestAsync(pipeline, offset),
                new ResponseClassifier(),
                maxRetries : 5);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 0, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(25, reliableStream.Position);

            Assert.ThrowsAsync <OperationCanceledException>(async() => await ReadAsync(reliableStream, _buffer, 0, 25, new CancellationToken(true)));

            AssertReads(_buffer, 25);
        }
Ejemplo n.º 8
0
        public async Task DisposesStreams()
        {
            var stream1 = new MockReadStream(100, throwAfter: 50);
            var stream2 = new MockReadStream(50, offset: 50);

            MockTransport mockTransport = CreateMockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            },
                new MockResponse(200)
            {
                ContentStream = stream2
            }
                );
            var pipeline = new HttpPipeline(mockTransport);

            Stream reliableStream = await CreateAsync(
                offset => SendTestRequest(pipeline, offset),
                offset => SendTestRequestAsync(pipeline, offset),
                ResponseClassifier.Shared, maxRetries : 5);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 0, 25));
            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 25, 25));
            Assert.AreEqual(50, await ReadAsync(reliableStream, _buffer, 50, 50));
            Assert.True(stream1.IsDisposed);

            Assert.AreEqual(0, await ReadAsync(reliableStream, _buffer, 0, 50));
            Assert.False(stream2.IsDisposed);

            reliableStream.Dispose();
            Assert.True(stream2.IsDisposed);

            AssertReads(_buffer, 100);
        }
Ejemplo n.º 9
0
 public async Task ReadDataFromStreamNewStreamNeeded()
 {
     Assert.AreSame(Data, await FileStreamer.ReadDataFromStream <BasicData>(Filename));
     MockFile.Verify(x => x.OpenRead(Filename), Times.Once);
     MockReadStream.Verify(x => x.Close(), Times.Once);
     MockReadStream.Verify(x => x.Dispose(), Times.Once);
     MockMessagePackSerializer.Verify(x => x.Deserialize <BasicData>(ReadStream), Times.Once);
 }
Ejemplo n.º 10
0
        public async Task <bool> CloseStreamWhenThereIsNoStream()
        {
            var result = FileStreamer.CloseStream(Filename);

            MockWriteStream
            .Verify(x => x.Close(), Times.Never);
            MockReadStream
            .Verify(x => x.Close(), Times.Never);
            return(await result);
        }
Ejemplo n.º 11
0
        public async Task DisposeOfStreamWhenThereIsNoStream()
        {
            await FileStreamer.DisposeOfStream(Filename);

            MockWriteStream
            .Verify(x => x.Close(), Times.Never);
            MockReadStream
            .Verify(x => x.Dispose(), Times.Never);
            MockWriteStream
            .Verify(x => x.Close(), Times.Never);
            MockReadStream
            .Verify(x => x.Dispose(), Times.Never);
        }
Ejemplo n.º 12
0
        public async Task DisposeOfStreamWhenThereIsAReadStream()
        {
            await FileStreamer.ReadDataFromStream <BasicData>(Filename);

            await FileStreamer.DisposeOfStream(Filename);

            MockWriteStream
            .Verify(x => x.Close(), Times.Never);
            MockWriteStream
            .Verify(x => x.Dispose(), Times.Never);
            MockReadStream
            .Verify(x => x.Close(), Times.Once);
            MockReadStream
            .Verify(x => x.Dispose(), Times.Once);
        }
Ejemplo n.º 13
0
        public async Task <bool> CloseStreamWhenThereIsAWriteStream()
        {
            await FileStreamer.WriteDataToStream(Filename, Data);

            var result = FileStreamer.CloseStream(Filename);

            MockWriteStream
            .Verify(x => x.Close(), Times.Once);
            MockWriteStream
            .Verify(x => x.Dispose(), Times.Once);
            MockReadStream
            .Verify(x => x.Close(), Times.Never);
            MockReadStream
            .Verify(x => x.Dispose(), Times.Never);
            return(await result);
        }
Ejemplo n.º 14
0
        public async Task DisposeOfStreamWhenThereIsAWriteStreamIsAlreadyDisposed()
        {
            await FileStreamer.WriteDataToStream(Filename, Data);

            await FileStreamer.DisposeOfStream(Filename);

            await FileStreamer.DisposeOfStream(Filename);

            MockWriteStream
            .Verify(x => x.Close(), Times.Never);
            MockWriteStream
            .Verify(x => x.Dispose(), Times.Once);
            MockReadStream
            .Verify(x => x.Close(), Times.Never);
            MockReadStream
            .Verify(x => x.Dispose(), Times.Never);
        }
Ejemplo n.º 15
0
        public async Task <bool> CloseStreamWhenThereIsAReadStreamIsAlreadyClosed()
        {
            await FileStreamer.ReadDataFromStream <BasicData>(Filename);

            await FileStreamer.CloseStream(Filename);

            var result = FileStreamer.CloseStream(Filename);

            MockWriteStream
            .Verify(x => x.Close(), Times.Never);
            MockWriteStream
            .Verify(x => x.Dispose(), Times.Never);
            MockReadStream
            .Verify(x => x.Close(), Times.Once);
            MockReadStream
            .Verify(x => x.Dispose(), Times.Once);
            return(await result);
        }
        public async Task RetriesOnNonCustomerCancellationToken()
        {
            var stream1 = new MockReadStream(100, throwAfter: 50, exceptionType: typeof(OperationCanceledException));
            var stream2 = new MockReadStream(50, offset: 50);

            MockTransport mockTransport = CreateMockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            },
                new MockResponse(200)
            {
                ContentStream = stream2
            }
                );
            var pipeline = new HttpPipeline(mockTransport);

            Stream reliableStream = await CreateAsync(
                offset => SendTestRequest(pipeline, offset),
                offset => SendTestRequestAsync(pipeline, offset),
                new ResponseClassifier(),
                maxRetries : 5);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 0, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(25, reliableStream.Position);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 25, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(50, reliableStream.Position);

            Assert.AreEqual(50, await ReadAsync(reliableStream, _buffer, 50, 50));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(100, reliableStream.Position);

            AssertReads(_buffer, 100);
        }
Ejemplo n.º 17
0
        public async Task MaintainsGlobalLengthAndPosition()
        {
            var stream1 = new MockReadStream(100, throwAfter: 50, canSeek: true);
            var stream2 = new MockReadStream(50, offset: 50);

            MockTransport mockTransport = CreateMockTransport(
                new MockResponse(200)
            {
                ContentStream = stream1
            },
                new MockResponse(200)
            {
                ContentStream = stream2
            }
                );
            var pipeline = new HttpPipeline(mockTransport);

            Stream reliableStream = await CreateAsync(
                offset => SendTestRequest(pipeline, offset),
                offset => SendTestRequestAsync(pipeline, offset),
                ResponseClassifier.Shared, maxRetries : 5);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 0, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(25, reliableStream.Position);

            Assert.AreEqual(25, await ReadAsync(reliableStream, _buffer, 25, 25));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(50, reliableStream.Position);

            Assert.AreEqual(50, await ReadAsync(reliableStream, _buffer, 50, 50));
            Assert.AreEqual(100, reliableStream.Length);
            Assert.AreEqual(100, reliableStream.Position);

            Assert.AreEqual(0, await ReadAsync(reliableStream, _buffer, 0, 50));
            AssertReads(_buffer, 100);
        }