Directory: | ./ |
---|---|
File: | TESTS/TEST_DETACH_POOL/main.cpp |
Date: | 2025-05-16 18:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 66 | 66 | 100.0% |
Branches: | 99 | 99 | 100.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | |||
2 | /*************************************** | ||
3 | Auteur : Pierre Aubert | ||
4 | Mail : pierre.aubert@lapp.in2p3.fr | ||
5 | Licence : CeCILL-C | ||
6 | ****************************************/ | ||
7 | |||
8 | #include <sstream> | ||
9 | #include <iostream> | ||
10 | #include "phoenix_assert.h" | ||
11 | #include "DetachPool.h" | ||
12 | |||
13 | ///Check status convert to string | ||
14 | 1 | void checkStatusToString(){ | |
15 |
5/5✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 17 taken 1 times.
|
1 | phoenix_assert(phoenix_statusToString(PEXIT_STATUS_NOT_FIHISHED) == "\033[33mStill Running\033[0m"); |
16 |
5/5✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 17 taken 1 times.
|
1 | phoenix_assert(phoenix_statusToString(PEXIT_STATUS_OK) == "\033[32mSUCCESS\033[0m"); |
17 |
5/5✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 17 taken 1 times.
|
1 | phoenix_assert(phoenix_statusToString(PEXIT_STATUS_FAIL) == "\033[31mFAIL\033[0m"); |
18 |
5/5✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 17 taken 1 times.
|
1 | phoenix_assert(phoenix_statusToString(42) == "\033[31mFAIL\033[0m"); //Any unknown values are considered as fail |
19 | 1 | } | |
20 | |||
21 | ///Example of thread with perform work | ||
22 | /** @param[out] exitStatus : exit status of the current thread | ||
23 | * @param workingTime : ellapsed time in second, where the thread will pretend to work | ||
24 | */ | ||
25 | 12 | void functionInDetachedThread(int & exitStatus, int workingTime){ | |
26 | 12 | sleep(workingTime); //Simulate some computation | |
27 | |||
28 | 2 | exitStatus = PEXIT_STATUS_OK; | |
29 | 2 | } | |
30 | |||
31 | ///Check DetachPool | ||
32 | /** @return true on success, false otherwise | ||
33 | */ | ||
34 | 1 | bool checkDetachPool(){ | |
35 | // std::cout << "checkDetachPool : Start" << std::endl; | ||
36 |
1/1✓ Branch 1 taken 1 times.
|
1 | DetachPool pool; |
37 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | pool.addThread("", functionInDetachedThread, 2); |
38 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for(size_t i(0lu); i < 5; ++i){ |
39 |
1/1✓ Branch 1 taken 5 times.
|
5 | std::stringstream threadDescription; |
40 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
|
5 | threadDescription << "Thread " << i; |
41 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
|
5 | pool.addThread(threadDescription.str(), functionInDetachedThread, 2 + i); |
42 |
1/1✓ Branch 1 taken 5 times.
|
5 | usleep(10); |
43 |
1/1✓ Branch 1 taken 5 times.
|
5 | pool.refresh(); //If some thread finish when others are started |
44 | 5 | } | |
45 | // std::cerr << "checkDetachPool : let's wait until all threads have finished" << std::endl; | ||
46 |
1/1✓ Branch 1 taken 1 times.
|
1 | pool.waitUntilAllFinish(100000); |
47 |
1/1✓ Branch 1 taken 1 times.
|
1 | ListThreadDetachInfo listThread = pool.getListThreadInfo(); |
48 |
1/1✓ Branch 1 taken 1 times.
|
1 | phoenix_printThreadStatus(listThread); |
49 | // std::cout << "checkDetachPool : OK" << std::endl; | ||
50 | 1 | return true; | |
51 | 1 | } | |
52 | |||
53 | ///Check DetachPool | ||
54 | /** @return true on success, false otherwise | ||
55 | */ | ||
56 | 1 | bool checkDetachPoolLessWait(){ | |
57 | // std::cout << "checkDetachPoolLessWait : Start" << std::endl; | ||
58 |
1/1✓ Branch 1 taken 1 times.
|
1 | DetachPool pool; |
59 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | pool.addThread("", functionInDetachedThread, 2); |
60 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
|
6 | for(size_t i(0lu); i < 5; ++i){ |
61 |
1/1✓ Branch 1 taken 5 times.
|
5 | std::stringstream threadDescription; |
62 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
|
5 | threadDescription << "Thread " << i; |
63 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 4 taken 5 times.
|
5 | pool.addThread(threadDescription.str(), functionInDetachedThread, 2 + i); |
64 |
1/1✓ Branch 1 taken 5 times.
|
5 | usleep(10); |
65 |
1/1✓ Branch 1 taken 5 times.
|
5 | pool.refresh(); //If some thread finish when others are started |
66 | 5 | } | |
67 | // std::cerr << "checkDetachPoolLessWait : let's wait until all threads have finished (but only 3 seconds)" << std::endl; | ||
68 |
1/1✓ Branch 1 taken 1 times.
|
1 | pool.waitUntilAllFinish(300000); |
69 |
1/1✓ Branch 1 taken 1 times.
|
1 | ListThreadDetachInfo listThread = pool.getListThreadInfo(); |
70 |
1/1✓ Branch 1 taken 1 times.
|
1 | phoenix_printThreadStatus(listThread); |
71 | // std::cout << "checkDetachPoolLessWait : OK" << std::endl; | ||
72 | 1 | return true; | |
73 | 1 | } | |
74 | |||
75 | ///Check copy of DetachPool | ||
76 | 1 | void testCopyDetachPool(){ | |
77 |
1/1✓ Branch 1 taken 1 times.
|
1 | DetachPool pool; |
78 |
1/1✓ Branch 1 taken 1 times.
|
1 | pool.setDestructionWaitTime(42); |
79 |
1/1✓ Branch 1 taken 1 times.
|
1 | pool.setRefreshTime(1000); |
80 |
1/1✓ Branch 1 taken 1 times.
|
1 | DetachPool cpyPool(pool); |
81 |
1/1✓ Branch 1 taken 1 times.
|
1 | DetachPool equalPool; |
82 |
1/1✓ Branch 1 taken 1 times.
|
1 | equalPool = pool; |
83 |
6/6✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
|
1 | phoenix_assert(pool.getDestructorWaitTime() == cpyPool.getDestructorWaitTime()); |
84 |
6/6✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
|
1 | phoenix_assert(pool.getRefreshTime() == cpyPool.getRefreshTime()); |
85 |
6/6✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
|
1 | phoenix_assert(pool.getDestructorWaitTime() == equalPool.getDestructorWaitTime()); |
86 |
6/6✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
✓ Branch 19 taken 1 times.
|
1 | phoenix_assert(pool.getRefreshTime() == equalPool.getRefreshTime()); |
87 | 1 | } | |
88 | |||
89 | ///Check DetachPool | ||
90 | /** @return true on success, false otherwise | ||
91 | */ | ||
92 | 1 | bool checkDetachPoolEmpty(){ | |
93 | // std::cout << "checkDetachPoolEmpty : Start" << std::endl; | ||
94 |
1/1✓ Branch 1 taken 1 times.
|
1 | DetachPool pool; |
95 | // std::cerr << "checkDetachPoolEmpty : let's wait until all threads have finished (but only 3 seconds)" << std::endl; | ||
96 |
1/1✓ Branch 1 taken 1 times.
|
1 | pool.waitUntilAllFinish(300000); |
97 |
1/1✓ Branch 1 taken 1 times.
|
1 | ListThreadDetachInfo listThread = pool.getListThreadInfo(); |
98 |
1/1✓ Branch 1 taken 1 times.
|
1 | phoenix_printThreadStatus(listThread); |
99 | // std::cout << "checkDetachPoolEmpty : OK" << std::endl; | ||
100 | 1 | return true; | |
101 | 1 | } | |
102 | |||
103 | 1 | int main(int argc, char **argv){ | |
104 | 1 | checkStatusToString(); | |
105 |
5/5✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
|
1 | phoenix_assert(checkDetachPool()); |
106 |
5/5✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
|
1 | phoenix_assert(checkDetachPoolLessWait()); |
107 | 1 | testCopyDetachPool(); | |
108 |
5/5✓ Branch 2 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 16 taken 1 times.
|
1 | phoenix_assert(checkDetachPoolEmpty()); |
109 | 1 | return 0; | |
110 | } | ||
111 | |||
112 | |||
113 | |||
114 | |||
115 |