comparison UnitTestsSources/MultiThreadingTests.cpp @ 2610:3ff4c50647ea jobs

moving the old scheduler to the graveyard
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 19 May 2018 16:40:26 +0200
parents f2b9d3256060
children 83ac5a05ce84
comparison
equal deleted inserted replaced
2609:f7a84b551ee4 2610:3ff4c50647ea
33 33
34 #include "PrecompiledHeadersUnitTests.h" 34 #include "PrecompiledHeadersUnitTests.h"
35 #include "gtest/gtest.h" 35 #include "gtest/gtest.h"
36 36
37 #include "../Core/JobsEngine/JobsEngine.h" 37 #include "../Core/JobsEngine/JobsEngine.h"
38 #include "../Core/MultiThreading/Locker.h" 38 #include "../Core/MultiThreading/SharedMessageQueue.h"
39 #include "../Core/OrthancException.h" 39 #include "../Core/OrthancException.h"
40 #include "../Core/SystemToolbox.h" 40 #include "../Core/SystemToolbox.h"
41 #include "../Core/Toolbox.h" 41 #include "../Core/Toolbox.h"
42 #include "../OrthancServer/Scheduler/ServerScheduler.h"
43 42
44 using namespace Orthanc; 43 using namespace Orthanc;
45 44
46 namespace 45 namespace
47 { 46 {
102 catch (OrthancException&) 101 catch (OrthancException&)
103 { 102 {
104 } 103 }
105 } 104 }
106 105
107
108
109
110 #include "../Core/DicomNetworking/ReusableDicomUserConnection.h"
111
112 TEST(ReusableDicomUserConnection, DISABLED_Basic)
113 {
114 ReusableDicomUserConnection c;
115 c.SetMillisecondsBeforeClose(200);
116 printf("START\n"); fflush(stdout);
117
118 {
119 RemoteModalityParameters remote("STORESCP", "localhost", 2000, ModalityManufacturer_Generic);
120 ReusableDicomUserConnection::Locker lock(c, "ORTHANC", remote);
121 lock.GetConnection().StoreFile("/home/jodogne/DICOM/Cardiac/MR.X.1.2.276.0.7230010.3.1.4.2831157719.2256.1336386844.676281");
122 }
123
124 printf("**\n"); fflush(stdout);
125 SystemToolbox::USleep(1000000);
126 printf("**\n"); fflush(stdout);
127
128 {
129 RemoteModalityParameters remote("STORESCP", "localhost", 2000, ModalityManufacturer_Generic);
130 ReusableDicomUserConnection::Locker lock(c, "ORTHANC", remote);
131 lock.GetConnection().StoreFile("/home/jodogne/DICOM/Cardiac/MR.X.1.2.276.0.7230010.3.1.4.2831157719.2256.1336386844.676277");
132 }
133
134 SystemToolbox::ServerBarrier();
135 printf("DONE\n"); fflush(stdout);
136 }
137
138
139
140 class Tutu : public IServerCommand
141 {
142 private:
143 int factor_;
144
145 public:
146 Tutu(int f) : factor_(f)
147 {
148 }
149
150 virtual bool Apply(ListOfStrings& outputs,
151 const ListOfStrings& inputs)
152 {
153 for (ListOfStrings::const_iterator
154 it = inputs.begin(); it != inputs.end(); ++it)
155 {
156 int a = boost::lexical_cast<int>(*it);
157 int b = factor_ * a;
158
159 printf("%d * %d = %d\n", a, factor_, b);
160
161 //if (a == 84) { printf("BREAK\n"); return false; }
162
163 outputs.push_back(boost::lexical_cast<std::string>(b));
164 }
165
166 SystemToolbox::USleep(30000);
167
168 return true;
169 }
170 };
171
172
173 static void Tata(ServerScheduler* s, ServerJob* j, bool* done)
174 {
175 typedef IServerCommand::ListOfStrings ListOfStrings;
176
177 while (!(*done))
178 {
179 ListOfStrings l;
180 s->GetListOfJobs(l);
181 for (ListOfStrings::iterator it = l.begin(); it != l.end(); ++it)
182 {
183 printf(">> %s: %0.1f\n", it->c_str(), 100.0f * s->GetProgress(*it));
184 }
185 SystemToolbox::USleep(3000);
186 }
187 }
188
189
190 TEST(MultiThreading, ServerScheduler)
191 {
192 ServerScheduler scheduler(10);
193
194 ServerJob job;
195 ServerCommandInstance& f2 = job.AddCommand(new Tutu(2));
196 ServerCommandInstance& f3 = job.AddCommand(new Tutu(3));
197 ServerCommandInstance& f4 = job.AddCommand(new Tutu(4));
198 ServerCommandInstance& f5 = job.AddCommand(new Tutu(5));
199 f2.AddInput(boost::lexical_cast<std::string>(42));
200 //f3.AddInput(boost::lexical_cast<std::string>(42));
201 //f4.AddInput(boost::lexical_cast<std::string>(42));
202 f2.ConnectOutput(f3);
203 f3.ConnectOutput(f4);
204 f4.ConnectOutput(f5);
205
206 f3.SetConnectedToSink(true);
207 f5.SetConnectedToSink(true);
208
209 job.SetDescription("tutu");
210
211 bool done = false;
212 boost::thread t(Tata, &scheduler, &job, &done);
213
214
215 //scheduler.Submit(job);
216
217 IServerCommand::ListOfStrings l;
218 scheduler.SubmitAndWait(l, job);
219
220 ASSERT_EQ(2u, l.size());
221 ASSERT_EQ(42 * 2 * 3, boost::lexical_cast<int>(l.front()));
222 ASSERT_EQ(42 * 2 * 3 * 4 * 5, boost::lexical_cast<int>(l.back()));
223
224 for (IServerCommand::ListOfStrings::iterator i = l.begin(); i != l.end(); i++)
225 {
226 printf("** %s\n", i->c_str());
227 }
228
229 //SystemToolbox::ServerBarrier();
230 //SystemToolbox::USleep(3000000);
231
232 scheduler.Stop();
233
234 done = true;
235 if (t.joinable())
236 {
237 t.join();
238 }
239 }
240 106
241 107
242 108
243 class DummyJob : public Orthanc::IJob 109 class DummyJob : public Orthanc::IJob
244 { 110 {