comparison UnitTestsSources/MultiThreadingTests.cpp @ 1396:ac4efabeb80c

Migration of the orthanc-client as a separate project
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 01 Jun 2015 11:15:55 +0200
parents 7b6925b0890d
children d710ea64f0fd
comparison
equal deleted inserted replaced
1395:785e01da36a1 1396:ac4efabeb80c
36 36
37 #include "../OrthancServer/Scheduler/ServerScheduler.h" 37 #include "../OrthancServer/Scheduler/ServerScheduler.h"
38 38
39 #include "../Core/OrthancException.h" 39 #include "../Core/OrthancException.h"
40 #include "../Core/Toolbox.h" 40 #include "../Core/Toolbox.h"
41 #include "../Core/MultiThreading/ArrayFilledByThreads.h"
42 #include "../Core/MultiThreading/Locker.h" 41 #include "../Core/MultiThreading/Locker.h"
43 #include "../Core/MultiThreading/Mutex.h" 42 #include "../Core/MultiThreading/Mutex.h"
44 #include "../Core/MultiThreading/ReaderWriterLock.h" 43 #include "../Core/MultiThreading/ReaderWriterLock.h"
45 #include "../Core/MultiThreading/ThreadedCommandProcessor.h"
46 44
47 using namespace Orthanc; 45 using namespace Orthanc;
48 46
49 namespace 47 namespace
50 { 48 {
51 class DynamicInteger : public ICommand 49 class DynamicInteger : public IDynamicObject
52 { 50 {
53 private: 51 private:
54 int value_; 52 int value_;
55 std::set<int>& target_; 53 std::set<int>& target_;
56 54
62 60
63 int GetValue() const 61 int GetValue() const
64 { 62 {
65 return value_; 63 return value_;
66 } 64 }
67
68 virtual bool Execute()
69 {
70 static boost::mutex mutex;
71 boost::mutex::scoped_lock lock(mutex);
72 target_.insert(value_);
73 return true;
74 }
75 }; 65 };
76 66 }
77 class MyFiller : public ArrayFilledByThreads::IFiller
78 {
79 private:
80 int size_;
81 unsigned int created_;
82 std::set<int> set_;
83
84 public:
85 MyFiller(int size) : size_(size), created_(0)
86 {
87 }
88
89 virtual size_t GetFillerSize()
90 {
91 return size_;
92 }
93
94 virtual IDynamicObject* GetFillerItem(size_t index)
95 {
96 static boost::mutex mutex;
97 boost::mutex::scoped_lock lock(mutex);
98 created_++;
99 return new DynamicInteger(index * 2, set_);
100 }
101
102 unsigned int GetCreatedCount() const
103 {
104 return created_;
105 }
106
107 std::set<int> GetSet()
108 {
109 return set_;
110 }
111 };
112 }
113
114
115 67
116 68
117 TEST(MultiThreading, SharedMessageQueueBasic) 69 TEST(MultiThreading, SharedMessageQueueBasic)
118 { 70 {
119 std::set<int> s; 71 std::set<int> s;
148 q.Enqueue(new DynamicInteger(20, s)); 100 q.Enqueue(new DynamicInteger(20, s));
149 throw OrthancException("Nope"); 101 throw OrthancException("Nope");
150 } 102 }
151 catch (OrthancException&) 103 catch (OrthancException&)
152 { 104 {
153 }
154 }
155
156
157 TEST(MultiThreading, ArrayFilledByThreadEmpty)
158 {
159 MyFiller f(0);
160 ArrayFilledByThreads a(f);
161 a.SetThreadCount(1);
162 ASSERT_EQ(0, a.GetSize());
163 }
164
165
166 TEST(MultiThreading, ArrayFilledByThread1)
167 {
168 MyFiller f(100);
169 ArrayFilledByThreads a(f);
170 a.SetThreadCount(1);
171 ASSERT_EQ(100, a.GetSize());
172 for (size_t i = 0; i < a.GetSize(); i++)
173 {
174 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue());
175 }
176 }
177
178
179 TEST(MultiThreading, ArrayFilledByThread4)
180 {
181 MyFiller f(100);
182 ArrayFilledByThreads a(f);
183 a.SetThreadCount(4);
184 ASSERT_EQ(100, a.GetSize());
185 for (size_t i = 0; i < a.GetSize(); i++)
186 {
187 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue());
188 }
189
190 ASSERT_EQ(100u, f.GetCreatedCount());
191
192 a.Invalidate();
193
194 ASSERT_EQ(100, a.GetSize());
195 ASSERT_EQ(200u, f.GetCreatedCount());
196 ASSERT_EQ(4u, a.GetThreadCount());
197 ASSERT_TRUE(f.GetSet().empty());
198
199 for (size_t i = 0; i < a.GetSize(); i++)
200 {
201 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue());
202 }
203 }
204
205
206 TEST(MultiThreading, CommandProcessor)
207 {
208 ThreadedCommandProcessor p(4);
209
210 std::set<int> s;
211
212 for (size_t i = 0; i < 100; i++)
213 {
214 p.Post(new DynamicInteger(i * 2, s));
215 }
216
217 p.Join();
218
219 for (size_t i = 0; i < 200; i++)
220 {
221 if (i % 2)
222 ASSERT_TRUE(s.find(i) == s.end());
223 else
224 ASSERT_TRUE(s.find(i) != s.end());
225 } 105 }
226 } 106 }
227 107
228 108
229 TEST(MultiThreading, Mutex) 109 TEST(MultiThreading, Mutex)