Mercurial > hg > orthanc
annotate UnitTestsSources/MultiThreading.cpp @ 891:d0a08d8881b7 plugins
configuration
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 16 Jun 2014 15:40:58 +0200 |
parents | 84513f2ee1f3 |
children | 394a19d44f9d |
rev | line source |
---|---|
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
1 /** |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
4 * Belgium |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
5 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
6 * This program is free software: you can redistribute it and/or |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
7 * modify it under the terms of the GNU General Public License as |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
9 * License, or (at your option) any later version. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
10 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
11 * In addition, as a special exception, the copyright holders of this |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
12 * program give permission to link the code of its release with the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
14 * that use the same license as the "OpenSSL" library), and distribute |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
15 * the linked executables. You must obey the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
16 * in all respects for all of the code used other than "OpenSSL". If you |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
17 * modify file(s) with this exception, you may extend this exception to |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
18 * your version of the file(s), but you are not obligated to do so. If |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
19 * you do not wish to do so, delete this exception statement from your |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
20 * version. If you delete this exception statement from all source files |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
21 * in the program, then also delete it here. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
22 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
23 * This program is distributed in the hope that it will be useful, but |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
26 * General Public License for more details. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
27 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
28 * You should have received a copy of the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
30 **/ |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
31 |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
32 |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
827
diff
changeset
|
33 #include "PrecompiledHeadersUnitTests.h" |
723 | 34 #include "gtest/gtest.h" |
35 | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
36 #include <glog/logging.h> |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
37 |
723 | 38 #include "../Core/OrthancException.h" |
39 #include "../Core/Toolbox.h" | |
40 #include "../Core/MultiThreading/ArrayFilledByThreads.h" | |
760 | 41 #include "../Core/MultiThreading/Locker.h" |
42 #include "../Core/MultiThreading/Mutex.h" | |
43 #include "../Core/MultiThreading/ReaderWriterLock.h" | |
723 | 44 #include "../Core/MultiThreading/ThreadedCommandProcessor.h" |
45 | |
46 using namespace Orthanc; | |
47 | |
48 namespace | |
49 { | |
50 class DynamicInteger : public ICommand | |
51 { | |
52 private: | |
53 int value_; | |
54 std::set<int>& target_; | |
55 | |
56 public: | |
57 DynamicInteger(int value, std::set<int>& target) : | |
58 value_(value), target_(target) | |
59 { | |
60 } | |
61 | |
62 int GetValue() const | |
63 { | |
64 return value_; | |
65 } | |
66 | |
67 virtual bool Execute() | |
68 { | |
69 static boost::mutex mutex; | |
70 boost::mutex::scoped_lock lock(mutex); | |
71 target_.insert(value_); | |
72 return true; | |
73 } | |
74 }; | |
75 | |
76 class MyFiller : public ArrayFilledByThreads::IFiller | |
77 { | |
78 private: | |
79 int size_; | |
80 unsigned int created_; | |
81 std::set<int> set_; | |
82 | |
83 public: | |
84 MyFiller(int size) : size_(size), created_(0) | |
85 { | |
86 } | |
87 | |
88 virtual size_t GetFillerSize() | |
89 { | |
90 return size_; | |
91 } | |
92 | |
93 virtual IDynamicObject* GetFillerItem(size_t index) | |
94 { | |
95 static boost::mutex mutex; | |
96 boost::mutex::scoped_lock lock(mutex); | |
97 created_++; | |
98 return new DynamicInteger(index * 2, set_); | |
99 } | |
100 | |
101 unsigned int GetCreatedCount() const | |
102 { | |
103 return created_; | |
104 } | |
105 | |
106 std::set<int> GetSet() | |
107 { | |
108 return set_; | |
109 } | |
110 }; | |
111 } | |
112 | |
113 | |
114 | |
115 | |
116 TEST(MultiThreading, SharedMessageQueueBasic) | |
117 { | |
118 std::set<int> s; | |
119 | |
120 SharedMessageQueue q; | |
121 ASSERT_TRUE(q.WaitEmpty(0)); | |
122 q.Enqueue(new DynamicInteger(10, s)); | |
123 ASSERT_FALSE(q.WaitEmpty(1)); | |
124 q.Enqueue(new DynamicInteger(20, s)); | |
125 q.Enqueue(new DynamicInteger(30, s)); | |
126 q.Enqueue(new DynamicInteger(40, s)); | |
127 | |
128 std::auto_ptr<DynamicInteger> i; | |
129 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(10, i->GetValue()); | |
130 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(20, i->GetValue()); | |
131 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(30, i->GetValue()); | |
132 ASSERT_FALSE(q.WaitEmpty(1)); | |
133 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(40, i->GetValue()); | |
134 ASSERT_TRUE(q.WaitEmpty(0)); | |
135 ASSERT_EQ(NULL, q.Dequeue(1)); | |
136 } | |
137 | |
138 | |
139 TEST(MultiThreading, SharedMessageQueueClean) | |
140 { | |
141 std::set<int> s; | |
142 | |
143 try | |
144 { | |
145 SharedMessageQueue q; | |
146 q.Enqueue(new DynamicInteger(10, s)); | |
147 q.Enqueue(new DynamicInteger(20, s)); | |
148 throw OrthancException("Nope"); | |
149 } | |
150 catch (OrthancException&) | |
151 { | |
152 } | |
153 } | |
154 | |
155 | |
156 TEST(MultiThreading, ArrayFilledByThreadEmpty) | |
157 { | |
158 MyFiller f(0); | |
159 ArrayFilledByThreads a(f); | |
160 a.SetThreadCount(1); | |
161 ASSERT_EQ(0, a.GetSize()); | |
162 } | |
163 | |
164 | |
165 TEST(MultiThreading, ArrayFilledByThread1) | |
166 { | |
167 MyFiller f(100); | |
168 ArrayFilledByThreads a(f); | |
169 a.SetThreadCount(1); | |
170 ASSERT_EQ(100, a.GetSize()); | |
171 for (size_t i = 0; i < a.GetSize(); i++) | |
172 { | |
173 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); | |
174 } | |
175 } | |
176 | |
177 | |
178 TEST(MultiThreading, ArrayFilledByThread4) | |
179 { | |
180 MyFiller f(100); | |
181 ArrayFilledByThreads a(f); | |
182 a.SetThreadCount(4); | |
183 ASSERT_EQ(100, a.GetSize()); | |
184 for (size_t i = 0; i < a.GetSize(); i++) | |
185 { | |
186 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); | |
187 } | |
188 | |
189 ASSERT_EQ(100u, f.GetCreatedCount()); | |
190 | |
191 a.Invalidate(); | |
192 | |
193 ASSERT_EQ(100, a.GetSize()); | |
194 ASSERT_EQ(200u, f.GetCreatedCount()); | |
195 ASSERT_EQ(4u, a.GetThreadCount()); | |
196 ASSERT_TRUE(f.GetSet().empty()); | |
197 | |
198 for (size_t i = 0; i < a.GetSize(); i++) | |
199 { | |
200 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); | |
201 } | |
202 } | |
203 | |
204 | |
205 TEST(MultiThreading, CommandProcessor) | |
206 { | |
207 ThreadedCommandProcessor p(4); | |
208 | |
209 std::set<int> s; | |
210 | |
211 for (size_t i = 0; i < 100; i++) | |
212 { | |
213 p.Post(new DynamicInteger(i * 2, s)); | |
214 } | |
215 | |
216 p.Join(); | |
217 | |
218 for (size_t i = 0; i < 200; i++) | |
219 { | |
220 if (i % 2) | |
221 ASSERT_TRUE(s.find(i) == s.end()); | |
222 else | |
223 ASSERT_TRUE(s.find(i) != s.end()); | |
224 } | |
225 } | |
760 | 226 |
227 | |
228 TEST(MultiThreading, Mutex) | |
229 { | |
230 Mutex mutex; | |
231 Locker locker(mutex); | |
232 } | |
233 | |
234 | |
235 TEST(MultiThreading, ReaderWriterLock) | |
236 { | |
237 ReaderWriterLock lock; | |
238 | |
239 { | |
240 Locker locker1(lock.ForReader()); | |
241 Locker locker2(lock.ForReader()); | |
242 } | |
243 | |
244 { | |
245 Locker locker3(lock.ForWriter()); | |
246 } | |
247 } | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
248 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
249 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
250 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
251 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
252 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
253 #include "../OrthancServer/DicomProtocol/ReusableDicomUserConnection.h" |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
254 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
255 TEST(ReusableDicomUserConnection, DISABLED_Basic) |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
256 { |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
257 ReusableDicomUserConnection c; |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
258 c.SetMillisecondsBeforeClose(200); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
259 printf("START\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
260 { |
776 | 261 ReusableDicomUserConnection::Locker lock(c, "STORESCP", "localhost", 2000, ModalityManufacturer_Generic); |
262 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
|
263 } |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
264 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
265 printf("**\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
266 Toolbox::USleep(1000000); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
267 printf("**\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
268 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
269 { |
776 | 270 ReusableDicomUserConnection::Locker lock(c, "STORESCP", "localhost", 2000, ModalityManufacturer_Generic); |
271 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
|
272 } |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
273 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
274 Toolbox::ServerBarrier(); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
275 printf("DONE\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
276 } |