2017-01-11 04:19:43 +01:00
|
|
|
/*
|
|
|
|
|
SessionCommandFunction(Test) - function test of command dispatch via SessionCommand facade
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2017, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2017-01-11 05:01:47 +01:00
|
|
|
extern "C" {
|
|
|
|
|
#include "common/interfaceregistry.h"
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-11 04:19:43 +01:00
|
|
|
#include "proc/control/proc-dispatcher.hpp"
|
|
|
|
|
#include "proc/control/command-def.hpp"
|
|
|
|
|
#include "gui/ctrl/command-handler.hpp"
|
2017-01-13 08:01:37 +01:00
|
|
|
#include "gui/interact/invocation-trail.hpp"
|
2017-01-14 04:19:58 +01:00
|
|
|
#include "backend/thread-wrapper.hpp"
|
|
|
|
|
#include "lib/typed-counter.hpp"
|
2017-01-13 08:42:09 +01:00
|
|
|
//#include "lib/format-cout.hpp" //////////TODO
|
2017-01-11 04:19:43 +01:00
|
|
|
#include "lib/symbol.hpp"
|
2017-01-11 05:01:47 +01:00
|
|
|
#include "lib/util.hpp"
|
2017-01-11 04:19:43 +01:00
|
|
|
|
2017-01-14 04:19:58 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
|
#include <vector>
|
2017-01-11 05:01:47 +01:00
|
|
|
#include <string>
|
2017-01-11 04:19:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace proc {
|
|
|
|
|
namespace control {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// using std::function;
|
|
|
|
|
// using std::rand;
|
2017-01-14 04:19:58 +01:00
|
|
|
using boost::lexical_cast;
|
2017-01-11 04:19:43 +01:00
|
|
|
using lib::test::randTime;
|
2017-01-13 08:01:37 +01:00
|
|
|
using gui::interact::InvocationTrail;
|
2017-01-11 04:19:43 +01:00
|
|
|
using gui::ctrl::CommandHandler;
|
2017-01-13 08:01:37 +01:00
|
|
|
using lib::diff::GenNode;
|
2017-01-11 05:26:53 +01:00
|
|
|
using lib::diff::Rec;
|
2017-01-11 04:19:43 +01:00
|
|
|
using lib::time::Time;
|
|
|
|
|
using lib::time::TimeVar;
|
|
|
|
|
using lib::time::Duration;
|
|
|
|
|
using lib::time::Offset;
|
2017-01-14 04:19:58 +01:00
|
|
|
using lib::time::FSecs;
|
|
|
|
|
using lib::FamilyMember;
|
2017-01-11 04:19:43 +01:00
|
|
|
using lib::Symbol;
|
2017-01-11 05:01:47 +01:00
|
|
|
using util::isnil;
|
|
|
|
|
using std::string;
|
2017-01-14 04:19:58 +01:00
|
|
|
using std::vector;
|
2017-01-11 04:19:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace { // test fixture...
|
|
|
|
|
|
2017-01-14 04:19:58 +01:00
|
|
|
/* === parameters for multi-threaded stress test === */
|
|
|
|
|
|
|
|
|
|
uint NUM_THREADS_DEFAULT = 20; ///< @note _not_ const, can be overridden by command line argument
|
|
|
|
|
uint NUM_INVOC_PER_THRED = 10;
|
|
|
|
|
uint MAX_RAND_DELAY_ms = 10;
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
maybeOverride (uint& configSetting, Arg cmdline, uint paramNr)
|
|
|
|
|
{
|
|
|
|
|
if (paramNr < cmdline.size())
|
|
|
|
|
configSetting = lexical_cast<uint>(cmdline[paramNr]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-11 04:19:43 +01:00
|
|
|
/* === mock operation to be dispatched as command === */
|
|
|
|
|
|
|
|
|
|
const Symbol COMMAND_ID{"test.dispatch.function.command"};
|
2017-01-13 08:42:09 +01:00
|
|
|
const Symbol COMMAND_I1{"test.dispatch.function.command.instance-1"};
|
|
|
|
|
const Symbol COMMAND_I2{"test.dispatch.function.command.instance-2"};
|
2017-01-11 04:19:43 +01:00
|
|
|
|
2017-01-11 05:26:53 +01:00
|
|
|
TimeVar testCommandState = randTime();
|
2017-01-11 04:19:43 +01:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
operate (Duration dur, Offset offset, int factor)
|
|
|
|
|
{
|
2017-01-11 06:09:34 +01:00
|
|
|
testCommandState += Offset(dur) + offset*factor;
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-13 01:10:05 +01:00
|
|
|
Time
|
2017-01-11 04:19:43 +01:00
|
|
|
capture (Duration, Offset, int)
|
|
|
|
|
{
|
2017-01-11 05:26:53 +01:00
|
|
|
return testCommandState;
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2017-01-13 01:10:05 +01:00
|
|
|
undoIt (Duration, Offset, int, Time oldState)
|
2017-01-11 04:19:43 +01:00
|
|
|
{
|
2017-01-13 08:01:37 +01:00
|
|
|
testCommandState = oldState;
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}//(End) test fixture
|
|
|
|
|
|
2017-01-14 04:19:58 +01:00
|
|
|
|
2017-01-11 05:26:53 +01:00
|
|
|
#define __DELAY__ usleep(10000);
|
2017-01-11 04:19:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************************//**
|
|
|
|
|
* @test verify integrated functionality of command dispatch through the SessionCommand facade.
|
|
|
|
|
* - operate lifecycle of the supporting components,
|
|
|
|
|
* similar to activating the »session subsystem«
|
|
|
|
|
* - generate command messages similar to what is received from the UI-Bus
|
|
|
|
|
* - us the handler mechanism from gui::ctrl::CoreService to talk to the facade
|
|
|
|
|
* - have a specially rigged command function to observe invocation
|
|
|
|
|
* - wait for the session loop thread to dispatch this command
|
|
|
|
|
* - verify that commands are really executed single-threaded
|
|
|
|
|
*
|
|
|
|
|
* @see proc::SessionSubsystem
|
|
|
|
|
* @see ProcDispatcher
|
|
|
|
|
* @see CommandQueue_test
|
|
|
|
|
* @see AbstractTangible_test::invokeCommand()
|
|
|
|
|
*/
|
|
|
|
|
class SessionCommandFunction_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
//------------------FIXTURE
|
|
|
|
|
public:
|
|
|
|
|
SessionCommandFunction_test()
|
|
|
|
|
{
|
|
|
|
|
CommandDef (COMMAND_ID)
|
|
|
|
|
.operation (operate)
|
|
|
|
|
.captureUndo (capture)
|
|
|
|
|
.undoOperation (undoIt)
|
|
|
|
|
;
|
2017-01-13 08:42:09 +01:00
|
|
|
Command(COMMAND_ID).storeDef(COMMAND_I1);
|
|
|
|
|
Command(COMMAND_ID).storeDef(COMMAND_I2);
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
|
|
|
|
~SessionCommandFunction_test()
|
|
|
|
|
{
|
|
|
|
|
Command::remove (COMMAND_ID);
|
2017-01-13 08:42:09 +01:00
|
|
|
Command::remove (COMMAND_I1);
|
|
|
|
|
Command::remove (COMMAND_I2);
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
|
|
|
|
//-------------(End)FIXTURE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
2017-01-14 04:19:58 +01:00
|
|
|
run (Arg args_for_stresstest)
|
2017-01-11 04:19:43 +01:00
|
|
|
{
|
2017-01-11 05:01:47 +01:00
|
|
|
lumiera_interfaceregistry_init();
|
|
|
|
|
lumiera::throwOnError();
|
|
|
|
|
|
2017-01-11 04:19:43 +01:00
|
|
|
startDispatcher();
|
2017-01-11 05:26:53 +01:00
|
|
|
perform_simpleInvocation();
|
2017-01-13 08:42:09 +01:00
|
|
|
perform_messageInvocation();
|
2017-01-14 04:19:58 +01:00
|
|
|
perform_massivelyParallel(args_for_stresstest);
|
2017-01-11 04:19:43 +01:00
|
|
|
stopDispatcher();
|
2017-01-11 05:01:47 +01:00
|
|
|
|
|
|
|
|
lumiera_interfaceregistry_destroy();
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-14 04:19:58 +01:00
|
|
|
/** @test start the session loop thread,
|
|
|
|
|
* similar to what the »session subsystem« does
|
2017-01-11 05:01:47 +01:00
|
|
|
* @note we are _not_ actually starting the subsystem
|
|
|
|
|
* @see facade.cpp
|
|
|
|
|
*/
|
2017-01-11 04:19:43 +01:00
|
|
|
void
|
|
|
|
|
startDispatcher()
|
|
|
|
|
{
|
2017-01-11 05:01:47 +01:00
|
|
|
CHECK (not ProcDispatcher::instance().isRunning());
|
|
|
|
|
|
|
|
|
|
ProcDispatcher::instance().start ([&] (string* problemMessage)
|
|
|
|
|
{
|
|
|
|
|
CHECK (isnil (*problemMessage));
|
|
|
|
|
thread_has_ended = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
CHECK (ProcDispatcher::instance().isRunning());
|
|
|
|
|
CHECK (not thread_has_ended);
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
2017-01-11 05:01:47 +01:00
|
|
|
bool thread_has_ended{false};
|
2017-01-11 04:19:43 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
stopDispatcher()
|
|
|
|
|
{
|
2017-01-11 05:01:47 +01:00
|
|
|
CHECK (ProcDispatcher::instance().isRunning());
|
|
|
|
|
ProcDispatcher::instance().requestStop();
|
|
|
|
|
|
2017-01-11 05:26:53 +01:00
|
|
|
__DELAY__
|
2017-01-11 05:01:47 +01:00
|
|
|
CHECK (not ProcDispatcher::instance().isRunning());
|
|
|
|
|
CHECK (thread_has_ended);
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
perform_simpleInvocation()
|
|
|
|
|
{
|
2017-01-13 08:42:09 +01:00
|
|
|
string cmdID {COMMAND_I1};
|
2017-01-11 05:26:53 +01:00
|
|
|
Rec arguments {Duration(15,10), Time(500,0), -1};
|
|
|
|
|
|
2017-01-13 08:42:09 +01:00
|
|
|
CHECK (not Command(COMMAND_I1).canExec());
|
2017-01-11 05:26:53 +01:00
|
|
|
SessionCommand::facade().bindArg (cmdID, arguments);
|
2017-01-13 08:42:09 +01:00
|
|
|
CHECK (Command(COMMAND_I1).canExec());
|
2017-01-11 05:26:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
Time prevState = testCommandState;
|
|
|
|
|
SessionCommand::facade().invoke(cmdID);
|
|
|
|
|
|
|
|
|
|
__DELAY__
|
2017-01-13 08:01:37 +01:00
|
|
|
CHECK (testCommandState - prevState == Time(0, 1)); // execution added 1500ms -1*500ms == 1sec
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test invoke a command in the same way as CoreService does
|
|
|
|
|
* when handling command messages from the UI-Bus
|
|
|
|
|
* - use the help of an InvocationTrail, similar to what the
|
|
|
|
|
* [generic UI element](\ref gui::model::Tangible) does
|
|
|
|
|
* - generate a argument binding message
|
|
|
|
|
* - generate a "bang!" message
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
perform_messageInvocation()
|
|
|
|
|
{
|
2017-01-13 08:01:37 +01:00
|
|
|
// this happens "somewhere" in the UI interaction control framework
|
2017-01-13 08:42:09 +01:00
|
|
|
InvocationTrail invoTrail{Command(COMMAND_I2)};
|
2017-01-13 08:01:37 +01:00
|
|
|
|
|
|
|
|
// this happens within some tangible UI element (widget / controller)
|
2017-01-13 08:42:09 +01:00
|
|
|
GenNode argumentBindingMessage = invoTrail.bindMsg (Rec {Duration(25,10), Time(500,0), -2});
|
2017-01-13 08:01:37 +01:00
|
|
|
GenNode commandTriggerMessage = invoTrail.bangMsg ();
|
2017-01-13 08:42:09 +01:00
|
|
|
CHECK (argumentBindingMessage.idi.getSym() == string{COMMAND_I2});
|
|
|
|
|
CHECK (commandTriggerMessage.idi.getSym() == string{COMMAND_I2});
|
|
|
|
|
CHECK (not Command::canExec(COMMAND_I2));
|
2017-01-13 08:01:37 +01:00
|
|
|
|
|
|
|
|
// this happens, when CoreService receives command messages from UI-Bus
|
|
|
|
|
CommandHandler handler1{argumentBindingMessage};
|
|
|
|
|
argumentBindingMessage.data.accept(handler1); // handler is a visitor for the message payload
|
|
|
|
|
|
2017-01-13 08:42:09 +01:00
|
|
|
CHECK (Command::canExec(COMMAND_I2));
|
|
|
|
|
CHECK (not Command::canUndo(COMMAND_I2));
|
2017-01-13 08:01:37 +01:00
|
|
|
Time prevState = testCommandState;
|
|
|
|
|
|
|
|
|
|
// now handling the message to trigger execution
|
|
|
|
|
CommandHandler handler2{commandTriggerMessage};
|
|
|
|
|
commandTriggerMessage.data.accept(handler2);
|
|
|
|
|
|
|
|
|
|
__DELAY__
|
2017-01-13 08:42:09 +01:00
|
|
|
CHECK (Command::canUndo(COMMAND_I2));
|
2017-01-13 08:01:37 +01:00
|
|
|
CHECK (testCommandState - prevState == Time(500, 1)); // execution added 2500ms -2*500ms == 1.5sec
|
2017-01-11 04:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test verify that commands are properly enqueued
|
|
|
|
|
* and executed one by one
|
|
|
|
|
* - create several threads to send random command messages
|
|
|
|
|
* - verify that, after executing all commands, the internal
|
|
|
|
|
* state variable reflects the result of a proper
|
|
|
|
|
* sequential calculation and summation
|
|
|
|
|
*/
|
|
|
|
|
void
|
2017-01-14 04:19:58 +01:00
|
|
|
perform_massivelyParallel(Arg args_for_stresstest)
|
2017-01-11 04:19:43 +01:00
|
|
|
{
|
2017-01-14 04:19:58 +01:00
|
|
|
maybeOverride(NUM_THREADS_DEFAULT, args_for_stresstest, 1);
|
|
|
|
|
maybeOverride(NUM_INVOC_PER_THRED, args_for_stresstest, 2);
|
|
|
|
|
maybeOverride(MAX_RAND_DELAY_ms, args_for_stresstest, 3);
|
|
|
|
|
|
|
|
|
|
class InvocationProducer
|
|
|
|
|
: backend::ThreadJoinable
|
|
|
|
|
{
|
|
|
|
|
FamilyMember<InvocationProducer> id_;
|
|
|
|
|
string id_buffer_{COMMAND_ID + ".thread-"+id_};
|
|
|
|
|
Symbol cmdID_{cStr(id_buffer_)};
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
fabricateCommands()
|
|
|
|
|
{
|
|
|
|
|
syncPoint();
|
|
|
|
|
InvocationTrail invoTrail{Command(cmdID_)};
|
|
|
|
|
|
|
|
|
|
for (uint i=0; i<NUM_INVOC_PER_THRED; ++i)
|
|
|
|
|
{
|
|
|
|
|
__randomDelay();
|
|
|
|
|
sendCommandMessage (invoTrail.bindMsg (Rec {Duration(7*id_, 2), Time(500,0), int(-i)}));
|
|
|
|
|
|
|
|
|
|
__randomDelay();
|
|
|
|
|
sendCommandMessage (invoTrail.bangMsg());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
sendCommandMessage(GenNode msg)
|
|
|
|
|
{
|
|
|
|
|
CommandHandler handler{msg};
|
|
|
|
|
msg.data.accept(handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
__randomDelay()
|
|
|
|
|
{
|
|
|
|
|
usleep (1000L * (1 + rand() % MAX_RAND_DELAY_ms));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
InvocationProducer()
|
|
|
|
|
: ThreadJoinable("test command producer", [&](){ fabricateCommands(); })
|
|
|
|
|
{
|
|
|
|
|
Command(COMMAND_ID).storeDef(cmdID_);
|
|
|
|
|
this->sync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~InvocationProducer()
|
|
|
|
|
{
|
|
|
|
|
this->join().maybeThrow();
|
|
|
|
|
Command::remove (cmdID_);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Time prevState = testCommandState;
|
|
|
|
|
|
|
|
|
|
// fire up several threads to issue commands in parallel...
|
|
|
|
|
vector<InvocationProducer> producerThreads{NUM_THREADS_DEFAULT};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FSecs expectedOffset{0};
|
|
|
|
|
for (uint i=0; i<NUM_THREADS_DEFAULT; ++i)
|
|
|
|
|
for (uint j=0; j<NUM_INVOC_PER_THRED; ++j)
|
|
|
|
|
expectedOffset += FSecs(i*7,2) - FSecs(j,2);
|
|
|
|
|
|
|
|
|
|
while (not ProcDispatcher::instance().empty());
|
|
|
|
|
|
|
|
|
|
CHECK (testCommandState - prevState == Time(expectedOffset));
|
|
|
|
|
|
|
|
|
|
}// Note: leaving this scope blocks for joining all producer threads
|
2017-01-11 04:19:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (SessionCommandFunction_test, "function controller");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}}} // namespace proc::control::test
|