diff --git a/tests/AlarmActorTest.cpp b/tests/AlarmActorTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e83ac8ec6762e12a508e40e1c52a1b1de92a037f --- /dev/null +++ b/tests/AlarmActorTest.cpp @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright © 2020 Florian Fischer +#include <cstdlib> // for exit, EXIT_FAILURE, EXIT_SUC... +#include <iostream> // for operator<<, basic_ostream + +#include "Actor.hpp" // for Actor +#include "BinaryPrivateSemaphore.hpp" // for BPS +#include "CountingPrivateSemaphore.hpp" // for CPS +#include "Fiber.hpp" // for Fiber +#include "Runtime.hpp" // for Runtime +#include "emper.hpp" // for spawn + +class AlarmActor : public Actor<BPS*> { + protected: + void receive(BPS* sem) override { sem->signal(); } + + public: + AlarmActor(Runtime& runtime) : Actor(runtime) {} + void stop() { Actor::stop(); } +}; + +auto main(int argc, char* argv[]) -> int { + unsigned int sleeper_count = 10; + unsigned int sleeps = 1000; + + if (argc > 3) { + std::cerr << "Usage: " << argv[0] << " [fiber count] [block count]" << std::endl; + exit(EXIT_FAILURE); + } + + const int DECIMAL = 10; + if (argc > 1) { + sleeper_count = strtol(argv[1], nullptr, DECIMAL); + } + + if (argc > 2) { + sleeps = strtol(argv[2], nullptr, DECIMAL); + } + + Runtime runtime; + + AlarmActor alarmActor(runtime); + alarmActor.start(); + + Fiber* fiber = Fiber::from([&] { + CPS cps; + for (unsigned int i = 0; i < sleeper_count; ++i) { + spawn( + [&alarmActor, &sleeps] { + for (unsigned int i = 1; i <= sleeps; ++i) { + BPS sem; + alarmActor.tell(&sem); + sem.wait(); + } + }, + cps); + } + + // Wait for the sleeping fibers to finish + cps.wait(); + + // Wait for the actor to become idle. + bool actorIdle = alarmActor.waitUntilIdle(60 * 1000); + if (!actorIdle) { + std::cerr << "FAILURE: Actor did not went idle"; + exit(EXIT_FAILURE); + } + + alarmActor.stop(); + + exit(EXIT_SUCCESS); + }); + + runtime.schedule(*fiber); + + runtime.waitUntilFinished(); + + return EXIT_FAILURE; +} diff --git a/tests/meson.build b/tests/meson.build index e1c2f709cb7c25948cb176c905825b400a8ca4d2..f90f264a019772805412207a1239a41bc4023a0d 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -29,6 +29,11 @@ tests = { 'description': 'Simple Actor Test', }, + 'AlarmActorTest.cpp': + { + 'description': 'Use an Actor to unblock fibers using BPS', + }, + 'SimpleLawsTest.cpp': { 'description': 'Simple LAWS scheduling strategy test',