2009-01-18 16:59:24 +01:00
|
|
|
|
/*
|
|
|
|
|
|
ThreadWrapperJoin(Test) - wait blocking on termination of a thread
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** 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. See the file COPYING for further details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
* *****************************************************************/
|
2009-01-18 16:59:24 +01:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file thread-wrapper-join-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref ThreadWrapperJoin_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
2009-01-18 16:59:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2010-01-24 14:05:32 +01:00
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2023-09-21 23:23:55 +02:00
|
|
|
|
#include "lib/thread.hpp"
|
2009-01-18 16:59:24 +01:00
|
|
|
|
#include "lib/error.hpp"
|
|
|
|
|
|
|
2023-09-29 21:38:22 +02:00
|
|
|
|
#include <chrono>
|
2009-01-18 16:59:24 +01:00
|
|
|
|
|
|
|
|
|
|
using test::Test;
|
2023-09-29 21:38:22 +02:00
|
|
|
|
using std::this_thread::sleep_for;
|
|
|
|
|
|
using namespace std::chrono_literals;
|
2009-01-18 16:59:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-09-21 23:23:55 +02:00
|
|
|
|
namespace lib {
|
2010-01-24 14:05:32 +01:00
|
|
|
|
namespace test {
|
2009-01-18 16:59:24 +01:00
|
|
|
|
|
2023-09-29 21:38:22 +02:00
|
|
|
|
using error::LUMIERA_ERROR_LOGIC;
|
2010-01-24 14:05:32 +01:00
|
|
|
|
namespace {
|
2009-01-18 16:59:24 +01:00
|
|
|
|
|
2023-09-29 21:38:22 +02:00
|
|
|
|
const int DESTRUCTION_CODE = -23;
|
2009-01-18 16:59:24 +01:00
|
|
|
|
|
2023-09-29 21:38:22 +02:00
|
|
|
|
LUMIERA_ERROR_DEFINE(SPECIAL, "007 exception");
|
|
|
|
|
|
|
|
|
|
|
|
#define Type(_EXPR_) lib::test::showType<decltype(_EXPR_)>()
|
2010-01-24 14:05:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/***********************************************************************//**
|
2023-09-29 21:38:22 +02:00
|
|
|
|
* @test verify the special flavour of the thread-wrapper, allowing to block
|
|
|
|
|
|
* waiting on thread end and then pass result or error state.
|
|
|
|
|
|
* @see thread.hpp
|
|
|
|
|
|
* @see Result_test
|
|
|
|
|
|
* @see ThreadWrapper_test
|
|
|
|
|
|
* @see SyncLocking_test
|
2010-01-24 14:05:32 +01:00
|
|
|
|
*/
|
|
|
|
|
|
class ThreadWrapperJoin_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
seedRand();
|
|
|
|
|
|
|
2023-09-29 21:38:22 +02:00
|
|
|
|
simpleUse();
|
|
|
|
|
|
returnValue();
|
|
|
|
|
|
detectFailure();
|
|
|
|
|
|
joinOnceOnly();
|
2010-01-24 14:05:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2023-09-29 21:38:22 +02:00
|
|
|
|
simpleUse()
|
|
|
|
|
|
{
|
|
|
|
|
|
ThreadJoinable theThread{"test join-1"
|
|
|
|
|
|
,[]{ sleep_for (10ms); }};
|
|
|
|
|
|
CHECK (theThread);
|
|
|
|
|
|
theThread.join(); // blocks until thread-function has terminated
|
|
|
|
|
|
CHECK (not theThread);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2010-01-24 14:05:32 +01:00
|
|
|
|
theAction (int secretValue) ///< to be run in a new thread...
|
|
|
|
|
|
{
|
2023-09-29 21:38:22 +02:00
|
|
|
|
sleep_for (100ms); // pause 100ms prior to modifying
|
2010-01-24 14:05:32 +01:00
|
|
|
|
|
|
|
|
|
|
if (DESTRUCTION_CODE == secretValue)
|
2023-09-29 21:38:22 +02:00
|
|
|
|
throw error::External{"special agent detected"
|
|
|
|
|
|
, LUMIERA_ERROR_SPECIAL};
|
2010-01-24 14:05:32 +01:00
|
|
|
|
else
|
2023-09-29 21:38:22 +02:00
|
|
|
|
return secretValue+42;
|
2010-01-24 14:05:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2023-09-29 21:38:22 +02:00
|
|
|
|
returnValue()
|
2010-01-24 14:05:32 +01:00
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
int mySecret = rani(1000);
|
2010-01-24 14:05:32 +01:00
|
|
|
|
|
2023-09-29 21:38:22 +02:00
|
|
|
|
ThreadJoinable theThread{"test join-2"
|
|
|
|
|
|
,&ThreadWrapperJoin_test::theAction
|
|
|
|
|
|
, this, mySecret};
|
|
|
|
|
|
|
|
|
|
|
|
// Note: join() passes the result value captured in the thread
|
|
|
|
|
|
CHECK (mySecret+42 == theThread.join());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
detectFailure()
|
|
|
|
|
|
{
|
|
|
|
|
|
ThreadJoinable theThread{"test join-3"
|
|
|
|
|
|
,&ThreadWrapperJoin_test::theAction
|
|
|
|
|
|
, this, DESTRUCTION_CODE};
|
|
|
|
|
|
|
|
|
|
|
|
// join() actually returns a proxy...
|
|
|
|
|
|
auto res = theThread.join();
|
|
|
|
|
|
CHECK (Type(res) == "Result<int>"_expect);
|
2010-01-24 14:05:32 +01:00
|
|
|
|
|
2023-09-29 21:38:22 +02:00
|
|
|
|
// can detect that the thread was aborted with an exception
|
|
|
|
|
|
CHECK (not res.isValid());
|
|
|
|
|
|
VERIFY_ERROR(SPECIAL, res.maybeThrow() );
|
|
|
|
|
|
VERIFY_ERROR(SPECIAL, (int)res );
|
2010-01-24 14:05:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2024-11-16 04:52:58 +01:00
|
|
|
|
joinOnceOnly()
|
2010-01-24 14:05:32 +01:00
|
|
|
|
{
|
2024-11-16 04:52:58 +01:00
|
|
|
|
ThreadJoinable theThread{"test join-4"
|
2023-09-29 21:38:22 +02:00
|
|
|
|
,[]{ sleep_for (10ms); }};
|
|
|
|
|
|
theThread.join();
|
2010-01-24 14:05:32 +01:00
|
|
|
|
|
2023-09-29 21:38:22 +02:00
|
|
|
|
VERIFY_ERROR(LOGIC, theThread.join() );
|
|
|
|
|
|
VERIFY_ERROR(LOGIC, theThread.join() );
|
2010-01-24 14:05:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (ThreadWrapperJoin_test, "function common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-21 23:23:55 +02:00
|
|
|
|
}} // namespace lib::test
|