diff --git a/src/Daqifi.Core.Tests/Daqifi.Core.Tests.csproj b/src/Daqifi.Core.Tests/Daqifi.Core.Tests.csproj index a398292..5d29885 100644 --- a/src/Daqifi.Core.Tests/Daqifi.Core.Tests.csproj +++ b/src/Daqifi.Core.Tests/Daqifi.Core.Tests.csproj @@ -12,11 +12,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/src/Daqifi.Core.Tests/Device/SdCard/SdCardCsvFileParserTests.cs b/src/Daqifi.Core.Tests/Device/SdCard/SdCardCsvFileParserTests.cs index 5bbf24a..246dd20 100644 --- a/src/Daqifi.Core.Tests/Device/SdCard/SdCardCsvFileParserTests.cs +++ b/src/Daqifi.Core.Tests/Device/SdCard/SdCardCsvFileParserTests.cs @@ -447,7 +447,7 @@ public async Task ParseAsync_ProgressReporting_CallsCallback() var parser = new global::Daqifi.Core.Device.SdCard.SdCardCsvFileParser(); var options = new global::Daqifi.Core.Device.SdCard.SdCardParseOptions { - Progress = new Progress(p => + Progress = new SynchronousProgress(p => { progressCalls++; lastProgress = p; diff --git a/src/Daqifi.Core.Tests/Device/SdCard/SdCardFileParserTests.cs b/src/Daqifi.Core.Tests/Device/SdCard/SdCardFileParserTests.cs index 2f3c946..2600f39 100644 --- a/src/Daqifi.Core.Tests/Device/SdCard/SdCardFileParserTests.cs +++ b/src/Daqifi.Core.Tests/Device/SdCard/SdCardFileParserTests.cs @@ -485,7 +485,7 @@ public async Task ParseAsync_ReportsProgressMonotonically() var progressReports = new List(); var options = new SdCardParseOptions { - Progress = new Progress(p => progressReports.Add(p)) + Progress = new SynchronousProgress(p => progressReports.Add(p)) }; // Act @@ -493,9 +493,6 @@ public async Task ParseAsync_ReportsProgressMonotonically() // Enumerate samples to force full parse await ToListAsync(session.Samples); - // Allow progress handler to execute (Progress posts to SynchronizationContext) - await Task.Delay(100); - // Assert — at least one progress report Assert.NotEmpty(progressReports); diff --git a/src/Daqifi.Core.Tests/Device/SdCard/SdCardFileReceiverTests.cs b/src/Daqifi.Core.Tests/Device/SdCard/SdCardFileReceiverTests.cs index 1d479f7..516a703 100644 --- a/src/Daqifi.Core.Tests/Device/SdCard/SdCardFileReceiverTests.cs +++ b/src/Daqifi.Core.Tests/Device/SdCard/SdCardFileReceiverTests.cs @@ -115,14 +115,11 @@ public async Task ReceiveAsync_ProgressReporting_BytesReceivedIncreases() var receiver = new SdCardFileReceiver(sourceStream, bufferSize: 100); var progressReports = new System.Collections.Generic.List(); - var progress = new Progress(p => progressReports.Add(p)); + var progress = new SynchronousProgress(p => progressReports.Add(p)); // Act await receiver.ReceiveAsync(destinationStream, "test.bin", progress); - // Allow progress callbacks to fire (they're posted to the sync context) - await Task.Delay(100); - // Assert — we should have received at least one progress report Assert.NotEmpty(progressReports); Assert.All(progressReports, p => diff --git a/src/Daqifi.Core.Tests/Device/SdCard/SdCardJsonFileParserTests.cs b/src/Daqifi.Core.Tests/Device/SdCard/SdCardJsonFileParserTests.cs index 3dedca8..d3ec598 100644 --- a/src/Daqifi.Core.Tests/Device/SdCard/SdCardJsonFileParserTests.cs +++ b/src/Daqifi.Core.Tests/Device/SdCard/SdCardJsonFileParserTests.cs @@ -253,15 +253,16 @@ public async Task ParseAsync_ProgressReporting_CallsCallback() ); var progressCalls = 0; + var lastProgress = default(global::Daqifi.Core.Device.SdCard.SdCardParseProgress); var parser = new global::Daqifi.Core.Device.SdCard.SdCardJsonFileParser(); var options = new global::Daqifi.Core.Device.SdCard.SdCardParseOptions { FallbackTimestampFrequency = 100, - Progress = new Progress(p => + Progress = new SynchronousProgress(p => { progressCalls++; + lastProgress = p; Assert.True(p.BytesRead >= 0); - Assert.Equal(250, p.MessagesRead); }) }; @@ -272,6 +273,7 @@ public async Task ParseAsync_ProgressReporting_CallsCallback() // Assert Assert.Equal(250, samples.Count); Assert.True(progressCalls >= 2); // At least 2 progress updates (100-line batches + final) + Assert.Equal(250, lastProgress.MessagesRead); } [Fact] diff --git a/src/Daqifi.Core.Tests/Device/SdCard/SynchronousProgress.cs b/src/Daqifi.Core.Tests/Device/SdCard/SynchronousProgress.cs new file mode 100644 index 0000000..9a4245e --- /dev/null +++ b/src/Daqifi.Core.Tests/Device/SdCard/SynchronousProgress.cs @@ -0,0 +1,13 @@ +using System; + +namespace Daqifi.Core.Tests.Device.SdCard; + +/// +/// A synchronous implementation for tests. +/// Unlike , this invokes the callback inline on the +/// calling thread, avoiding race conditions in assertions. +/// +internal sealed class SynchronousProgress(Action handler) : IProgress +{ + public void Report(T value) => handler(value); +}