private async Task OnSessionTestAsync(bool partitioned, bool sessionEnabled, int maxConcurrentCalls, ReceiveMode mode, bool autoComplete) { await ServiceBusScope.UsingTopicAsync(partitioned, sessionEnabled, async (topicName, subscriptionName) => { TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var topicClient = new TopicClient(TestUtility.NamespaceConnectionString, topicName); var subscriptionClient = new SubscriptionClient( TestUtility.NamespaceConnectionString, topicClient.TopicName, subscriptionName, ReceiveMode.PeekLock); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = true }; var testSessionHandler = new TestSessionHandler( subscriptionClient.ReceiveMode, sessionHandlerOptions, topicClient.InnerSender, subscriptionClient.SessionPumpHost); // Send messages to Session await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandler(sessionHandlerOptions); // Verify messages were received. await testSessionHandler.VerifyRun(); testSessionHandler.ClearData(); } finally { await subscriptionClient.CloseAsync(); await topicClient.CloseAsync(); } }); // test UnregisterSessionHandler can wait for message handling upto the timeout user defined. await ServiceBusScope.UsingTopicAsync(partitioned, sessionEnabled, async (topicName, subscriptionName) => { TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var topicClient = new TopicClient(TestUtility.NamespaceConnectionString, topicName); var subscriptionClient = new SubscriptionClient( TestUtility.NamespaceConnectionString, topicClient.TopicName, subscriptionName, ReceiveMode.PeekLock); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = true }; var testSessionHandler = new TestSessionHandler( subscriptionClient.ReceiveMode, sessionHandlerOptions, topicClient.InnerSender, subscriptionClient.SessionPumpHost); // Send messages to Session await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(subscriptionClient.ReceiveMode == ReceiveMode.PeekLock, 8); // Session handler set up has greater latency than message handler. Delay here to enable some processing time of the session pump. await Task.Delay(TimeSpan.FromSeconds(2)); // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(10)); Assert.True(testSessionHandler.ReceivedMessageCount == maxConcurrentCalls); // Reregister won't have any problems testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(subscriptionClient.ReceiveMode == ReceiveMode.PeekLock, 0); await testSessionHandler.WaitForAllMessagesReceived(ExpectedMessageCount); Assert.True(testSessionHandler.ReceivedMessageCount == ExpectedMessageCount); testSessionHandler.ClearData(); } finally { await subscriptionClient.CloseAsync(); await topicClient.CloseAsync(); } }); // test UnregisterSessionHandler can close down in time when message handling takes longer than wait timeout user defined. await ServiceBusScope.UsingTopicAsync(partitioned, sessionEnabled, async (topicName, subscriptionName) => { TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var topicClient = new TopicClient(TestUtility.NamespaceConnectionString, topicName); var subscriptionClient = new SubscriptionClient( TestUtility.NamespaceConnectionString, topicClient.TopicName, subscriptionName, ReceiveMode.PeekLock); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = true }; var testSessionHandler = new TestSessionHandler( subscriptionClient.ReceiveMode, sessionHandlerOptions, topicClient.InnerSender, subscriptionClient.SessionPumpHost); // Send messages to Session await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(subscriptionClient.ReceiveMode == ReceiveMode.PeekLock, 8); // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(2)); Assert.True(testSessionHandler.ReceivedMessageCount == 0); // Reregister won't have any problems testSessionHandler.RegisterSessionHandlerAndRecordReceivedMessageCount(subscriptionClient.ReceiveMode == ReceiveMode.PeekLock, 0); await testSessionHandler.WaitForAllMessagesReceived(ExpectedMessageCount); Assert.True(testSessionHandler.ReceivedMessageCount == ExpectedMessageCount); testSessionHandler.ClearData(); } finally { await subscriptionClient.CloseAsync(); await topicClient.CloseAsync(); } }); }
private async Task OnSessionTestAsync(bool partitioned, bool sessionEnabled, int maxConcurrentCalls, ReceiveMode mode, bool autoComplete) { await ServiceBusScope.UsingTopicAsync(partitioned, sessionEnabled, async (topicName, subscriptionName) => { TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var topicClient = new TopicClient(TestUtility.NamespaceConnectionString, topicName); var subscriptionClient = new SubscriptionClient( TestUtility.NamespaceConnectionString, topicClient.TopicName, subscriptionName, ReceiveMode.PeekLock); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = true }; var testSessionHandler = new TestSessionHandler( subscriptionClient.ReceiveMode, sessionHandlerOptions, topicClient.InnerSender, subscriptionClient.SessionPumpHost); // Send messages to Session await testSessionHandler.SendSessionMessages(); // Register handler testSessionHandler.RegisterSessionHandler(sessionHandlerOptions); // Verify messages were received. await testSessionHandler.VerifyRun(); testSessionHandler.ClearData(); } finally { await subscriptionClient.CloseAsync(); await topicClient.CloseAsync(); } }); // test UnregisterSessionHandler can wait for message handling upto the timeout user defined. await ServiceBusScope.UsingTopicAsync(partitioned, sessionEnabled, async (topicName, subscriptionName) => { TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var topicClient = new TopicClient(TestUtility.NamespaceConnectionString, topicName); var subscriptionClient = new SubscriptionClient( TestUtility.NamespaceConnectionString, topicClient.TopicName, subscriptionName, ReceiveMode.PeekLock); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = true }; var testSessionHandler = new TestSessionHandler( subscriptionClient.ReceiveMode, sessionHandlerOptions, topicClient.InnerSender, subscriptionClient.SessionPumpHost); // Send messages to Session await testSessionHandler.SendSessionMessages(); // Register handler var count = 0; testSessionHandler.RegisterSessionHandler( async(session, message, token) => { await Task.Delay(TimeSpan.FromSeconds(8)); TestUtility.Log($"Received Session: {session.SessionId} message: SequenceNumber: {message.SystemProperties.SequenceNumber}"); if (subscriptionClient.ReceiveMode == ReceiveMode.PeekLock && !sessionHandlerOptions.AutoComplete) { await session.CompleteAsync(message.SystemProperties.LockToken); } Interlocked.Increment(ref count); }, sessionHandlerOptions); await Task.Delay(TimeSpan.FromSeconds(2)); // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(10)); Assert.True(count == maxConcurrentCalls); testSessionHandler.ClearData(); } finally { await subscriptionClient.CloseAsync(); await topicClient.CloseAsync(); } }); // test UnregisterSessionHandler can close down in time when message handling takes longer than wait timeout user defined. await ServiceBusScope.UsingTopicAsync(partitioned, sessionEnabled, async (topicName, subscriptionName) => { TestUtility.Log($"Topic: {topicName}, MaxConcurrentCalls: {maxConcurrentCalls}, Receive Mode: {mode.ToString()}, AutoComplete: {autoComplete}"); var topicClient = new TopicClient(TestUtility.NamespaceConnectionString, topicName); var subscriptionClient = new SubscriptionClient( TestUtility.NamespaceConnectionString, topicClient.TopicName, subscriptionName, ReceiveMode.PeekLock); try { var sessionHandlerOptions = new SessionHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentSessions = maxConcurrentCalls, MessageWaitTimeout = TimeSpan.FromSeconds(5), AutoComplete = true }; var testSessionHandler = new TestSessionHandler( subscriptionClient.ReceiveMode, sessionHandlerOptions, topicClient.InnerSender, subscriptionClient.SessionPumpHost); // Send messages to Session await testSessionHandler.SendSessionMessages(); // Register handler var count = 0; testSessionHandler.RegisterSessionHandler( async(session, message, token) => { await Task.Delay(TimeSpan.FromSeconds(8)); TestUtility.Log($"Received Session: {session.SessionId} message: SequenceNumber: {message.SystemProperties.SequenceNumber}"); if (subscriptionClient.ReceiveMode == ReceiveMode.PeekLock && !sessionHandlerOptions.AutoComplete) { await session.CompleteAsync(message.SystemProperties.LockToken); } Interlocked.Increment(ref count); }, sessionHandlerOptions); await Task.Delay(TimeSpan.FromSeconds(2)); // UnregisterSessionHandler should wait up to the provided timeout to finish the message handling tasks await testSessionHandler.UnregisterSessionHandler(TimeSpan.FromSeconds(2)); Assert.True(count == 0); testSessionHandler.ClearData(); } finally { await subscriptionClient.CloseAsync(); await topicClient.CloseAsync(); } }); }