361{
362 std::cout << "SystemRoutinesSynchronizationTest" << std::endl;
368 std::shared_ptr<Horizon::System::RuntimeRoutineContext> routine_1 = std::make_shared<Horizon::System::RuntimeRoutineContext>(srm,
Horizon::System::RUNTIME_SYNC_NONE);
374
375 auto work = std::make_shared<TestWork>(routine_1);
376 routine_1->get_runtime_synchronization_mutex().lock();
378 routine_1->get_runtime_synchronization_mutex().unlock();
379 work->execute();
380 while(!work->has_result())
381 {
382 std::this_thread::sleep_for(std::chrono::seconds(1));
383 };
384 auto work2 = std::make_shared<TestWorkWithUseResult>(routine_1);
385
386 routine_1->get_runtime_synchronization_mutex().lock();
388 routine_1->get_runtime_synchronization_mutex().unlock();
389 auto work3 = std::make_shared<TestWork>(routine_1);
390 routine_1->get_runtime_synchronization_mutex().lock();
392 routine_1->get_runtime_synchronization_mutex().unlock();
393 auto work4 = std::make_shared<TestWork>(routine_1);
394 routine_1->get_runtime_synchronization_mutex().lock();
396 routine_1->get_runtime_synchronization_mutex().unlock();
397
398 routine_1->push(work2);
399 routine_1->push(work3);
400 routine_1->push(work4);
401
402 auto work_gl_1 = std::make_shared<TestWorkWithUseResult>(routine_2);
403 routine_2->get_runtime_synchronization_mutex().lock();
405 work_gl_1->set_previous_result(work->get_result());
406 routine_2->get_runtime_synchronization_mutex().unlock();
407
408 routine_2->push(work_gl_1);
409
410 auto work_p_1 = std::make_shared<TestWorkWithUseResult>(routine_3);
411 routine_3->get_runtime_synchronization_mutex().lock();
413 work_p_1->set_previous_result(work->get_result());
414 routine_3->get_runtime_synchronization_mutex().unlock();
415
416 routine_3->push(work_p_1);
417
418 auto work_s_1 = std::make_shared<TestWorkWithUseResult>(routine_4);
419 routine_4->get_runtime_synchronization_mutex().lock();
421 work_s_1->set_previous_result(work->get_result());
422 routine_4->get_runtime_synchronization_mutex().unlock();
423
424 routine_4->push(work_s_1);
425
426 auto work_n_1 = std::make_shared<TestWorkWithUseResult>(routine_5);
427 routine_5->get_runtime_synchronization_mutex().lock();
429 work_n_1->set_previous_result(work->get_result());
430 routine_5->get_runtime_synchronization_mutex().unlock();
431
432 routine_5->push(work_n_1);
433
434 srm.push(routine_1);
435 srm.push(routine_2);
436 srm.push(routine_3);
437 srm.push(routine_4);
438 srm.push(routine_5);
439
440 std::thread thread_1 = std::thread([&](){
441 srm.process_queue();
442 });
443
444 std::thread thread_2 = std::thread([&]() {
445 while(!work_gl_1->has_result()) { srm_gl.process_queue(); }
446 });
447
448 std::thread thread_3 = std::thread([&]() {
449 routine_3->get_control_agent().cancel();
451 srm_p.process_queue();
452 }
453 });
454
455 std::thread thread_4 = std::thread([&]() {
456 routine_4->get_control_agent().cancel();
458 srm_s.process_queue();
459 }
460 });
461
462 std::thread thread_5 = std::thread([&]() {
463 while(!work_n_1->has_result()) { srm_n.process_queue(); }
464 });
465
466 while (!(work->has_result() && work2->has_result() && work3->has_result() && work4->has_result() && work_gl_1->has_result() && work_n_1->has_result()))
467 {
468 std::cout << "work->has_result() : " << work->has_result() << std::endl;
469 std::cout << "work2->has_result() : " << work2->has_result() << std::endl;
470 std::cout << "work3->has_result() : " << work3->has_result() << std::endl;
471 std::cout << "work4->has_result() : " << work4->has_result() << std::endl;
472 std::cout << "work_gl_1->has_result() : " << work_gl_1->has_result() << std::endl;
473 std::cout << "work_n_1->has_result() : " << work_n_1->has_result() << std::endl;
474 std::this_thread::sleep_for(std::chrono::seconds(1));
475 };
476
480 {
481 std::cout << "routine_1 context result : " << routine_1->get_context_result() << std::endl;
482 std::cout << "routine_2 context result : " << routine_2->get_context_result() << std::endl;
483 std::cout << "routine_3 context result : " << routine_3->get_context_result() << std::endl;
484 std::this_thread::sleep_for(std::chrono::seconds(1));
485 };
486
490
491
492
493
495 {
496
497 std::cout << "routine_5 context result : " << routine_5->get_context_result() << std::endl;
498 std::this_thread::sleep_for(std::chrono::seconds(1));
499 };
500
501 routine_1->get_runtime_synchronization_mutex().lock();
502 BOOST_CHECK_EQUAL(work->get_result().get_one(), 400);
503 BOOST_CHECK_EQUAL(work2->get_result().get_one(), 600);
504 BOOST_CHECK_EQUAL(work3->get_result().get_one(), 800);
505 BOOST_CHECK_EQUAL(work4->get_result().get_one(), 1000);
506 routine_1->get_runtime_synchronization_mutex().unlock();
507
508 routine_2->get_runtime_synchronization_mutex().lock();
509 BOOST_CHECK_EQUAL(work_gl_1->get_result().get_one(), 1000);
510 routine_2->get_runtime_synchronization_mutex().unlock();
511
512 BOOST_CHECK_EQUAL(work_s_1->has_result(), false);
513
514 routine_5->get_runtime_synchronization_mutex().lock();
515 BOOST_CHECK_EQUAL(work_n_1->get_result().get_one(), 800);
516 routine_5->get_runtime_synchronization_mutex().unlock();
517
518 thread_1.join();
519 thread_2.join();
520 thread_3.join();
521 thread_4.join();
522 thread_5.join();
523}
@ RUNTIME_CONTEXT_FAIL
Definition: System.hpp:108
@ RUNTIME_CONTEXT_PASS
Definition: System.hpp:109
@ RUNTIME_CONTEXT_NO_STATE
Definition: System.hpp:107
@ RUNTIME_WORK_QUEUE_CANCELLED
Definition: System.hpp:69
@ RUNTIME_NETWORKING
Definition: System.hpp:84
@ RUNTIME_SCRIPTVM
Definition: System.hpp:87