public async Task Peek() { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var messageCt = 10; IEnumerable <ServiceBusMessage> sentMessages = GetMessages(messageCt); await sender.SendBatchAsync(sentMessages); await using var receiver = new ServiceBusReceiverClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); Dictionary <string, string> sentMessageIdToLabel = new Dictionary <string, string>(); foreach (ServiceBusMessage message in sentMessages) { sentMessageIdToLabel.Add(message.MessageId, Encoding.Default.GetString(message.Body)); } var ct = 0; foreach (ServiceBusMessage peekedMessage in await receiver.PeekBatchAsync( maxMessages: messageCt)) { var peekedText = Encoding.Default.GetString(peekedMessage.Body); ct++; } Assert.AreEqual(messageCt, ct); } }
public async Task CompleteMessages() { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var messageCount = 10; IEnumerable <ServiceBusMessage> messages = GetMessages(messageCount); await sender.SendBatchAsync(messages); var receiver = new ServiceBusReceiverClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var receivedMessageCount = 0; var messageEnum = messages.GetEnumerator(); foreach (var item in await receiver.ReceiveBatchAsync(messageCount)) { receivedMessageCount++; messageEnum.MoveNext(); Assert.AreEqual(item.MessageId, messageEnum.Current.MessageId); await receiver.CompleteAsync(item.SystemProperties.LockToken); } Assert.AreEqual(receivedMessageCount, messageCount); var message = receiver.PeekAsync(); Assert.IsNull(message.Result); } }
public async Task ReceiveMessagesInReceiveAndDeleteMode() { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var messageCount = 10; IEnumerable <ServiceBusMessage> messages = GetMessages(messageCount); await sender.SendBatchAsync(messages); var clientOptions = new ServiceBusReceiverClientOptions() { ReceiveMode = ReceiveMode.ReceiveAndDelete, }; var receiver = new ServiceBusReceiverClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName, clientOptions); var receivedMessageCount = 0; var messageEnum = messages.GetEnumerator(); foreach (var item in await receiver.ReceiveBatchAsync(messageCount).ConfigureAwait(false)) { messageEnum.MoveNext(); Assert.AreEqual(item.MessageId, messageEnum.Current.MessageId); receivedMessageCount++; } Assert.AreEqual(receivedMessageCount, messageCount); var message = receiver.PeekAsync(); Assert.IsNull(message.Result); } }
public async Task DeferMessages(bool isSessionSpecified) { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var messageCount = 10; var sessionId = "sessionId1"; IEnumerable <ServiceBusMessage> messages = GetMessages(messageCount, sessionId); await sender.SendBatchAsync(messages); var clientOptions = new ServiceBusReceiverClientOptions() { SessionId = isSessionSpecified ? sessionId : null, IsSessionEntity = true, }; var receiver = new ServiceBusReceiverClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName, clientOptions); var receivedMessageCount = 0; var messageEnum = messages.GetEnumerator(); foreach (var item in await receiver.ReceiveBatchAsync(messageCount)) { receivedMessageCount++; messageEnum.MoveNext(); Assert.AreEqual(item.MessageId, messageEnum.Current.MessageId); Assert.AreEqual(item.SessionId, messageEnum.Current.SessionId); await receiver.DeferAsync(item.SystemProperties.LockToken); } Assert.AreEqual(receivedMessageCount, messageCount); // TODO: Call ReceiveDeferredMessageAsync() to verify the messages } }
public async Task Peek_IncrementsSequenceNmber(int messageCt) { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var sessionId = Guid.NewGuid().ToString(); // send the messages IEnumerable <ServiceBusMessage> sentMessages = GetMessages(messageCt, sessionId); await sender.SendBatchAsync(sentMessages); var options = new ServiceBusReceiverClientOptions() { SessionId = sessionId, IsSessionEntity = true }; var receiver = new ServiceBusReceiverClient( TestEnvironment.ServiceBusConnectionString, scope.QueueName, options); long seq = 0; for (int i = 0; i < messageCt; i++) { ServiceBusMessage msg = await receiver.PeekAsync(); Assert.IsTrue(msg.SystemProperties.SequenceNumber > seq); if (seq > 0) { Assert.IsTrue(msg.SystemProperties.SequenceNumber == seq + 1); } seq = msg.SystemProperties.SequenceNumber; } } }
public async Task Send_ConnString() { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); await sender.SendBatchAsync(GetMessages(10)); } }
public async Task Receive_StopProcessing(int numThreads) { await using (var scope = await ServiceBusScope.CreateWithQueue( enablePartitioning: false, enableSession: false)) { await using var sender = new ServiceBusSenderClient( TestEnvironment.ServiceBusConnectionString, scope.QueueName); int numMessages = 50; await sender.SendBatchAsync(GetMessages(numMessages)); await using var processor = new ServiceBusProcessorClient( TestEnvironment.ServiceBusConnectionString, scope.QueueName); int messageProcessedCt = 0; // stop processing halfway through int stopAfterMessagesCt = numMessages / 2; var options = new ProcessingOptions() { MaxConcurrentCalls = numThreads }; TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); processor.ProcessMessageAsync += ProcessMessage; processor.ProcessErrorAsync += ExceptionHandler; await processor.StartProcessingAsync(options); async Task ProcessMessage(ServiceBusMessage message, ServiceBusSession session) { Interlocked.Increment(ref messageProcessedCt); if (messageProcessedCt == stopAfterMessagesCt) { await processor.StopProcessingAsync(); tcs.TrySetResult(true); } } await tcs.Task; var remainingCt = 0; var receiver = new ServiceBusReceiverClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); foreach (ServiceBusMessage message in await receiver.ReceiveBatchAsync(numMessages)) { remainingCt++; } // can't assert on the exact amount processed due to threads that // are already in flight when calling StopProcessingAsync, but we can at least verify that there are remaining messages Assert.IsTrue(remainingCt > 0); Assert.IsTrue(messageProcessedCt < numMessages); } }
public async Task DeadLetterMessages(bool isSessionSpecified) { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var messageCount = 10; var sessionId = "sessionId1"; IEnumerable <ServiceBusMessage> messages = GetMessages(messageCount, sessionId); await sender.SendBatchAsync(messages); var clientOptions = new ServiceBusReceiverClientOptions() { SessionId = isSessionSpecified ? sessionId : null, IsSessionEntity = true, }; var receiver = new ServiceBusReceiverClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName, clientOptions); var receivedMessageCount = 0; var messageEnum = messages.GetEnumerator(); foreach (var item in await receiver.ReceiveBatchAsync(messageCount)) { receivedMessageCount++; messageEnum.MoveNext(); Assert.AreEqual(item.MessageId, messageEnum.Current.MessageId); Assert.AreEqual(item.SessionId, messageEnum.Current.SessionId); await receiver.DeadLetterAsync(item.SystemProperties.LockToken); } Assert.AreEqual(receivedMessageCount, messageCount); var message = receiver.PeekAsync(); Assert.IsNull(message.Result); // TODO: System.InvalidOperationException : Cannot create a MessageSession for a sub-queue. // messageEnum.Reset(); // receivedMessageCount = 0; // string deadLetterQueuePath = EntityNameFormatter.FormatDeadLetterPath(scope.QueueName); // var deadLetterReceiver = new ServiceBusReceiverClient(TestEnvironment.ServiceBusConnectionString, deadLetterQueuePath, sessionOptions); // foreach (var item in await deadLetterReceiver.ReceiveBatchAsync(messageCount)) // { // receivedMessageCount++; // messageEnum.MoveNext(); // Assert.AreEqual(item.MessageId, messageEnum.Current.MessageId); // Assert.AreEqual(item.SessionId, messageEnum.Current.SessionId); // await deadLetterReceiver.CompleteAsync(item.SystemProperties.LockToken); // } // Assert.AreEqual(receivedMessageCount, messageCount); // var deadLetterMessage = deadLetterReceiver.PeekAsync(); // Assert.IsNull(deadLetterMessage.Result); } }
public async Task Peek_Session(long?sequenceNumber, string partitionKey) { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var messageCt = 10; var sessionId = Guid.NewGuid().ToString(); // send the messages IEnumerable <ServiceBusMessage> sentMessages = GetMessages(messageCt, sessionId, partitionKey); await sender.SendBatchAsync(sentMessages); Dictionary <string, ServiceBusMessage> sentMessageIdToMsg = new Dictionary <string, ServiceBusMessage>(); foreach (ServiceBusMessage message in sentMessages) { sentMessageIdToMsg.Add(message.MessageId, message); } var options = new ServiceBusReceiverClientOptions() { SessionId = sessionId, IsSessionEntity = true }; var receiver = new ServiceBusReceiverClient( TestEnvironment.ServiceBusConnectionString, scope.QueueName, options); sequenceNumber ??= 1; // verify peeked == send var ct = 0; foreach (ServiceBusMessage peekedMessage in await receiver.PeekBatchBySequenceAsync( fromSequenceNumber: (long)sequenceNumber, maxMessages: messageCt)) { var peekedText = Encoding.Default.GetString(peekedMessage.Body); var sentMsg = sentMessageIdToMsg[peekedMessage.MessageId]; sentMessageIdToMsg.Remove(peekedMessage.MessageId); Assert.AreEqual(Encoding.Default.GetString(sentMsg.Body), peekedText); Assert.AreEqual(sentMsg.PartitionKey, peekedMessage.PartitionKey); Assert.IsTrue(peekedMessage.SystemProperties.SequenceNumber >= sequenceNumber); TestContext.Progress.WriteLine($"{peekedMessage.Label}: {peekedText}"); ct++; } if (sequenceNumber == 1) { Assert.AreEqual(messageCt, ct); } } }
public async Task Receive_Event(int numThreads) { await using (var scope = await ServiceBusScope.CreateWithQueue( enablePartitioning: false, enableSession: false)) { await using var sender = new ServiceBusSenderClient( TestEnvironment.ServiceBusConnectionString, scope.QueueName); // use double the number of threads so we can make sure we test that we don't // retrieve more messages than expected when there are more messages available await sender.SendBatchAsync(GetMessages(numThreads * 2)); await using var receiver = new ServiceBusReceiverClient( TestEnvironment.ServiceBusConnectionString, scope.QueueName); int messageCt = 0; var options = new MessageHandlerOptions() { MaxConcurrentCalls = numThreads }; TaskCompletionSource <bool>[] completionSources = Enumerable .Range(0, numThreads) .Select(index => new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously)) .ToArray(); var completionSourceIndex = -1; receiver.ProcessMessageAsync += ProcessMessage; receiver.ProcessErrorAsync += ExceptionHandler; await receiver.StartReceivingAsync(options); async Task ProcessMessage(ServiceBusMessage message) { await receiver.CompleteAsync(message.SystemProperties.LockToken); Interlocked.Increment(ref messageCt); var setIndex = Interlocked.Increment(ref completionSourceIndex); completionSources[setIndex].TrySetResult(true); } await Task.WhenAll(completionSources.Select(source => source.Task)); // we complete each thread after one message being processed, so the total number of messages // processed should equal the number of threads Assert.AreEqual(numThreads, messageCt); } }
public async Task RoundRobinSessions() { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var messageCt = 10; HashSet <string> sessions = new HashSet <string>() { "1", "2", "3" }; // send the messages foreach (string session in sessions) { await sender.SendBatchAsync(GetMessages(messageCt, session)); } // create receiver not scoped to a specific session for (int i = 0; i < 10; i++) { var options = new ServiceBusReceiverClientOptions() { IsSessionEntity = true }; var receiver = new ServiceBusReceiverClient( TestEnvironment.ServiceBusConnectionString, scope.QueueName, options); foreach (ServiceBusMessage peekedMessage in await receiver.PeekBatchBySequenceAsync( fromSequenceNumber: 1, maxMessages: 10)) { var sessionId = await receiver.Session.GetSessionIdAsync(); Assert.AreEqual(sessionId, peekedMessage.SessionId); } // Close the receiver client when we are done with it. Since the sessionClient doesn't own the underlying connection, the connection remains open, but the session link will be closed. await receiver.CloseAsync(); } } }
public async Task PeekMultipleSessions_ShouldThrow() { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var messageCt = 10; var sessionId = Guid.NewGuid().ToString(); // send the messages IEnumerable <ServiceBusMessage> sentMessages = GetMessages(messageCt, sessionId); await sender.SendBatchAsync(sentMessages); var options = new ServiceBusReceiverClientOptions() { SessionId = sessionId, IsSessionEntity = true }; var receiver1 = new ServiceBusReceiverClient( TestEnvironment.ServiceBusConnectionString, scope.QueueName, options); var receiver2 = new ServiceBusReceiverClient( TestEnvironment.ServiceBusConnectionString, scope.QueueName, options); Dictionary <string, ServiceBusMessage> sentMessageIdToMsg = new Dictionary <string, ServiceBusMessage>(); // peek the messages Assert.That(() => ScheduleTasksAsync(), Throws.Exception); async Task ScheduleTasksAsync() { Task peek1 = receiver1.PeekBatchBySequenceAsync( fromSequenceNumber: 1, maxMessages: messageCt); Task peek2 = receiver2.PeekBatchBySequenceAsync( fromSequenceNumber: 1, maxMessages: messageCt); await Task.WhenAll(peek1, peek2); } } }
public async Task ReceiveMessagesInPeekLockMode() { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true)) { await using var sender = new ServiceBusSenderClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var messageCount = 10; var sessionId = "sessionId1"; IEnumerable <ServiceBusMessage> messages = GetMessages(messageCount, sessionId); await sender.SendBatchAsync(messages); ServiceBusConnection conn = new ServiceBusConnection(TestEnvironment.ServiceBusConnectionString, scope.QueueName); var options = new ServiceBusReceiverClientOptions() { SessionId = sessionId, IsSessionEntity = true }; var receiver = new ServiceBusReceiverClient(TestEnvironment.ServiceBusConnectionString, scope.QueueName, options); var receivedMessageCount = 0; var messageEnum = messages.GetEnumerator(); foreach (var item in await receiver.ReceiveBatchAsync(messageCount)) { receivedMessageCount++; messageEnum.MoveNext(); Assert.AreEqual(item.MessageId, messageEnum.Current.MessageId); Assert.AreEqual(item.SessionId, messageEnum.Current.SessionId); Assert.AreEqual(item.SystemProperties.DeliveryCount, 1); } Assert.AreEqual(receivedMessageCount, messageCount); messageEnum.Reset(); foreach (var item in await receiver.PeekBatchAsync(messageCount)) { messageEnum.MoveNext(); Assert.AreEqual(item.SessionId, messageEnum.Current.SessionId); Assert.AreEqual(item.MessageId, messageEnum.Current.MessageId); } } }