Skip to content

Commit

Permalink
Stabilize unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-m committed Jun 27, 2024
1 parent d1445d7 commit d4687da
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ public async Task OnError_SingleRetry_RetriesOnce_With_GivenDelay(int maxConcurr

var eventBroker = scope.ServiceProvider.GetRequiredService<IEventBroker>();
var eventsTracker = scope.ServiceProvider.GetRequiredService<EventsTracker>();
eventsTracker.ExpectedItemsCount = 2;
var event1 = new TestEvent("test");

// Act
await eventBroker.Publish(event1);
await eventsTracker.Wait(TimeSpan.FromSeconds(1));
await eventsTracker.Wait(TimeSpan.FromSeconds(2));

// Assert
Assert.Equal(2, eventsTracker.Items.Count);
var timestamps = eventsTracker.Items.OrderBy(x => x.Timestamp).Select(x => x.Timestamp).ToArray();
Assert.Equal(100, (timestamps[1] - timestamps[0]).TotalMilliseconds, tolerance: 60);
Assert.Equal(100, (timestamps[1] - timestamps[0]).TotalMilliseconds, tolerance: 50);
}

[Theory]
Expand All @@ -52,6 +53,7 @@ public async Task OnError_MultipleRetries_RetriesMultipleTimes_With_GivenDelay(i

var eventBroker = scope.ServiceProvider.GetRequiredService<IEventBroker>();
var eventsTracker = scope.ServiceProvider.GetRequiredService<EventsTracker>();
eventsTracker.ExpectedItemsCount = 4;
var event1 = new TestEvent("test");

// Act
Expand All @@ -61,9 +63,9 @@ public async Task OnError_MultipleRetries_RetriesMultipleTimes_With_GivenDelay(i
// Assert
Assert.Equal(4, eventsTracker.Items.Count);
var timestamps = eventsTracker.Items.OrderBy(x => x.Timestamp).Select(x => x.Timestamp).ToArray();
Assert.Equal(100, (timestamps[1] - timestamps[0]).TotalMilliseconds, tolerance: 60);
Assert.Equal(300, (timestamps[2] - timestamps[1]).TotalMilliseconds, tolerance: 60);
Assert.Equal(600, (timestamps[3] - timestamps[2]).TotalMilliseconds, tolerance: 80);
Assert.Equal(100, (timestamps[1] - timestamps[0]).TotalMilliseconds, tolerance: 50);
Assert.Equal(300, (timestamps[2] - timestamps[1]).TotalMilliseconds, tolerance: 50);
Assert.Equal(600, (timestamps[3] - timestamps[2]).TotalMilliseconds, tolerance: 50);
}

[Theory]
Expand All @@ -84,11 +86,12 @@ public async Task OnError_MultipleRetries_Event_IsTheSameInstance_EveryTime(int

var eventBroker = scope.ServiceProvider.GetRequiredService<IEventBroker>();
var eventsTracker = scope.ServiceProvider.GetRequiredService<EventsTracker>();
eventsTracker.ExpectedItemsCount = 4;
var event1 = new TestEvent("test");

// Act
await eventBroker.Publish(event1);
await eventsTracker.Wait(TimeSpan.FromSeconds(2));
await eventsTracker.Wait(TimeSpan.FromSeconds(3));

// Assert
Assert.Equal(4, eventsTracker.Items.Count);
Expand Down

0 comments on commit d4687da

Please sign in to comment.