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