public
Description:
Homepage: http://www.iserviceoriented.com
Clone URL: git://github.com/jezell/iserviceoriented.git
100644 153 lines (105 sloc) 4.325 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using NUnit.Framework;
using IServiceOriented.ServiceBus.Threading;
using System.Threading;
 
namespace IServiceOriented.ServiceBus.UnitTests
{
[TestFixture]
    public class TestWorkerThreads
    {
[Test]
        public void Can_Start_And_Stop_Workers()
        {
            long count = 0;
 
            using(WorkerThreads threads = new WorkerThreads(TimeSpan.FromSeconds(5), (ts, obj) => { Interlocked.Increment(ref count); Thread.Sleep(100); }))
            {
                int index = threads.AddWorker();
 
                // pause a bit and make sure thread is working
                Thread.Sleep(1000);
 
                Assert.AreEqual(1, threads.Count);
 
                long curCount = Interlocked.Read(ref count);
 
                Assert.AreNotEqual(0, curCount); // has value been incremented
 
                threads.RemoveWorker(index);
 
                Assert.AreEqual(0, threads.Count);
 
                // pause a bit and make sure thread is actually dead
                long countAfterStop = Interlocked.Read(ref count);
 
                Thread.Sleep(1000);
 
                curCount = Interlocked.Read(ref count);
 
                Assert.AreEqual(curCount, countAfterStop);
            }
 
        }
 
[Test]
        public void Worker_Threads_Stop_On_Dispose()
        {
            long count = 0;
 
            WorkerThreads threads = new WorkerThreads(TimeSpan.FromSeconds(5), (ts, obj) => { Interlocked.Increment(ref count); Thread.Sleep(100); });
            try
            {
                int index = threads.AddWorker();
 
                // pause a bit and make sure thread is working
                Thread.Sleep(1000);
 
                Assert.AreEqual(1, threads.Count);
 
                long curCount = Interlocked.Read(ref count);
 
                Assert.Greater(count, 0);
                
                threads.Dispose();
 
                // pause a bit and make sure thread is actually dead
                long countAfterStop = Interlocked.Read(ref count);
 
                Thread.Sleep(1000);
 
                curCount = Interlocked.Read(ref count);
 
                Assert.AreEqual(curCount, countAfterStop);
            }
            finally
            {
                threads.Dispose();
            }
        }
 
[Test]
        public void RemoveWorker_Aborts_Thread_After_Timeout()
        {
            long count = 0;
 
            using (WorkerThreads threads = new WorkerThreads(TimeSpan.FromSeconds(5), (ts, obj) => { Interlocked.Increment(ref count); Thread.Sleep(1000 * 30); Assert.Fail(); }))
            {
                int index = threads.AddWorker();
 
                // pause a bit and make sure thread is working
                Thread.Sleep(1000);
 
                Assert.AreEqual(1, threads.Count);
 
                long curCount = Interlocked.Read(ref count);
 
                Assert.AreNotEqual(0, curCount); // has value been incremented
 
                threads.RemoveWorker(index);
 
                Assert.AreEqual(0, threads.Count);
 
                // pause a bit and make sure thread is actually dead
                long countAfterStop = Interlocked.Read(ref count);
 
                Thread.Sleep(1000);
 
                curCount = Interlocked.Read(ref count);
 
                Assert.AreEqual(curCount, countAfterStop);
            }
 
        }
 
 
[Test]
        public void RemoveAll_Removes_All_Threads()
        {
            using (WorkerThreads threads = new WorkerThreads(TimeSpan.FromSeconds(5), (ts, obj) => { }))
            {
                for (int i = 0; i < 10; i++)
                {
                    threads.AddWorker();
                }
 
                Assert.AreEqual(10, threads.Count);
 
                threads.RemoveAll();
 
                Assert.AreEqual(0, threads.Count);
            }
        }
 
[Test]
        public void State_Is_Passed_To_Workers()
        {
            string state = "this is some state";
 
            using (WorkerThreads threads = new WorkerThreads(TimeSpan.FromSeconds(5), (ts, obj) => { Assert.AreEqual(state, obj); }))
            {
                threads.AddWorker(state);
 
                Thread.Sleep(1000);
            }
        }
 
    }
}