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
12
13#include "ocean/base/Lock.h"
14#include "ocean/base/Thread.h"
16
17#include <atomic>
18
19namespace Ocean
20{
21
22namespace Test
23{
24
25namespace TestBase
26{
27
28/**
29 * This class implements tests for the Thread class.
30 * @ingroup testbase
31 */
32class OCEAN_TEST_BASE_EXPORT TestThread
33{
34 public:
35
36 /**
37 * Tests all functions.
38 * @param testDuration Number of seconds for each test, with range (0, infinity)
39 * @return True, if succeeded
40 */
41 static bool test(const double testDuration);
42
43 /**
44 * Tests to waitForValue() function without lock object.
45 * @param testDuration Number of seconds for the test, with range (0, infinity)
46 * @return True, if succeeded
47 */
48 static bool testWaitForValueWithoutLock(const double testDuration);
49
50 /**
51 * Tests to waitForValue() function with lock object.
52 * @param testDuration Number of seconds for the test, with range (0, infinity)
53 * @return True, if succeeded
54 */
55 static bool testWaitForValueWithLock(const double testDuration);
56
57 protected:
58
59 /**
60 * Sets a parameter to a specified value after a specified delay.
61 * @param object The object which will be set
62 * @param value The value to use
63 * @param delay The delay in seconds, with range [0, infinity)
64 * @param lock The lock to be used
65 * @param isSet Will be set to True if the value was set
66 * @tparam TObject The data type of the object
67 * @tparam TValue The data type of the value
68 */
69 template <typename TObject, typename TValue>
70 static void setValueDelayed(TObject& object, const TValue& value, const double delay, Lock& lock, std::atomic<bool>& isSet);
71};
72
73template <typename TObject, typename TValue>
74void TestThread::setValueDelayed(TObject& object, const TValue& value, const double delay, Lock& lock, std::atomic<bool>& isSet)
75{
76 ocean_assert(!isSet);
77
78#ifdef OCEAN_USE_GTEST
79
80 // running the test with Gtest will result in extreme bad results when using Thread::sleep(),
81 // thus using a while loop instead
82
83 const Timestamp startTimestamp(true);
84
85 while (!startTimestamp.hasTimePassed(delay))
86 {
87 Thread::sleep(0u);
88 }
89
90#else
91
92 Thread::sleep((unsigned int)(Timestamp::seconds2milliseconds(delay)));
93
94#endif // OCEAN_USE_GTEST
95
96 const ScopedLock scopedLock(lock);
97
98 object = value;
99
100 isSet = true;
101}
102
103}
104
105}
106
107}
108
109#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:135
This class implements tests for the Thread class.
Definition TestThread.h:33
static bool test(const double testDuration)
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:74
static void sleep(unsigned int ms)
Sleeps the calling thread for a given time.
This class implements a timestamp.
Definition Timestamp.h:36
static constexpr int64_t seconds2milliseconds(const double seconds)
Converts seconds to milliseconds.
Definition Timestamp.h:408
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:290
The namespace covering the entire Ocean framework.
Definition Accessor.h:15