コード例 #1
0
        public async Task DispatchAsync_WhenMultiConcurrent_ReturnsOk(int size)
        {
            // Arrange
            var barrier   = new Barrier(size);
            var mqChannel = new MqChannel();
            var channels  = new List <IChannel>
            {
                mqChannel,
            };
            var dispatcher = new BarrierDispatcher(barrier, channels);

            // Act
            var tasks = new List <Task <DispatchStatus> >();

            for (int i = 0; i < size; i++)
            {
                var t = dispatcher.DispatchAsync($"My message {DateTime.Now.ToString("o")}");
                tasks.Add(t);
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            // Assert
            var contentTask = tasks.Where(t => t.Result != DispatchStatus.Success).ToList();

            contentTask.Should().BeEmpty();
        }
コード例 #2
0
        public void SubscribeOnQueue(Action <IModel, BasicDeliverEventArgs> handler, string queueName)
        {
            var consumer = new EventingBasicConsumer(MqChannel);

            consumer.Received += (channel, args) => handler(MqChannel, args);

            MqChannel.BasicConsume(queueName, MqChannelConfig.AutoAck, consumer);
        }
コード例 #3
0
        public byte[] WaitMessageFromQueue(string queueName)
        {
            BasicGetResult result = null;

            while (result == null)
            {
                result = MqChannel.BasicGet(queueName, MqChannelConfig.AutoAck);
                Thread.Sleep(50);
            }

            return(result.Body);
        }
コード例 #4
0
        public async Task PublishAsync_WhenConcurrent_ReturnsOk()
        {
            // Arrange
            var barrier         = new Barrier(2);
            var barrierChannel1 = new MqChannel();
            var barrierChannel2 = new MqChannel();

            // Act
            var t1 = barrierChannel1.PublishAsync($"My message {DateTime.Now.ToString("o")}");
            var t2 = barrierChannel2.PublishAsync($"My message {DateTime.Now.ToString("o")}");

            await Task.WhenAll(new Task[] { t1, t2 }).ConfigureAwait(false);

            // Assert
            t1.Result.Should().Be(PublishStatus.Success);
            t2.Result.Should().Be(PublishStatus.Success);
        }
コード例 #5
0
        public async Task PublishAsync_WhenConcurrentWithInput_ReturnsOk(int numberOfMessages)
        {
            // Arrange
            var barrier   = new Barrier(numberOfMessages);
            var taskList  = new List <Task <PublishStatus> >();
            var mqChannel = new MqChannel();

            // Act
            for (int i = 0; i < numberOfMessages; i++)
            {
                taskList.Add(mqChannel.PublishAsync($"My message {DateTime.Now.ToString("o")}"));
            }

            await Task.WhenAll(taskList).ConfigureAwait(false);

            // Assert
            taskList.Where(t => t.Result == PublishStatus.Success).Count().Should().Be(numberOfMessages);
        }
コード例 #6
0
 public void CreateExchange(string name, ExchangeTypeEnum type)
 {
     MqChannel.ExchangeDeclare(name, GetExchangeTypeByEnum(type), Config.Durable, Config.AutoDelete, null);
 }
コード例 #7
0
 public void BindQueue(string queueName, string exchangeName, string routingKey)
 {
     MqChannel.QueueBind(queueName, exchangeName, routingKey, null);
 }
コード例 #8
0
 public void DeleteQueue(string queueName)
 {
     MqChannel.QueueDelete(queueName);
 }
コード例 #9
0
 public void ClearQueue(string queueName)
 {
     MqChannel.QueuePurge(queueName);
 }
コード例 #10
0
 public void CreateQueue(string queueName)
 {
     MqChannel.QueueDeclare(queueName, QueueConfig.Durable, false, false, null);
 }
コード例 #11
0
 public void PublishMessage(byte[] body, string routingKey, string exchangeName)
 {
     MqChannel.BasicPublish(exchangeName, routingKey, null, body);
 }