Пример #1
0
        private void FireMessageReceivedEvent(MessageEnvelope messageEnvelope)
        {
            var task = new Task(() =>
            {
                var eventArgs = new MessageReceivedEventArgs(messageEnvelope);

                MessageReceived?.Invoke(this, eventArgs);

                if (!autoAcknowledgeMessages)
                {
                    if (eventArgs.Response == MessageResponse.Ack)
                    {
                        Channel.BasicAck(messageEnvelope.DeliveryTag, false);
                    }
                    else if (eventArgs.Response == MessageResponse.Nack)
                    {
                        Channel.BasicAck(messageEnvelope.DeliveryTag, false);
                    }
                    else if (eventArgs.Response == MessageResponse.RejectPermanent)
                    {
                        Channel.BasicReject(messageEnvelope.DeliveryTag, false);
                    }
                    else
                    {
                        Channel.BasicReject(messageEnvelope.DeliveryTag, true);
                    }
                }

                limitedConsumeSempahore?.Give();
            });

            task.Start();
        }
        public void GiveTakeTest()
        {
            var sem = new SimpleCountingSemaphore(3, 0);

            sem.Take();
            Assert.AreEqual(sem.Count, 1);
            sem.Take();
            Assert.AreEqual(sem.Count, 2);
            sem.Take();
            Assert.AreEqual(sem.Count, 3);

            var result = sem.TryTake();

            Assert.IsFalse(result);
            Assert.AreEqual(sem.Count, 3);

            Timer timer = new Timer(200);

            timer.Elapsed += (sender, e) => sem.Give();
            timer.Start();

            sem.Take();
            Assert.AreEqual(sem.Count, 3);

            sem.Give();
            Assert.AreEqual(sem.Count, 2);
            sem.Give();
            Assert.AreEqual(sem.Count, 1);
            sem.Give();
            Assert.AreEqual(sem.Count, 0);
            sem.Give();
            Assert.AreEqual(sem.Count, 0);

            sem.Dispose();
            CatchExceptionHelper.VerifyExceptionCaught <ObjectDisposedException>(() => sem.Take());
        }