public
Description:
Homepage: http://www.iserviceoriented.com
Clone URL: git://github.com/jezell/iserviceoriented.git
100644 161 lines (108 sloc) 4.806 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using IServiceOriented.ServiceBus.Delivery;
using IServiceOriented.ServiceBus.Services;
using System.Threading;
 
namespace IServiceOriented.ServiceBus.UnitTests
{
[TestFixture]
    public class TestTimerRuntimeService
    {
[Test]
        public void Action_Ticks_On_Intervals()
        {
            long count = 0;
 
            TimerRuntimeService timerService = new TimerRuntimeService();
            timerService.AddEvent(new TimerEvent(() => { Interlocked.Increment(ref count); }, TimeSpan.FromMilliseconds(1000)));
            
            DeliveryCore deliveryCore = new DirectDeliveryCore();
 
            using (ServiceBusRuntime runtime = new ServiceBusRuntime(deliveryCore, timerService))
            {
                runtime.Start();
 
                Thread.Sleep(9100);
 
                long curCount = Interlocked.Read(ref count);
                
                runtime.Stop();
 
                Assert.AreEqual(9, curCount);
            }
        }
 
[Test]
        public void Action_Starts_On_Start()
        {
            long count = 0;
 
            TimerRuntimeService timerService = new TimerRuntimeService();
            timerService.AddEvent(new TimerEvent(() => { Interlocked.Increment(ref count); }, TimeSpan.FromMilliseconds(100)));
 
            DeliveryCore deliveryCore = new DirectDeliveryCore();
 
            using (ServiceBusRuntime runtime = new ServiceBusRuntime(deliveryCore, timerService))
            {
                Thread.Sleep(500);
 
                long curCount = Interlocked.Read(ref count);
                Assert.AreEqual(0, curCount);
                
                runtime.Start();
 
                Thread.Sleep(500);
 
                curCount = Interlocked.Read(ref count);
 
                runtime.Stop();
 
                Assert.GreaterOrEqual(curCount,0);
            }
        }
 
[Test]
        public void Action_Stops_On_Stop()
        {
            long count = 0;
 
            TimerRuntimeService timerService = new TimerRuntimeService();
            timerService.AddEvent(new TimerEvent(() => { Interlocked.Increment(ref count); }, TimeSpan.FromMilliseconds(1000)));
 
            DeliveryCore deliveryCore = new DirectDeliveryCore();
 
            using (ServiceBusRuntime runtime = new ServiceBusRuntime(deliveryCore, timerService))
            {
                runtime.Start();
 
                Thread.Sleep(1100);
 
                long beforeStopCount = Interlocked.Read(ref count);
 
                runtime.Stop();
 
                Assert.GreaterOrEqual(beforeStopCount, 0);
 
                Thread.Sleep(1100);
 
                long afterStopCount = Interlocked.Read(ref count);
 
                Assert.GreaterOrEqual(beforeStopCount, afterStopCount);
            }
        }
 
[Test]
        public void Event_Auto_Starts_When_Running()
        {
            long count = 0;
 
            TimerRuntimeService timerService = new TimerRuntimeService();
            var evt = new TimerEvent(() => { Interlocked.Increment(ref count); }, TimeSpan.FromMilliseconds(100));
            
            DeliveryCore deliveryCore = new DirectDeliveryCore();
 
            using (ServiceBusRuntime runtime = new ServiceBusRuntime(deliveryCore, timerService))
            {
                runtime.Start();
 
                Thread.Sleep(500);
 
                long curCount = Interlocked.Read(ref count);
                Assert.AreEqual(0, curCount);
 
                timerService.AddEvent(evt);
 
                Thread.Sleep(500);
 
                curCount = Interlocked.Read(ref count);
 
                runtime.Stop();
 
                Assert.GreaterOrEqual(curCount, 3);
            }
        }
 
[Test]
        public void Event_Starts_At_Preset_Time()
        {
            long count = 0;
 
            TimerRuntimeService timerService = new TimerRuntimeService();
            timerService.AddEvent(new TimerEvent(() => { Interlocked.Increment(ref count); }, TimeSpan.FromMilliseconds(100), DateTime.Now.AddSeconds(5)));
 
            DeliveryCore deliveryCore = new DirectDeliveryCore();
 
            using (ServiceBusRuntime runtime = new ServiceBusRuntime(deliveryCore, timerService))
            {
                runtime.Start();
 
                Thread.Sleep(3000);
 
                long curCount = Interlocked.Read(ref count);
                Assert.AreEqual(0, curCount);
 
 
                Thread.Sleep(3000);
 
                curCount = Interlocked.Read(ref count);
 
                runtime.Stop();
 
                Assert.GreaterOrEqual(curCount, 0);
            }
        }
        
    }
}