Ejemplo n.º 1
0
        private void RunProducerInternal <TMessage>(
            IProducerQueue
            <TMessage> queue, long messageCount, Func <QueueProducerConfiguration, AdditionalMessageData> generateData, bool sendViaBatch)
            where TMessage : class
        {
            var numberOfJobs = Convert.ToInt32(messageCount);
            var jobs         = Enumerable.Range(0, numberOfJobs)
                               .Select(i => GenerateMessage.Create <TMessage>());

            if (sendViaBatch)
            {
                var messages = new List <QueueMessage <TMessage, IAdditionalMessageData> >(numberOfJobs);
                messages.AddRange(from job in jobs let data = generateData(queue.Configuration) select data != null ? new QueueMessage <TMessage, IAdditionalMessageData>(job, data) : new QueueMessage <TMessage, IAdditionalMessageData>(job, null));
                var result    = queue.Send(messages);
                var errorList = result.Where(p => result.Any(l => p.SendingException != null))
                                .ToList();
                if (result.HasErrors)
                {
                    Assert.False(result.HasErrors, errorList[0].SendingException.ToString());
                }
                else
                {
                    Assert.False(result.HasErrors);
                }
            }
            else
            {
                Parallel.ForEach(jobs, job =>
                {
                    var data = generateData(queue.Configuration);
                    if (data != null)
                    {
                        var result  = queue.Send(job, data);
                        var message = string.Empty;
                        if (result.SendingException != null)
                        {
                            message = result.SendingException.ToString();
                        }
                        Assert.False(result.HasError, message);
                    }
                    else
                    {
                        var result  = queue.Send(job);
                        var message = string.Empty;
                        if (result.SendingException != null)
                        {
                            message = result.SendingException.ToString();
                        }
                        Assert.False(result.HasError, message);
                    }
                });
            }
        }
        private async Task RunProducerInternalAsync <TMessage>(
            IProducerQueue
            <TMessage> queue, long messageCount, Func <QueueProducerConfiguration, AdditionalMessageData> generateData, bool sendViaBatch)
            where TMessage : class
        {
            var numberOfJobs = Convert.ToInt32(messageCount);
            var jobs         = Enumerable.Range(0, numberOfJobs)
                               .Select(i => GenerateMessage.Create <TMessage>());

            if (sendViaBatch)
            {
                var messages = new List <QueueMessage <TMessage, IAdditionalMessageData> >(numberOfJobs);
                messages.AddRange(from job in jobs let data = generateData(queue.Configuration) select data != null ? new QueueMessage <TMessage, IAdditionalMessageData>(job, data) : new QueueMessage <TMessage, IAdditionalMessageData>(job, null));
                var results = await queue.SendAsync(messages).ConfigureAwait(false);

                Assert.False(results.HasErrors);
            }
            else
            {
                foreach (var job in jobs)
                {
                    var data = generateData(queue.Configuration);
                    if (data != null)
                    {
                        var result = await queue.SendAsync(job, data).ConfigureAwait(false);

                        Assert.False(result.HasError);
                    }
                    else
                    {
                        var result = await queue.SendAsync(job).ConfigureAwait(false);

                        Assert.False(result.HasError);
                    }
                }
            }
        }