Skip to content

Commit

Permalink
Extend tests
Browse files Browse the repository at this point in the history
- Extend the integration tests to include publishing to multiple topics in a single batch.
- Fix broken test assertion after previous refactor.
- Disable IDE0130.
  • Loading branch information
martincostello committed Sep 2, 2024
1 parent 96c994d commit 42dbb52
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 5 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dotnet_naming_style.attribute_upper_camel_case_style.required_prefix=Attribute
dotnet_naming_symbols.private_constants_symbols.applicable_accessibilities=private
dotnet_naming_symbols.private_constants_symbols.applicable_kinds=field
dotnet_naming_symbols.private_constants_symbols.required_modifiers=const
dotnet_style_namespace_match_folder=false
dotnet_style_parentheses_in_arithmetic_binary_operators=never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators=never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators=never_if_unnecessary:none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,65 @@ await WhenBatchAsync(
await handler.Received(batchSize).Handle(Arg.Is<SimpleMessage>((m) => messages.Any(y => y.Id == m.Id)));
});
}

[AwsTheory]
[InlineData(10, 10)]
[InlineData(10, 100)]
[InlineData(5, 100)]
public async Task Then_Multiple_Message_Types_Are_Handled(int maxBatchSize, int batchSize)
{
// Arrange
var completionSource1 = new TaskCompletionSource<object>();
var handler1 = CreateHandler<SimpleMessage>(completionSource1, batchSize);

var completionSource2 = new TaskCompletionSource<object>();
var handler2 = CreateHandler<AnotherSimpleMessage>(completionSource2, batchSize);

var services = GivenJustSaying()
.ConfigureJustSaying((builder) =>
{
builder.WithLoopbackQueue<SimpleMessage>(UniqueName);
builder.WithLoopbackQueue<AnotherSimpleMessage>(UniqueName + "ish");
})
.AddSingleton(handler1)
.AddSingleton(handler2);

var messages = new List<Message>();
for (int i = 0; i < batchSize; i++)
{
messages.Add(new SimpleMessage
{
Content = $"Message {i} of {batchSize} with max batch size {maxBatchSize}"
});
}

for (int i = 0; i < batchSize; i++)
{
messages.Add(new AnotherSimpleMessage
{
Content = $"Message {i} of {batchSize} with max batch size {maxBatchSize}"
});
}

await WhenBatchAsync(
services,
async (publisher, listener, cancellationToken) =>
{
await listener.StartAsync(cancellationToken);
await publisher.StartAsync(cancellationToken);
// Act
await publisher.PublishAsync(messages, new PublishBatchMetadata
{
BatchSize = maxBatchSize
}, cancellationToken);
// Assert
completionSource1.Task.Wait(cancellationToken);
await handler1.Received(batchSize).Handle(Arg.Is<SimpleMessage>((m) => messages.Any(y => y.Id == m.Id)));
completionSource2.Task.Wait(cancellationToken);
await handler2.Received(batchSize).Handle(Arg.Is<AnotherSimpleMessage>((m) => messages.Any(y => y.Id == m.Id)));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,65 @@ await WhenBatchAsync(
await handler.Received(batchSize).Handle(Arg.Is<SimpleMessage>((m) => messages.Any(x => x.Id == m.Id)));
});
}

[AwsTheory]
[InlineData(10, 10)]
[InlineData(10, 20)]
[InlineData(5, 10)]
public async Task Then_Multiple_Message_Types_Are_Handled(int maxBatchSize, int batchSize)
{
// Arrange
var completionSource1 = new TaskCompletionSource<object>();
var handler1 = CreateHandler<SimpleMessage>(completionSource1, batchSize);

var completionSource2 = new TaskCompletionSource<object>();
var handler2 = CreateHandler<AnotherSimpleMessage>(completionSource2, batchSize);

var services = GivenJustSaying()
.ConfigureJustSaying((builder) =>
{
builder.WithLoopbackTopic<SimpleMessage>(UniqueName);
builder.WithLoopbackTopic<AnotherSimpleMessage>(UniqueName + "ish");
})
.AddSingleton(handler1)
.AddSingleton(handler2);

var messages = new List<Message>();
for (int i = 0; i < batchSize; i++)
{
messages.Add(new SimpleMessage
{
Content = $"Message {i} of {batchSize} with max batch size {maxBatchSize}"
});
}

for (int i = 0; i < batchSize; i++)
{
messages.Add(new AnotherSimpleMessage
{
Content = $"Message {i} of {batchSize} with max batch size {maxBatchSize}"
});
}

await WhenBatchAsync(
services,
async (publisher, listener, cancellationToken) =>
{
await listener.StartAsync(cancellationToken);
await publisher.StartAsync(cancellationToken);
// Act
await publisher.PublishAsync(messages, new PublishBatchMetadata
{
BatchSize = batchSize
}, cancellationToken);
// Assert
completionSource1.Task.Wait(cancellationToken);
await handler1.Received(batchSize).Handle(Arg.Is<SimpleMessage>((m) => messages.Any(x => x.Id == m.Id)));
completionSource2.Task.Wait(cancellationToken);
await handler2.Received(batchSize).Handle(Arg.Is<AnotherSimpleMessage>((m) => messages.Any(x => x.Id == m.Id)));
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using JustSaying.Messaging;
using JustSaying.Models;
using JustSaying.TestingFramework;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -26,7 +25,7 @@ public async Task Then_An_Exception_Is_Thrown()
await batchPublisher.StartAsync(CancellationToken.None);

// Act and Assert
exception = await Assert.ThrowsAsync<InvalidOperationException>(() => batchPublisher.PublishAsync(new List<Message> {new SimpleMessage() }, CancellationToken.None));
exception.Message.ShouldBe("Error publishing message, no publishers registered. Has the bus been started?");
exception = await Assert.ThrowsAsync<InvalidOperationException>(() => batchPublisher.PublishAsync([new SimpleMessage()], CancellationToken.None));
exception.Message.ShouldBe("Error publishing message batch, no publishers registered. Has the bus been started?");
}
}
4 changes: 2 additions & 2 deletions tests/JustSaying.TestingFramework/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static RegionEndpoint Region
/// <summary>
/// Gets a value indicating whether an AWS simulator is configured for use.
/// </summary>
public static bool IsSimulatorConfigured => (SimulatorUrl != null);
public static bool IsSimulatorConfigured => SimulatorUrl != null;

/// <summary>
/// Gets the URL for the configured AWS simulator, if any.
Expand Down Expand Up @@ -111,4 +111,4 @@ public static RegionEndpoint SecondaryRegion
private static string RegionName => Environment.GetEnvironmentVariable("AWS_REGION");

private static string SecondaryRegionName => Environment.GetEnvironmentVariable("AWS_REGION_SECONDARY");
}
}

0 comments on commit 42dbb52

Please sign in to comment.