public List <Sender> Before_Sequential()
        {
            var generator = new MessageSenderLifeCycleManager(async(s, s1, arg3) =>
            {
                await Task.Yield();
                return(new Sender {
                    IsClosed = IsClosed
                });
            }, 8);

            var senders = new List <Sender>(8);

            for (int i = 0; i < 8; i++)
            {
                senders.Add(generator.Get($"entitypath-{i}", $"entitypath-{i}", $"entitypath-{i}"));
            }

            return(senders);
        }
        public Sender[] Before_Concurrent()
        {
            var generator = new MessageSenderLifeCycleManager(async(s, s1, arg3) =>
            {
                await Task.Yield();
                return(new Sender {
                    IsClosed = IsClosed
                });
            }, 8);

            var senders = new Sender[8];

            Parallel.For(0, 8, i =>
            {
                senders[i] = generator.Get($"entitypath-{i}", $"entitypath-{i}", $"entitypath-{i}");
            });

            return(senders);
        }
        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");
        }