2017-08-05 16:10:25 +02:00
|
|
|
/*
|
|
|
|
|
CallQueue(Test) - verify queue based dispatch of bound function objects
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
/** @file call-queue-test.cpp
|
|
|
|
|
** unit test \ref CallQueue_test
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2017-08-06 15:21:31 +02:00
|
|
|
#include "lib/scoped-collection.hpp"
|
2023-10-05 01:17:58 +02:00
|
|
|
#include "lib/sync-barrier.hpp"
|
|
|
|
|
#include "lib/thread.hpp"
|
2017-08-06 15:21:31 +02:00
|
|
|
#include "lib/sync.hpp"
|
2017-08-07 01:00:36 +02:00
|
|
|
#include "lib/util.hpp"
|
2017-08-05 16:10:25 +02:00
|
|
|
|
|
|
|
|
#include "lib/call-queue.hpp"
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace test{
|
|
|
|
|
|
2017-08-06 15:21:31 +02:00
|
|
|
using lib::Sync;
|
2023-10-05 01:17:58 +02:00
|
|
|
using lib::SyncBarrier;
|
|
|
|
|
using lib::ThreadJoinable;
|
2017-08-06 15:21:31 +02:00
|
|
|
|
2017-08-05 17:28:31 +02:00
|
|
|
using util::isnil;
|
2017-08-05 16:10:25 +02:00
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-05 18:44:25 +02:00
|
|
|
namespace { // test fixture
|
|
|
|
|
|
2017-08-06 15:21:31 +02:00
|
|
|
// --------random-stress-test------
|
|
|
|
|
uint const NUM_OF_THREADS = 50;
|
|
|
|
|
uint const MAX_RAND_INCMT = 200;
|
|
|
|
|
uint const MAX_RAND_STEPS = 500;
|
2017-08-07 01:00:36 +02:00
|
|
|
uint const MAX_RAND_DELAY = 1000;
|
2017-08-06 15:21:31 +02:00
|
|
|
// --------random-stress-test------
|
2017-08-07 01:00:36 +02:00
|
|
|
|
2017-08-06 15:21:31 +02:00
|
|
|
|
2017-08-05 18:44:25 +02:00
|
|
|
uint calc_sum = 0;
|
|
|
|
|
uint ctor_sum = 0;
|
|
|
|
|
uint dtor_sum = 0;
|
|
|
|
|
|
|
|
|
|
template<uint i>
|
|
|
|
|
struct Dummy
|
|
|
|
|
{
|
|
|
|
|
uint val_;
|
|
|
|
|
|
|
|
|
|
Dummy()
|
|
|
|
|
: val_(i)
|
|
|
|
|
{
|
|
|
|
|
ctor_sum += (val_+1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~Dummy()
|
|
|
|
|
{
|
|
|
|
|
dtor_sum += val_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
operator++()
|
|
|
|
|
{
|
|
|
|
|
return ++val_;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<uint i>
|
|
|
|
|
void
|
|
|
|
|
increment (Dummy<i>&& dummy) //NOTE: dummy is consumed here
|
|
|
|
|
{
|
|
|
|
|
calc_sum += ++dummy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//(End) test fixture
|
2017-08-05 16:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************************//**
|
|
|
|
|
* @test verify a helper component for dispatching functors through a threadsafe queue.
|
|
|
|
|
* - simple usage
|
|
|
|
|
* - enqueue and dequeue several functors
|
2023-10-05 01:17:58 +02:00
|
|
|
* - multithreaded load test
|
2017-08-05 16:10:25 +02:00
|
|
|
* @see lib::CallQueue
|
2018-11-15 23:59:23 +01:00
|
|
|
* @see stage::NotificationService usage example
|
2017-08-07 01:00:36 +02:00
|
|
|
* @see [DemoGuiRoundtrip](http://issues.lumiera.org/ticket/1099 "Ticket #1099")
|
2017-08-05 16:10:25 +02:00
|
|
|
*/
|
|
|
|
|
class CallQueue_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
run (Arg)
|
|
|
|
|
{
|
|
|
|
|
verify_SimpleUse();
|
|
|
|
|
verify_Consistency();
|
|
|
|
|
verify_ThreadSafety();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
verify_SimpleUse ()
|
|
|
|
|
{
|
2017-08-05 17:28:31 +02:00
|
|
|
CallQueue queue;
|
|
|
|
|
CHECK (isnil (queue));
|
|
|
|
|
|
|
|
|
|
int val = 2;
|
|
|
|
|
queue.feed ([&]() { val = -1; });
|
|
|
|
|
CHECK (1 == queue.size());
|
|
|
|
|
CHECK (val == 2);
|
|
|
|
|
|
|
|
|
|
queue.invoke();
|
|
|
|
|
CHECK (val == -1);
|
|
|
|
|
CHECK (0 == queue.size());
|
|
|
|
|
|
|
|
|
|
queue.invoke();
|
|
|
|
|
CHECK (0 == queue.size());
|
2017-08-05 16:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-08-05 18:44:25 +02:00
|
|
|
/**
|
|
|
|
|
* @test consistency of queue data handling
|
|
|
|
|
* - functors of various types can be mixed
|
|
|
|
|
* - entries are moved in and out of the queue
|
|
|
|
|
* - no copying whatsoever happens
|
|
|
|
|
* - each entry gets invoked
|
|
|
|
|
* - all entries are invoked in order
|
|
|
|
|
* - enqueuing and dequeuing can be interspersed
|
|
|
|
|
* - no memory leaks in bound argument data
|
|
|
|
|
*/
|
2017-08-05 16:10:25 +02:00
|
|
|
void
|
|
|
|
|
verify_Consistency ()
|
|
|
|
|
{
|
2017-08-05 18:44:25 +02:00
|
|
|
calc_sum = 0;
|
|
|
|
|
ctor_sum = 0;
|
|
|
|
|
dtor_sum = 0;
|
|
|
|
|
|
|
|
|
|
CallQueue queue;
|
|
|
|
|
queue.feed ([]() { increment(Dummy<0>{}); }); //NOTE: each lambda binds a different instantiation of the increment template
|
|
|
|
|
queue.feed ([]() { increment(Dummy<1>{}); }); // and each invocation closes over an anonymous rvalue instance
|
|
|
|
|
queue.feed ([]() { increment(Dummy<2>{}); });
|
|
|
|
|
|
|
|
|
|
queue.invoke();
|
|
|
|
|
queue.invoke();
|
|
|
|
|
queue.feed ([]() { increment(Dummy<3>{}); });
|
|
|
|
|
queue.feed ([]() { increment(Dummy<4>{}); });
|
|
|
|
|
|
|
|
|
|
queue.invoke();
|
|
|
|
|
queue.invoke();
|
|
|
|
|
queue.invoke();
|
|
|
|
|
|
|
|
|
|
uint expected = (5+1)*5/2;
|
|
|
|
|
CHECK (calc_sum = expected);
|
|
|
|
|
CHECK (ctor_sum = expected);
|
|
|
|
|
CHECK (dtor_sum = expected);
|
2017-08-05 16:10:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-08-06 15:21:31 +02:00
|
|
|
|
|
|
|
|
struct Worker
|
2023-10-05 01:17:58 +02:00
|
|
|
: ThreadJoinable<>
|
2017-08-07 01:00:36 +02:00
|
|
|
, Sync<>
|
2017-08-06 15:21:31 +02:00
|
|
|
{
|
2017-08-07 01:00:36 +02:00
|
|
|
uint64_t producerSum = 0;
|
|
|
|
|
uint64_t consumerSum = 0;
|
|
|
|
|
|
2023-10-05 01:17:58 +02:00
|
|
|
SyncBarrier& trigger_;
|
|
|
|
|
|
2017-08-07 01:00:36 +02:00
|
|
|
void
|
|
|
|
|
countConsumerCall (uint increment)
|
|
|
|
|
{
|
|
|
|
|
Lock sync(this); // NOTE: will be invoked from some random other thread
|
|
|
|
|
consumerSum += increment;
|
|
|
|
|
}
|
2017-08-06 15:21:31 +02:00
|
|
|
|
2023-10-05 01:17:58 +02:00
|
|
|
Worker(CallQueue& queue, SyncBarrier& commonTrigger)
|
2017-08-06 15:21:31 +02:00
|
|
|
: ThreadJoinable{"CallQueue_test: concurrent dispatch"
|
|
|
|
|
, [&]() {
|
2017-08-07 01:00:36 +02:00
|
|
|
uint cnt = rand() % MAX_RAND_STEPS;
|
|
|
|
|
uint delay = rand() % MAX_RAND_DELAY;
|
|
|
|
|
|
2023-10-05 01:17:58 +02:00
|
|
|
trigger_.sync(); // block until all threads are ready
|
2017-08-06 15:21:31 +02:00
|
|
|
for (uint i=0; i<cnt; ++i)
|
2017-08-07 01:00:36 +02:00
|
|
|
{
|
|
|
|
|
uint increment = rand() % MAX_RAND_INCMT;
|
|
|
|
|
queue.feed ([=]() { countConsumerCall(increment); });
|
|
|
|
|
producerSum += increment;
|
|
|
|
|
usleep (delay);
|
|
|
|
|
queue.invoke(); // NOTE: dequeue one functor added during our sleep
|
|
|
|
|
} // and thus belonging to some random other thread
|
2017-08-06 15:21:31 +02:00
|
|
|
}}
|
2023-10-05 01:17:58 +02:00
|
|
|
, trigger_{commonTrigger}
|
2017-08-06 15:21:31 +02:00
|
|
|
{ }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using Workers = lib::ScopedCollection<Worker>;
|
|
|
|
|
|
|
|
|
|
|
2017-08-07 01:00:36 +02:00
|
|
|
/**
|
|
|
|
|
* @test torture the CallQueue by massively multithreaded dispatch
|
|
|
|
|
* - start #NUM_OF_THREADS (e.g. 50) threads in parallel
|
|
|
|
|
* - each of those has a randomised execution pattern to
|
|
|
|
|
* add new functors and dispatch other thread's functors
|
|
|
|
|
*/
|
2017-08-05 16:10:25 +02:00
|
|
|
void
|
|
|
|
|
verify_ThreadSafety()
|
|
|
|
|
{
|
2017-08-06 15:21:31 +02:00
|
|
|
CallQueue queue;
|
2023-10-05 01:17:58 +02:00
|
|
|
SyncBarrier trigger{NUM_OF_THREADS + 1};
|
2017-08-06 15:21:31 +02:00
|
|
|
|
|
|
|
|
// Start a bunch of threads with random access pattern
|
|
|
|
|
Workers workers{NUM_OF_THREADS,
|
|
|
|
|
[&](Workers::ElementHolder& storage)
|
|
|
|
|
{
|
2023-10-05 01:17:58 +02:00
|
|
|
storage.create<Worker> (queue, trigger);
|
2017-08-06 15:21:31 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-26 04:40:54 +02:00
|
|
|
// unleash all worker functions
|
2023-10-05 01:17:58 +02:00
|
|
|
trigger.sync();
|
2018-03-26 04:40:54 +02:00
|
|
|
|
2023-10-05 01:17:58 +02:00
|
|
|
// wait for termination of all threads and detect possible exceptions
|
|
|
|
|
bool allFine{true};
|
2017-08-07 01:00:36 +02:00
|
|
|
for (auto& worker : workers)
|
2023-10-05 01:17:58 +02:00
|
|
|
allFine &= worker.join().isValid();
|
|
|
|
|
CHECK (allFine);
|
2017-08-07 01:00:36 +02:00
|
|
|
|
2017-08-06 15:21:31 +02:00
|
|
|
// collect the results of all worker threads
|
2017-08-07 01:00:36 +02:00
|
|
|
uint64_t globalProducerSum = 0;
|
|
|
|
|
uint64_t globalConsumerSum = 0;
|
2017-08-06 15:21:31 +02:00
|
|
|
for (auto& worker : workers)
|
|
|
|
|
{
|
2017-08-07 01:00:36 +02:00
|
|
|
globalProducerSum += worker.producerSum;
|
|
|
|
|
globalConsumerSum += worker.consumerSum;
|
2017-08-06 15:21:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VERIFY: locally recorded partial sums match total sum
|
2017-08-07 01:00:36 +02:00
|
|
|
CHECK (globalProducerSum == globalConsumerSum);
|
2017-08-05 16:10:25 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (CallQueue_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|