public void Caches_single_manager_for_reuse() { var creator = new InterceptingCreator(); var lifecycleManager = new NamespaceManagerLifeCycleManagerInternal(creator); var first = lifecycleManager.Get(AzureServiceBusConnectionString.Value); var second = lifecycleManager.Get(AzureServiceBusConnectionString.Value); Assert.AreEqual(1, creator.InvocationCount); Assert.AreEqual(first, second); }
public async Task Should_throw_if_at_least_one_batch_fails() { // cleanup await TestUtility.Delete("myqueue", "myqueue2"); // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var clientLifecycleManager = new MessageSenderLifeCycleManager(messageSenderCreator, settings); var router = new OutgoingBatchRouter(new BatchedOperationsToBrokeredMessagesConverter(settings), clientLifecycleManager, settings, new ThrowOnOversizedBrokeredMessages()); // create the queue var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("myqueue", namespaceManager); // perform the test var dispatcher = new Dispatcher(router, new FakeBatcher()); // validate Assert.ThrowsAsync <MessagingEntityNotFoundException>(async() => await dispatcher.Dispatch(new TransportOperations(), new TransportTransaction(), new ContextBag())); }
public async Task Can_send_a_brokered_message() { // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set <TopologySettings>(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); // create the queue var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("myqueue", namespaceManager); // perform the test var sender = await messageSenderCreator.Create("myqueue", null, "namespace"); await sender.Send(new BrokeredMessage()); //validate var queue = await namespaceManager.GetQueue("myqueue"); Assert.IsTrue(queue.MessageCount > 0); //cleanup await namespaceManager.DeleteQueue("myqueue"); }
public async Task Should_throw_exception_for_a_message_that_exceeds_maximum_size() { // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var clientLifecycleManager = new MessageSenderLifeCycleManager(messageSenderCreator, settings); var router = new OutgoingBatchRouter(new BatchedOperationsToBrokeredMessagesConverter(settings), clientLifecycleManager, settings, new ThrowOnOversizedBrokeredMessages()); // create the queue var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("myqueue", namespaceManager); // setup the batch var @namespace = new RuntimeNamespaceInfo("namespace", AzureServiceBusConnectionString.Value); var bytes = Enumerable.Range(0, settings.Get <int>(WellKnownConfigurationKeys.Connectivity.MessageSenders.MaximumMessageSizeInKilobytes) * 1024).Select(x => (byte)(x % 256)).ToArray(); var batch = new BatchInternal { Destinations = new TopologySectionInternal { Entities = new List <EntityInfoInternal> { new EntityInfoInternal { Namespace = @namespace, Path = "MyQueue", Type = EntityType.Queue } }, Namespaces = new List <RuntimeNamespaceInfo> { @namespace } }, RequiredDispatchConsistency = DispatchConsistency.Default, Operations = new List <BatchedOperationInternal> { new BatchedOperationInternal { Message = new OutgoingMessage("Id-1", new Dictionary <string, string>(), bytes), DeliveryConstraints = new List <DeliveryConstraint>() } } }; // perform the test Assert.That(async() => await router.RouteBatch(batch, null, DispatchConsistency.Default), Throws.Exception.TypeOf <MessageTooLargeException>()); }
public void Requests_creation_of_new_manager_for_namespace_initially() { var creator = new InterceptingCreator(); var lifecycleManager = new NamespaceManagerLifeCycleManagerInternal(creator); lifecycleManager.Get(AzureServiceBusConnectionString.Value); Assert.IsTrue(creator.HasBeenInvoked); }
public async Task Should_dispatch_multiple_batches() { // cleanup await TestUtility.Delete("myqueue", "myqueue2"); // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set(new TopologySettings()); settings.Set("NServiceBus.Routing.EndpointName", "myqueue"); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var clientLifecycleManager = new MessageSenderLifeCycleManager(messageSenderCreator, settings); var router = new OutgoingBatchRouter(new BatchedOperationsToBrokeredMessagesConverter(settings), clientLifecycleManager, settings, new ThrowOnOversizedBrokeredMessages()); // create the queue var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("myqueue", namespaceManager); await creator.Create("myqueue2", namespaceManager); // perform the test var dispatcher = new Dispatcher(router, new FakeBatcher()); await dispatcher.Dispatch(new TransportOperations(), new TransportTransaction(), new ContextBag()); //validate var queue = await namespaceManager.GetQueue("myqueue"); Assert.IsTrue(queue.MessageCount == 2, $"'myqueue' was expected to have 2 message, but it didn't ({queue.MessageCount} found)"); queue = await namespaceManager.GetQueue("myqueue2"); Assert.IsTrue(queue.MessageCount == 2, $"'myqueue2' was expected to have 2 message, but it didn't ({queue.MessageCount} found)"); }
public async Task Can_start_stop_and_restart_notifier() { // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set <TopologySettings>(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageReceiverCreator = new MessageReceiverCreator(messagingFactoryLifeCycleManager, settings); var clientEntityLifeCycleManager = new MessageReceiverLifeCycleManager(messageReceiverCreator, settings); var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var brokeredMessageConverter = new BrokeredMessagesToIncomingMessagesConverter(settings, new PassThroughMapper(settings)); // create the queue var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("myqueue", namespaceManager); // perform the test var notifier = new MessageReceiverNotifier(clientEntityLifeCycleManager, brokeredMessageConverter, BuildMessageReceiverNotifierSettings(settings)); notifier.Initialize(new EntityInfoInternal { Path = "myqueue", Namespace = new RuntimeNamespaceInfo("namespace", AzureServiceBusConnectionString.Value) }, (message, context) => TaskEx.Completed, null, null, null, 10); notifier.Start(); await notifier.Stop(); notifier.Start(); await notifier.Stop(); //cleanup await namespaceManager.DeleteQueue("myqueue"); }
public async Task Can_receive_a_brokered_message() { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageReceiverCreator = new MessageReceiverCreator(messagingFactoryLifeCycleManager, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); // create the queue var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("myqueue", namespaceManager); // put a message on the queue var sender = await messageSenderCreator.Create("myqueue", null, "namespace"); await sender.Send(new BrokeredMessage()); // perform the test var receiver = await messageReceiverCreator.Create("myqueue", "namespace"); var completed = new AsyncManualResetEvent(false); var error = new AsyncManualResetEvent(false); Exception ex = null; var received = false; var options = new OnMessageOptions(); options.ExceptionReceived += (o, args) => { ex = args.Exception; error.Set(); }; receiver.OnMessage(message => { received = true; completed.Set(); return(TaskEx.Completed); }, options); await Task.WhenAny(completed.WaitAsync(cts.Token).IgnoreCancellation(), error.WaitAsync(cts.Token).IgnoreCancellation()); // validate Assert.IsTrue(received); Assert.IsNull(ex); //cleanup await receiver.CloseAsync(); await namespaceManager.DeleteQueue("myqueue"); }
public async Task Can_receive_messages_with_prefetch_fast() { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); settings.Set(WellKnownConfigurationKeys.Connectivity.MessageReceivers.PrefetchCount, 500); settings.Set(WellKnownConfigurationKeys.Connectivity.MessageReceivers.ReceiveMode, ReceiveMode.PeekLock); settings.Set(WellKnownConfigurationKeys.Connectivity.SendViaReceiveQueue, true); // default values set by DefaultConfigurationValues.Apply - shouldn't hardcode those here, so OK to use settings var messageReceiverNotifierSettings = new MessageReceiverNotifierSettings( ReceiveMode.PeekLock, settings.HasExplicitValue <TransportTransactionMode>() ? settings.Get <TransportTransactionMode>() : settings.SupportedTransactionMode(), settings.Get <TimeSpan>(WellKnownConfigurationKeys.Connectivity.MessageReceivers.AutoRenewTimeout), settings.Get <int>(WellKnownConfigurationKeys.Connectivity.NumberOfClientsPerEntity)); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifeCycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifeCycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageReceiverCreator = new MessageReceiverCreator(messagingFactoryLifeCycleManager, settings); var creator = new AzureServiceBusQueueCreator(new TopologyQueueSettings(), settings); var brokeredMessageConverter = new BrokeredMessagesToIncomingMessagesConverter(settings, new PassThroughMapper(settings)); // create the queue var namespaceManager = namespaceLifeCycleManager.Get("namespace"); var queue = await creator.Create("myqueue", namespaceManager); var receivedMessages = 0; var completed = new AsyncManualResetEvent(false); // sending messages to the queue var senderFactory = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var sender = await senderFactory.Create("myqueue", null, "namespace"); var counter = 0; var tasks = new List <Task>(); for (var j = 0; j < 10; j++) { var batch = new List <BrokeredMessage>(); for (var i = 0; i < 100; i++) { batch.Add(new BrokeredMessage(Encoding.UTF8.GetBytes("Whatever" + counter))); counter++; } tasks.Add(sender.RetryOnThrottleAsync(s => s.SendBatch(batch), s => s.SendBatch(batch.Select(x => x.Clone())), TimeSpan.FromSeconds(10), 5)); } await Task.WhenAll(tasks); var faulted = tasks.Count(task => task.IsFaulted); var expected = 1000 - faulted; // sending messages to the queue is done var notifier = new MessageReceiverNotifier(messageReceiverCreator, brokeredMessageConverter, messageReceiverNotifierSettings); notifier.Initialize(new EntityInfoInternal { Path = "myqueue", Namespace = new RuntimeNamespaceInfo("namespace", AzureServiceBusConnectionString.Value) }, (message, context) => { var numberOfMessages = Interlocked.Increment(ref receivedMessages); if (numberOfMessages == expected) { completed.Set(); } return(TaskEx.Completed); }, null, null, null, 32); var sw = new Stopwatch(); sw.Start(); notifier.Start(); await completed.WaitAsync(cts.Token).IgnoreCancellation(); sw.Stop(); await notifier.Stop(); Assert.IsTrue(receivedMessages == expected); Console.WriteLine($"Receiving {receivedMessages} messages took {sw.ElapsedMilliseconds} milliseconds"); Console.WriteLine("Total of {0} msgs / second", (double)receivedMessages / sw.ElapsedMilliseconds * 1000); // make sure messages are auto-completed Assert.That(queue.MessageCount, Is.EqualTo(0), "Messages where not completed!"); //cleanup await namespaceManager.DeleteQueue("myqueue"); }
public async Task Can_send_batches_of_brokered_messages_fast() { // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); var extensions = new TransportExtensions <AzureServiceBusTransport>(settings); extensions.NumberOfClientsPerEntity(5); extensions.MessagingFactories() .NumberOfMessagingFactoriesPerNamespace(5) .BatchFlushInterval(TimeSpan.FromMilliseconds(0)); // turns of native batching // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var entityLifecycleManager = new MessageSenderLifeCycleManager(messageSenderCreator, settings); var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); // create the queue var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("myqueue", namespaceManager); // perform the test var sw = new Stopwatch(); sw.Start(); var counter = 0; var tasks = new List <Task>(); for (var j = 0; j < 10; j++) { var batch = new List <BrokeredMessage>(); for (var i = 0; i < 1000; i++) { batch.Add(new BrokeredMessage()); counter++; } var sender = await entityLifecycleManager.Get("myqueue", null, "namespace"); tasks.Add(sender.RetryOnThrottleAsync(s => s.SendBatch(batch), s => s.SendBatch(batch.Select(x => x.Clone())), TimeSpan.FromSeconds(10), 5)); } await Task.WhenAll(tasks); //validate sw.Stop(); Console.WriteLine("Sending {1} messages took {0} milliseconds", sw.ElapsedMilliseconds, counter); var faulted = tasks.Count(task => task.IsFaulted); Console.WriteLine("Total of {0} msgs / second", (double)(10000 - faulted) / sw.ElapsedMilliseconds * 1000); Assert.IsTrue(sw.ElapsedMilliseconds < TimeSpan.FromMinutes(1).TotalMilliseconds); //cleanup await namespaceManager.DeleteQueue("myqueue"); }
public async Task Can_route_an_outgoing_batch_of_messages() { // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set <TopologySettings>(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var clientLifecycleManager = new MessageSenderLifeCycleManager(messageSenderCreator, settings); var router = new OutgoingBatchRouter(new BatchedOperationsToBrokeredMessagesConverter(settings), clientLifecycleManager, settings, new ThrowOnOversizedBrokeredMessages()); // create the queue var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("myqueue", namespaceManager); // setup the batch var @namespace = new RuntimeNamespaceInfo("namespace", AzureServiceBusConnectionString.Value); var bytes = Encoding.UTF8.GetBytes("Whatever"); var batch = new BatchInternal { Destinations = new TopologySectionInternal { Entities = new List <EntityInfoInternal> { new EntityInfoInternal { Namespace = @namespace, Path = "MyQueue", Type = EntityType.Queue } }, Namespaces = new List <RuntimeNamespaceInfo> { @namespace } }, RequiredDispatchConsistency = DispatchConsistency.Default, Operations = new List <BatchedOperationInternal> { new BatchedOperationInternal { Message = new OutgoingMessage("Id-1", new Dictionary <string, string>(), bytes), DeliveryConstraints = new List <DeliveryConstraint>() }, new BatchedOperationInternal { Message = new OutgoingMessage("Id-2", new Dictionary <string, string>(), bytes), DeliveryConstraints = new List <DeliveryConstraint>() } } }; // perform the test await router.RouteBatch(batch, null, DispatchConsistency.Default); //validate var queue = await namespaceManager.GetQueue("myqueue"); Assert.IsTrue(queue.MessageCount == 2); //cleanup await namespaceManager.DeleteQueue("myqueue"); }
public async Task Should_invoke_non_throwing_oversized_brokered_message_handler_for_a_message_that_exceeds_maximum_size_only_once_even_if_fallback_namespace_is_set() { // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set <TopologySettings>(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("primary", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); namespacesDefinition.Add("fallback", AzureServiceBusConnectionString.Fallback, NamespacePurpose.Partitioning); var oversizedHandler = new MyOversizedBrokeredMessageHandler(); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var clientLifecycleManager = new MessageSenderLifeCycleManager(messageSenderCreator, settings); var router = new OutgoingBatchRouter(new BatchedOperationsToBrokeredMessagesConverter(settings), clientLifecycleManager, settings, oversizedHandler); // create the queue & fallback queue var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var namespaceManager = namespaceLifecycleManager.Get("primary"); await creator.Create("myqueue", namespaceManager); var fallbackNamespaceManager = namespaceLifecycleManager.Get("fallback"); await creator.Create("myqueue", fallbackNamespaceManager); // setup the batch var @namespace = new RuntimeNamespaceInfo("primary", AzureServiceBusConnectionString.Value); var @fallback = new RuntimeNamespaceInfo("fallback", AzureServiceBusConnectionString.Value, mode: NamespaceMode.Passive); var bytes = Enumerable.Range(0, settings.Get <int>(WellKnownConfigurationKeys.Connectivity.MessageSenders.MaximumMessageSizeInKilobytes) * 1024).Select(x => (byte)(x % 256)).ToArray(); var batch = new BatchInternal { Destinations = new TopologySectionInternal { Entities = new List <EntityInfoInternal> { new EntityInfoInternal { Namespace = @namespace, Path = "myqueue", Type = EntityType.Queue } }, Namespaces = new List <RuntimeNamespaceInfo> { @namespace, @fallback } }, RequiredDispatchConsistency = DispatchConsistency.Default, Operations = new List <BatchedOperationInternal> { new BatchedOperationInternal { Message = new OutgoingMessage("Id-1", new Dictionary <string, string>(), bytes), DeliveryConstraints = new List <DeliveryConstraint>() } } }; // perform the test await router.RouteBatch(batch, null, DispatchConsistency.Default); // validate Assert.True(oversizedHandler.InvocationCount == 1); //cleanup await fallbackNamespaceManager.DeleteQueue("myqueue"); await namespaceManager.DeleteQueue("myqueue"); }
public async Task Should_route_via_active_namespace_first() { // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set <TopologySettings>(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("primary", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); namespacesDefinition.Add("fallback", AzureServiceBusConnectionString.Fallback, NamespacePurpose.Partitioning); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var clientLifecycleManager = new MessageSenderLifeCycleManager(messageSenderCreator, settings); var router = new OutgoingBatchRouter(new BatchedOperationsToBrokeredMessagesConverter(settings), clientLifecycleManager, settings, new ThrowOnOversizedBrokeredMessages()); // create the fallback queue (but not the queue in the primary to emulate that it is down) var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var primaryNamespaceManager = namespaceLifecycleManager.Get("primary"); var fallbackNamespaceManager = namespaceLifecycleManager.Get("fallback"); await creator.Create("myqueue", primaryNamespaceManager); await creator.Create("myqueue", fallbackNamespaceManager); // setup the batch var @namespace = new RuntimeNamespaceInfo("primary", AzureServiceBusConnectionString.Value); var fallback = new RuntimeNamespaceInfo("fallback", AzureServiceBusConnectionString.Fallback, mode: NamespaceMode.Passive); var bytes = Encoding.UTF8.GetBytes("Whatever"); var batch = new BatchInternal { Destinations = new TopologySectionInternal { Entities = new List <EntityInfoInternal> { new EntityInfoInternal { Namespace = @namespace, Path = "MyQueue", Type = EntityType.Queue }, new EntityInfoInternal { Namespace = fallback, Path = "MyQueue", Type = EntityType.Queue } }, Namespaces = new List <RuntimeNamespaceInfo> { @namespace, fallback } }, RequiredDispatchConsistency = DispatchConsistency.Default, Operations = new List <BatchedOperationInternal> { new BatchedOperationInternal { Message = new OutgoingMessage("SomeId", new Dictionary <string, string>(), bytes), DeliveryConstraints = new List <DeliveryConstraint>() }, } }; // perform the test await router.RouteBatch(batch, null, DispatchConsistency.Default); //validate var queueOnPrimaryNamespace = await primaryNamespaceManager.GetQueue("myqueue"); Assert.IsTrue(queueOnPrimaryNamespace.MessageCount > 0, "expected to have messages in the primary queue, but there were no messages"); var queueOnSecondaryNamespace = await fallbackNamespaceManager.GetQueue("myqueue"); Assert.IsTrue(queueOnSecondaryNamespace.MessageCount == 0, "expected NOT to have messages in the secondary queue, but there were no messages"); //cleanup await primaryNamespaceManager.DeleteQueue("myqueue"); await fallbackNamespaceManager.DeleteQueue("myqueue"); }
public async Task Can_receive_an_incoming_message() { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageReceiverCreator = new MessageReceiverCreator(messagingFactoryLifeCycleManager, settings); var messageSenderCreator = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var brokeredMessageConverter = new BrokeredMessagesToIncomingMessagesConverter(settings, new PassThroughMapper(settings)); // create the queue var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("myqueue", namespaceManager); // put a message on the queue var sender = await messageSenderCreator.Create("myqueue", null, "namespace"); await sender.Send(new BrokeredMessage()); // perform the test var notifier = new MessageReceiverNotifier(messageReceiverCreator, brokeredMessageConverter, BuildMessageReceiverNotifierSettings(settings)); var completed = new AsyncManualResetEvent(false); var error = new AsyncManualResetEvent(false); Exception ex = null; var received = false; notifier.Initialize(new EntityInfoInternal { Path = "myqueue", Namespace = new RuntimeNamespaceInfo("namespace", AzureServiceBusConnectionString.Value) }, (message, context) => { received = true; completed.Set(); return(TaskEx.Completed); }, exception => { ex = exception; error.Set(); return(TaskEx.Completed); }, null, null, 1); notifier.Start(); await Task.WhenAny(completed.WaitAsync(cts.Token).IgnoreCancellation(), error.WaitAsync(cts.Token).IgnoreCancellation()); // validate Assert.IsTrue(received); Assert.IsNull(ex); //cleanup await notifier.Stop(); await namespaceManager.DeleteQueue("myqueue"); }
public async Task Should_have_rolled_back_message_to_error_queue() { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // set lock duration on a queue to 5 seconds and emulate message processing that takes longer than that, but less than AutoRenewTimeout settings.Get <TopologySettings>().QueueSettings.LockDuration = TimeSpan.FromSeconds(2); settings.Set(WellKnownConfigurationKeys.Connectivity.MessageReceivers.AutoRenewTimeout, TimeSpan.FromSeconds(5)); // default values set by DefaultConfigurationValues.Apply - shouldn't hardcode those here, so OK to use settings var messageReceiverNotifierSettings = new MessageReceiverNotifierSettings( ReceiveMode.PeekLock, TransportTransactionMode.SendsAtomicWithReceive, settings.Get <TimeSpan>(WellKnownConfigurationKeys.Connectivity.MessageReceivers.AutoRenewTimeout), settings.Get <int>(WellKnownConfigurationKeys.Connectivity.NumberOfClientsPerEntity)); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageReceiverCreator = new MessageReceiverCreator(messagingFactoryLifeCycleManager, settings); var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var brokeredMessageConverter = new BrokeredMessagesToIncomingMessagesConverter(settings, new PassThroughMapper(settings)); // create the queue var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("completionfailure", namespaceManager); var completed = new AsyncManualResetEvent(false); // sending messages to the queue var senderFactory = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var sender = await senderFactory.Create("completionfailure", null, "namespace"); var messageToSend = new BrokeredMessage(Encoding.UTF8.GetBytes("Whatever")) { MessageId = Guid.NewGuid().ToString() }; await sender.Send(messageToSend); // sending messages to the queue is done var rolledBack = new RollbackDetection(); var notifier = new MessageReceiverNotifier(messageReceiverCreator, brokeredMessageConverter, messageReceiverNotifierSettings); notifier.Initialize(new EntityInfoInternal { Path = "completionfailure", Namespace = new RuntimeNamespaceInfo("namespace", AzureServiceBusConnectionString.Value) }, (message, context) => { if (message.MessageId == messageToSend.MessageId) { Transaction.Current.EnlistVolatile(new EmulateCompletionFailure(), EnlistmentOptions.EnlistDuringPrepareRequired); throw new Exception("Force processing failure"); } return(TaskEx.Completed); }, null, null, context => { if (context.Message.MessageId == messageToSend.MessageId) { Transaction.Current.EnlistVolatile(rolledBack, EnlistmentOptions.None); completed.Set(); } return(Task.FromResult(ErrorHandleResult.Handled)); }, 1); var sw = new Stopwatch(); sw.Start(); notifier.Start(); await completed.WaitAsync(cts.Token).IgnoreCancellation(); sw.Stop(); await notifier.Stop(); Assert.IsTrue(rolledBack.RolledBack, "Should have rolled back the error message."); Console.WriteLine($"Callback processing took {sw.ElapsedMilliseconds} milliseconds"); //cleanup await namespaceManager.DeleteQueue("completionfailure"); }
public async Task AutoRenewTimeout_will_extend_lock_for_processing_to_finish() { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(60)); // default settings var settings = DefaultConfigurationValues.Apply(SettingsHolderFactory.BuildWithSerializer()); settings.Set(new TopologySettings()); var namespacesDefinition = settings.Get <NamespaceConfigurations>(WellKnownConfigurationKeys.Topology.Addressing.Namespaces); namespacesDefinition.Add("namespace", AzureServiceBusConnectionString.Value, NamespacePurpose.Partitioning); // set lock duration on a queue to 5 seconds and emulate message processing that takes longer than that, but less than AutoRenewTimeout settings.Get <TopologySettings>().QueueSettings.LockDuration = TimeSpan.FromSeconds(2); settings.Set(WellKnownConfigurationKeys.Connectivity.MessageReceivers.AutoRenewTimeout, TimeSpan.FromSeconds(5)); // default values set by DefaultConfigurationValues.Apply - shouldn't hardcode those here, so OK to use settings var messageReceiverNotifierSettings = new MessageReceiverNotifierSettings( ReceiveMode.PeekLock, settings.HasExplicitValue <TransportTransactionMode>() ? settings.Get <TransportTransactionMode>() : settings.SupportedTransactionMode(), settings.Get <TimeSpan>(WellKnownConfigurationKeys.Connectivity.MessageReceivers.AutoRenewTimeout), settings.Get <int>(WellKnownConfigurationKeys.Connectivity.NumberOfClientsPerEntity)); // setup the infrastructure var namespaceManagerCreator = new NamespaceManagerCreator(settings); var namespaceLifecycleManager = new NamespaceManagerLifeCycleManagerInternal(namespaceManagerCreator); var messagingFactoryCreator = new MessagingFactoryCreator(namespaceLifecycleManager, settings); var messagingFactoryLifeCycleManager = new MessagingFactoryLifeCycleManager(messagingFactoryCreator, settings); var messageReceiverCreator = new MessageReceiverCreator(messagingFactoryLifeCycleManager, settings); var creator = new AzureServiceBusQueueCreator(settings.Get <TopologySettings>().QueueSettings, settings); var brokeredMessageConverter = new BrokeredMessagesToIncomingMessagesConverter(settings, new PassThroughMapper(settings)); // create the queue var namespaceManager = namespaceLifecycleManager.Get("namespace"); await creator.Create("autorenewtimeout", namespaceManager); var receivedMessages = 0; var completed = new AsyncManualResetEvent(false); // sending messages to the queue var senderFactory = new MessageSenderCreator(messagingFactoryLifeCycleManager, settings); var sender = await senderFactory.Create("autorenewtimeout", null, "namespace"); var messageToSend = new BrokeredMessage(Encoding.UTF8.GetBytes("Whatever")) { MessageId = Guid.NewGuid().ToString() }; await sender.Send(messageToSend); // sending messages to the queue is done var notifier = new MessageReceiverNotifier(messageReceiverCreator, brokeredMessageConverter, messageReceiverNotifierSettings); notifier.Initialize(new EntityInfoInternal { Path = "autorenewtimeout", Namespace = new RuntimeNamespaceInfo("namespace", AzureServiceBusConnectionString.Value) }, async(message, context) => { if (message.MessageId == messageToSend.MessageId) { Interlocked.Increment(ref receivedMessages); if (receivedMessages > 1) { Assert.Fail("Callback should only receive one message once, but it did more than that."); } await Task.Delay(TimeSpan.FromSeconds(5), cts.Token); completed.Set(); } }, null, null, null, 1); var sw = new Stopwatch(); sw.Start(); notifier.Start(); await completed.WaitAsync(cts.Token).IgnoreCancellation(); sw.Stop(); await notifier.Stop(); Assert.AreEqual(1, receivedMessages, $"Expected to receive message once, but got {receivedMessages}."); Console.WriteLine($"Callback processing took {sw.ElapsedMilliseconds} milliseconds"); //cleanup await namespaceManager.DeleteQueue("autorenewtimeout"); }