/* ACTIVITY-DETECTOR.hpp - test scaffolding to observe activities within 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 activity-detector.hpp ** Diagnostic setup to instrument and observe \ref Activity activations. ** The [Scheduler](\ref scheduler.hpp) powering the Lumiera render engine ** is implemented in terms of Activities, which can be time-bound and depend ** on each other. For performance reasons, these _operational atoms_ must be ** implemented as a tightly knit network of lightweight POD records without ** much indirection. This setup poses a challenge for unit tests and similar ** white box testing, due to the lack of a managed platform and any further ** means of indirection and extension. As a remedy, a set of preconfigured ** _detector Activity records_ is provided, which drop off event log messages ** by side effect. These detector probes can be wired in as decorators into ** an otherwise valid Activity-Term, allowing to watch and verify patterns ** of invocation. ** ** # Usage ** ** An ActivityDetector instance can be created in local storage to get an arsenal ** of probing tools and detectors, which are internally wired to record activation ** into an lib::test::EventLog embedded into the ActivityDetector instance. A ** _verification DSL_ is provided, internally relying on the building blocks and ** the chained-search mechanism known from the EventLog. To distinguish similar ** invocations and activations, a common _sequence number_ is maintained within ** the ActivityDetector instance, which can be incremented explicitly. All ** relevant events also capture the current sequence number as an attribute ** of the generated log record. ** ** ## Observation tools ** - ActivityDetector::buildDiadnosticFun(id) generates a functor object with ** _arbitrary signature,_ which records any invocation and arguments. ** The corresponding verification matcher is #verifyInvocation(id) ** - ActivityDetector::buildMockJobFunctor(id) a JobFunctor implementation ** suitably rigged to record invocations and arguments ** - ActivityDetector::buildActivationProbe a debugging Activity to record activation ** - ActivityDetector::insertActivationTap hooks this Activation-Probe before an ** existing Activity-connection, so that passing on the activation can be detected ** - ActivityDetector::watchGate rig a `GATE` activity by prepending and appending ** an Activation-Probe, so that both incoming and outgoing activations can be traced ** - ActivityDetector::executionCtx test setup of the execution environment abstraction ** for performing chains of Activities; it provides the expected λ-functions as ** instances of ActivityDetctor::DiagnosticFun, so that any invocation is recorded ** ** @see SchedulerActivity_test ** @see ActivityDetector_test ** @see EventLog_test (demonstration of EventLog capbabilities) */ #ifndef VAULT_GEAR_TEST_ACTIVITY_DETECTOR_H #define VAULT_GEAR_TEST_ACTIVITY_DETECTOR_H #include "vault/common.hpp" #include "lib/test/test-helper.hpp" #include "lib/test/event-log.hpp" #include "vault/gear/job.h" #include "vault/gear/activity.hpp" #include "vault/gear/nop-job-functor.hpp" #include "lib/time/timevalue.hpp" #include "lib/meta/variadic-helper.hpp" #include "lib/meta/function.hpp" #include "lib/wrapper.hpp" #include "lib/format-util.hpp" #include "lib/util.hpp" #include #include #include #include namespace vault{ namespace gear { namespace test { using std::string; using std::function; using lib::time::TimeValue; using lib::time::Time; using lib::time::FSecs; using lib::time::Offset; using lib::meta::RebindVariadic; using util::isnil; using std::forward; using std::move; namespace {// Diagnostic markers const string MARK_INC{"IncSeq"}; const string MARK_SEQ{"Seq"}; using SIG_JobDiagnostic = void(Time, int32_t); const size_t JOB_ARG_POS_TIME = 0; const string CTX_POST{"CTX-post"}; const string CTX_WORK{"CTX-work"}; const string CTX_DONE{"CTX-done"}; const string CTX_TICK{"CTX-tick"}; Offset POLL_WAIT_DELAY{FSecs(1)}; Time SCHED_TIME_MARKER{555,5}; ///< marker value for "current scheduler time" used in tests } class ActivityDetector; /** * @internal ongoing evaluation and match of observed activities. * @remark this temporary object provides a builder API for creating * chained verifications, similar to the usage of lib::test::EventLog. * Moreover, it is convertible to `bool` to retrieve the verification result. */ class ActivityMatch : private lib::test::EventMatch { using _Parent = lib::test::EventMatch; ActivityMatch (lib::test::EventMatch&& matcher) : _Parent{move (matcher)} { } friend class ActivityDetector; public: // standard copy acceptable /** final evaluation of the verification query, * usually triggered from the unit test `CHECK()`. * @note failure cause is printed to STDERR. */ operator bool() const { return _Parent::operator bool(); } /* query builder(s) to find a match stepping forwards */ ActivityMatch& beforeInvocation (string match) { return delegate (&EventMatch::beforeCall, move(match)); } // more here... /* query builders to find a match stepping backwards */ ActivityMatch& afterInvocation (string match) { return delegate (&EventMatch::afterCall, move(match)); } // more here... /** qualifier: additionally match the function arguments */ template ActivityMatch& arg (ARGS const& ...args) { return delegate (&EventMatch::arg, args...); } /** qualifier: additionally require the indicated sequence number */ ActivityMatch& seq (uint seqNr) { _Parent::attrib (MARK_SEQ, util::toString (seqNr)); return *this; } /** special query to match an increment of the sequence number */ ActivityMatch& beforeSeqIncrement (uint seqNr) { _Parent::beforeEvent(MARK_INC, util::toString(seqNr)); return *this; } ActivityMatch& afterSeqIncrement (uint seqNr) { _Parent::afterEvent(MARK_INC, util::toString(seqNr)); return *this; } /** qualifier: additionally match the nominal time argument of JobFunctor invocation */ ActivityMatch& timeArg (Time const& time) { return delegate (&EventMatch::argPos