Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BusBuilder prototype first-step #431

Merged
merged 44 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
aacbbc3
BusBuilder prototype first-step
martincostello Nov 21, 2018
f54a902
Start to flesh out the builder
martincostello Nov 24, 2018
d2495ff
Support DI and subscriptions
martincostello Nov 24, 2018
b8c17a1
Use nested builders
martincostello Nov 25, 2018
71afc43
Use region parameter
martincostello Nov 25, 2018
8acc1ae
Add missing DI registrations
martincostello Nov 25, 2018
1a3b70f
Add builder method for service URI
martincostello Nov 25, 2018
96dfa31
Use emulator
martincostello Nov 25, 2018
5d45a73
Fix queue creation dependencies
martincostello Nov 25, 2018
bee0635
Use the same single client factory
martincostello Nov 25, 2018
555aac3
Adjust formatting
martincostello Nov 25, 2018
30c617b
Move tests to integration project
martincostello Nov 25, 2018
e0e7af4
Refactor subscription building
martincostello Nov 25, 2018
bdf3c3d
Add more members to SqsReadConfigurationBuilder
martincostello Nov 25, 2018
3e03a82
Remove IBuilder<T>
martincostello Nov 25, 2018
7ab5836
Fix compliation errors
martincostello Nov 26, 2018
b7d2660
Make IMessageSerializationFactory lazy
martincostello Nov 26, 2018
3aeba99
Make IMessageMonitor configurable
martincostello Nov 26, 2018
95774a6
Add IMessageLockAsync to builder
martincostello Nov 26, 2018
52fb475
Add null checks
martincostello Nov 26, 2018
8f3ba38
Move default services to own type
martincostello Nov 26, 2018
ac37362
Move services to new ServicesBuilder
martincostello Nov 26, 2018
6bca063
Update test to use Services()
martincostello Nov 26, 2018
9aeef4c
Add publication support
martincostello Nov 26, 2018
9c0ec4d
Extend test to publish and subscribe
martincostello Nov 26, 2018
5cb1df2
Add missing method for IsPointToPoint
martincostello Nov 26, 2018
1a1c447
Separate queues and topic subscriptions
martincostello Nov 26, 2018
e735f5e
Move builder to root namespace
martincostello Nov 27, 2018
44448ff
Move IServiceCollectionExtensions
martincostello Nov 27, 2018
1909a89
Add XML documentation
martincostello Nov 27, 2018
bebbbf7
Split listen and publish
martincostello Nov 27, 2018
d136bca
Add configuration "callback" extensibility
martincostello Nov 27, 2018
39c3fc9
Fix duplication regions
martincostello Nov 27, 2018
34791c4
Add duplicate check
martincostello Nov 27, 2018
311b86e
Extend builder integration tests
martincostello Nov 27, 2018
6555ff7
Extend integration tests
martincostello Nov 27, 2018
0013cf7
Use SimulatorUrl
martincostello Nov 27, 2018
0df22cb
Add builder for configuring writes
martincostello Nov 27, 2018
0bca671
Fix test concurrency
martincostello Nov 27, 2018
170ef61
Rename parameter
martincostello Nov 27, 2018
536a989
Remove commented out [Obsolete] attributes
martincostello Nov 29, 2018
4cff106
Merge branch 'master' into Builder-RFC
martincostello Dec 4, 2018
06f56a2
Wait less
martincostello Dec 10, 2018
1b61c41
Use var instead of explicit type
martincostello Dec 13, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 271 additions & 0 deletions JustSaying.IntegrationTests/Fluent/MessagingBusBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JustSaying.Fluent;
using JustSaying.Messaging;
using JustSaying.Messaging.MessageHandling;
using JustSaying.Messaging.Monitoring;
using JustSaying.Models;
using JustSaying.TestingFramework;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Shouldly;
using Xunit.Abstractions;

namespace JustSaying.IntegrationTests
{
public class MessagingBusBuilderTests
{
public MessagingBusBuilderTests(ITestOutputHelper outputHelper)
{
OutputHelper = outputHelper;
}

private ITestOutputHelper OutputHelper { get; }

[AwsFact]
public async Task Can_Create_Messaging_Bus_Fluently_For_A_Queue()
{
// Arrange
var services = new ServiceCollection()
.AddLogging((p) => p.AddXUnit(OutputHelper))
.AddJustSaying(
(builder) =>
{
builder.Client((options) => options.WithBasicCredentials("accessKey", "secretKey").WithServiceUri(TestEnvironment.SimulatorUrl))
.Messaging((options) => options.WithRegions("eu-west-1"))
AnthonySteele marked this conversation as resolved.
Show resolved Hide resolved
.Publications((options) => options.WithQueue<QueueMessage>())
.Subscriptions((options) => options.ForQueue<QueueMessage>())
.Services((options) => options.WithMessageMonitoring(() => new MyMonitor()));
})
.AddJustSayingHandler<QueueMessage, QueueHandler>();

IServiceProvider serviceProvider = services.BuildServiceProvider();

IMessagePublisher publisher = serviceProvider.GetRequiredService<IMessagePublisher>();
IMessagingBus listener = serviceProvider.GetRequiredService<IMessagingBus>();

using (var source = new CancellationTokenSource(TimeSpan.FromSeconds(20)))
{
// Act
listener.Start(source.Token);

var message = new QueueMessage();

await publisher.PublishAsync(message, source.Token);

// Assert
while (!source.IsCancellationRequested && !QueueHandler.MessageIds.Contains(message.Id))
{
await Task.Delay(TimeSpan.FromSeconds(0.2), source.Token);
}

QueueHandler.MessageIds.ShouldContain(message.Id);
}
}

[AwsFact]
public async Task Can_Create_Messaging_Bus_Fluently_For_A_Topic()
{
// Arrange
var services = new ServiceCollection()
.AddLogging((p) => p.AddXUnit(OutputHelper))
.AddJustSaying(
(builder) =>
{
builder.Client((options) => options.WithBasicCredentials("accessKey", "secretKey").WithServiceUri(TestEnvironment.SimulatorUrl))
.Messaging((options) => options.WithRegions("eu-west-1"))
.Publications((options) => options.WithTopic<TopicMessage>())
.Subscriptions((options) => options.ForTopic<TopicMessage>());
})
.AddJustSayingHandler<TopicMessage, TopicHandler>();

IServiceProvider serviceProvider = services.BuildServiceProvider();

IMessagePublisher publisher = serviceProvider.GetRequiredService<IMessagePublisher>();
IMessagingBus listener = serviceProvider.GetRequiredService<IMessagingBus>();

using (var source = new CancellationTokenSource(TimeSpan.FromSeconds(20)))
{
// Act
listener.Start(source.Token);

var message = new TopicMessage();

await publisher.PublishAsync(message, source.Token);

// Assert
while (!source.IsCancellationRequested && !TopicHandler.MessageIds.Contains(message.Id))
{
await Task.Delay(TimeSpan.FromSeconds(0.2), source.Token);
}

TopicHandler.MessageIds.ShouldContain(message.Id);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test here is what it's about 👏 The api here looks much more approachable than v6


[AwsFact]
public void Can_Create_Messaging_Bus()
{
// Arrange
var services = new ServiceCollection()
.AddLogging((p) => p.AddXUnit(OutputHelper))
.AddJustSaying("eu-west-1")
.AddJustSayingHandler<QueueMessage, QueueHandler>();

IServiceProvider serviceProvider = services.BuildServiceProvider();

IMessagePublisher publisher = serviceProvider.GetRequiredService<IMessagePublisher>();
IMessagingBus listener = serviceProvider.GetRequiredService<IMessagingBus>();

using (var source = new CancellationTokenSource(TimeSpan.FromSeconds(20)))
{
// Act
listener.Start(source.Token);
}
}

[AwsFact]
public async Task Can_Create_Messaging_Bus_With_Contributors()
{
// Arrange
var services = new ServiceCollection()
.AddLogging((p) => p.AddXUnit(OutputHelper))
.AddJustSaying()
.AddSingleton<IMessageBusConfigurationContributor, AwsContributor>()
.AddSingleton<IMessageBusConfigurationContributor, MessagingContributor>()
.AddSingleton<IMessageBusConfigurationContributor, QueueContributor>()
.AddSingleton<IMessageBusConfigurationContributor, RegionContributor>()
.AddJustSayingHandler<QueueMessage, QueueHandler>()
.AddSingleton<MyMonitor>();

IServiceProvider serviceProvider = services.BuildServiceProvider();

IMessagePublisher publisher = serviceProvider.GetRequiredService<IMessagePublisher>();
IMessagingBus listener = serviceProvider.GetRequiredService<IMessagingBus>();

using (var source = new CancellationTokenSource(TimeSpan.FromSeconds(20)))
{
// Act
listener.Start(source.Token);

var message = new QueueMessage();

await publisher.PublishAsync(message, source.Token);

// Assert
while (!source.IsCancellationRequested && !QueueHandler.MessageIds.Contains(message.Id))
{
await Task.Delay(TimeSpan.FromSeconds(0.2), source.Token);
}

QueueHandler.MessageIds.ShouldContain(message.Id);
}
}

private sealed class AwsContributor : IMessageBusConfigurationContributor
{
public void Configure(MessagingBusBuilder builder)
{
builder.Client(
(options) => options.WithSessionCredentials("accessKeyId", "secretKeyId", "token")
.WithServiceUri(TestEnvironment.SimulatorUrl));
}
}

private sealed class MessagingContributor : IMessageBusConfigurationContributor
{
public MessagingContributor(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}

private IServiceProvider ServiceProvider { get; }

public void Configure(MessagingBusBuilder builder)
{
builder.Services((p) => p.WithMessageMonitoring(ServiceProvider.GetRequiredService<MyMonitor>));
}
}

private sealed class QueueContributor : IMessageBusConfigurationContributor
{
public void Configure(MessagingBusBuilder builder)
{
builder.Publications((p) => p.WithQueue<QueueMessage>())
.Subscriptions((p) => p.ForQueue<QueueMessage>());
}
}

private sealed class RegionContributor : IMessageBusConfigurationContributor
{
public void Configure(MessagingBusBuilder builder)
{
builder.Messaging((p) => p.WithRegion("eu-west-1"));
}
}

private sealed class QueueMessage : Message
{
}

private sealed class QueueHandler : IHandlerAsync<QueueMessage>
{
internal static ConcurrentBag<Guid> MessageIds { get; } = new ConcurrentBag<Guid>();

public Task<bool> Handle(QueueMessage message)
{
MessageIds.Add(message.Id);
return Task.FromResult(true);
}
}

private sealed class TopicMessage : Message
{
}

private sealed class TopicHandler : IHandlerAsync<TopicMessage>
{
internal static ConcurrentBag<Guid> MessageIds { get; } = new ConcurrentBag<Guid>();

public Task<bool> Handle(TopicMessage message)
{
MessageIds.Add(message.Id);
return Task.FromResult(true);
}
}

private sealed class MyMonitor : IMessageMonitor
{
public void HandleException(Type messageType)
{
}

public void HandleThrottlingTime(long handleTimeMs)
{
}

public void HandleTime(long handleTimeMs)
{
}

public void IncrementThrottlingStatistic()
{
}

public void IssuePublishingMessage()
{
}

public void PublishMessageTime(long handleTimeMs)
{
}

public void ReceiveMessageTime(long handleTimeMs, string queueName, string region)
{
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>$(NoWarn);CA1001;CA1034;CA1052;CA1054;CA1063;CA1307;CA1816;CA1822;CA2007</NoWarn>
<NoWarn>$(NoWarn);CA1001;CA1034;CA1052;CA1054;CA1063;CA1307;CA1707;CA1812;CA1816;CA1822;CA2007</NoWarn>
</PropertyGroup>
<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
Expand All @@ -15,6 +15,7 @@
<PackageReference Include="AWSSDK.Core" Version="3.3.25.3" />
<PackageReference Include="AWSSDK.SimpleNotificationService" Version="3.3.1.11" />
<PackageReference Include="AWSSDK.SQS" Version="3.3.3.19" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="StructureMap" Version="4.7.0" />
Expand Down
2 changes: 2 additions & 0 deletions JustSaying.TestingFramework/LocalAwsClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public IAmazonSimpleNotificationService GetSnsClient(RegionEndpoint region)
var credentials = new AnonymousAWSCredentials();
var clientConfig = new AmazonSimpleNotificationServiceConfig
{
RegionEndpoint = region,
ServiceURL = ServiceUrl.ToString()
};

Expand All @@ -32,6 +33,7 @@ public IAmazonSQS GetSqsClient(RegionEndpoint region)
var credentials = new AnonymousAWSCredentials();
var clientConfig = new AmazonSQSConfig
{
RegionEndpoint = region,
ServiceURL = ServiceUrl.ToString()
};

Expand Down
2 changes: 1 addition & 1 deletion JustSaying/CreateMeABus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class CreateMeABus
public static Func<IAwsClientFactory> DefaultClientFactory { get; set; }
= () => new DefaultAwsClientFactory();

public static JustSayingFluentlyDependencies WithLogging(ILoggerFactory loggerFactory) =>
public static JustSayingFluentlyDependencies WithLogging(ILoggerFactory loggerFactory) =>
new JustSayingFluentlyDependencies { LoggerFactory = loggerFactory};
}
}
Loading