From 88c147ab679cea58f949075cb55b68a9d0c9faf7 Mon Sep 17 00:00:00 2001 From: Nelson Parente Date: Mon, 5 Oct 2026 02:33:49 +0200 Subject: [PATCH] test(cryptography): wait for CompleteAsync in the empty-stream tests ProcessStreamAsync writes requests and reads responses on two separate tasks. With a mocked response stream that ends at once, the reader can complete the output channel before the writer calls CompleteAsync, so Verify(CompleteAsync, Times.Once) can run too early. Wait for the request stream to complete before the verification, as the other tests in these files already do. Signed-off-by: Nelson Parente Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017JhKUDV9dbs5JDz2uwZqz5 --- .../Encryption/DecryptionStreamProcessorTests.cs | 4 ++++ .../Encryption/EncryptionStreamProcessorTests.cs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/test/Dapr.Cryptography.Test/Encryption/DecryptionStreamProcessorTests.cs b/test/Dapr.Cryptography.Test/Encryption/DecryptionStreamProcessorTests.cs index d2049e32e..f9bb22a9f 100644 --- a/test/Dapr.Cryptography.Test/Encryption/DecryptionStreamProcessorTests.cs +++ b/test/Dapr.Cryptography.Test/Encryption/DecryptionStreamProcessorTests.cs @@ -185,9 +185,12 @@ public async Task ProcessStreamAsync_AssignsSequenceNumbersInOrder() [Fact] public async Task ProcessStreamAsync_WithEmptyStream_SendsNoRequests() { + var requestStreamCompleted = new TaskCompletionSource(); + var mockRequestStream = new Mock>(); mockRequestStream .Setup(x => x.CompleteAsync()) + .Callback(() => requestStreamCompleted.SetResult()) .Returns(Task.CompletedTask); var mockResponseStream = new Mock>(); @@ -203,6 +206,7 @@ public async Task ProcessStreamAsync_WithEmptyStream_SendsNoRequests() using var processor = new DecryptionStreamProcessor(); await processor.ProcessStreamAsync(inputStream, call, 4096, options, TestContext.Current.CancellationToken); await foreach (var _ in processor.GetProcessedDataAsync(TestContext.Current.CancellationToken)) { } + await requestStreamCompleted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken); mockRequestStream.Verify( x => x.WriteAsync(It.IsAny(), It.IsAny()), diff --git a/test/Dapr.Cryptography.Test/Encryption/EncryptionStreamProcessorTests.cs b/test/Dapr.Cryptography.Test/Encryption/EncryptionStreamProcessorTests.cs index 5169ab2ac..65e31a265 100644 --- a/test/Dapr.Cryptography.Test/Encryption/EncryptionStreamProcessorTests.cs +++ b/test/Dapr.Cryptography.Test/Encryption/EncryptionStreamProcessorTests.cs @@ -186,9 +186,12 @@ public async Task ProcessStreamAsync_AssignsSequenceNumbersInOrder() [Fact] public async Task ProcessStreamAsync_WithEmptyStream_SendsNoRequests() { + var requestStreamCompleted = new TaskCompletionSource(); + var mockRequestStream = new Mock>(); mockRequestStream .Setup(x => x.CompleteAsync()) + .Callback(() => requestStreamCompleted.SetResult()) .Returns(Task.CompletedTask); var mockResponseStream = new Mock>(); @@ -204,6 +207,7 @@ public async Task ProcessStreamAsync_WithEmptyStream_SendsNoRequests() using var processor = new EncryptionStreamProcessor(); await processor.ProcessStreamAsync(inputStream, call, options, 4096, TestContext.Current.CancellationToken); await foreach (var _ in processor.GetProcessedDataAsync(TestContext.Current.CancellationToken)) { } + await requestStreamCompleted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken); mockRequestStream.Verify( x => x.WriteAsync(It.IsAny(), It.IsAny()),