From 914b521d0e68c695f9814971c8e25c6197bbccea Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Tue, 11 Feb 2020 21:47:04 +0100 Subject: [PATCH] Add activity-test-wait testing test() and wait_for() for activities. Currently, only Exec are tested. Some failing tests are left commented. --- teshsuite/s4u/CMakeLists.txt | 4 +- .../activity-test-wait/activity-test-wait.cpp | 238 ++++++++++++++++++ .../activity-test-wait.tesh | 2 + 3 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 teshsuite/s4u/activity-test-wait/activity-test-wait.cpp create mode 100644 teshsuite/s4u/activity-test-wait/activity-test-wait.tesh diff --git a/teshsuite/s4u/CMakeLists.txt b/teshsuite/s4u/CMakeLists.txt index 01d3643955..d6623f6b7d 100644 --- a/teshsuite/s4u/CMakeLists.txt +++ b/teshsuite/s4u/CMakeLists.txt @@ -1,5 +1,5 @@ foreach(x actor actor-autorestart - activity-lifecycle + activity-lifecycle activity-test-wait comm-pt2pt wait-any-for cloud-interrupt-migration cloud-sharing concurrent_rw storage_client_server listen_async pid ) @@ -14,7 +14,7 @@ endforeach() ## Add the tests. ## Some need to be run with all factories, some don't need tesh to run foreach(x actor actor-autorestart - activity-lifecycle wait-any-for + activity-lifecycle activity-test-wait wait-any-for cloud-interrupt-migration concurrent_rw) set(tesh_files ${tesh_files} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh) ADD_TESH_FACTORIES(tesh-s4u-${x} "thread;ucontext;raw;boost" --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/s4u/${x} --setenv srcdir=${CMAKE_HOME_DIRECTORY}/teshsuite/s4u/${x} --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --cd ${CMAKE_BINARY_DIR}/teshsuite/s4u/${x} ${CMAKE_HOME_DIRECTORY}/teshsuite/s4u/${x}/${x}.tesh) diff --git a/teshsuite/s4u/activity-test-wait/activity-test-wait.cpp b/teshsuite/s4u/activity-test-wait/activity-test-wait.cpp new file mode 100644 index 0000000000..66c61db4a5 --- /dev/null +++ b/teshsuite/s4u/activity-test-wait/activity-test-wait.cpp @@ -0,0 +1,238 @@ +/* Copyright (c) 2010-2020. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "simgrid/s4u.hpp" + +#include + +XBT_LOG_NEW_DEFAULT_CATEGORY(s4u_test, "Messages specific for this s4u example"); + +std::vector all_hosts; + +/* Helper function easing the testing of actor's ending condition */ +static void assert_exit(bool exp_failed, double duration) +{ + double expected_time = simgrid::s4u::Engine::get_clock() + duration; + simgrid::s4u::this_actor::on_exit([exp_failed, expected_time](bool got_failed) { + xbt_assert(exp_failed == got_failed, "Exit failure status mismatch. Expected %d, got %d", exp_failed, got_failed); + xbt_assert(std::fabs(expected_time - simgrid::s4u::Engine::get_clock()) < 0.001, "Exit time mismatch. Expected %f", + expected_time); + XBT_VERB("Checks on exit successful"); + }); +} +/* Helper function in charge of running a test and doing some sanity checks afterward */ +static void run_test(const char* test_name, const std::function& test) +{ + simgrid::s4u::Actor::create(test_name, all_hosts[0], test); + simgrid::s4u::this_actor::sleep_for(10); + + /* Check that no actor remain (but on host[0], where main_dispatcher lives */ + for (unsigned int i = 0; i < all_hosts.size(); i++) { + std::vector all_actors = all_hosts[i]->get_all_actors(); + unsigned int expected_count = (i == 0) ? 1 : 0; // host[0] contains main_dispatcher, all other are empty + if (all_actors.size() != expected_count) { + XBT_CRITICAL("Host %s contains %zu actors but %u are expected (i=%u). Existing actors: ", + all_hosts[i]->get_cname(), all_actors.size(), expected_count, i); + for (auto act : all_actors) + XBT_CRITICAL(" - %s", act->get_cname()); + xbt_die("This is wrong"); + } + } + xbt_assert("TODO: Check that all LMM are empty"); + XBT_INFO("SUCCESS: %s", test_name); + XBT_INFO("#########################################################################################################"); +} + +/** + ** Each tests + **/ + +// Calls exec->test() and returns its result +static bool tester_test(simgrid::s4u::ExecPtr& exec) +{ + return exec->test(); +} + +// Calls exec->wait_for(Duration * 0.0125) and returns true when exec is terminated, just like test() +template bool tester_wait(simgrid::s4u::ExecPtr& exec) +{ + bool ret; + try { + exec->wait_for(Duration * 0.0125); + ret = true; + } catch (const simgrid::TimeoutException&) { + ret = false; + } catch (const simgrid::Exception&) { + ret = true; + } + return ret; +} + +using tester_type = decltype(tester_test); + +template void test_trivial() +{ + XBT_INFO("%s: Launch a execute(5s), and let it proceed before test", __func__); + + simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() { + assert_exit(false, 6.); + auto exec = simgrid::s4u::this_actor::exec_async(500000000); + simgrid::s4u::this_actor::sleep_for(6.0); + xbt_assert(Test(exec), "exec should be terminated now"); + }); + exec5->join(); +} + +template void test_basic() +{ + XBT_INFO("%s: Launch a execute(5s), and test while it proceeds", __func__); + + simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() { + assert_exit(false, 6.); + auto exec = simgrid::s4u::this_actor::exec_async(500000000); + for (int i = 0; i < 3; i++) { + xbt_assert(not Test(exec), "exec finished too soon (i = %d)!?", i); + simgrid::s4u::this_actor::sleep_for(2.0); + } + xbt_assert(Test(exec), "exec should be terminated now"); + }); + exec5->join(); +} + +template void test_cancel() +{ + XBT_INFO("%s: Launch a execute(5s), and cancel it after 2s", __func__); + + simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], []() { + assert_exit(false, 2.); + auto exec = simgrid::s4u::this_actor::exec_async(500000000); + simgrid::s4u::this_actor::sleep_for(2.0); + exec->cancel(); + xbt_assert(Test(exec), "exec should be terminated now"); + }); + exec5->join(); +} + +template void test_failure_actor_sleep() +{ + XBT_INFO("%s: Launch a execute(5s), and kill running actor after 2s (sleep during exec)", __func__); + + simgrid::s4u::ExecPtr exec; + simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&exec]() { + assert_exit(true, 2.); + exec = simgrid::s4u::this_actor::exec_async(500000000); + simgrid::s4u::this_actor::sleep_for(6.0); + XBT_ERROR("should not be here!"); + }); + simgrid::s4u::this_actor::sleep_for(2.0); + xbt_assert(not Test(exec), "exec finished too soon!?"); + exec5->kill(); + xbt_assert(Test(exec), "exec should be terminated now"); +} + +template void test_failure_host_sleep() +{ + XBT_INFO("%s: Launch a execute(5s), and shutdown host 2s (sleep during exec)", __func__); + + simgrid::s4u::ExecPtr exec; + simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&exec]() { + assert_exit(true, 2.); + exec = simgrid::s4u::this_actor::exec_async(500000000); + simgrid::s4u::this_actor::sleep_for(6.0); + XBT_ERROR("should not be here!"); + }); + simgrid::s4u::this_actor::sleep_for(2.0); + xbt_assert(not Test(exec), "exec finished too soon!?"); + exec5->get_host()->turn_off(); + exec5->get_host()->turn_on(); + xbt_assert(Test(exec), "exec should be terminated now"); +} + +template void test_failure_actor_wait() +{ + XBT_INFO("%s: Launch a execute(5s), and kill running actor after 2s (wait for exec)", __func__); + + simgrid::s4u::ExecPtr exec; + simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&exec]() { + assert_exit(true, 2.); + exec = simgrid::s4u::this_actor::exec_async(500000000); + exec->wait(); + XBT_ERROR("should not be here!"); + }); + simgrid::s4u::this_actor::sleep_for(2.0); + xbt_assert(not Test(exec), "exec finished too soon!?"); + exec5->kill(); + xbt_assert(Test(exec), "exec should be terminated now"); +} + +template void test_failure_host_wait() +{ + XBT_INFO("%s: Launch a execute(5s), and shutdown host 2s (wait for exec)", __func__); + + simgrid::s4u::ExecPtr exec; + simgrid::s4u::ActorPtr exec5 = simgrid::s4u::Actor::create("exec5", all_hosts[1], [&exec]() { + assert_exit(true, 2.); + exec = simgrid::s4u::this_actor::exec_async(500000000); + exec->wait(); + XBT_ERROR("should not be here!"); + }); + simgrid::s4u::this_actor::sleep_for(2.0); + xbt_assert(not Test(exec), "exec finished too soon!?"); + exec5->get_host()->turn_off(); + exec5->get_host()->turn_on(); + xbt_assert(Test(exec), "exec should be terminated now"); +} + +/* We need an extra actor here, so that it can sleep until the end of each test */ +static void main_dispatcher() +{ + XBT_INFO("***** Using *****"); + run_test("exec and test once", test_trivial); + run_test("exec and test many", test_basic); + run_test("cancel and test", test_cancel); + // run_test("actor failure and test / sleep", test_failure_actor_sleep); + // run_test("host failure and test / sleep", test_failure_host_sleep); + run_test("actor failure and test / wait", test_failure_actor_wait); + run_test("host failure and test / wait", test_failure_host_wait); + + XBT_INFO("***** Using > *****"); + run_test("exec and wait<0> once", test_trivial>); + // run_test("exec and wait<0> many", test_basic>); + run_test("cancel and wait<0>", test_cancel>); + // run_test("actor failure and wait<0> / sleep", test_failure_actor_sleep>); + // run_test("host failure and wait<0> / sleep", test_failure_host_sleep>); + // run_test("actor failure and wait<0> / wait", test_failure_actor_wait>); + // run_test("host failure and wait<0> / wait", test_failure_host_wait>); + + XBT_INFO("***** Using > *****"); + run_test("exec and wait<1> once", test_trivial>); + // run_test("exec and wait<1> many", test_basic>); + run_test("cancel and wait<1>", test_cancel>); + // run_test("actor failure and wait<1> / sleep", test_failure_actor_sleep>); + // run_test("host failure and wait<1> / sleep", test_failure_host_sleep>); + // run_test("actor failure and wait<1> / wait", test_failure_actor_wait>); + // run_test("host failure and wait<1> / wait", test_failure_host_wait>); +} + +int main(int argc, char* argv[]) +{ + simgrid::s4u::Engine e(&argc, argv); + + const char* platf = argv[1]; + if (argc <= 1) { + XBT_WARN("No platform file provided. Using './testing_platform.xml'"); + platf = "./testing_platform.xml"; + } + e.load_platform(platf); + + all_hosts = e.get_all_hosts(); + simgrid::s4u::Actor::create("main_dispatcher", all_hosts[0], main_dispatcher); + + e.run(); + + XBT_INFO("Simulation done"); + + return 0; +} diff --git a/teshsuite/s4u/activity-test-wait/activity-test-wait.tesh b/teshsuite/s4u/activity-test-wait/activity-test-wait.tesh new file mode 100644 index 0000000000..0952e520c8 --- /dev/null +++ b/teshsuite/s4u/activity-test-wait/activity-test-wait.tesh @@ -0,0 +1,2 @@ +! output display +$ ${bindir:=.}/activity-test-wait ${srcdir:=.}/../activity-lifecycle/testing_platform.xml -- 2.20.1