Ocean
Loading...
Searching...
No Matches
TestThread.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_TEST_TESTBASE_TEST_THREAD_H
9#define META_OCEAN_TEST_TESTBASE_TEST_THREAD_H
10
13
14#include "ocean/base/Lock.h"
15#include "ocean/base/Thread.h"
17
18#include <atomic>
19
20namespace Ocean
21{
22
23namespace Test
24{
25
26namespace TestBase
27{
28
29/**
30 * This class implements tests for the Thread class.
31 * @ingroup testbase
32 */
33class OCEAN_TEST_BASE_EXPORT TestThread
34{
35 public:
36
37 /**
38 * Tests all functions.
39 * @param testDuration Number of seconds for each test, with range (0, infinity)
40 * @param selector The test selector
41 * @return True, if succeeded
42 */
43 static bool test(const double testDuration, const TestSelector& selector = TestSelector());
44
45 /**
46 * Tests to waitForValue() function without lock object.
47 * @param testDuration Number of seconds for the test, with range (0, infinity)
48 * @return True, if succeeded
49 */
50 static bool testWaitForValueWithoutLock(const double testDuration);
51
52 /**
53 * Tests to waitForValue() function with lock object.
54 * @param testDuration Number of seconds for the test, with range (0, infinity)
55 * @return True, if succeeded
56 */
57 static bool testWaitForValueWithLock(const double testDuration);
58
59 protected:
60
61 /**
62 * Sets a parameter to a specified value after a specified delay.
63 * @param object The object which will be set
64 * @param value The value to use
65 * @param delay The delay in seconds, with range [0, infinity)
66 * @param lock The lock to be used
67 * @param isSet Will be set to True if the value was set
68 * @tparam TObject The data type of the object
69 * @tparam TValue The data type of the value
70 */
71 template <typename TObject, typename TValue>
72 static void setValueDelayed(TObject& object, const TValue& value, const double delay, Lock& lock, std::atomic<bool>& isSet);
73};
74
75template <typename TObject, typename TValue>
76void TestThread::setValueDelayed(TObject& object, const TValue& value, const double delay, Lock& lock, std::atomic<bool>& isSet)
77{
78 ocean_assert(!isSet);
79
80#ifdef OCEAN_USE_GTEST
81
82 // running the test with Gtest will result in extreme bad results when using Thread::sleep(),
83 // thus using a while loop instead
84
85 const Timestamp startTimestamp(true);
86
87 while (!startTimestamp.hasTimePassed(delay))
88 {
89 Thread::sleep(0u);
90 }
91
92#else
93
94 Thread::sleep((unsigned int)(Timestamp::seconds2milliseconds(delay)));
95
96#endif // OCEAN_USE_GTEST
97
98 const ScopedLock scopedLock(lock);
99
100 object = value;
101
102 isSet = true;
103}
104
105}
106
107}
108
109}
110
111#endif // META_OCEAN_TEST_TESTBASE_TEST_THREAD_H
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a scoped lock object for recursive lock objects.
Definition Lock.h:147
This class implements tests for the Thread class.
Definition TestThread.h:34
static bool test(const double testDuration, const TestSelector &selector=TestSelector())
Tests all functions.
static bool testWaitForValueWithoutLock(const double testDuration)
Tests to waitForValue() function without lock object.
static bool testWaitForValueWithLock(const double testDuration)
Tests to waitForValue() function with lock object.
static void setValueDelayed(TObject &object, const TValue &value, const double delay, Lock &lock, std::atomic< bool > &isSet)
Sets a parameter to a specified value after a specified delay.
Definition TestThread.h:76
This class implements a test selector that parses test function strings and determines which tests sh...
Definition TestSelector.h:51
static void sleep(unsigned int ms)
Sleeps the calling thread for a given time.
This class implements a timestamp.
Definition Timestamp.h:63
static constexpr int64_t seconds2milliseconds(const double seconds)
Converts seconds to milliseconds.
Definition Timestamp.h:664
bool hasTimePassed(const double seconds, const Timestamp &currentTimestamp=Timestamp(true)) const
Returns whether a specified amount of time has passed since this timestamp.
Definition Timestamp.h:546
The namespace covering the entire Ocean framework.
Definition Accessor.h:15