/* SchedulerService(Test) - component integration test for the scheduler Copyright (C) Lumiera.org 2023, Hermann Vosseler This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *****************************************************/ /** @file scheduler-usage-test.cpp ** unit test \ref SchedulerService_test */ #include "lib/test/run.hpp" #include "activity-detector.hpp" #include "vault/gear/scheduler.hpp" #include "lib/time/timevalue.hpp" #include "lib/format-cout.hpp" #include "lib/test/microbenchmark.hpp" #include "lib/test/diagnostic-output.hpp"///////////////TODO #include "lib/util.hpp" //#include #include using test::Test; //using std::move; //using util::isSameObject; namespace vault{ namespace gear { namespace test { // using lib::time::FrameRate; // using lib::time::Offset; using util::isnil; using lib::time::Time; using std::this_thread::sleep_for; namespace { ////////////////////////////////////////////////////////////////////TICKET #1055 want to construct lumiera Time from std::chrono literals Time t100us = Time{FSecs{1, 10'000}}; Time t200us = t100us + t100us; Time t500us = t200us + t200us + t100us; Time t1ms = Time{1,0}; const uint TYPICAL_TIME_FOR_ONE_SCHEDULE_us = 20; } /*************************************************************************//** * @test Scheduler component integration test: add and process dependent jobs. * @see SchedulerActivity_test * @see SchedulerInvocation_test * @see SchedulerCommutator_test * @see SchedulerLoadControl_test */ class SchedulerService_test : public Test { virtual void run (Arg) { // simpleUsage(); // verify_StartStop(); verify_LoadFactor(); // invokeWorkFunction(); walkingDeadline(); } /** @test TODO demonstrate a simple usage scenario * @todo WIP 10/23 ✔ define ⟶ 🔁 implement */ void simpleUsage() { BlockFlowAlloc bFlow; EngineObserver watch; Scheduler scheduler{bFlow, watch}; } /** @test get the scheduler into running state * @todo WIP 10/23 ✔ define ⟶ ✔ implement */ void verify_StartStop() { BlockFlowAlloc bFlow; EngineObserver watch; Scheduler scheduler{bFlow, watch}; CHECK (isnil (scheduler)); Activity dummy{Activity::FEED}; auto postIt = [&] { auto& schedCtx = Scheduler::ExecutionCtx::from(scheduler); schedCtx.post (RealClock::now()+t200us, &dummy, schedCtx); }; scheduler.ignite(); CHECK (isnil (scheduler)); // no start without any post() postIt(); scheduler.ignite(); CHECK (not isnil (scheduler)); scheduler.terminateProcessing(); CHECK (isnil (scheduler)); postIt(); postIt(); scheduler.ignite(); CHECK (not isnil (scheduler)); //... and just walk away => scheduler unwinds cleanly from destructor }// Note: BlockFlow and WorkForce unwinding is covered in dedicated tests /** @test TODO verify the scheduler processes and winds down automatically * when falling empty. * @todo WIP 10/23 ✔ define ⟶ 🔁 implement */ void verify_LoadFactor() { BlockFlowAlloc bFlow; EngineObserver watch; Scheduler scheduler{bFlow, watch}; CHECK (isnil (scheduler)); Activity dummy{Activity::FEED}; auto anchor = RealClock::now(); auto wuff = [&](Time when =RealClock::now()){ return _raw(when) - _raw(anchor); }; auto createLoad = [&](Offset start, uint cnt) { // use internal API (this test is declared as friend) auto& schedCtx = Scheduler::ExecutionCtx::from(scheduler); for (uint i=0; i= start and wasClose (invoked, start); }; cout << "Scheduled right away..."< up-front delay"< 500); // this proves that there was a delay to wait for the next schedule CHECK (delay_us < 1000); pullWork(); // if we now re-invoke the work-Function as instructed... CHECK (wasInvoked(start)); // then the next schedule is already slightly overdue and immediately invoked CHECK (scheduler.empty()); // the queue is empty and thus this thread will be sent to sleep CHECK (delay_us < 20200); // but beforehand the sleep-cycle is re-shuffled by a wait between 0 ... 20ms CHECK (slip_us < 300); CHECK (activity::PASS == res); // instruction to check back once pullWork(); CHECK (activity::WAIT == res); // but next call will send this thread to sleep right away CHECK (delay_us < 40); cout << "follow-up with some distance => follow-up delay"< 900); // yet this thread was afterwards kept in sleep to await the next task; CHECK (activity::PASS == res); // returns instruction to re-invoke immediately CHECK (not scheduler.empty()); // since there is still work in the queue start += t1ms; // (just re-adjust the reference point to calculate slip_us) pullWork(); // re-invoke immediately as instructed CHECK (wasInvoked(start)); // Result: also the next Activity has been dispatched CHECK (slip_us < 400); // not much slip CHECK (delay_us < 20200); // ...and the post-delay is used to re-shuffle the sleep cycle as usual CHECK (activity::PASS == res); // since queue is empty, we will call back once... CHECK (scheduler.empty()); pullWork(); CHECK (activity::WAIT == res); // and then go to sleep. cout << "already tended-next => re-target capacity"<