-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test suite for unacked message tracker
- Loading branch information
1 parent
d759c06
commit 26cd957
Showing
6 changed files
with
178 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 159 additions & 5 deletions
164
tests/DotPulsar.Tests/Internal/MessageAcksTrackerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,172 @@ | ||
namespace DotPulsar.Tests.Internal | ||
{ | ||
using DotPulsar.Internal; | ||
using DotPulsar.Abstractions; | ||
using FluentAssertions; | ||
using System.Buffers; | ||
using System.Linq; | ||
using Xunit; | ||
using System; | ||
using AutoFixture; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using AutoFixture.AutoNSubstitute; | ||
using NSubstitute; | ||
using System.Diagnostics; | ||
|
||
public class MessageAcksTrackerTests | ||
public class UnackedMessageTrackerTests | ||
{ | ||
[Fact] | ||
public void Test_Instance() | ||
{ | ||
var tracker = new MessageAcksTracker(1, 2, 3); | ||
tracker.Should().BeOfType<MessageAcksTracker>(); | ||
var tracker = new UnackedMessageTracker(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(1)); | ||
tracker.Should().BeOfType<UnackedMessageTracker>(); | ||
} | ||
|
||
|
||
[Fact] | ||
public async void Test_AwaitingAck_Elapsed() | ||
{ | ||
//Arrange | ||
var messageId = MessageId.Latest; | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
|
||
//Act | ||
var awaiting = new AwaitingAck(messageId); | ||
await Task.Delay(TimeSpan.FromMilliseconds(123)); | ||
sw.Stop(); | ||
|
||
//Assert | ||
awaiting.Elapsed.Should().BeCloseTo(sw.Elapsed, 1); | ||
} | ||
|
||
[Fact] | ||
public async void Test_Start_Message() | ||
{ | ||
//Arrange | ||
var fixture = new Fixture(); | ||
fixture.Customize(new AutoNSubstituteCustomization()); | ||
var consumer = Substitute.For<IConsumer>(); | ||
var messageId = MessageId.Latest; | ||
var cts = new CancellationTokenSource(); | ||
|
||
|
||
var tracker = new UnackedMessageTracker( | ||
TimeSpan.FromMilliseconds(10), | ||
TimeSpan.FromMilliseconds(1)); | ||
|
||
//Act | ||
tracker.Add(messageId); | ||
cts.CancelAfter(20); | ||
try { await tracker.Start(consumer, cts.Token); } | ||
catch (TaskCanceledException) { } | ||
|
||
//Assert | ||
await consumer | ||
.Received(1) | ||
.RedeliverUnacknowledgedMessages( | ||
Arg.Is(EquivalentTo(new List<MessageId>() { messageId })), | ||
Arg.Any<CancellationToken>()); | ||
} | ||
|
||
[Fact] | ||
public async void Test_Start_Message_Ack_In_Time() | ||
{ | ||
//Arrange | ||
var fixture = new Fixture(); | ||
fixture.Customize(new AutoNSubstituteCustomization()); | ||
var consumer = Substitute.For<IConsumer>(); | ||
var messageId = MessageId.Latest; | ||
var cts = new CancellationTokenSource(); | ||
|
||
|
||
var tracker = new UnackedMessageTracker( | ||
TimeSpan.FromMilliseconds(10), | ||
TimeSpan.FromMilliseconds(1)); | ||
|
||
//Act | ||
tracker.Add(messageId); | ||
cts.CancelAfter(20); | ||
var _ = Task.Delay(5).ContinueWith(_ => tracker.Ack(messageId)); | ||
try { await tracker.Start(consumer, cts.Token); } | ||
catch (TaskCanceledException) { } | ||
|
||
//Assert | ||
await consumer | ||
.DidNotReceive() | ||
.RedeliverUnacknowledgedMessages( | ||
Arg.Any<IEnumerable<MessageId>>(), | ||
Arg.Any<CancellationToken>()); | ||
} | ||
|
||
[Fact] | ||
public async void Test_Start_Message_Ack_Too_Late() | ||
{ | ||
//Arrange | ||
var fixture = new Fixture(); | ||
fixture.Customize(new AutoNSubstituteCustomization()); | ||
var consumer = Substitute.For<IConsumer>(); | ||
var messageId = MessageId.Latest; | ||
var cts = new CancellationTokenSource(); | ||
|
||
|
||
var tracker = new UnackedMessageTracker( | ||
TimeSpan.FromMilliseconds(10), | ||
TimeSpan.FromMilliseconds(1)); | ||
|
||
//Act | ||
tracker.Add(messageId); | ||
cts.CancelAfter(20); | ||
|
||
var _ = Task.Delay(15).ContinueWith(_ => tracker.Ack(messageId)); | ||
try { await tracker.Start(consumer, cts.Token); } | ||
catch (TaskCanceledException) { } | ||
|
||
//Assert | ||
await consumer | ||
.Received(1) | ||
.RedeliverUnacknowledgedMessages( | ||
Arg.Any<IEnumerable<MessageId>>(), | ||
Arg.Any<CancellationToken>()); | ||
} | ||
|
||
[Fact] | ||
public async void Test_Start_Redeliver_Only_Cnce() | ||
{ | ||
//Arrange | ||
var fixture = new Fixture(); | ||
fixture.Customize(new AutoNSubstituteCustomization()); | ||
var consumer = Substitute.For<IConsumer>(); | ||
var messageId = MessageId.Latest; | ||
var cts = new CancellationTokenSource(); | ||
|
||
|
||
var tracker = new UnackedMessageTracker( | ||
TimeSpan.FromMilliseconds(10), | ||
TimeSpan.FromMilliseconds(5)); | ||
|
||
//Act | ||
tracker.Add(messageId); | ||
cts.CancelAfter(50); | ||
try { await tracker.Start(consumer, cts.Token); } | ||
catch (TaskCanceledException) { } | ||
|
||
//Assert | ||
await consumer | ||
.Received(1) | ||
.RedeliverUnacknowledgedMessages( | ||
Arg.Any<IEnumerable<MessageId>>(), | ||
Arg.Any<CancellationToken>()); | ||
} | ||
|
||
|
||
private Expression<Predicate<IEnumerable<T>>> EquivalentTo<T>(IEnumerable<T> enumerable) => | ||
x => IsEquivalentIEnumerable(enumerable, x); | ||
|
||
|
||
private bool IsEquivalentIEnumerable<T>(IEnumerable<T> a, IEnumerable<T> b) => | ||
a.Count() == b.Count() && a.Zip(b, (a_, b_) => a_.Equals(b_)).All(_ => _); | ||
} | ||
} |