Example #1
0
        public void TryWriteBeyondBuffer()
        {
            var fake   = new FakeWriter();
            var writer = new BufferedClientStreamWriter <string>(fake, 2);
            var task1  = writer.WriteAsync("1");
            var task2  = writer.WriteAsync("2");
            var task3  = writer.TryWriteAsync("3");

            Assert.Null(task3);         // Couldn't write.

            fake.CompleteCurrentTask(); // Message 1
            WaitForSpace(writer);

            // Now the buffer is smaller, we can write again.
            var task4 = writer.TryWriteAsync("4");

            Assert.NotNull(task4);
            // Try to write completion, will fail as buffer is full.
            var completionTask1 = writer.TryWriteCompleteAsync();

            Assert.Null(completionTask1);

            fake.CompleteCurrentTask(); // Message 2
            WaitForSpace(writer);

            // Now the buffer is smaller, we can write completion.
            var completionTask2 = writer.TryWriteCompleteAsync();

            Assert.NotNull(completionTask2);

            fake.CompleteCurrentTask(); // Message 4
            fake.CompleteCurrentTask(); // Completion

            AssertCompletedWithStatus(task1, TaskStatus.RanToCompletion);
            AssertCompletedWithStatus(task2, TaskStatus.RanToCompletion);
            AssertCompletedWithStatus(task4, TaskStatus.RanToCompletion);
            AssertCompletedWithStatus(completionTask2, TaskStatus.RanToCompletion);

            fake.AssertMessages("1", "2", "4");
            fake.AssertCompleted();
        }
Example #2
0
        public void BehaviorAfterCompleteCalled()
        {
            var fake   = new FakeWriter();
            var writer = new BufferedClientStreamWriter <string>(fake, 5);

            writer.WriteCompleteAsync();

            // The (object) casts are because to make xUnit understand that the call itself should throw;
            // we don't return a failed task.
            Assert.Throws <InvalidOperationException>(() => (object)writer.WriteCompleteAsync());
            Assert.Throws <InvalidOperationException>(() => (object)writer.WriteAsync("x"));
            Assert.Null(writer.TryWriteAsync("y"));
        }
 /// <inheritdoc/>
 public override Task TryWriteAsync(StreamingRecognizeRequest message) =>
 _writeBuffer.TryWriteAsync(ModifyRequest(message));