RoveComm C++ 3.0.0
Welcome to the RoveComm C++ repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of RoveComm C++. The RoveComm C++ project is the RoveComm interface for high level systems and should not be used for microcontrollers.
Loading...
Searching...
No Matches
testutils Namespace Reference

The testutils namespace contains utility functions for running tests. More...

Functions

void RunTimedTest (const std::function< void()> &fnTestCode, int nMaxRetries, int nTimeoutMS)
 Run a test with a timeout and retries.

Detailed Description

The testutils namespace contains utility functions for running tests.

Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-02-09

Function Documentation

◆ RunTimedTest()

void testutils::RunTimedTest ( const std::function< void()> & fnTestCode,
int nMaxRetries,
int nTimeoutMS )

Run a test with a timeout and retries.

Parameters
fnTestCode- The test code to run.
nMaxRetries- The maximum number of times to retry the test.
nTimeoutMS- The timeout for the test in milliseconds.
Author
Eli Byrd (edbgk.nosp@m.k@ms.nosp@m.t.edu)
Date
2024-02-09
32 {
33 int nAttempts = 0;
34
35 while (nAttempts < nMaxRetries)
36 {
37 std::atomic<bool> testComplete(false);
38
39 // Start a new thread to run the test
40 std::thread testThread(
41 [&testComplete, fnTestCode]()
42 {
43 // Run the test code
44 fnTestCode();
45 testComplete = true;
46 });
47
48 // Wait for the test to complete or timeout
49 auto start = std::chrono::steady_clock::now();
50
51 bool bFailed = false;
52 while (!testComplete)
53 {
54 auto now = std::chrono::steady_clock::now();
55 if (now - start > std::chrono::milliseconds(nTimeoutMS))
56 {
57 std::cerr << "Test timed out after " << nTimeoutMS << " ms" << std::endl;
58
59 testThread.detach();
60
61 // testThread.join();
62 bFailed = true;
63 break;
64 }
65 }
66
67 if (testComplete && !bFailed)
68 {
69 testThread.join();
70 break;
71 }
72
73 nAttempts++;
74 }
75
76 if (nAttempts >= nMaxRetries)
77 {
78 FAIL() << "Test failed after " << nMaxRetries << " attempts";
79 }
80 }
Here is the caller graph for this function: