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 784389c commit d1445d7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
18 changes: 11 additions & 7 deletions test/M.EventBrokerSlim.Tests/RetryFromHandleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public async Task Handle_SingleRetry_RetriesOnce_With_GivenDelay(int maxConcurre

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

// Act
Expand All @@ -29,7 +30,7 @@ public async Task Handle_SingleRetry_RetriesOnce_With_GivenDelay(int maxConcurre
// 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 @@ -50,6 +51,7 @@ public async Task Handle_SingleRetry_RetriesOnce_With_ZeroDelay(int maxConcurren

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

// Act
Expand All @@ -59,7 +61,7 @@ public async Task Handle_SingleRetry_RetriesOnce_With_ZeroDelay(int maxConcurren
// Assert
Assert.Equal(2, eventsTracker.Items.Count);
var timestamps = eventsTracker.Items.OrderBy(x => x.Timestamp).Select(x => x.Timestamp).ToArray();
Assert.Equal(0, (timestamps[1] - timestamps[0]).TotalMilliseconds, tolerance: 60);
Assert.Equal(0, (timestamps[1] - timestamps[0]).TotalMilliseconds, tolerance: 50);
}

[Theory]
Expand All @@ -80,18 +82,19 @@ public async Task Handle_MultipleRetries_RetriesMultipleTimes_With_GivenDelay(in

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(1));
await eventsTracker.Wait(TimeSpan.FromSeconds(2));

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

[Theory]
Expand All @@ -112,11 +115,12 @@ public async Task Handle_MultipleRetries_Event_IsTheSameInstance_EveryTime(int m

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(1));
await eventsTracker.Wait(TimeSpan.FromSeconds(2));

// Assert
Assert.Equal(4, eventsTracker.Items.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ public async Task Handle_SingleRetry_RetriesOnce_With_GivenDelay(int maxConcurre
sc => sc.AddEventBroker(
x => x.WithMaxConcurrentHandlers(maxConcurrentHandlers)
.AddTransient<TestEvent, TestEventHandler>())
.AddSingleton(new HandlerSettings(RetryAttempts: 1, Delay: TimeSpan.FromMilliseconds(100)))
.AddSingleton(new HandlerSettings(RetryAttempts: 1, Delay: TimeSpan.FromMilliseconds(300)))
.AddSingleton<EventsTracker>());

using var scope = services.CreateScope();

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

// Act
Expand All @@ -29,7 +30,7 @@ public async Task Handle_SingleRetry_RetriesOnce_With_GivenDelay(int maxConcurre
// 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 @@ -50,11 +51,12 @@ public async Task Handle_MultipleRetries_RetriesMultipleTimes_With_GivenDelay(in

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
28 changes: 16 additions & 12 deletions test/M.EventBrokerSlim.Tests/RetryFromOnErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ public async Task OnError_SingleRetry_RetriesOnce_With_GivenDelay(int maxConcurr
sc => sc.AddEventBroker(
x => x.WithMaxConcurrentHandlers(maxConcurrentHandlers)
.AddTransient<TestEvent, TestEventHandler>())
.AddSingleton(new HandlerSettings(RetryAttempts: 1, Delay: TimeSpan.FromMilliseconds(100)))
.AddSingleton(new HandlerSettings(RetryAttempts: 1, Delay: TimeSpan.FromMilliseconds(300)))
.AddSingleton<EventsTracker>());

using var scope = services.CreateScope();

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

// Act
Expand All @@ -29,7 +30,7 @@ public async Task OnError_SingleRetry_RetriesOnce_With_GivenDelay(int maxConcurr
// 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(300, (timestamps[1] - timestamps[0]).TotalMilliseconds, tolerance: 50);
}

[Theory]
Expand All @@ -43,25 +44,26 @@ public async Task OnError_MultipleRetries_RetriesMultipleTimes_With_GivenDelay(i
sc => sc.AddEventBroker(
x => x.WithMaxConcurrentHandlers(maxConcurrentHandlers)
.AddTransient<TestEvent, TestEventHandler>())
.AddSingleton(new HandlerSettings(RetryAttempts: 3, Delay: TimeSpan.FromMilliseconds(150)))
.AddSingleton(new HandlerSettings(RetryAttempts: 3, Delay: TimeSpan.FromMilliseconds(300)))
.AddSingleton<EventsTracker>());

using var scope = services.CreateScope();

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(1));
await eventsTracker.Wait(TimeSpan.FromSeconds(2));

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

[Theory]
Expand All @@ -82,6 +84,7 @@ public async Task OnError_MultipleRetries_RetriesMultipleTimes_With_ZeroDelay(in

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

// Act
Expand All @@ -91,9 +94,9 @@ public async Task OnError_MultipleRetries_RetriesMultipleTimes_With_ZeroDelay(in
// Assert
Assert.Equal(4, eventsTracker.Items.Count);
var timestamps = eventsTracker.Items.OrderBy(x => x.Timestamp).Select(x => x.Timestamp).ToArray();
Assert.Equal(0, (timestamps[1] - timestamps[0]).TotalMilliseconds, tolerance: 60);
Assert.Equal(0, (timestamps[2] - timestamps[1]).TotalMilliseconds, tolerance: 60);
Assert.Equal(0, (timestamps[3] - timestamps[2]).TotalMilliseconds, tolerance: 60);
Assert.Equal(0, (timestamps[1] - timestamps[0]).TotalMilliseconds, tolerance: 50);
Assert.Equal(0, (timestamps[2] - timestamps[1]).TotalMilliseconds, tolerance: 50);
Assert.Equal(0, (timestamps[3] - timestamps[2]).TotalMilliseconds, tolerance: 50);
}

[Theory]
Expand All @@ -107,18 +110,19 @@ public async Task OnError_MultipleRetries_Event_IsTheSameInstance_EveryTime(int
sc => sc.AddEventBroker(
x => x.WithMaxConcurrentHandlers(maxConcurrentHandlers)
.AddTransient<TestEvent, TestEventHandler>())
.AddSingleton(new HandlerSettings(RetryAttempts: 3, Delay: TimeSpan.FromMilliseconds(150)))
.AddSingleton(new HandlerSettings(RetryAttempts: 3, Delay: TimeSpan.FromMilliseconds(300)))
.AddSingleton<EventsTracker>());

using var scope = services.CreateScope();

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(1));
await eventsTracker.Wait(TimeSpan.FromSeconds(2));

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

0 comments on commit d1445d7

Please sign in to comment.