Directory: | ./ |
---|---|
File: | TESTS/TEST_SIGNAL_CATCHER/main_thread.cpp |
Date: | 2025-05-16 18:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 41 | 41 | 100.0% |
Branches: | 44 | 45 | 97.8% |
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 <unistd.h> | ||
9 | |||
10 | #include <sstream> | ||
11 | #include <fstream> | ||
12 | #include <iostream> | ||
13 | |||
14 | #include <thread> | ||
15 | #include <vector> | ||
16 | |||
17 | #include "SignalCatcher.h" | ||
18 | |||
19 | ///Function executed by a thread | ||
20 | /** @param[out] b : true on success, false otherwise | ||
21 | * @param threadId : id of the current thread | ||
22 | */ | ||
23 | 2 | void threadFunction(char & b, size_t threadId){ | |
24 |
1/1✓ Branch 1 taken 2 times.
|
2 | std::stringstream fileName; |
25 |
3/3✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
|
2 | fileName << "threadFunction_" << threadId << ".log"; |
26 | |||
27 |
1/1✓ Branch 1 taken 2 times.
|
2 | std::ofstream fs; |
28 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
2 | fs.open(fileName.str()); |
29 | |||
30 | 2 | bool isSignalRecived(false); | |
31 | 2 | size_t delay(10lu); | |
32 |
3/4✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
|
6 | for(size_t i(0lu); i < delay && !isSignalRecived; ++i){ |
33 |
5/5✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
✓ Branch 10 taken 4 times.
✓ Branch 13 taken 4 times.
|
4 | fs << "thread("<<threadId<<") : Waiting for signal : i = " << i << std::endl; |
34 |
1/1✓ Branch 1 taken 4 times.
|
4 | isSignalRecived = phoenix_isSignalRecived(SIGQUIT); |
35 |
1/1✓ Branch 1 taken 4 times.
|
4 | usleep(1000000); |
36 | } | ||
37 |
3/3✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
|
2 | fs << "testSignalCatch : isSignalRecived = " << isSignalRecived << std::endl; |
38 |
1/1✓ Branch 1 taken 2 times.
|
2 | fs.close(); |
39 | 2 | b = isSignalRecived; | |
40 | 2 | } | |
41 | |||
42 | ///Check Signal Catcher | ||
43 | /** @return true on success, false otherwise | ||
44 | */ | ||
45 | 1 | bool testSignalCatch(){ | |
46 |
1/1✓ Branch 1 taken 1 times.
|
1 | phoenix_addSignalCatcher(SIGQUIT); |
47 | |||
48 | 1 | size_t nbThread(2lu); | |
49 | |||
50 | 1 | std::vector<char> vecRes; | |
51 |
1/1✓ Branch 1 taken 1 times.
|
1 | vecRes.resize(nbThread); |
52 | |||
53 | 1 | std::vector<std::thread> vecThread; | |
54 |
1/1✓ Branch 1 taken 1 times.
|
1 | vecThread.resize(nbThread); |
55 | |||
56 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for(size_t i(0lu); i < nbThread; ++i){ |
57 |
1/1✓ Branch 3 taken 2 times.
|
2 | vecThread[i] = std::thread(threadFunction, std::ref(vecRes[i]), i); |
58 | } | ||
59 | |||
60 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for(size_t i(0lu); i < nbThread; ++i){ |
61 |
1/1✓ Branch 2 taken 2 times.
|
2 | vecThread[i].join(); |
62 | } | ||
63 | 1 | bool b(true); | |
64 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for(size_t i(0lu); i < nbThread; ++i){ |
65 | 2 | b &= (bool)vecRes[i]; | |
66 | } | ||
67 | |||
68 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
1 | std::cout << "testSignalCatch : b = " << b << std::endl; |
69 | 1 | return b; | |
70 | 1 | } | |
71 | |||
72 | 1 | int main(int argc, char **argv){ | |
73 |
1/1✓ Branch 1 taken 1 times.
|
1 | bool b(testSignalCatch()); |
74 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
1 | std::cout << "final : b = " << b << std::endl; |
75 |
1/1✓ Branch 1 taken 1 times.
|
1 | std::ofstream fs; |
76 |
1/1✓ Branch 1 taken 1 times.
|
1 | fs.open("test_phoenix_thread_signal_catcher_thread.log"); |
77 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
|
1 | fs << b << std::endl; |
78 |
1/1✓ Branch 1 taken 1 times.
|
1 | fs.close(); |
79 | 1 | return b - 1; | |
80 | 1 | } | |
81 | |||
82 | |||
83 | |||
84 | |||
85 |