public void Enqueue_SendsTheJobId()
        {
            // Arrange
            using (var queue = CleanRabbitMqQueueAttribute.GetMessageQueue("my-queue"))
            {
                // Act
#if FEATURE_TRANSACTIONSCOPE
                var connection = new Mock <IDbConnection>().Object;
                queue.Enqueue(connection, "my-queue", "job-id");
#else
                var connection = new Mock <System.Data.Common.DbConnection>().Object;
                using (var transaction = connection.BeginTransaction())
                {
                    queue.Enqueue(connection, transaction, "my-queue", "job-id");
                    transaction.Commit();
                }
#endif

                // Assert
                var fetchedJob = queue.Dequeue(new[] { "my-queue" }, _token);
                fetchedJob.RemoveFromQueue();

                var body = fetchedJob.JobId;

                Assert.Equal("job-id", body);
            }
        }
        public void Dequeue_ReturnsFetchedJob_FromOtherQueues_IfFirstAreEmpty()
        {
            RabbitMqUtils.EnqueueJobId("queue-2", "job-id");
            using (var queue = CleanRabbitMqQueueAttribute.GetMessageQueue("queue-1", "queue-2"))
            {
                var fetchedJob = queue.Dequeue(new[] { "queue-1", "queue-2" }, _token);

                Assert.Equal("job-id", fetchedJob.JobId);
            }
        }
        public void Dequeue_ThrowsCanceledException_WhenTokenHasBeenCancelled()
        {
            using (var queue = CleanRabbitMqQueueAttribute.GetMessageQueue("my-queue"))
            {
                var token = new CancellationToken(true);

                Assert.Throws <OperationCanceledException>(
                    () => queue.Dequeue(new[] { "my-queue" }, token));
            }
        }
        public void Dequeue_ReturnsFetchedJob_WithJobId()
        {
            RabbitMqUtils.EnqueueJobId("my-queue", "job-id");

            using (var queue = CleanRabbitMqQueueAttribute.GetMessageQueue("my-queue"))
            {
                var fetchedJob = queue.Dequeue(new[] { "my-queue" }, _token);
                fetchedJob.RemoveFromQueue();

                Assert.Equal("job-id", fetchedJob.JobId);
            }
        }
示例#5
0
        public static void EnqueueJobId(string queue, string jobId)
        {
            using (var messageQueue = CleanRabbitMqQueueAttribute.GetMessageQueue(queue))
            {
                var body = Encoding.UTF8.GetBytes(jobId);

                var properties = messageQueue.Channel.CreateBasicProperties();
                properties.SetPersistent(true);

                messageQueue.Channel.BasicPublish("", queue, properties, body);
            }
        }
        public void DisposeWithoutRemoval_OnFetchedJob_ReturnsTheJobToTheQueue()
        {
            // Arrange
            RabbitMqUtils.EnqueueJobId("my-queue", "job-id");

            using (var queue = CleanRabbitMqQueueAttribute.GetMessageQueue("my-queue"))
            {
                // Act
                var fetchedJob = queue.Dequeue(new[] { "my-queue" }, _token);
                fetchedJob.Dispose();

                // Assert
                var jobId = RabbitMqUtils.DequeueJobId("my-queue", TimeSpan.FromSeconds(5));
                Assert.Equal("job-id", jobId);
            }
        }
        public void DisposeWithoutRemoval_OnFetchedJob_CanBeDequeuedAgain()
        {
            // Arrange
            RabbitMqUtils.EnqueueJobId("my-queue", "job-id");

            using (var queue = CleanRabbitMqQueueAttribute.GetMessageQueue("my-queue"))
            {
                // Act
                var fetchedJob = queue.Dequeue(new[] { "my-queue" }, _token);
                fetchedJob.Dispose();

                // Assert
                var newFetchedJob = queue.Dequeue(new[] { "my-queue" }, _token);

                Assert.Equal("job-id", newFetchedJob.JobId);
            }
        }
        public void Enqueue_SendsTheJobId()
        {
            // Arrange
            using (var queue = CleanRabbitMqQueueAttribute.GetMessageQueue("my-queue"))
            {
                // Act
                queue.Enqueue(new Mock <IDbConnection>().Object, "my-queue", "job-id");

                // Assert
                var fetchedJob = queue.Dequeue(new[] { "my-queue" }, _token);
                fetchedJob.RemoveFromQueue();

                var body = fetchedJob.JobId;

                Assert.Equal("job-id", body);
            }
        }
        public void RemoveFromQueue_OnFetchedJob_RemovesTheJobCompletely()
        {
            // Arrange
            RabbitMqUtils.EnqueueJobId("my-queue", "job-id");
            using (var queue = CleanRabbitMqQueueAttribute.GetMessageQueue("my-queue"))
            {
                // Act
                using (var fetchedJob = queue.Dequeue(new[] { "my-queue" }, _token))
                {
                    fetchedJob.RemoveFromQueue();
                }

                // Assert
                var exception = Assert.Throws <TimeoutException>(
                    () => RabbitMqUtils.DequeueJobId("my-queue", TimeSpan.FromSeconds(1)));

                Assert.Equal(exception.Message, "my-queue");
            }
        }
        public void Dequeue_MakesJobInvisibleForOtherFetchers()
        {
            // Arrange
            RabbitMqUtils.EnqueueJobId("my-queue", "job-id9");

            using (var queue = CleanRabbitMqQueueAttribute.GetMessageQueue("my-queue"))
            {
                // Act
                var fetchedJob = queue.Dequeue(new[] { "my-queue" }, _token);

                // Assert
                Assert.NotNull(fetchedJob);

                var exception = Assert.Throws <TimeoutException>(
                    () => RabbitMqUtils.DequeueJobId("my-queue", TimeSpan.FromSeconds(1)));

                Assert.Equal(exception.Message, "my-queue");
            }
        }
示例#11
0
        public static string DequeueJobId(string queue, TimeSpan timeout)
        {
            int timeoutMilliseconds = (int)timeout.TotalMilliseconds;

            using (var messageQueue = CleanRabbitMqQueueAttribute.GetMessageQueue(queue))
            {
                messageQueue.Channel.BasicQos(0, 1, false);
                var consumer = new QueueingBasicConsumer(messageQueue.Channel);
                messageQueue.Channel.BasicConsume(queue, false, consumer);

                BasicDeliverEventArgs message;
                bool dequeued = consumer.Queue.Dequeue(timeoutMilliseconds, out message);
                if (dequeued == false)
                {
                    throw new TimeoutException(queue);
                }

                return(Encoding.UTF8.GetString(message.Body));
            }
        }