async Task CompleteOnPeekedMessagesShouldThrowTest()
        {
            var connection = new ServiceBusNamespaceConnection(TestUtility.NamespaceConnectionString);
            var receiver   = connection.CreateMessageReceiver(TestConstants.NonPartitionedQueueName, ReceiveMode.ReceiveAndDelete);
            var sender     = connection.CreateMessageSender(TestConstants.NonPartitionedQueueName);

            try
            {
                await TestUtility.SendMessagesAsync(sender, 1);

                var message = await receiver.PeekAsync();

                Assert.NotNull(message);
                await
                Assert.ThrowsAsync <InvalidOperationException>(
                    async() => await receiver.CompleteAsync(message.SystemProperties.LockToken));

                message = await receiver.ReceiveAsync();

                Assert.NotNull((object)message);
            }
            finally
            {
                await sender.CloseAsync().ConfigureAwait(false);

                await receiver.CloseAsync().ConfigureAwait(false);
            }
        }
        async Task ReceiveShouldReturnNoLaterThanServerWaitTimeTest(string queueName, int messageCount = 1)
        {
            var connection = new ServiceBusNamespaceConnection(TestUtility.NamespaceConnectionString);
            var receiver   = connection.CreateMessageReceiver(queueName, ReceiveMode.ReceiveAndDelete);
            var sender     = connection.CreateMessageSender(queueName);

            try
            {
                await this.ReceiveShouldReturnNoLaterThanServerWaitTimeTestCase(sender, receiver, messageCount);
            }
            finally
            {
                await sender.CloseAsync().ConfigureAwait(false);

                await receiver.CloseAsync().ConfigureAwait(false);
            }
        }
        async Task MessageReceiverAndMessageSenderCreationWorksAsExpected(string queueName, int messageCount = 10)
        {
            var connection = new ServiceBusNamespaceConnection(TestUtility.NamespaceConnectionString);
            var receiver   = connection.CreateMessageReceiver(queueName, ReceiveMode.PeekLock);
            var sender     = connection.CreateMessageSender(queueName);

            try
            {
                await this.PeekLockTestCase(sender, receiver, messageCount);
            }
            finally
            {
                await sender.CloseAsync().ConfigureAwait(false);

                await receiver.CloseAsync().ConfigureAwait(false);
            }
        }
        async Task MessageLockLostExceptionTest()
        {
            const int messageCount = 2;
            var       connection   = new ServiceBusNamespaceConnection(TestUtility.NamespaceConnectionString);
            var       receiver     = connection.CreateMessageReceiver(TestConstants.NonPartitionedQueueName, ReceiveMode.PeekLock);
            var       sender       = connection.CreateMessageSender(TestConstants.NonPartitionedQueueName);

            try
            {
                await TestUtility.SendMessagesAsync(sender, messageCount);

                var receivedMessages = await TestUtility.ReceiveMessagesAsync(receiver, messageCount);

                Assert.True(receivedMessages.Count() == messageCount);

                // Let the messages expire
                await Task.Delay(TimeSpan.FromMinutes(1));

                // Complete should throw
                await
                Assert.ThrowsAsync <MessageLockLostException>(
                    async() => await TestUtility.CompleteMessagesAsync(receiver, receivedMessages));

                receivedMessages = await TestUtility.ReceiveMessagesAsync(receiver, messageCount);

                Assert.True(receivedMessages.Count() == messageCount);

                await TestUtility.CompleteMessagesAsync(receiver, receivedMessages);
            }
            finally
            {
                await sender.CloseAsync().ConfigureAwait(false);

                await receiver.CloseAsync().ConfigureAwait(false);
            }
        }