Mercurial > hg > orthanc-client
comparison UnitTestsSources/MultiThreadingTests.cpp @ 1:fd402e53d263
new files
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 01 Jun 2015 11:12:20 +0200 |
parents | |
children | 1fb480a156fd |
comparison
equal
deleted
inserted
replaced
0:ebc1e38ef615 | 1:fd402e53d263 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "../Core/MultiThreading/ThreadedCommandProcessor.h" | |
34 #include "../Core/MultiThreading/ArrayFilledByThreads.h" | |
35 | |
36 | |
37 using namespace Orthanc; | |
38 | |
39 namespace | |
40 { | |
41 class DynamicInteger : public ICommand | |
42 { | |
43 private: | |
44 int value_; | |
45 std::set<int>& target_; | |
46 | |
47 public: | |
48 DynamicInteger(int value, std::set<int>& target) : | |
49 value_(value), target_(target) | |
50 { | |
51 } | |
52 | |
53 int GetValue() const | |
54 { | |
55 return value_; | |
56 } | |
57 | |
58 virtual bool Execute() | |
59 { | |
60 static boost::mutex mutex; | |
61 boost::mutex::scoped_lock lock(mutex); | |
62 target_.insert(value_); | |
63 return true; | |
64 } | |
65 }; | |
66 | |
67 class MyFiller : public ArrayFilledByThreads::IFiller | |
68 { | |
69 private: | |
70 int size_; | |
71 unsigned int created_; | |
72 std::set<int> set_; | |
73 | |
74 public: | |
75 MyFiller(int size) : size_(size), created_(0) | |
76 { | |
77 } | |
78 | |
79 virtual size_t GetFillerSize() | |
80 { | |
81 return size_; | |
82 } | |
83 | |
84 virtual IDynamicObject* GetFillerItem(size_t index) | |
85 { | |
86 static boost::mutex mutex; | |
87 boost::mutex::scoped_lock lock(mutex); | |
88 created_++; | |
89 return new DynamicInteger(index * 2, set_); | |
90 } | |
91 | |
92 unsigned int GetCreatedCount() const | |
93 { | |
94 return created_; | |
95 } | |
96 | |
97 std::set<int> GetSet() | |
98 { | |
99 return set_; | |
100 } | |
101 }; | |
102 } | |
103 | |
104 | |
105 | |
106 TEST(MultiThreading, ArrayFilledByThreadEmpty) | |
107 { | |
108 MyFiller f(0); | |
109 ArrayFilledByThreads a(f); | |
110 a.SetThreadCount(1); | |
111 ASSERT_EQ(0, a.GetSize()); | |
112 } | |
113 | |
114 | |
115 TEST(MultiThreading, ArrayFilledByThread1) | |
116 { | |
117 MyFiller f(100); | |
118 ArrayFilledByThreads a(f); | |
119 a.SetThreadCount(1); | |
120 ASSERT_EQ(100, a.GetSize()); | |
121 for (size_t i = 0; i < a.GetSize(); i++) | |
122 { | |
123 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); | |
124 } | |
125 } | |
126 | |
127 | |
128 TEST(MultiThreading, ArrayFilledByThread4) | |
129 { | |
130 MyFiller f(100); | |
131 ArrayFilledByThreads a(f); | |
132 a.SetThreadCount(4); | |
133 ASSERT_EQ(100, a.GetSize()); | |
134 for (size_t i = 0; i < a.GetSize(); i++) | |
135 { | |
136 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); | |
137 } | |
138 | |
139 ASSERT_EQ(100u, f.GetCreatedCount()); | |
140 | |
141 a.Invalidate(); | |
142 | |
143 ASSERT_EQ(100, a.GetSize()); | |
144 ASSERT_EQ(200u, f.GetCreatedCount()); | |
145 ASSERT_EQ(4u, a.GetThreadCount()); | |
146 ASSERT_TRUE(f.GetSet().empty()); | |
147 | |
148 for (size_t i = 0; i < a.GetSize(); i++) | |
149 { | |
150 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue()); | |
151 } | |
152 } | |
153 | |
154 | |
155 | |
156 | |
157 TEST(MultiThreading, CommandProcessor) | |
158 { | |
159 ThreadedCommandProcessor p(4); | |
160 | |
161 std::set<int> s; | |
162 | |
163 for (size_t i = 0; i < 100; i++) | |
164 { | |
165 p.Post(new DynamicInteger(i * 2, s)); | |
166 } | |
167 | |
168 p.Join(); | |
169 | |
170 for (size_t i = 0; i < 200; i++) | |
171 { | |
172 if (i % 2) | |
173 ASSERT_TRUE(s.find(i) == s.end()); | |
174 else | |
175 ASSERT_TRUE(s.find(i) != s.end()); | |
176 } | |
177 } |