public async Task ReceiverThrowsAfterSessionLockLost(bool isSessionSpecified) { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true, lockDuration: TimeSpan.FromSeconds(5))) { await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString); ServiceBusSender sender = client.CreateSender(scope.QueueName); var sessionId1 = "sessionId1"; await sender.SendAsync(GetMessage(sessionId1)); // send another session message before the one we are interested in to make sure that when isSessionSpecified=true, it is being respected await sender.SendAsync(GetMessage("sessionId2")); ServiceBusSessionReceiver receiver = await client.CreateSessionReceiverAsync(scope.QueueName, sessionId : isSessionSpecified?sessionId1 : null); if (isSessionSpecified) { Assert.AreEqual(sessionId1, receiver.SessionId); } var message = await receiver.ReceiveAsync(); await Task.Delay((receiver.SessionLockedUntil - DateTime.UtcNow) + TimeSpan.FromSeconds(5)); Assert.That(async() => await receiver.ReceiveAsync(), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.SetSessionStateAsync(null), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.GetSessionStateAsync(), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.CompleteAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.CompleteAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.DeadLetterAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.DeferAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); Assert.That(async() => await receiver.AbandonAsync(message), Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)) .EqualTo(ServiceBusException.FailureReason.SessionLockLost)); } }
public async Task RenewSessionLock(bool isSessionSpecified) { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true)) { await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString); ServiceBusSender sender = client.CreateSender(scope.QueueName); var messageCount = 1; var sessionId1 = "sessionId1"; ServiceBusMessage message = GetMessage(sessionId1); // send another session message before the one we are interested in to make sure that when isSessionSpecified=true, it is being respected await sender.SendAsync(GetMessage("sessionId2")); await sender.SendAsync(message); ServiceBusSessionReceiver receiver = await client.CreateSessionReceiverAsync( scope.QueueName, new ServiceBusSessionReceiverOptions { SessionId = isSessionSpecified ? sessionId1 : null }); if (isSessionSpecified) { Assert.AreEqual(sessionId1, receiver.SessionId); } ServiceBusReceivedMessage[] receivedMessages = (await receiver.ReceiveBatchAsync(messageCount)).ToArray(); var receivedMessage = receivedMessages.First(); var firstLockedUntilUtcTime = receiver.SessionLockedUntil; // Sleeping for 10 seconds... await Task.Delay(10000); await receiver.RenewSessionLockAsync(); Assert.True(receiver.SessionLockedUntil >= firstLockedUntilUtcTime + TimeSpan.FromSeconds(10)); // Complete Messages await receiver.CompleteAsync(receivedMessage.LockToken); Assert.AreEqual(messageCount, receivedMessages.Length); if (isSessionSpecified) { Assert.AreEqual(message.MessageId, receivedMessage.MessageId); } var peekedMessage = receiver.PeekAsync(); Assert.IsNull(peekedMessage.Result); } }
public async Task GetAndSetSessionStateTest(bool isSessionSpecified) { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true)) { await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString); ServiceBusSender sender = client.CreateSender(scope.QueueName); var sessionId = "test-sessionId"; ServiceBusMessage message = GetMessage(sessionId); await sender.SendAsync(message); ServiceBusSessionReceiver receiver = await client.CreateSessionReceiverAsync( scope.QueueName, new ServiceBusSessionReceiverOptions { SessionId = isSessionSpecified ? sessionId : null }); ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveAsync(); Assert.AreEqual(message.MessageId, receivedMessage.MessageId); Assert.AreEqual(message.SessionId, receivedMessage.SessionId); var sessionStateString = "Received Message From Session!"; var sessionState = Encoding.UTF8.GetBytes(sessionStateString); await receiver.SetSessionStateAsync(sessionState); var returnedSessionState = await receiver.GetSessionStateAsync(); var returnedSessionStateString = Encoding.UTF8.GetString(returnedSessionState); Assert.AreEqual(sessionStateString, returnedSessionStateString); // Complete message using Session Receiver await receiver.CompleteAsync(receivedMessage); var peekedMessage = receiver.PeekAsync(); Assert.IsNull(peekedMessage.Result); sessionStateString = "Completed Message On Session!"; sessionState = Encoding.UTF8.GetBytes(sessionStateString); await receiver.SetSessionStateAsync(sessionState); returnedSessionState = await receiver.GetSessionStateAsync(); returnedSessionStateString = Encoding.UTF8.GetString(returnedSessionState); Assert.AreEqual(sessionStateString, returnedSessionStateString); } }