public async Task ClosesTheQueueClientsProperlyOnShutdown() { var composer = new ServiceBusComposer(); composer.WithAdditionalServices(services => { services.ConfigureServiceBus(options => { options.RegisterQueue("testQueue").WithConnectionString("testConnectionString"); options.RegisterQueue("testQueue2").WithConnectionString("testConnectionString2"); options.RegisterQueue("testQueue3").WithConnectionString("testConnectionString3"); }); }); var provider = await composer.ComposeAndSimulateStartup(); var factory = provider.GetRequiredService <FakeQueueClientFactory>(); var clientMocks = factory.GetAllRegisteredQueueClients(); foreach (var clientMock in clientMocks) { clientMock.Mock.Setup(o => o.CloseAsync()).Returns(Task.CompletedTask).Verifiable(); } await provider.SimulateStopHost(token : new CancellationToken()); foreach (var clientMock in clientMocks) { clientMock.Mock.Verify(o => o.CloseAsync(), Times.Once); } }
public async Task FailsSilentlyWhenAnErrorOccursBuildingAQueueClient() { var composer = new ServiceBusComposer(); composer.OverrideQueueClientFactory(new FailingQueueClientFactory()); composer.WithAdditionalServices( services => { services.ConfigureServiceBus( options => { options.RegisterQueue("testQueue").WithConnectionString("testConnectionString"); }); }); var provider = await composer.ComposeAndSimulateStartup(); var registry = provider.GetService <ServiceBusRegistry>(); await Assert.ThrowsAsync <MessageSenderUnavailableException>( async() => { await registry.GetQueueSender("testQueue").SendAsync(new Message()); }); }
public async Task CanRegisterQueueWithConnection() { var composer = new ServiceBusComposer(); var serviceBusConnection = new ServiceBusConnection( "Endpoint=sb://labepdvsb.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=TOEhvlmrOoLjHfxhYJ3xjoLtVZrMQLqP8MUwrv5flOA="); var factory = new Mock <IQueueClientFactory>(); factory .Setup(o => o.Create(It.Is <QueueOptions>(opts => opts.Connection == serviceBusConnection))) .Returns((QueueOptions o) => new QueueClientMock("testQueue").QueueClient) .Verifiable(); composer.WithAdditionalServices( services => { services.OverrideQueueClientFactory(factory.Object); services.ConfigureServiceBus( options => { options.RegisterQueue("testQueue").WithConnection(serviceBusConnection); }); }); var provider = await composer.ComposeAndSimulateStartup(); var registry = provider.GetService <IServiceBusRegistry>(); factory.VerifyAll(); Assert.Equal("testQueue", registry.GetQueueSender("testQueue")?.Name); }
public async Task CanRegisterSubscriptionWithoutRegisteringTopic() { Task OnMessageReceived(MessageContext context) { Assert.Equal("testTopic/Subscriptions/testsubscription001", context.Receiver.Name); return(Task.CompletedTask); } var composer = new ServiceBusComposer(); composer.WithAdditionalServices(services => { services.ConfigureServiceBus(options => { options .RegisterSubscription("testTopic", "testsubscription001") .WithConnectionString("testConnectionString") .WithCustomMessageHandler <FakeMessageHandler>(); }); services.AddSingleton(new FakeMessageHandler(OnMessageReceived)); }); var provider = await composer.ComposeAndSimulateStartup(); var factory = provider.GetRequiredService <FakeSubscriptionClientFactory>(); var client = factory.GetAllRegisteredSubscriptionClients().First(); await client.TriggerMessageReception(new Message(), CancellationToken.None); }
public async Task CanRegisterSubscriptionWithRetryPolicy() { var composer = new ServiceBusComposer(); var retryPolicy = new NoRetry(); var factory = new Mock <ISubscriptionClientFactory>(); factory .Setup(o => o.Create(It.Is <SubscriptionOptions>(opts => opts.RetryPolicy == retryPolicy))) .Returns((SubscriptionOptions o) => new SubscriptionClientMock("testSubscription").Client) .Verifiable(); composer.WithAdditionalServices( services => { services.OverrideSubscriptionClientFactory(factory.Object); services.ConfigureServiceBus( options => { options.RegisterSubscription("testTopic", "testSubscription") .WithConnectionString("testConnectionString") .WithRetryPolicy(retryPolicy); }); }); var provider = await composer.ComposeAndSimulateStartup(); var registry = provider.GetService <IServiceBusRegistry>(); factory.VerifyAll(); }
public async Task CanRegisterQueueWithConnectionString() { var composer = new ServiceBusComposer(); var factory = new Mock <IQueueClientFactory>(); factory .Setup(o => o.Create(It.Is <QueueOptions>(opts => opts.ConnectionString == "testConnectionString"))) .Returns((QueueOptions o) => new QueueClientMock("testQueue").QueueClient) .Verifiable(); composer.WithAdditionalServices( services => { services.OverrideQueueClientFactory(factory.Object); services.ConfigureServiceBus( options => { options.RegisterQueue("testQueue").WithConnectionString("testConnectionString"); }); }); var provider = await composer.ComposeAndSimulateStartup(); var registry = provider.GetService <IServiceBusRegistry>(); factory.VerifyAll(); Assert.Equal("testQueue", registry.GetQueueSender("testQueue")?.Name); }
public async Task FailsSilentlyIfASubscriptionClientDoesNotCloseProperlyOnShutdown() { var composer = new ServiceBusComposer(); composer.WithAdditionalServices(services => { services.ConfigureServiceBus(options => { options.RegisterSubscription("testtopic1", "testsub1").WithConnectionString("testConnectionString1"); options.RegisterSubscription("testtopic2", "testsub1").WithConnectionString("testConnectionString2"); options.RegisterSubscription("testtopic3", "testsub1").WithConnectionString("testConnectionString3"); }); }); var provider = await composer.ComposeAndSimulateStartup(); var factory = provider.GetRequiredService <FakeSubscriptionClientFactory>(); var clientMocks = factory.GetAllRegisteredSubscriptionClients(); clientMocks[0].Mock.Setup(o => o.CloseAsync()).Returns(Task.CompletedTask).Verifiable(); clientMocks[1].Mock.Setup(o => o.CloseAsync()).Throws <SocketException>().Verifiable(); clientMocks[2].Mock.Setup(o => o.CloseAsync()).Returns(Task.CompletedTask).Verifiable(); await provider.SimulateStopHost(token : new CancellationToken()); foreach (var clientMock in clientMocks) { clientMock.Mock.Verify(o => o.CloseAsync(), Times.Once); } }
public async Task CanRegisterTopicWithReceiveMode() { var composer = new ServiceBusComposer(); var factory = new Mock <ITopicClientFactory>(); factory .Setup(o => o.Create(It.Is <TopicOptions>(opts => opts.ReceiveMode == ReceiveMode.ReceiveAndDelete))) .Returns((TopicOptions o) => new TopicClientMock("testTopic").Client) .Verifiable(); composer.WithAdditionalServices( services => { services.OverrideTopicClientFactory(factory.Object); services.ConfigureServiceBus( options => { options.RegisterTopic("testTopic") .WithConnectionString("testConnectionString") .WithReceiveMode(ReceiveMode.ReceiveAndDelete); }); }); var provider = await composer.ComposeAndSimulateStartup(); var registry = provider.GetService <IServiceBusRegistry>(); factory.VerifyAll(); Assert.Equal("testTopic", registry.GetTopicSender("testTopic")?.Name); }
public async Task CustomExceptionHandlerIsCalledWhenExceptionOccurs() { var composer = new ServiceBusComposer(); var fakeExceptionHandler = new FakeExceptionHandler(); composer.WithAdditionalServices( services => { services.AddSingleton <FakeMessageHandler>(); services.AddSingleton(fakeExceptionHandler); services.ConfigureServiceBus( options => { options .RegisterQueue("testQueue") .WithConnectionString("connectionStringTest") .WithCustomMessageHandler <FakeMessageHandler>() .WithCustomExceptionHandler <FakeExceptionHandler>(); }); }); var provider = await composer.ComposeAndSimulateStartup(); var clientMock = provider.GetQueueClientMock("testQueue"); var sentArgs = new ExceptionReceivedEventArgs(new Exception(), "", "", "", ""); await clientMock.TriggerExceptionOccured(sentArgs); fakeExceptionHandler.Mock.Verify( o => o.HandleExceptionAsync(sentArgs), Times.Once); }
public async Task CannotRegisterTwoTopicsWithTheSameName() { var composer = new ServiceBusComposer(); composer.WithAdditionalServices(services => { services.ConfigureServiceBus(options => { options.RegisterTopic("testTopic"); options.RegisterTopic("testTopic"); }); }); await Assert.ThrowsAnyAsync <DuplicateTopicRegistrationException>(async() => await composer.ComposeAndSimulateStartup()); }
public async Task CanRegisterSubscriptions() { var composer = new ServiceBusComposer(); var fakeMessageHandler = new FakeMessageHandler(); composer.WithAdditionalServices(services => { services.AddSingleton(fakeMessageHandler); services.ConfigureServiceBus(options => { options .RegisterSubscription("testTopic", "testsubscription001") .WithConnectionString("testConnectionString") .WithCustomMessageHandler <FakeMessageHandler>(); options .RegisterSubscription("testTopic", "testsubscription002") .WithConnectionString("testConnectionString") .WithCustomMessageHandler <FakeMessageHandler>(); }); }); var provider = await composer.ComposeAndSimulateStartup(); var clients = provider.GetRequiredService <FakeSubscriptionClientFactory>().GetAllRegisteredSubscriptionClients(); var message = new Message(); foreach (var client in clients) { await client.TriggerMessageReception(message, CancellationToken.None); } fakeMessageHandler.Mock.Verify( o => o.HandleMessageAsync( It.Is <MessageContext>( context => context.Message == message && context.Receiver.Name == "testTopic/Subscriptions/testsubscription001")), Times.Once); fakeMessageHandler.Mock.Verify( o => o.HandleMessageAsync( It.Is <MessageContext>( context => context.Message == message && context.Receiver.Name == "testTopic/Subscriptions/testsubscription002")), Times.Once); }
public async Task CannotRegisterTwoSubscriptionWithTheSameName() { var composer = new ServiceBusComposer(); composer.WithAdditionalServices(services => { services.ConfigureServiceBus(options => { options .RegisterSubscription("testTopic", "testsubscription001") .WithConnectionString("testConnectionString"); options .RegisterSubscription("testTopic", "testsubscription001") .WithConnectionString("testConnectionString"); }); }); await Assert.ThrowsAnyAsync <DuplicateSubscriptionRegistrationException>(async() => await composer.ComposeAndSimulateStartup()); }
public async Task AScopeIsCreatedForEachMessageReceived() { var composer = new ServiceBusComposer(); composer.WithAdditionalServices( services => { services.AddSingleton <InstanceRegistry>(); services.AddSingleton <SingletonObject>(); services.AddScoped <ScopedObject>(); services.AddTransient <TransientObject>(); services.AddTransient <InstanceCounterMessageHandler>(); services.ConfigureServiceBus( options => { options .RegisterQueue("testQueue") .WithConnectionString("connectionStringTest") .WithCustomMessageHandler <InstanceCounterMessageHandler>(); }); }); var provider = await composer.ComposeAndSimulateStartup(); var clientMock = provider.GetQueueClientMock("testQueue"); await clientMock.TriggerMessageReception(new Message(), new CancellationToken()); await clientMock.TriggerMessageReception(new Message(), new CancellationToken()); var registry = provider.GetService <InstanceRegistry>(); var numberOfSingletonInstances = registry.Instances.Where(o => o is SingletonObject).GroupBy(o => o).Count(); var numberOfScopedInstances = registry.Instances.Where(o => o is ScopedObject).GroupBy(o => o).Count(); var numberOfTransientInstances = registry.Instances.Where(o => o is TransientObject).GroupBy(o => o).Count(); Assert.Equal(1, numberOfSingletonInstances); Assert.Equal(2, numberOfScopedInstances); Assert.Equal(4, numberOfTransientInstances); }
private async Task <QueueClientMock> RegisterHandlerAndComposeServiceBus(Func <MessageContext, Task> callback) { var composer = new ServiceBusComposer(); composer.WithAdditionalServices(services => { services.AddSingleton(new FakeMessageHandler(callback)); services.ConfigureServiceBus(options => { options.RegisterQueue("testQueue") .WithConnectionString("testConnectionString") .WithCustomMessageHandler <FakeMessageHandler>(); }); }); var provider = await composer.ComposeAndSimulateStartup(); return(provider.GetQueueClientMock("testQueue")); }
private async Task <(IMessageSender sender, QueueClientMock clientMock)> ComposeServiceBusAndGetSender() { var composer = new ServiceBusComposer(); composer.WithAdditionalServices(services => { services.ConfigureServiceBus(options => { options.RegisterQueue("testQueue") .WithConnectionString("testConnectionString"); }); }); var provider = await composer.ComposeAndSimulateStartup(); return( provider.GetRequiredService <IServiceBusRegistry>().GetQueueSender("testQueue"), provider.GetQueueClientMock("testQueue") ); }
public async Task CanRegisterAndRetrieveTopics() { var composer = new ServiceBusComposer(); composer.WithAdditionalServices(services => { services.ConfigureServiceBus(options => { options.RegisterTopic("testTopic").WithConnectionString("testConnectionString"); options.RegisterTopic("testTopic2").WithConnectionString("testConnectionString2"); options.RegisterTopic("testTopic3").WithConnectionString("testConnectionString3"); }); }); var provider = await composer.ComposeAndSimulateStartup(); var registry = provider.GetService <IServiceBusRegistry>(); Assert.Equal("testTopic", registry.GetTopicSender("testTopic")?.Name); Assert.Equal("testTopic2", registry.GetTopicSender("testTopic2")?.Name); Assert.Equal("testTopic3", registry.GetTopicSender("testTopic3")?.Name); }
public async Task DoesntThrowExceptionWhenServiceBusIsDeactivated() { var services = new ServiceCollection(); services.AddLogging(); services.AddServiceBus(false); services.ConfigureServiceBus( options => { options.RegisterQueue("testQueue").WithConnectionString("testConnectionString"); }); var provider = services.BuildServiceProvider(); await provider.SimulateStartHost(token : new CancellationToken()); var composer = new ServiceBusComposer(); composer.OverrideQueueClientFactory(new FailingQueueClientFactory()); var registry = provider.GetService <ServiceBusRegistry>(); await registry.GetQueueSender("testQueue").SendAsync(new Message()); }
public async Task CustomMessageHandlerCanReceiveMessages() { var composer = new ServiceBusComposer(); var fakeMessageHandler = new FakeMessageHandler(); composer.WithAdditionalServices( services => { services.AddSingleton(fakeMessageHandler); services.ConfigureServiceBus( options => { options .RegisterSubscription("testTopic", "testSub") .WithConnectionString("connectionStringTest") .WithCustomMessageHandler <FakeMessageHandler>(); }); }); var provider = await composer.ComposeAndSimulateStartup(); var clientMock = provider.GetSubscriptionClientMock("testSub"); var sentMessage = new Message(); var sentToken = new CancellationToken(); await clientMock.TriggerMessageReception(sentMessage, sentToken); fakeMessageHandler.Mock .Verify( o => o.HandleMessageAsync( It.Is <MessageContext>( context => context.Message == sentMessage && context.Receiver.Name == "testTopic/Subscriptions/testSub" && context.Token == sentToken)), Times.Once); }