Mercurial > hg > orthanc
annotate UnitTestsSources/MultiThreading.cpp @ 813:b640aeee11ba
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 08 May 2014 15:15:55 +0200 |
parents | be87dd517416 |
children | 9ae0bb3f188b 3d6f9b7d0add |
rev | line source |
---|---|
723 | 1 #include "gtest/gtest.h" |
2 | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
3 #include <glog/logging.h> |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
4 |
723 | 5 #include "../Core/OrthancException.h" |
6 #include "../Core/Toolbox.h" | |
7 #include "../Core/MultiThreading/ArrayFilledByThreads.h" | |
760 | 8 #include "../Core/MultiThreading/Locker.h" |
9 #include "../Core/MultiThreading/Mutex.h" | |
10 #include "../Core/MultiThreading/ReaderWriterLock.h" | |
723 | 11 #include "../Core/MultiThreading/ThreadedCommandProcessor.h" |
12 | |
13 using namespace Orthanc; | |
14 | |
15 namespace | |
16 { | |
17 class DynamicInteger : public ICommand | |
18 { | |
19 private: | |
20 int value_; | |
21 std::set<int>& target_; | |
22 | |
23 public: | |
24 DynamicInteger(int value, std::set<int>& target) : | |
25 value_(value), target_(target) | |
26 { | |
27 } | |
28 | |
29 int GetValue() const | |
30 { | |
31 return value_; | |
32 } | |
33 | |
34 virtual bool Execute() | |
35 { | |
36 static boost::mutex mutex; | |
37 boost::mutex::scoped_lock lock(mutex); | |
38 target_.insert(value_); | |
39 return true; | |
40 } | |
41 }; | |
42 | |
43 class MyFiller : public ArrayFilledByThreads::IFiller | |
44 { | |
45 private: | |
46 int size_; | |
47 unsigned int created_; | |
48 std::set<int> set_; | |
49 | |
50 public: | |
51 MyFiller(int size) : size_(size), created_(0) | |
52 { | |
53 } | |
54 | |
55 virtual size_t GetFillerSize() | |
56 { | |
57 return size_; | |
58 } | |
59 | |
60 virtual IDynamicObject* GetFillerItem(size_t index) | |
61 { | |
62 static boost::mutex mutex; | |
63 boost::mutex::scoped_lock lock(mutex); | |
64 created_++; | |
65 return new DynamicInteger(index * 2, set_); | |
66 } | |
67 | |
68 unsigned int GetCreatedCount() const | |
69 { | |
70 return created_; | |
71 } | |
72 | |
73 std::set<int> GetSet() | |
74 { | |
75 return set_; | |
76 } | |
77 }; | |
78 } | |
79 | |
80 | |
81 | |
82 | |
83 TEST(MultiThreading, SharedMessageQueueBasic) | |
84 { | |
85 std::set<int> s; | |
86 | |
87 SharedMessageQueue q; | |
88 ASSERT_TRUE(q.WaitEmpty(0)); | |
89 q.Enqueue(new DynamicInteger(10, s)); | |
90 ASSERT_FALSE(q.WaitEmpty(1)); | |
91 q.Enqueue(new DynamicInteger(20, s)); | |
92 q.Enqueue(new DynamicInteger(30, s)); | |
93 q.Enqueue(new DynamicInteger(40, s)); | |
94 | |
95 std::auto_ptr<DynamicInteger> i; | |
96 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(10, i->GetValue()); | |
97 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(20, i->GetValue()); | |
98 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(30, i->GetValue()); | |
99 ASSERT_FALSE(q.WaitEmpty(1)); | |
100 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(40, i->GetValue()); | |
101 ASSERT_TRUE(q.WaitEmpty(0)); | |
102 ASSERT_EQ(NULL, q.Dequeue(1)); | |
103 } | |
104 | |
105 | |
106 TEST(MultiThreading, SharedMessageQueueClean) | |
107 { | |
108 std::set<int> s; | |
109 | |
110 try | |
111 { | |
112 SharedMessageQueue q; | |
113 q.Enqueue(new DynamicInteger(10, s)); | |
114 q.Enqueue(new DynamicInteger(20, s)); | |
115 throw OrthancException("Nope"); | |
116 } | |
117 catch (OrthancException&) | |
118 { | |
119 } | |
120 } | |
121 | |
122 | |
123 TEST(MultiThreading, ArrayFilledByThreadEmpty) | |
124 { | |
125 MyFiller f(0); | |
126 ArrayFilledByThreads a(f); | |
127 a.SetThreadCount(1); | |
128 ASSERT_EQ(0, a.GetSize()); | |
129 } | |
130 | |
131 | |
132 TEST(MultiThreading, ArrayFilledByThread1) | |
133 { | |
134 MyFiller f(100); | |
135 ArrayFilledByThreads a(f); | |
136 a.SetThreadCount(1); | |
137 ASSERT_EQ(100, a.GetSize()); | |
138 for (size_t i = 0; i < a.GetSize(); i++) | |
139 { | |
140 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); | |
141 } | |
142 } | |
143 | |
144 | |
145 TEST(MultiThreading, ArrayFilledByThread4) | |
146 { | |
147 MyFiller f(100); | |
148 ArrayFilledByThreads a(f); | |
149 a.SetThreadCount(4); | |
150 ASSERT_EQ(100, a.GetSize()); | |
151 for (size_t i = 0; i < a.GetSize(); i++) | |
152 { | |
153 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); | |
154 } | |
155 | |
156 ASSERT_EQ(100u, f.GetCreatedCount()); | |
157 | |
158 a.Invalidate(); | |
159 | |
160 ASSERT_EQ(100, a.GetSize()); | |
161 ASSERT_EQ(200u, f.GetCreatedCount()); | |
162 ASSERT_EQ(4u, a.GetThreadCount()); | |
163 ASSERT_TRUE(f.GetSet().empty()); | |
164 | |
165 for (size_t i = 0; i < a.GetSize(); i++) | |
166 { | |
167 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); | |
168 } | |
169 } | |
170 | |
171 | |
172 TEST(MultiThreading, CommandProcessor) | |
173 { | |
174 ThreadedCommandProcessor p(4); | |
175 | |
176 std::set<int> s; | |
177 | |
178 for (size_t i = 0; i < 100; i++) | |
179 { | |
180 p.Post(new DynamicInteger(i * 2, s)); | |
181 } | |
182 | |
183 p.Join(); | |
184 | |
185 for (size_t i = 0; i < 200; i++) | |
186 { | |
187 if (i % 2) | |
188 ASSERT_TRUE(s.find(i) == s.end()); | |
189 else | |
190 ASSERT_TRUE(s.find(i) != s.end()); | |
191 } | |
192 } | |
760 | 193 |
194 | |
195 TEST(MultiThreading, Mutex) | |
196 { | |
197 Mutex mutex; | |
198 Locker locker(mutex); | |
199 } | |
200 | |
201 | |
202 TEST(MultiThreading, ReaderWriterLock) | |
203 { | |
204 ReaderWriterLock lock; | |
205 | |
206 { | |
207 Locker locker1(lock.ForReader()); | |
208 Locker locker2(lock.ForReader()); | |
209 } | |
210 | |
211 { | |
212 Locker locker3(lock.ForWriter()); | |
213 } | |
214 } | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
215 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
216 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
217 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
218 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
219 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
220 #include "../OrthancServer/DicomProtocol/ReusableDicomUserConnection.h" |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
221 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
222 TEST(ReusableDicomUserConnection, DISABLED_Basic) |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
223 { |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
224 ReusableDicomUserConnection c; |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
225 c.SetMillisecondsBeforeClose(200); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
226 printf("START\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
227 { |
776 | 228 ReusableDicomUserConnection::Locker lock(c, "STORESCP", "localhost", 2000, ModalityManufacturer_Generic); |
229 lock.GetConnection().StoreFile("/home/jodogne/DICOM/Cardiac/MR.X.1.2.276.0.7230010.3.1.4.2831157719.2256.1336386844.676281"); | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
230 } |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
231 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
232 printf("**\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
233 Toolbox::USleep(1000000); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
234 printf("**\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
235 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
236 { |
776 | 237 ReusableDicomUserConnection::Locker lock(c, "STORESCP", "localhost", 2000, ModalityManufacturer_Generic); |
238 lock.GetConnection().StoreFile("/home/jodogne/DICOM/Cardiac/MR.X.1.2.276.0.7230010.3.1.4.2831157719.2256.1336386844.676277"); | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
239 } |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
240 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
241 Toolbox::ServerBarrier(); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
242 printf("DONE\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
243 } |