Skip to content

Commit dea6fa8

Browse files
Copilotdavidfowl
andcommitted
Extract helper methods for clearing activities and asserting no activities
Co-authored-by: davidfowl <[email protected]>
1 parent c9582ef commit dea6fa8

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

tests/Aspire.Hosting.Tests/Publishing/PipelineActivityReporterTests.cs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,13 @@ public async Task CompleteTaskAsync_IdempotentWhenCompletedWithSameState()
388388
await task.CompleteAsync(null, cancellationToken: CancellationToken.None);
389389

390390
// Clear activities
391-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
391+
ClearActivities(reporter);
392392

393393
// Act - Try to complete the same task again with the same state (should be idempotent, no exception)
394394
await task.CompleteAsync(null, cancellationToken: CancellationToken.None);
395395

396396
// Assert - No new activity should be emitted (noop)
397-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
397+
AssertNoActivitiesEmitted(reporter);
398398

399399
var taskInternal = Assert.IsType<ReportingTask>(task);
400400
Assert.Equal(CompletionState.Completed, taskInternal.CompletionState);
@@ -413,13 +413,13 @@ public async Task CompleteTaskAsync_IdempotentWhenCompletedWithDifferentState()
413413
await task.CompleteAsync(null, CompletionState.Completed, cancellationToken: CancellationToken.None);
414414

415415
// Clear activities
416-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
416+
ClearActivities(reporter);
417417

418418
// Act - Try to complete with a different state (should be idempotent, no exception)
419419
await task.CompleteAsync("Error", CompletionState.CompletedWithError, cancellationToken: CancellationToken.None);
420420

421421
// Assert - No new activity should be emitted (noop)
422-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
422+
AssertNoActivitiesEmitted(reporter);
423423

424424
var taskInternal = Assert.IsType<ReportingTask>(task);
425425
Assert.Equal(CompletionState.Completed, taskInternal.CompletionState); // Original state is retained
@@ -437,13 +437,13 @@ public async Task CompleteStepAsync_IdempotentWhenCompletedWithSameState()
437437
await step.CompleteAsync("Complete", cancellationToken: CancellationToken.None);
438438

439439
// Clear activities
440-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
440+
ClearActivities(reporter);
441441

442442
// Act - Try to complete the same step again with the same state (should be idempotent, no exception)
443443
await step.CompleteAsync("Complete again", cancellationToken: CancellationToken.None);
444444

445445
// Assert - No new activity should be emitted (noop)
446-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
446+
AssertNoActivitiesEmitted(reporter);
447447

448448
var stepInternal = Assert.IsType<ReportingStep>(step);
449449
Assert.Equal(CompletionState.Completed, stepInternal.CompletionState);
@@ -462,13 +462,13 @@ public async Task CompleteStepAsync_IdempotentWhenCompletedWithDifferentState()
462462
await step.CompleteAsync("Complete", CompletionState.Completed, cancellationToken: CancellationToken.None);
463463

464464
// Clear activities
465-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
465+
ClearActivities(reporter);
466466

467467
// Act - Try to complete with a different state (should be idempotent, no exception)
468468
await step.CompleteAsync("Error", CompletionState.CompletedWithError, cancellationToken: CancellationToken.None);
469469

470470
// Assert - No new activity should be emitted (noop)
471-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
471+
AssertNoActivitiesEmitted(reporter);
472472

473473
var stepInternal = Assert.IsType<ReportingStep>(step);
474474
Assert.Equal(CompletionState.Completed, stepInternal.CompletionState); // Original state is retained
@@ -690,7 +690,7 @@ public async Task DisposeAsync_StepWithCompletedTasks_CompletesWithSuccessState(
690690
await task2.SucceedAsync(null, CancellationToken.None);
691691

692692
// Clear previous activities
693-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
693+
ClearActivities(reporter);
694694

695695
// Act
696696
await step.DisposeAsync();
@@ -721,13 +721,13 @@ public async Task DisposeAsync_StepAlreadyCompleted_DoesNotCompleteAgain()
721721
await step.CompleteAsync("Step completed manually", CompletionState.Completed, CancellationToken.None);
722722

723723
// Clear activities
724-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
724+
ClearActivities(reporter);
725725

726726
// Act - Dispose should not cause another completion
727727
await step.DisposeAsync();
728728

729729
// Assert - No new activities should be emitted
730-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
730+
AssertNoActivitiesEmitted(reporter);
731731
var stepInternal = Assert.IsType<ReportingStep>(step);
732732
Assert.Equal(CompletionState.Completed, stepInternal.CompletionState);
733733
Assert.Equal("Step completed manually", stepInternal.CompletionText);
@@ -936,13 +936,13 @@ public async Task Log_DoesNothingWhenStepIsComplete()
936936
await step.CompleteAsync("Completed", CompletionState.Completed, CancellationToken.None);
937937

938938
// Clear activities
939-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
939+
ClearActivities(reporter);
940940

941941
// Act - Step is completed, so logging should be a no-op
942942
step.Log(LogLevel.Information, "Test log", enableMarkdown: false);
943943

944944
// Assert - No new activity should be emitted
945-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
945+
AssertNoActivitiesEmitted(reporter);
946946
}
947947

948948
[Fact]
@@ -986,13 +986,13 @@ public async Task CompleteTaskAsync_IdempotentWithWarning()
986986
await task.CompleteAsync("Warning message", CompletionState.CompletedWithWarning, CancellationToken.None);
987987

988988
// Clear activities
989-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
989+
ClearActivities(reporter);
990990

991991
// Act - Try to complete again with same state (should be idempotent)
992992
await task.CompleteAsync("Different warning message", CompletionState.CompletedWithWarning, CancellationToken.None);
993993

994994
// Assert - No new activity should be emitted
995-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
995+
AssertNoActivitiesEmitted(reporter);
996996

997997
var taskInternal = Assert.IsType<ReportingTask>(task);
998998
Assert.Equal(CompletionState.CompletedWithWarning, taskInternal.CompletionState);
@@ -1011,13 +1011,13 @@ public async Task CompleteTaskAsync_IdempotentWithError()
10111011
await task.CompleteAsync("Error message", CompletionState.CompletedWithError, CancellationToken.None);
10121012

10131013
// Clear activities
1014-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
1014+
ClearActivities(reporter);
10151015

10161016
// Act - Try to complete again with same state (should be idempotent)
10171017
await task.CompleteAsync("Different error message", CompletionState.CompletedWithError, CancellationToken.None);
10181018

10191019
// Assert - No new activity should be emitted
1020-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
1020+
AssertNoActivitiesEmitted(reporter);
10211021

10221022
var taskInternal = Assert.IsType<ReportingTask>(task);
10231023
Assert.Equal(CompletionState.CompletedWithError, taskInternal.CompletionState);
@@ -1035,13 +1035,13 @@ public async Task CompleteStepAsync_IdempotentWithWarning()
10351035
await step.CompleteAsync("Warning", CompletionState.CompletedWithWarning, CancellationToken.None);
10361036

10371037
// Clear activities
1038-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
1038+
ClearActivities(reporter);
10391039

10401040
// Act - Try to complete again with same state (should be idempotent)
10411041
await step.CompleteAsync("Different warning", CompletionState.CompletedWithWarning, CancellationToken.None);
10421042

10431043
// Assert - No new activity should be emitted
1044-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
1044+
AssertNoActivitiesEmitted(reporter);
10451045

10461046
var stepInternal = Assert.IsType<ReportingStep>(step);
10471047
Assert.Equal(CompletionState.CompletedWithWarning, stepInternal.CompletionState);
@@ -1059,13 +1059,13 @@ public async Task CompleteStepAsync_IdempotentWithError()
10591059
await step.CompleteAsync("Error", CompletionState.CompletedWithError, CancellationToken.None);
10601060

10611061
// Clear activities
1062-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
1062+
ClearActivities(reporter);
10631063

10641064
// Act - Try to complete again with same state (should be idempotent)
10651065
await step.CompleteAsync("Different error", CompletionState.CompletedWithError, CancellationToken.None);
10661066

10671067
// Assert - No new activity should be emitted
1068-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
1068+
AssertNoActivitiesEmitted(reporter);
10691069

10701070
var stepInternal = Assert.IsType<ReportingStep>(step);
10711071
Assert.Equal(CompletionState.CompletedWithError, stepInternal.CompletionState);
@@ -1090,13 +1090,13 @@ public async Task CompleteTaskAsync_IdempotentWhenTransitioningBetweenTerminalSt
10901090
await task.CompleteAsync("First completion", firstState, CancellationToken.None);
10911091

10921092
// Clear activities
1093-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
1093+
ClearActivities(reporter);
10941094

10951095
// Act - Try to complete with different state (should be idempotent, no exception)
10961096
await task.CompleteAsync("Second completion", secondState, CancellationToken.None);
10971097

10981098
// Assert - No new activity should be emitted (noop)
1099-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
1099+
AssertNoActivitiesEmitted(reporter);
11001100

11011101
var taskInternal = Assert.IsType<ReportingTask>(task);
11021102
Assert.Equal(firstState, taskInternal.CompletionState); // Original state is retained
@@ -1120,18 +1120,28 @@ public async Task CompleteStepAsync_IdempotentWhenTransitioningBetweenTerminalSt
11201120
await step.CompleteAsync("First completion", firstState, CancellationToken.None);
11211121

11221122
// Clear activities
1123-
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
1123+
ClearActivities(reporter);
11241124

11251125
// Act - Try to complete with different state (should be idempotent, no exception)
11261126
await step.CompleteAsync("Second completion", secondState, CancellationToken.None);
11271127

11281128
// Assert - No new activity should be emitted (noop)
1129-
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
1129+
AssertNoActivitiesEmitted(reporter);
11301130

11311131
var stepInternal = Assert.IsType<ReportingStep>(step);
11321132
Assert.Equal(firstState, stepInternal.CompletionState); // Original state is retained
11331133
}
11341134

1135+
private static void ClearActivities(PipelineActivityReporter reporter)
1136+
{
1137+
while (reporter.ActivityItemUpdated.Reader.TryRead(out _)) { }
1138+
}
1139+
1140+
private static void AssertNoActivitiesEmitted(PipelineActivityReporter reporter)
1141+
{
1142+
Assert.False(reporter.ActivityItemUpdated.Reader.TryRead(out _));
1143+
}
1144+
11351145
private PipelineActivityReporter CreatePublishingReporter()
11361146
{
11371147
return new PipelineActivityReporter(_interactionService, NullLogger<PipelineActivityReporter>.Instance);

0 commit comments

Comments
 (0)