723
|
1 #include "gtest/gtest.h"
|
|
2
|
|
3 #include "../Core/OrthancException.h"
|
|
4 #include "../Core/Toolbox.h"
|
|
5 #include "../Core/MultiThreading/ArrayFilledByThreads.h"
|
|
6 #include "../Core/MultiThreading/ThreadedCommandProcessor.h"
|
|
7
|
|
8 using namespace Orthanc;
|
|
9
|
|
10 namespace
|
|
11 {
|
|
12 class DynamicInteger : public ICommand
|
|
13 {
|
|
14 private:
|
|
15 int value_;
|
|
16 std::set<int>& target_;
|
|
17
|
|
18 public:
|
|
19 DynamicInteger(int value, std::set<int>& target) :
|
|
20 value_(value), target_(target)
|
|
21 {
|
|
22 }
|
|
23
|
|
24 int GetValue() const
|
|
25 {
|
|
26 return value_;
|
|
27 }
|
|
28
|
|
29 virtual bool Execute()
|
|
30 {
|
|
31 static boost::mutex mutex;
|
|
32 boost::mutex::scoped_lock lock(mutex);
|
|
33 target_.insert(value_);
|
|
34 return true;
|
|
35 }
|
|
36 };
|
|
37
|
|
38 class MyFiller : public ArrayFilledByThreads::IFiller
|
|
39 {
|
|
40 private:
|
|
41 int size_;
|
|
42 unsigned int created_;
|
|
43 std::set<int> set_;
|
|
44
|
|
45 public:
|
|
46 MyFiller(int size) : size_(size), created_(0)
|
|
47 {
|
|
48 }
|
|
49
|
|
50 virtual size_t GetFillerSize()
|
|
51 {
|
|
52 return size_;
|
|
53 }
|
|
54
|
|
55 virtual IDynamicObject* GetFillerItem(size_t index)
|
|
56 {
|
|
57 static boost::mutex mutex;
|
|
58 boost::mutex::scoped_lock lock(mutex);
|
|
59 created_++;
|
|
60 return new DynamicInteger(index * 2, set_);
|
|
61 }
|
|
62
|
|
63 unsigned int GetCreatedCount() const
|
|
64 {
|
|
65 return created_;
|
|
66 }
|
|
67
|
|
68 std::set<int> GetSet()
|
|
69 {
|
|
70 return set_;
|
|
71 }
|
|
72 };
|
|
73 }
|
|
74
|
|
75
|
|
76
|
|
77
|
|
78 TEST(MultiThreading, SharedMessageQueueBasic)
|
|
79 {
|
|
80 std::set<int> s;
|
|
81
|
|
82 SharedMessageQueue q;
|
|
83 ASSERT_TRUE(q.WaitEmpty(0));
|
|
84 q.Enqueue(new DynamicInteger(10, s));
|
|
85 ASSERT_FALSE(q.WaitEmpty(1));
|
|
86 q.Enqueue(new DynamicInteger(20, s));
|
|
87 q.Enqueue(new DynamicInteger(30, s));
|
|
88 q.Enqueue(new DynamicInteger(40, s));
|
|
89
|
|
90 std::auto_ptr<DynamicInteger> i;
|
|
91 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(10, i->GetValue());
|
|
92 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(20, i->GetValue());
|
|
93 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(30, i->GetValue());
|
|
94 ASSERT_FALSE(q.WaitEmpty(1));
|
|
95 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(40, i->GetValue());
|
|
96 ASSERT_TRUE(q.WaitEmpty(0));
|
|
97 ASSERT_EQ(NULL, q.Dequeue(1));
|
|
98 }
|
|
99
|
|
100
|
|
101 TEST(MultiThreading, SharedMessageQueueClean)
|
|
102 {
|
|
103 std::set<int> s;
|
|
104
|
|
105 try
|
|
106 {
|
|
107 SharedMessageQueue q;
|
|
108 q.Enqueue(new DynamicInteger(10, s));
|
|
109 q.Enqueue(new DynamicInteger(20, s));
|
|
110 throw OrthancException("Nope");
|
|
111 }
|
|
112 catch (OrthancException&)
|
|
113 {
|
|
114 }
|
|
115 }
|
|
116
|
|
117
|
|
118 TEST(MultiThreading, ArrayFilledByThreadEmpty)
|
|
119 {
|
|
120 MyFiller f(0);
|
|
121 ArrayFilledByThreads a(f);
|
|
122 a.SetThreadCount(1);
|
|
123 ASSERT_EQ(0, a.GetSize());
|
|
124 }
|
|
125
|
|
126
|
|
127 TEST(MultiThreading, ArrayFilledByThread1)
|
|
128 {
|
|
129 MyFiller f(100);
|
|
130 ArrayFilledByThreads a(f);
|
|
131 a.SetThreadCount(1);
|
|
132 ASSERT_EQ(100, a.GetSize());
|
|
133 for (size_t i = 0; i < a.GetSize(); i++)
|
|
134 {
|
|
135 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue());
|
|
136 }
|
|
137 }
|
|
138
|
|
139
|
|
140 TEST(MultiThreading, ArrayFilledByThread4)
|
|
141 {
|
|
142 MyFiller f(100);
|
|
143 ArrayFilledByThreads a(f);
|
|
144 a.SetThreadCount(4);
|
|
145 ASSERT_EQ(100, a.GetSize());
|
|
146 for (size_t i = 0; i < a.GetSize(); i++)
|
|
147 {
|
|
148 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue());
|
|
149 }
|
|
150
|
|
151 ASSERT_EQ(100u, f.GetCreatedCount());
|
|
152
|
|
153 a.Invalidate();
|
|
154
|
|
155 ASSERT_EQ(100, a.GetSize());
|
|
156 ASSERT_EQ(200u, f.GetCreatedCount());
|
|
157 ASSERT_EQ(4u, a.GetThreadCount());
|
|
158 ASSERT_TRUE(f.GetSet().empty());
|
|
159
|
|
160 for (size_t i = 0; i < a.GetSize(); i++)
|
|
161 {
|
|
162 ASSERT_EQ(2 * i, dynamic_cast<DynamicInteger&>(a.GetItem(i)).GetValue());
|
|
163 }
|
|
164 }
|
|
165
|
|
166
|
|
167 TEST(MultiThreading, CommandProcessor)
|
|
168 {
|
|
169 ThreadedCommandProcessor p(4);
|
|
170
|
|
171 std::set<int> s;
|
|
172
|
|
173 for (size_t i = 0; i < 100; i++)
|
|
174 {
|
|
175 p.Post(new DynamicInteger(i * 2, s));
|
|
176 }
|
|
177
|
|
178 p.Join();
|
|
179
|
|
180 for (size_t i = 0; i < 200; i++)
|
|
181 {
|
|
182 if (i % 2)
|
|
183 ASSERT_TRUE(s.find(i) == s.end());
|
|
184 else
|
|
185 ASSERT_TRUE(s.find(i) != s.end());
|
|
186 }
|
|
187 }
|