public async Task EnqueueThrowsIfMessageNotAcknowledged()
        {
            var queue = GivenUniqueQueueName();
            var message = GivenSampleMessage();
            var listener = new QueueListenerStub((m, c) => {});
            await MessageQueueingService.CreateQueue(queue, listener);
            await Assert.ThrowsAsync<MessageNotAcknowledgedException>(() => MessageQueueingService.EnqueueMessage(queue, message, Principal));
            await listener.Completed;
            listener.Dispose();

            await QueueOperationCompletion();
        }
 public async Task PrincipalIsPreservedWhenListenerInvoked()
 {
     var listener = new QueueListenerStub();
     GivenClaimsPrincipal();
     var queue = GivenUniqueQueueName();
     var message = GivenSampleMessage();
     await MessageQueueingService.CreateQueue(queue, listener);
     await MessageQueueingService.EnqueueMessage(queue, message, Principal);
     await listener.Completed;
     listener.Dispose();
     AssertPrincipalPreserved(listener.Context.Principal);
 }
        public async Task QueueListenerFiresWhenNewMessageEnqueued()
        {
            var listener = new QueueListenerStub();

            var queue = GivenUniqueQueueName();
            var message = GivenSampleMessage();
            await MessageQueueingService.CreateQueue(queue, listener);
            await MessageQueueingService.EnqueueMessage(queue, message, null);
            await listener.Completed;
            listener.Dispose();
            AssertMessageHandled(message, listener.Message);
        }
        public async Task OtherMessagesCanBeProcessedDuringRetryDelay()
        {
            var queue             = GivenUniqueQueueName();
            var failedMessage     = GivenSampleMessage();
            var subsequentMessage = GivenSampleMessage();

            var subsequentMessageHandled = new TaskCompletionSource <bool>();
            var listener = new QueueListenerStub((m, c) =>
            {
                if (m.Headers.MessageId == failedMessage.Headers.MessageId)
                {
                    throw new Exception("Test");
                }

                if (m.Headers.MessageId == subsequentMessage.Headers.MessageId)
                {
                    subsequentMessageHandled.TrySetResult(true);
                }
            });

            listener.CancellationToken.Register(() => subsequentMessageHandled.TrySetCanceled());

            var autoAcknowledgeOptions = new QueueOptions
            {
                AutoAcknowledge  = true,
                IsDurable        = false,
                ConcurrencyLimit = 1,
                RetryDelay       = TimeSpan.FromSeconds(30),
                MaxAttempts      = 10
            };

            await MessageQueueingService.CreateQueue(queue, listener, autoAcknowledgeOptions);

            await MessageQueueingService.EnqueueMessage(queue, failedMessage, Principal);

            await MessageQueueingService.EnqueueMessage(queue, subsequentMessage, Principal);

            await listener.Completed;
            var wasSubsequentMessageHandled = await subsequentMessageHandled.Task;

            listener.Dispose();

            await QueueOperationCompletion();

            Assert.True(wasSubsequentMessageHandled);
            await AssertMessageStillQueuedForRetry(queue, failedMessage);
            await AssertMessageNoLongerQueued(queue, subsequentMessage);
        }
        public async Task MessageCanBeAutomaticallyAcknowledged()
        {
            var queue = GivenUniqueQueueName();
            var message = GivenSampleMessage();
            var listener = new QueueListenerStub((m, c) => { });
            var autoAcknowledgeOptions = new QueueOptions
            {
                AutoAcknowledge = true,
                IsDurable = false
            };
            await MessageQueueingService.CreateQueue(queue, listener, autoAcknowledgeOptions);
            await MessageQueueingService.EnqueueMessage(queue, message, Principal);
            await listener.Completed;
            listener.Dispose();

            await QueueOperationCompletion();
        }
        public async Task MessageIsRemovedFromQueueWhenAcknowledged()
        {
            var queue    = GivenUniqueQueueName();
            var message  = GivenSampleMessage();
            var listener = new QueueListenerStub();
            await MessageQueueingService.CreateQueue(queue, listener);

            await MessageQueueingService.EnqueueMessage(queue, message, Principal);

            await listener.Completed;

            listener.Dispose();

            await QueueOperationCompletion();
            await AssertMessageNoLongerQueued(queue, message);

            VerificationSink.AssertExactly(1, DiagnosticEventType.MessageAcknowledged, message);
        }
        public async Task SecurityTokenShouldNotBeExposed()
        {
            GivenClaimsPrincipal();
            var queue   = GivenUniqueQueueName();
            var message = GivenSampleMessage();

            await GivenExistingQueuedMessage(queue, message, Principal);

            var listener = new QueueListenerStub();
            await MessageQueueingService.CreateQueue(queue, listener);

            await listener.Completed;

            listener.Dispose();

            await QueueOperationCompletion();

            Assert.Null(listener.Message.Headers.SecurityToken);
            Assert.Null(listener.Context.Headers.SecurityToken);
        }
        public async Task MessagesWithExpiredSecurityTokensAreHandledWithNullPrincipals()
        {
            const string expiredSecurityToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJ1bmlxdWVfbmFtZSI6InRlc3QiLCJleHAiOjE1MDkzODAzMjgsIm5iZiI6MTUwOTM3NjcyOCwicm9sZSI6WyJ1c2VyIl19.b7fnci1J9mhSzQphst71whua0SUuhJLcD4YLsq4zmVI";
            var          queue   = GivenUniqueQueueName();
            var          message = GivenSampleMessage(headers =>
            {
                headers.SecurityToken = expiredSecurityToken;
            });

            await GivenExistingQueuedMessage(queue, message, null);

            var listener = new QueueListenerStub();
            await MessageQueueingService.CreateQueue(queue, listener);

            await Task.Delay(TimeSpan.FromMilliseconds(100));

            await listener.Completed;

            listener.Dispose();
            AssertMessageHandled(message, listener.Message);
            Assert.Null(listener.Context.Principal);
        }
        public async Task MessageCanBeAutomaticallyAcknowledged()
        {
            var queue    = GivenUniqueQueueName();
            var message  = GivenSampleMessage();
            var listener = new QueueListenerStub((m, c) => { });
            var autoAcknowledgeOptions = new QueueOptions
            {
                AutoAcknowledge = true,
                IsDurable       = false
            };
            await MessageQueueingService.CreateQueue(queue, listener, autoAcknowledgeOptions);

            await MessageQueueingService.EnqueueMessage(queue, message, Principal);

            await listener.Completed;

            listener.Dispose();

            await QueueOperationCompletion();
            await AssertMessageNoLongerQueued(queue, message);

            VerificationSink.AssertExactly(1, DiagnosticEventType.MessageAcknowledged, message);
            VerificationSink.AssertNone(DiagnosticEventType.MessageNotAcknowledged, message);
        }