-
-
Notifications
You must be signed in to change notification settings - Fork 358
/
TestWorker_ErrorHandling.cs
73 lines (61 loc) · 2.74 KB
/
TestWorker_ErrorHandling.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Threading;
using NUnit.Framework;
using Rebus.Bus;
using Rebus.Messages;
using Rebus.Newtonsoft.JsonNET;
using Rebus.Persistence.InMemory;
using Shouldly;
namespace Rebus.Tests.Unit
{
[TestFixture]
public class TestWorker_ErrorHandling : FixtureBase
{
MessageReceiverForTesting receiveMessages;
Worker worker;
HandlerActivatorForTesting handlerActivatorForTesting;
protected override void DoSetUp()
{
receiveMessages = new MessageReceiverForTesting(new JsonMessageSerializer());
handlerActivatorForTesting = new HandlerActivatorForTesting();
worker = new Worker(new ErrorTracker(),
receiveMessages,
handlerActivatorForTesting,
new InMemorySubscriptionStorage(),
new JsonMessageSerializer(),
new InMemorySagaPersister(),
new TrivialPipelineInspector(), "Just some test worker");
}
[Test]
public void CanRaiseEventWithProperExceptionInformation()
{
// arrange
var exceptions = new List<Exception>();
var manualResetEvent = new ManualResetEvent(false);
worker.UserException += (w, exception) => exceptions.Add(exception);
worker.MessageFailedMaxNumberOfTimes += (message, text) => manualResetEvent.Set();
handlerActivatorForTesting.UseHandler(new MyMessageHandler());
worker.Start();
// act
receiveMessages.Deliver(new Message {Messages = new object[] {"woot!"}});
if (!manualResetEvent.WaitOne(5.Seconds())) Assert.Fail("Message was not delivered within timeout!");
// assert
exceptions.Count.ShouldBe(5);
var firstException = exceptions[0];
var exceptionToString = firstException.ToString();
exceptionToString.ShouldContain("InvalidOperationException");
exceptionToString.ShouldContain("This is the error message I want to see!");
exceptionToString.ShouldContain(typeof(MyMessageHandler).Name);
exceptionToString.ShouldContain(typeof(MyMessageHandler).Namespace);
exceptionToString.ShouldNotContain("TargetInvocationException");
}
class MyMessageHandler : IHandleMessages<string>
{
public void Handle(string message)
{
throw new InvalidOperationException("This is the error message I want to see!");
}
}
}
}