public
Description:
Homepage: http://www.iserviceoriented.com
Clone URL: git://github.com/jezell/iserviceoriented.git
iserviceoriented / IServiceOriented.ServiceBus.UnitTests / TestHeartbeatRuntimeService.cs
100644 126 lines (104 sloc) 6.601 kb
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using NUnit.Framework;
using IServiceOriented.ServiceBus.Services;
using IServiceOriented.ServiceBus.Delivery;
using IServiceOriented.ServiceBus.Dispatchers;
using System.Threading;
 
namespace IServiceOriented.ServiceBus.UnitTests
{
[TestFixture]
    public class TestHeartbeatRuntimeService
    {
[Test]
        public void Heartbeat_Timeout_Causes_Failure_Request()
        {
            using (HeartbeatRuntimeService heartbeatService = new HeartbeatRuntimeService())
            {
                using (ServiceBusRuntime runtime = new ServiceBusRuntime(new DirectDeliveryCore(), new TimerRuntimeService(), heartbeatService))
                {
                    heartbeatService.RegisterHeartbeat(new Heartbeat(Guid.NewGuid(), TimeSpan.FromSeconds(5), new PublishRequest(null, null, "BeatRequest"),
                                                        new PublishRequest(null, null, "Success"), new PublishRequest(null, null, "Failure"),
                                                        new PredicateMessageFilter(pr => (string)pr.Message == "BeatResponse"), TimeSpan.FromSeconds(1)));
 
                    AutoResetEvent failEvt = new AutoResetEvent(false);
                    AutoResetEvent successEvt = new AutoResetEvent(false);
                    AutoResetEvent responseEvt = new AutoResetEvent(false);
                    
                    /*runtime.Subscribe(new SubscriptionEndpoint("BeatResponse", null, null, null, new ActionDispatcher((se, md) => {
responseEvt.Set(); ThreadPool.QueueUserWorkItem(s => { Thread.Sleep(2000); runtime.PublishOneWay(new PublishRequest(null, null, "BeatResponse")); });
}),
new PredicateMessageFilter(pr => (string)pr.Message == "BeatRequest")));*/
                    
                    runtime.Subscribe(new SubscriptionEndpoint("Success", null, null, null, new ActionDispatcher((se, md) => successEvt.Set()), new PredicateMessageFilter(pr => (string)pr.Message == "Success")));
                    runtime.Subscribe(new SubscriptionEndpoint("Failure", null, null, null, new ActionDispatcher((se, md) => failEvt.Set()), new PredicateMessageFilter(pr => (string)pr.Message == "Failure")));
                                            
                    ServiceBusTest tester = new ServiceBusTest(runtime);
 
                    tester.StartAndStop(() =>
                    {
                        if (!failEvt.WaitOne(TimeSpan.FromSeconds(10)))
                        {
                            if (!responseEvt.WaitOne(0))
                            {
                                Assert.Fail("The heartbeat request was never published.");
                            }
                            
                            if (successEvt.WaitOne(0))
                            {
                                Assert.Fail("The heartbeat success event was published instead of the failure event");
                            }
                            Assert.Fail("The heartbeat failure event was not published");
                        }
 
                        
                    });
 
                }
            }
            
        }
 
[Test]
        public void Heartbeat_Requires_Timer_Service()
        {
            try
            {
                using (ServiceBusRuntime runtime = new ServiceBusRuntime(new DirectDeliveryCore(), new HeartbeatRuntimeService()))
                {
                    runtime.Start();
                }
 
                Assert.Fail();
            }
            catch(ValidationException) // should not be able to start service bus without both timer runtime service
            {
                
            }
        }
 
[Test]
        public void Heartbeat_Response_Causes_Success_Request()
        {
            using (HeartbeatRuntimeService heartbeatService = new HeartbeatRuntimeService())
            {
                using (ServiceBusRuntime runtime = new ServiceBusRuntime(new DirectDeliveryCore(), new TimerRuntimeService(), heartbeatService))
                {
                    heartbeatService.RegisterHeartbeat(new Heartbeat(Guid.NewGuid(), TimeSpan.FromSeconds(5), new PublishRequest(null, null, "BeatRequest"),
                                                        new PublishRequest(null, null, "Success"), new PublishRequest(null, null, "Failure"),
                                                        new PredicateMessageFilter(pr => (string)pr.Message == "BeatResponse"), TimeSpan.FromSeconds(1)));
 
                    AutoResetEvent responseEvt = new AutoResetEvent(false);
                    AutoResetEvent failEvt = new AutoResetEvent(false);
                    AutoResetEvent successEvt = new AutoResetEvent(false);
 
                    runtime.Subscribe(new SubscriptionEndpoint("BeatResponse", null, null, null, new ActionDispatcher((se, md) => { responseEvt.Set(); runtime.PublishOneWay(new PublishRequest(null, null, "BeatResponse")); }), new PredicateMessageFilter(pr => (string)pr.Message == "BeatRequest")));
                    runtime.Subscribe(new SubscriptionEndpoint("Success", null, null, null, new ActionDispatcher((se, md) => successEvt.Set()), new PredicateMessageFilter(pr => (string)pr.Message == "Success")));
                    runtime.Subscribe(new SubscriptionEndpoint("Failure", null, null, null, new ActionDispatcher((se, md) => failEvt.Set()), new PredicateMessageFilter(pr => (string)pr.Message == "Failure")));
                                            
                    ServiceBusTest tester = new ServiceBusTest(runtime);
 
                    tester.StartAndStop(() =>
                    {
                        if (!successEvt.WaitOne(TimeSpan.FromSeconds(10)))
                        {
                            if (!responseEvt.WaitOne(0))
                            {
                                Assert.Fail("The heartbeat request was never published.");
                            }
                            if (failEvt.WaitOne(0))
                            {
                                Assert.Fail("The heartbeat fail event was published instead of the success event");
                            }
                            Assert.Fail("The heartbeat success event was not published");
                        }
                    });
 
                }
            }
        }
    }
}