コード例 #1
0
        public async Task CancelPendingFlushLostOfCancellationsNoDataLost()
        {
            var writeSize         = 16;
            var singleWriteStream = new SingleWriteStream();

            MemoryStream = singleWriteStream;
            Writer       = new StreamPipeWriter(MemoryStream, minimumSegmentSize: writeSize);

            for (var i = 0; i < 10; i++)
            {
                FlushResult flushResult  = new FlushResult();
                var         expectedData = Encoding.ASCII.GetBytes(new string('a', writeSize));

                var tcs = new TaskCompletionSource <int>(TaskCreationOptions.RunContinuationsAsynchronously);
                // TaskCreationOptions.RunAsync

                var task = Task.Run(async() =>
                {
                    try
                    {
                        // Create two Segments
                        // First one will succeed to write, other one will hang.
                        for (var j = 0; j < 2; j++)
                        {
                            Writer.Write(expectedData);
                        }

                        var flushTask = Writer.FlushAsync();
                        tcs.SetResult(0);
                        flushResult = await flushTask;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        throw ex;
                    }
                });

                await tcs.Task;

                Writer.CancelPendingFlush();

                await task;

                Assert.True(flushResult.IsCanceled);
            }

            // Only half of the data was written because every other flush failed.
            Assert.Equal(16 * 10, ReadWithoutFlush().Length);

            // Start allowing all writes to make read succeed.
            singleWriteStream.AllowAllWrites = true;

            Assert.Equal(16 * 10 * 2, Read().Length);
        }
コード例 #2
0
        public async Task CheckBasicWritePipeApi()
        {
            var pipe            = new Pipe();
            var writeOnlyStream = new WriteOnlyPipeStream(pipe.Writer);
            var pipeWriter      = new StreamPipeWriter(writeOnlyStream);
            await pipeWriter.WriteAsync(new byte[10]);

            var res = await pipe.Reader.ReadAsync();

            Assert.Equal(new byte[10], res.Buffer.ToArray());
        }
コード例 #3
0
        public void WriteMessageAsync_ExceedSendSize_ThrowError()
        {
            // Arrange
            var ms = new MemoryStream();
            var pipeWriter = new StreamPipeWriter(ms);

            // Act
            var ex = Assert.ThrowsAsync<InvalidOperationException>(() => pipeWriter.WriteMessageAsync(new byte[] { 0x10, 0x10 }, new GrpcServiceOptions { SendMaxMessageSize = 1 }, flush: true));

            // Assert
            Assert.AreEqual("Sending message exceeds the maximum configured message size.", ex.Message);
        }
コード例 #4
0
        public async Task CheckBasicWriteStreamApi()
        {
            var stream          = new MemoryStream();
            var pipeWriter      = new StreamPipeWriter(stream);
            var writeOnlyStream = new WriteOnlyPipeStream(pipeWriter);

            await writeOnlyStream.WriteAsync(new byte[10]);

            stream.Position = 0;
            var res = await ReadFromStreamAsByteArrayAsync(10, stream);

            Assert.Equal(new byte[10], res);
        }
コード例 #5
0
        public async Task WriteMessageAsync_NoFlush_WriteNoData()
        {
            // Arrange
            var ms = new MemoryStream();
            var pipeWriter = new StreamPipeWriter(ms);

            // Act
            await pipeWriter.WriteMessageAsync(Encoding.UTF8.GetBytes("Hello world"), TestServiceOptions);

            // Assert
            var messageData = ms.ToArray();
            Assert.AreEqual(0, messageData.Length);
        }
コード例 #6
0
        public async Task CanAdvanceWithPartialConsumptionOfFirstSegment(int firstWriteLength)
        {
            Writer = new StreamPipeWriter(Stream, MinimumSegmentSize, new TestMemoryPool(maxBufferSize: 20000));
            await Writer.WriteAsync(Encoding.ASCII.GetBytes("a"));

            var memory = Writer.GetMemory(firstWriteLength);

            Writer.Advance(firstWriteLength);

            memory = Writer.GetMemory();
            Writer.Advance(memory.Length);

            await Writer.FlushAsync();

            Assert.Equal(firstWriteLength + memory.Length + 1, Read().Length);
        }
コード例 #7
0
        public async Task CanSerializeBodyRequestCorrectly()
        {
            var stream = new MemoryStream();

            using (var streamPipeWriter = new StreamPipeWriter(stream))
            {
                TextAnalysis.WriteRequest(streamPipeWriter, "This is a body of text\n with a new line");

                await streamPipeWriter.FlushAsync();
            }
            stream.Flush();
            stream.Seek(0, SeekOrigin.Begin);
            var text = new StreamReader(stream, Encoding.UTF8).ReadToEnd();

            text.ShouldBe(@"{""documents"":[{""language"":""en"",""id"":""doc"",""text"":""This is a body of text\n with a new line""}]}");
        }
コード例 #8
0
        public void WriteMessageAsync_ExceedSendSize_ThrowError()
        {
            // Arrange
            var context = HttpContextServerCallContextHelper.CreateServerCallContext(serviceOptions: new GrpcServiceOptions {
                SendMaxMessageSize = 1
            });
            var ms         = new MemoryStream();
            var pipeWriter = new StreamPipeWriter(ms);

            // Act
            var ex = Assert.ThrowsAsync <RpcException>(() => pipeWriter.WriteMessageAsync(new byte[] { 0x10, 0x10 }, context, flush: true));

            // Assert
            Assert.AreEqual("Sending message exceeds the maximum configured message size.", ex.Status.Detail);
            Assert.AreEqual(StatusCode.ResourceExhausted, ex.StatusCode);
        }
コード例 #9
0
ファイル: TextAnalysis.cs プロジェクト: brunomlopes/tagurl
        public async Task <IReadOnlyCollection <string> > Suggest(string url, string title, string body, string[] skipTags)
        {
            var document = $"{title}\n{body}".Trim();

            if (string.IsNullOrWhiteSpace(document))
            {
                return new string[] { }
            }
            ;

            var request    = new HttpRequestMessage(HttpMethod.Post, "https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/entities");
            var bodyStream = new MemoryStream();

            using (var streamPipeWriter = new StreamPipeWriter(bodyStream))
            {
                WriteRequest(streamPipeWriter, document);

                await streamPipeWriter.FlushAsync();
            }
            bodyStream.Seek(0, SeekOrigin.Begin);
            request.Content = new StreamContent(bodyStream)
            {
                Headers = { { "Content-Type", MediaTypeNames.Application.Json } }
            };

            var response = await client.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                throw new InvalidOperationException(response.ReasonPhrase + "\n" + await response.Content.ReadAsStringAsync());
                return(new string[] { });
            }

            var responseDoc = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());

            var doc = responseDoc.RootElement.GetProperty("documents")
                      .EnumerateArray()
                      .First(d => d.GetProperty("id").GetString() == _singleDocId);

            return(doc.GetProperty("entities")
                   .EnumerateArray()
                   .Select(entity => (name: entity.GetProperty("name").GetString(),
                                      matchCount: entity.GetProperty("matches").EnumerateArray().Count()))
                   .OrderByDescending(k => k.matchCount)
                   .Select(k => k.name.ToLowerInvariant().Replace(" ", "-"))
                   .ToArray());
        }
コード例 #10
0
        public async Task CheckNestedPipeApi()
        {
            var pipe   = new Pipe();
            var writer = pipe.Writer;

            for (var i = 0; i < 3; i++)
            {
                var writeOnlyStream = new WriteOnlyPipeStream(writer);
                writer = new StreamPipeWriter(writeOnlyStream);
            }

            await writer.WriteAsync(new byte[10]);

            var res = await pipe.Reader.ReadAsync();

            Assert.Equal(new byte[10], res.Buffer.ToArray());
        }
コード例 #11
0
        public async Task CheckNestedStreamApi()
        {
            var    stream          = new MemoryStream();
            Stream writeOnlyStream = stream;

            for (var i = 0; i < 3; i++)
            {
                var pipeWriter = new StreamPipeWriter(writeOnlyStream);
                writeOnlyStream = new WriteOnlyPipeStream(pipeWriter);
            }

            await writeOnlyStream.WriteAsync(new byte[10]);

            stream.Position = 0;
            var res = await ReadFromStreamAsByteArrayAsync(10, stream);

            Assert.Equal(new byte[10], res);
        }
コード例 #12
0
        public async Task WriteMessageAsync_EmptyMessage_WriteMessageWithNoData()
        {
            // Arrange
            var ms         = new MemoryStream();
            var pipeWriter = new StreamPipeWriter(ms);

            // Act
            await pipeWriter.WriteMessageAsync(Array.Empty <byte>(), TestServerCallContext, flush : true);

            // Assert
            var messageData = ms.ToArray();

            CollectionAssert.AreEqual(
                new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00,
                0x00,
                0x00,     // length = 0
            },
                messageData);
        }
コード例 #13
0
        public async Task CancelPendingFlushBetweenWritesAllDataIsPreserved()
        {
            MemoryStream = new SingleWriteStream();
            Writer       = new StreamPipeWriter(MemoryStream);
            FlushResult flushResult = new FlushResult();

            var tcs = new TaskCompletionSource <int>(TaskCreationOptions.RunContinuationsAsynchronously);

            var task = Task.Run(async() =>
            {
                try
                {
                    await Writer.WriteAsync(Encoding.ASCII.GetBytes("data"));

                    var writingTask = Writer.WriteAsync(Encoding.ASCII.GetBytes(" data"));
                    tcs.SetResult(0);
                    flushResult = await writingTask;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
            });

            await tcs.Task;

            Writer.CancelPendingFlush();

            await task;

            Assert.True(flushResult.IsCanceled);

            await Writer.WriteAsync(Encoding.ASCII.GetBytes(" more data"));

            Assert.Equal(Encoding.ASCII.GetBytes("data data more data"), Read());
        }
コード例 #14
0
        public async Task WriteMessageAsync_OneByteMessage_WriteData()
        {
            // Arrange
            var ms         = new MemoryStream();
            var pipeWriter = new StreamPipeWriter(ms);

            // Act
            await pipeWriter.WriteMessageAsync(new byte[] { 0x10 }, flush : true);

            // Assert
            var messageData = ms.ToArray();

            CollectionAssert.AreEqual(
                new byte[]
            {
                0x00,     // compression = 0
                0x00,
                0x00,
                0x00,
                0x01,     // length = 1
                0x10
            },
                messageData);
        }
コード例 #15
0
        public async Task WriteMessageAsync_HasCustomCompressionLevel_WriteCompressedDataWithLevel()
        {
            // Arrange
            var mockCompressionProvider = new MockCompressionProvider();
            var serviceOptions          = new GrpcServiceOptions
            {
                ResponseCompressionAlgorithm = "Mock",
                ResponseCompressionLevel     = System.IO.Compression.CompressionLevel.Optimal,
                CompressionProviders         =
                {
                    mockCompressionProvider
                }
            };

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Headers[GrpcProtocolConstants.MessageAcceptEncodingHeader] = "Mock";

            var context = HttpContextServerCallContextHelper.CreateServerCallContext(httpContext, serviceOptions);

            context.Initialize();

            var ms         = new MemoryStream();
            var pipeWriter = new StreamPipeWriter(ms);

            // Act
            await pipeWriter.WriteMessageAsync(new byte[] { 0x10 }, context, flush : true);

            // Assert
            Assert.AreEqual(System.IO.Compression.CompressionLevel.Optimal, mockCompressionProvider.ArgumentCompression);

            var messageData = ms.ToArray();

            Assert.AreEqual(1, messageData[0]);  // compression
            Assert.AreEqual(21, messageData[4]); // message length
        }
コード例 #16
0
ファイル: MessageHelpers.cs プロジェクト: lhutyra/grpc-dotnet
        public static void WriteMessage(Stream stream, byte[] message)
        {
            var pipeWriter = new StreamPipeWriter(stream);

            PipeExtensions.WriteMessageAsync(pipeWriter, message, flush: true).GetAwaiter().GetResult();
        }
コード例 #17
0
ファイル: PipeTest.cs プロジェクト: poke/aspnet_AspNetCore
 protected PipeTest()
 {
     MemoryStream = new MemoryStream();
     Writer       = new StreamPipeWriter(MemoryStream, MinimumSegmentSize, new TestMemoryPool());
     Reader       = new StreamPipeReader(MemoryStream, new StreamPipeReaderOptions(MinimumSegmentSize, minimumReadThreshold: 256, new TestMemoryPool()));
 }
コード例 #18
0
 protected PipeTest()
 {
     MemoryStream = new MemoryStream();
     Writer       = new StreamPipeWriter(MemoryStream, MinimumSegmentSize, new TestMemoryPool());
     Reader       = new StreamPipeReader(MemoryStream, MinimumSegmentSize, new TestMemoryPool());
 }
コード例 #19
0
 public async Task WriteCanBeCancelledViaProvidedCancellationToken()
 {
     var pipeWriter = new StreamPipeWriter(new HangingStream());
     var cts        = new CancellationTokenSource(1);
     await Assert.ThrowsAsync <TaskCanceledException>(async() => await pipeWriter.WriteAsync(Encoding.ASCII.GetBytes("data"), cts.Token));
 }
コード例 #20
0
 public void Setup()
 {
     _memoryStream = new NoopStream();
     _pipeWriter   = new StreamPipeWriter(_memoryStream);
 }