Skip to content

Commit 2952332

Browse files
author
Travis Nickels
committed
Add tests for MessageLostLockException
1 parent 17d98b7 commit 2952332

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
namespace NServiceBus.Transport.AzureServiceBus.TransportTests
2+
{
3+
using System.Threading.Tasks;
4+
using Azure.Messaging.ServiceBus;
5+
using NServiceBus.TransportTests;
6+
using NUnit.Framework;
7+
8+
[TestFixture]
9+
public class When_MessageLockLostException_is_thrown_from_process_message : NServiceBusTransportTest
10+
{
11+
[TestCase(TransportTransactionMode.ReceiveOnly)]
12+
public async Task Should_not_throw_exception_from_on_error(TransportTransactionMode transactionMode)
13+
{
14+
bool onErrorCalled = false;
15+
var onMessageCalled = CreateTaskCompletionSource<bool>();
16+
string criticalErrorDetails = null;
17+
18+
19+
await StartPump(
20+
(_, __) =>
21+
{
22+
onMessageCalled.TrySetResult(true);
23+
throw new ServiceBusException("from onMessage", ServiceBusFailureReason.MessageLockLost);
24+
},
25+
(_, __) =>
26+
{
27+
onErrorCalled = true;
28+
throw new ServiceBusException("from onError", ServiceBusFailureReason.MessageLockLost);
29+
},
30+
transactionMode
31+
);
32+
33+
await SendMessage(InputQueueName);
34+
35+
var onMessageResult = await onMessageCalled.Task.ConfigureAwait(false);
36+
37+
await StopPump();
38+
39+
Assert.IsTrue(onMessageResult, "The message handler should have been called.");
40+
Assert.IsFalse(onErrorCalled, "onError should not have been called when a MessageLostLock exception is thrown from onMessage when the transport is in receiveOnly mode.");
41+
Assert.IsNull(criticalErrorDetails, $"Should not invoke critical error for {nameof(ServiceBusException)}");
42+
}
43+
44+
[TestCase(TransportTransactionMode.None)]
45+
[TestCase(TransportTransactionMode.SendsAtomicWithReceive)]
46+
public async Task Should_throw_exception_from_on_error(TransportTransactionMode transactionMode)
47+
{
48+
bool onErrorCalled = false;
49+
var onMessageCalled = CreateTaskCompletionSource<bool>();
50+
string criticalErrorDetails = null;
51+
52+
53+
await StartPump(
54+
(_, __) =>
55+
{
56+
onMessageCalled.TrySetResult(true);
57+
throw new ServiceBusException("from onMessage", ServiceBusFailureReason.MessageLockLost);
58+
},
59+
(_, __) =>
60+
{
61+
onErrorCalled = true;
62+
throw new ServiceBusException("from onError", ServiceBusFailureReason.MessageLockLost);
63+
},
64+
transactionMode
65+
);
66+
67+
await SendMessage(InputQueueName);
68+
69+
var onMessageResult = await onMessageCalled.Task.ConfigureAwait(false);
70+
71+
await StopPump();
72+
73+
Assert.IsTrue(onMessageResult, "The message handler should have been called.");
74+
Assert.IsTrue(onErrorCalled, "onError should have been called when a MessageLostLock exception is thrown from onMessage when the transport is not receiveOnly mode.");
75+
Assert.IsNull(criticalErrorDetails, $"Should not invoke critical error for {nameof(ServiceBusException)}");
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)