comparison UnitTestsSources/MultiThreadingTests.cpp @ 2652:a3f0f61a14ca jobs

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 04 Jun 2018 12:36:24 +0200
parents 1da5a052c777
children d7815540bd81
comparison
equal deleted inserted replaced
2651:1da5a052c777 2652:a3f0f61a14ca
51 51
52 using namespace Orthanc; 52 using namespace Orthanc;
53 53
54 namespace 54 namespace
55 { 55 {
56 class DummyJob : public IJob
57 {
58 private:
59 bool fails_;
60 unsigned int count_;
61 unsigned int steps_;
62
63 public:
64 DummyJob() :
65 fails_(false),
66 count_(0),
67 steps_(4)
68 {
69 }
70
71 explicit DummyJob(bool fails) :
72 fails_(fails),
73 count_(0),
74 steps_(4)
75 {
76 }
77
78 virtual void Start()
79 {
80 }
81
82 virtual void SignalResubmit()
83 {
84 }
85
86 virtual JobStepResult ExecuteStep()
87 {
88 if (fails_)
89 {
90 return JobStepResult::Failure(ErrorCode_ParameterOutOfRange);
91 }
92 else if (count_ == steps_ - 1)
93 {
94 return JobStepResult::Success();
95 }
96 else
97 {
98 count_++;
99 return JobStepResult::Continue();
100 }
101 }
102
103 virtual void ReleaseResources()
104 {
105 }
106
107 virtual float GetProgress()
108 {
109 return static_cast<float>(count_) / static_cast<float>(steps_ - 1);
110 }
111
112 virtual void GetJobType(std::string& type)
113 {
114 type = "DummyJob";
115 }
116
117 virtual void Serialize(Json::Value& value)
118 {
119 }
120
121 virtual void GetPublicContent(Json::Value& value)
122 {
123 value["hello"] = "world";
124 }
125 };
126
127
128 class DummyInstancesJob : public SetOfInstancesJob
129 {
130 protected:
131 virtual bool HandleInstance(const std::string& instance)
132 {
133 return (instance != "nope");
134 }
135
136 public:
137 DummyInstancesJob()
138 {
139 }
140
141 DummyInstancesJob(const Json::Value& value) :
142 SetOfInstancesJob(value)
143 {
144 }
145
146 virtual void ReleaseResources()
147 {
148 }
149
150 virtual void GetJobType(std::string& s)
151 {
152 s = "DummyInstancesJob";
153 }
154 };
155
156
157 class DummyUnserializer : public GenericJobUnserializer
158 {
159 public:
160 virtual IJob* UnserializeJob(const Json::Value& value)
161 {
162 if (GetString(value, "Type") == "DummyInstancesJob")
163 {
164 return new DummyInstancesJob(value);
165 }
166 else
167 {
168 return GenericJobUnserializer::UnserializeJob(value);
169 }
170 }
171 };
172
173
56 class DynamicInteger : public IDynamicObject 174 class DynamicInteger : public IDynamicObject
57 { 175 {
58 private: 176 private:
59 int value_; 177 int value_;
60 std::set<int>& target_; 178 std::set<int>& target_;
113 } 231 }
114 232
115 233
116 234
117 235
118 class DummyJob : public Orthanc::IJob 236 static bool CheckState(JobsRegistry& registry,
119 {
120 private:
121 bool fails_;
122 unsigned int count_;
123 unsigned int steps_;
124
125 public:
126 DummyJob() :
127 fails_(false),
128 count_(0),
129 steps_(4)
130 {
131 }
132
133 explicit DummyJob(bool fails) :
134 fails_(fails),
135 count_(0),
136 steps_(4)
137 {
138 }
139
140 virtual void Start()
141 {
142 }
143
144 virtual void SignalResubmit()
145 {
146 }
147
148 virtual JobStepResult ExecuteStep()
149 {
150 if (fails_)
151 {
152 return JobStepResult::Failure(ErrorCode_ParameterOutOfRange);
153 }
154 else if (count_ == steps_ - 1)
155 {
156 return JobStepResult::Success();
157 }
158 else
159 {
160 count_++;
161 return JobStepResult::Continue();
162 }
163 }
164
165 virtual void ReleaseResources()
166 {
167 }
168
169 virtual float GetProgress()
170 {
171 return static_cast<float>(count_) / static_cast<float>(steps_ - 1);
172 }
173
174 virtual void GetJobType(std::string& type)
175 {
176 type = "DummyJob";
177 }
178
179 virtual void Serialize(Json::Value& value)
180 {
181 }
182
183 virtual void GetPublicContent(Json::Value& value)
184 {
185 value["hello"] = "world";
186 }
187 };
188
189
190 static bool CheckState(Orthanc::JobsRegistry& registry,
191 const std::string& id, 237 const std::string& id,
192 Orthanc::JobState state) 238 JobState state)
193 { 239 {
194 Orthanc::JobState s; 240 JobState s;
195 if (registry.GetState(s, id)) 241 if (registry.GetState(s, id))
196 { 242 {
197 return state == s; 243 return state == s;
198 } 244 }
199 else 245 else
201 return false; 247 return false;
202 } 248 }
203 } 249 }
204 250
205 251
206 static bool CheckErrorCode(Orthanc::JobsRegistry& registry, 252 static bool CheckErrorCode(JobsRegistry& registry,
207 const std::string& id, 253 const std::string& id,
208 Orthanc::ErrorCode code) 254 ErrorCode code)
209 { 255 {
210 Orthanc::JobInfo s; 256 JobInfo s;
211 if (registry.GetJobInfo(s, id)) 257 if (registry.GetJobInfo(s, id))
212 { 258 {
213 return code == s.GetStatus().GetErrorCode(); 259 return code == s.GetStatus().GetErrorCode();
214 } 260 }
215 else 261 else
238 ASSERT_TRUE(id.find(i1) != id.end()); 284 ASSERT_TRUE(id.find(i1) != id.end());
239 ASSERT_TRUE(id.find(i2) != id.end()); 285 ASSERT_TRUE(id.find(i2) != id.end());
240 ASSERT_TRUE(id.find(i3) != id.end()); 286 ASSERT_TRUE(id.find(i3) != id.end());
241 ASSERT_TRUE(id.find(i4) != id.end()); 287 ASSERT_TRUE(id.find(i4) != id.end());
242 288
243 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Pending)); 289 ASSERT_TRUE(CheckState(registry, i2, JobState_Pending));
244 290
245 { 291 {
246 JobsRegistry::RunningJob job(registry, 0); 292 JobsRegistry::RunningJob job(registry, 0);
247 ASSERT_TRUE(job.IsValid()); 293 ASSERT_TRUE(job.IsValid());
248 ASSERT_EQ(30, job.GetPriority()); 294 ASSERT_EQ(30, job.GetPriority());
249 ASSERT_EQ(i2, job.GetId()); 295 ASSERT_EQ(i2, job.GetId());
250 296
251 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Running)); 297 ASSERT_TRUE(CheckState(registry, i2, JobState_Running));
252 } 298 }
253 299
254 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Failure)); 300 ASSERT_TRUE(CheckState(registry, i2, JobState_Failure));
255 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Pending)); 301 ASSERT_TRUE(CheckState(registry, i3, JobState_Pending));
256 302
257 { 303 {
258 JobsRegistry::RunningJob job(registry, 0); 304 JobsRegistry::RunningJob job(registry, 0);
259 ASSERT_TRUE(job.IsValid()); 305 ASSERT_TRUE(job.IsValid());
260 ASSERT_EQ(20, job.GetPriority()); 306 ASSERT_EQ(20, job.GetPriority());
261 ASSERT_EQ(i3, job.GetId()); 307 ASSERT_EQ(i3, job.GetId());
262 308
263 job.MarkSuccess(); 309 job.MarkSuccess();
264 310
265 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Running)); 311 ASSERT_TRUE(CheckState(registry, i3, JobState_Running));
266 } 312 }
267 313
268 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Success)); 314 ASSERT_TRUE(CheckState(registry, i3, JobState_Success));
269 315
270 { 316 {
271 JobsRegistry::RunningJob job(registry, 0); 317 JobsRegistry::RunningJob job(registry, 0);
272 ASSERT_TRUE(job.IsValid()); 318 ASSERT_TRUE(job.IsValid());
273 ASSERT_EQ(10, job.GetPriority()); 319 ASSERT_EQ(10, job.GetPriority());
284 { 330 {
285 JobsRegistry::RunningJob job(registry, 1); 331 JobsRegistry::RunningJob job(registry, 1);
286 ASSERT_FALSE(job.IsValid()); 332 ASSERT_FALSE(job.IsValid());
287 } 333 }
288 334
289 Orthanc::JobState s; 335 JobState s;
290 ASSERT_TRUE(registry.GetState(s, i1)); 336 ASSERT_TRUE(registry.GetState(s, i1));
291 ASSERT_FALSE(registry.GetState(s, i2)); // Removed because oldest 337 ASSERT_FALSE(registry.GetState(s, i2)); // Removed because oldest
292 ASSERT_FALSE(registry.GetState(s, i3)); // Removed because second oldest 338 ASSERT_FALSE(registry.GetState(s, i3)); // Removed because second oldest
293 ASSERT_TRUE(registry.GetState(s, i4)); 339 ASSERT_TRUE(registry.GetState(s, i4));
294 340
304 350
305 std::string i1, i2; 351 std::string i1, i2;
306 registry.Submit(i1, new DummyJob(), 20); 352 registry.Submit(i1, new DummyJob(), 20);
307 registry.Submit(i2, new DummyJob(), 10); 353 registry.Submit(i2, new DummyJob(), 10);
308 354
309 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Pending)); 355 ASSERT_TRUE(CheckState(registry, i1, JobState_Pending));
310 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Pending)); 356 ASSERT_TRUE(CheckState(registry, i2, JobState_Pending));
311 357
312 { 358 {
313 JobsRegistry::RunningJob job1(registry, 0); 359 JobsRegistry::RunningJob job1(registry, 0);
314 JobsRegistry::RunningJob job2(registry, 0); 360 JobsRegistry::RunningJob job2(registry, 0);
315 361
317 ASSERT_TRUE(job2.IsValid()); 363 ASSERT_TRUE(job2.IsValid());
318 364
319 job1.MarkFailure(); 365 job1.MarkFailure();
320 job2.MarkSuccess(); 366 job2.MarkSuccess();
321 367
322 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Running)); 368 ASSERT_TRUE(CheckState(registry, i1, JobState_Running));
323 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Running)); 369 ASSERT_TRUE(CheckState(registry, i2, JobState_Running));
324 } 370 }
325 371
326 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Failure)); 372 ASSERT_TRUE(CheckState(registry, i1, JobState_Failure));
327 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Success)); 373 ASSERT_TRUE(CheckState(registry, i2, JobState_Success));
328 } 374 }
329 375
330 376
331 TEST(JobsRegistry, Resubmit) 377 TEST(JobsRegistry, Resubmit)
332 { 378 {
333 JobsRegistry registry; 379 JobsRegistry registry;
334 380
335 std::string id; 381 std::string id;
336 registry.Submit(id, new DummyJob(), 10); 382 registry.Submit(id, new DummyJob(), 10);
337 383
338 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 384 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
339 385
340 registry.Resubmit(id); 386 registry.Resubmit(id);
341 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 387 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
342 388
343 { 389 {
344 JobsRegistry::RunningJob job(registry, 0); 390 JobsRegistry::RunningJob job(registry, 0);
345 ASSERT_TRUE(job.IsValid()); 391 ASSERT_TRUE(job.IsValid());
346 job.MarkFailure(); 392 job.MarkFailure();
347 393
348 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 394 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
349 395
350 registry.Resubmit(id); 396 registry.Resubmit(id);
351 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 397 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
352 } 398 }
353 399
354 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Failure)); 400 ASSERT_TRUE(CheckState(registry, id, JobState_Failure));
355 401
356 registry.Resubmit(id); 402 registry.Resubmit(id);
357 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 403 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
358 404
359 { 405 {
360 JobsRegistry::RunningJob job(registry, 0); 406 JobsRegistry::RunningJob job(registry, 0);
361 ASSERT_TRUE(job.IsValid()); 407 ASSERT_TRUE(job.IsValid());
362 ASSERT_EQ(id, job.GetId()); 408 ASSERT_EQ(id, job.GetId());
363 409
364 job.MarkSuccess(); 410 job.MarkSuccess();
365 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 411 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
366 } 412 }
367 413
368 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); 414 ASSERT_TRUE(CheckState(registry, id, JobState_Success));
369 415
370 registry.Resubmit(id); 416 registry.Resubmit(id);
371 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); 417 ASSERT_TRUE(CheckState(registry, id, JobState_Success));
372 } 418 }
373 419
374 420
375 TEST(JobsRegistry, Retry) 421 TEST(JobsRegistry, Retry)
376 { 422 {
377 JobsRegistry registry; 423 JobsRegistry registry;
378 424
379 std::string id; 425 std::string id;
380 registry.Submit(id, new DummyJob(), 10); 426 registry.Submit(id, new DummyJob(), 10);
381 427
382 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 428 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
383 429
384 { 430 {
385 JobsRegistry::RunningJob job(registry, 0); 431 JobsRegistry::RunningJob job(registry, 0);
386 ASSERT_TRUE(job.IsValid()); 432 ASSERT_TRUE(job.IsValid());
387 job.MarkRetry(0); 433 job.MarkRetry(0);
388 434
389 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 435 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
390 } 436 }
391 437
392 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); 438 ASSERT_TRUE(CheckState(registry, id, JobState_Retry));
393 439
394 registry.Resubmit(id); 440 registry.Resubmit(id);
395 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); 441 ASSERT_TRUE(CheckState(registry, id, JobState_Retry));
396 442
397 registry.ScheduleRetries(); 443 registry.ScheduleRetries();
398 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 444 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
399 445
400 { 446 {
401 JobsRegistry::RunningJob job(registry, 0); 447 JobsRegistry::RunningJob job(registry, 0);
402 ASSERT_TRUE(job.IsValid()); 448 ASSERT_TRUE(job.IsValid());
403 job.MarkSuccess(); 449 job.MarkSuccess();
404 450
405 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 451 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
406 } 452 }
407 453
408 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); 454 ASSERT_TRUE(CheckState(registry, id, JobState_Success));
409 } 455 }
410 456
411 457
412 TEST(JobsRegistry, PausePending) 458 TEST(JobsRegistry, PausePending)
413 { 459 {
414 JobsRegistry registry; 460 JobsRegistry registry;
415 461
416 std::string id; 462 std::string id;
417 registry.Submit(id, new DummyJob(), 10); 463 registry.Submit(id, new DummyJob(), 10);
418 464
419 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 465 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
420 466
421 registry.Pause(id); 467 registry.Pause(id);
422 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); 468 ASSERT_TRUE(CheckState(registry, id, JobState_Paused));
423 469
424 registry.Pause(id); 470 registry.Pause(id);
425 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); 471 ASSERT_TRUE(CheckState(registry, id, JobState_Paused));
426 472
427 registry.Resubmit(id); 473 registry.Resubmit(id);
428 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); 474 ASSERT_TRUE(CheckState(registry, id, JobState_Paused));
429 475
430 registry.Resume(id); 476 registry.Resume(id);
431 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 477 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
432 } 478 }
433 479
434 480
435 TEST(JobsRegistry, PauseRunning) 481 TEST(JobsRegistry, PauseRunning)
436 { 482 {
437 JobsRegistry registry; 483 JobsRegistry registry;
438 484
439 std::string id; 485 std::string id;
440 registry.Submit(id, new DummyJob(), 10); 486 registry.Submit(id, new DummyJob(), 10);
441 487
442 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 488 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
443 489
444 { 490 {
445 JobsRegistry::RunningJob job(registry, 0); 491 JobsRegistry::RunningJob job(registry, 0);
446 ASSERT_TRUE(job.IsValid()); 492 ASSERT_TRUE(job.IsValid());
447 493
448 registry.Resubmit(id); 494 registry.Resubmit(id);
449 job.MarkPause(); 495 job.MarkPause();
450 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 496 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
451 } 497 }
452 498
453 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); 499 ASSERT_TRUE(CheckState(registry, id, JobState_Paused));
454 500
455 registry.Resubmit(id); 501 registry.Resubmit(id);
456 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); 502 ASSERT_TRUE(CheckState(registry, id, JobState_Paused));
457 503
458 registry.Resume(id); 504 registry.Resume(id);
459 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 505 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
460 506
461 { 507 {
462 JobsRegistry::RunningJob job(registry, 0); 508 JobsRegistry::RunningJob job(registry, 0);
463 ASSERT_TRUE(job.IsValid()); 509 ASSERT_TRUE(job.IsValid());
464 510
465 job.MarkSuccess(); 511 job.MarkSuccess();
466 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 512 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
467 } 513 }
468 514
469 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); 515 ASSERT_TRUE(CheckState(registry, id, JobState_Success));
470 } 516 }
471 517
472 518
473 TEST(JobsRegistry, PauseRetry) 519 TEST(JobsRegistry, PauseRetry)
474 { 520 {
475 JobsRegistry registry; 521 JobsRegistry registry;
476 522
477 std::string id; 523 std::string id;
478 registry.Submit(id, new DummyJob(), 10); 524 registry.Submit(id, new DummyJob(), 10);
479 525
480 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 526 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
481 527
482 { 528 {
483 JobsRegistry::RunningJob job(registry, 0); 529 JobsRegistry::RunningJob job(registry, 0);
484 ASSERT_TRUE(job.IsValid()); 530 ASSERT_TRUE(job.IsValid());
485 531
486 job.MarkRetry(0); 532 job.MarkRetry(0);
487 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 533 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
488 } 534 }
489 535
490 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); 536 ASSERT_TRUE(CheckState(registry, id, JobState_Retry));
491 537
492 registry.Pause(id); 538 registry.Pause(id);
493 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); 539 ASSERT_TRUE(CheckState(registry, id, JobState_Paused));
494 540
495 registry.Resume(id); 541 registry.Resume(id);
496 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 542 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
497 543
498 { 544 {
499 JobsRegistry::RunningJob job(registry, 0); 545 JobsRegistry::RunningJob job(registry, 0);
500 ASSERT_TRUE(job.IsValid()); 546 ASSERT_TRUE(job.IsValid());
501 547
502 job.MarkSuccess(); 548 job.MarkSuccess();
503 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 549 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
504 } 550 }
505 551
506 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); 552 ASSERT_TRUE(CheckState(registry, id, JobState_Success));
507 } 553 }
508 554
509 555
510 TEST(JobsRegistry, Cancel) 556 TEST(JobsRegistry, Cancel)
511 { 557 {
514 std::string id; 560 std::string id;
515 registry.Submit(id, new DummyJob(), 10); 561 registry.Submit(id, new DummyJob(), 10);
516 562
517 ASSERT_FALSE(registry.Cancel("nope")); 563 ASSERT_FALSE(registry.Cancel("nope"));
518 564
519 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 565 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
520 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success)); 566 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success));
521 567
522 ASSERT_TRUE(registry.Cancel(id)); 568 ASSERT_TRUE(registry.Cancel(id));
523 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Failure)); 569 ASSERT_TRUE(CheckState(registry, id, JobState_Failure));
524 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob)); 570 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob));
525 571
526 ASSERT_TRUE(registry.Cancel(id)); 572 ASSERT_TRUE(registry.Cancel(id));
527 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Failure)); 573 ASSERT_TRUE(CheckState(registry, id, JobState_Failure));
528 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob)); 574 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob));
529 575
530 ASSERT_TRUE(registry.Resubmit(id)); 576 ASSERT_TRUE(registry.Resubmit(id));
531 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 577 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
532 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob)); 578 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob));
533 579
534 { 580 {
535 JobsRegistry::RunningJob job(registry, 0); 581 JobsRegistry::RunningJob job(registry, 0);
536 ASSERT_TRUE(job.IsValid()); 582 ASSERT_TRUE(job.IsValid());
537 583
538 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success)); 584 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success));
539 585
540 job.MarkSuccess(); 586 job.MarkSuccess();
541 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 587 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
542 } 588 }
543 589
544 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); 590 ASSERT_TRUE(CheckState(registry, id, JobState_Success));
545 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success)); 591 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success));
546 592
547 ASSERT_TRUE(registry.Cancel(id)); 593 ASSERT_TRUE(registry.Cancel(id));
548 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); 594 ASSERT_TRUE(CheckState(registry, id, JobState_Success));
549 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success)); 595 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success));
550 596
551 registry.Submit(id, new DummyJob(), 10); 597 registry.Submit(id, new DummyJob(), 10);
552 598
553 { 599 {
554 JobsRegistry::RunningJob job(registry, 0); 600 JobsRegistry::RunningJob job(registry, 0);
555 ASSERT_TRUE(job.IsValid()); 601 ASSERT_TRUE(job.IsValid());
556 ASSERT_EQ(id, job.GetId()); 602 ASSERT_EQ(id, job.GetId());
557 603
558 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success)); 604 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success));
559 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 605 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
560 606
561 job.MarkCanceled(); 607 job.MarkCanceled();
562 } 608 }
563 609
564 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Failure)); 610 ASSERT_TRUE(CheckState(registry, id, JobState_Failure));
565 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob)); 611 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob));
566 612
567 ASSERT_TRUE(registry.Resubmit(id)); 613 ASSERT_TRUE(registry.Resubmit(id));
568 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 614 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
569 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob)); 615 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob));
570 616
571 ASSERT_TRUE(registry.Pause(id)); 617 ASSERT_TRUE(registry.Pause(id));
572 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); 618 ASSERT_TRUE(CheckState(registry, id, JobState_Paused));
573 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob)); 619 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob));
574 620
575 ASSERT_TRUE(registry.Cancel(id)); 621 ASSERT_TRUE(registry.Cancel(id));
576 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Failure)); 622 ASSERT_TRUE(CheckState(registry, id, JobState_Failure));
577 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob)); 623 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob));
578 624
579 ASSERT_TRUE(registry.Resubmit(id)); 625 ASSERT_TRUE(registry.Resubmit(id));
580 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); 626 ASSERT_TRUE(CheckState(registry, id, JobState_Pending));
581 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob)); 627 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob));
582 628
583 { 629 {
584 JobsRegistry::RunningJob job(registry, 0); 630 JobsRegistry::RunningJob job(registry, 0);
585 ASSERT_TRUE(job.IsValid()); 631 ASSERT_TRUE(job.IsValid());
586 ASSERT_EQ(id, job.GetId()); 632 ASSERT_EQ(id, job.GetId());
587 633
588 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success)); 634 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success));
589 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); 635 ASSERT_TRUE(CheckState(registry, id, JobState_Running));
590 636
591 job.MarkRetry(500); 637 job.MarkRetry(500);
592 } 638 }
593 639
594 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); 640 ASSERT_TRUE(CheckState(registry, id, JobState_Retry));
595 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success)); 641 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_Success));
596 642
597 ASSERT_TRUE(registry.Cancel(id)); 643 ASSERT_TRUE(registry.Cancel(id));
598 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Failure)); 644 ASSERT_TRUE(CheckState(registry, id, JobState_Failure));
599 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob)); 645 ASSERT_TRUE(CheckErrorCode(registry, id, ErrorCode_CanceledJob));
600 } 646 }
601 647
602 648
603 649
677 723
678 engine.Stop(); 724 engine.Stop();
679 } 725 }
680 726
681 727
682 #include "../OrthancServer/ServerContext.h"
683
684 TEST(JobsSerialization, GenericValues) 728 TEST(JobsSerialization, GenericValues)
685 { 729 {
686 GenericJobUnserializer unserializer; 730 GenericJobUnserializer unserializer;
687 731
688 Json::Value s; 732 Json::Value s;
689 std::auto_ptr<JobOperationValue> value;
690 733
691 { 734 {
692 NullOperationValue null; 735 NullOperationValue null;
693 null.Serialize(s); 736 null.Serialize(s);
694 } 737 }
695 738
739 std::auto_ptr<JobOperationValue> value;
740 ASSERT_THROW(unserializer.UnserializeJob(s), OrthancException);
741 ASSERT_THROW(unserializer.UnserializeOperation(s), OrthancException);
696 value.reset(unserializer.UnserializeValue(s)); 742 value.reset(unserializer.UnserializeValue(s));
743
697 ASSERT_EQ(JobOperationValue::Type_Null, value->GetType()); 744 ASSERT_EQ(JobOperationValue::Type_Null, value->GetType());
698 745
699 { 746 {
700 StringOperationValue str("Hello"); 747 StringOperationValue str("Hello");
701 str.Serialize(s); 748 str.Serialize(s);
702 } 749 }
703 750
751 ASSERT_THROW(unserializer.UnserializeJob(s), OrthancException);
752 ASSERT_THROW(unserializer.UnserializeOperation(s), OrthancException);
704 value.reset(unserializer.UnserializeValue(s)); 753 value.reset(unserializer.UnserializeValue(s));
754
705 ASSERT_EQ(JobOperationValue::Type_String, value->GetType()); 755 ASSERT_EQ(JobOperationValue::Type_String, value->GetType());
706 ASSERT_EQ("Hello", dynamic_cast<StringOperationValue&>(*value).GetContent()); 756 ASSERT_EQ("Hello", dynamic_cast<StringOperationValue&>(*value).GetContent());
757 }
758
759
760 TEST(JobsSerialization, GenericJobs)
761 {
762 DummyUnserializer unserializer;
763
764 Json::Value s;
765
766 {
767 DummyInstancesJob job;
768 job.SetDescription("description");
769 job.AddInstance("nope");
770 job.AddInstance("ok");
771 job.SetPermissive(true);
772 job.Serialize(s);
773 }
774
775 ASSERT_THROW(unserializer.UnserializeValue(s), OrthancException);
776 ASSERT_THROW(unserializer.UnserializeOperation(s), OrthancException);
777
778 std::auto_ptr<IJob> job;
779 job.reset(unserializer.UnserializeJob(s));
707 } 780 }
708 781
709 782
710 TEST(JobsSerialization, OrthancValues) 783 TEST(JobsSerialization, OrthancValues)
711 { 784 {
728 801
729 { 802 {
730 OrthancJobUnserializer unserializer(context); 803 OrthancJobUnserializer unserializer(context);
731 804
732 Json::Value s; 805 Json::Value s;
733 std::auto_ptr<JobOperationValue> value;
734 806
735 { 807 {
736 DicomInstanceOperationValue instance(context, id); 808 DicomInstanceOperationValue instance(context, id);
737 instance.Serialize(s); 809 instance.Serialize(s);
738 } 810 }
739 811
812 std::auto_ptr<JobOperationValue> value;
740 value.reset(unserializer.UnserializeValue(s)); 813 value.reset(unserializer.UnserializeValue(s));
741 ASSERT_EQ(JobOperationValue::Type_DicomInstance, value->GetType()); 814 ASSERT_EQ(JobOperationValue::Type_DicomInstance, value->GetType());
742 ASSERT_EQ(id, dynamic_cast<DicomInstanceOperationValue&>(*value).GetId()); 815 ASSERT_EQ(id, dynamic_cast<DicomInstanceOperationValue&>(*value).GetId());
743 816
744 { 817 {