Skip to content

Commit f05f775

Browse files
authored
Macos sigcheck (#6)
* Refactor signal handling in ContainerTests Refactor signal handling macros and improve exception safety in tests. * Fix signal handling in ContainerTests.cpp * Log caught signals in doctest signal handler * fixup
1 parent e71c091 commit f05f775

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
#if defined(_WIN32) || defined(_WIN64)
2727

28-
# define __TRY(SIGID) if (true)
29-
# define __CATCH else
30-
# define __FINALLY if (true)
28+
# define __TRY if (true)
29+
# define __CATCH else
30+
# define __FINALLY if (true)
3131
# define __ENDTRY
3232

3333
#else // POSIX / Unix-like
@@ -39,31 +39,40 @@ static thread_local sigjmp_buf __doctest_jumpbuf;
3939

4040
void __doctest_signal_handler(int sig)
4141
{
42-
if (sig != SIGKILL)
43-
::siglongjmp(__doctest_jumpbuf, 1); // jump back to safe point
42+
AXLOGI("Caught signal: {}", sig);
43+
::siglongjmp(__doctest_jumpbuf, 1); // jump back to safe point
4444
}
4545

46-
# define __TRY(sig_num) \
46+
# define __TRY \
4747
do \
4848
{ \
49-
const auto __sig_num = sig_num; \
5049
struct sigaction sa{}; \
5150
sa.sa_handler = __doctest_signal_handler; \
5251
sigemptyset(&sa.sa_mask); \
5352
sa.sa_flags = 0; \
54-
sigaction(__sig_num, &sa, nullptr); \
53+
for (int i = 1; i < NSIG; ++i) \
54+
{ \
55+
if (i == SIGKILL || i == SIGSTOP) \
56+
continue; \
57+
sigaction(i, &sa, nullptr); \
58+
} \
5559
int __ret = sigsetjmp(__doctest_jumpbuf, 1); \
5660
if (__ret == 0)
5761

5862
# define __CATCH else
5963

60-
# define __FINALLY \
61-
{ \
62-
struct sigaction sa_default{}; \
63-
sa_default.sa_handler = SIG_DFL; \
64-
sigemptyset(&sa_default.sa_mask); \
65-
sa_default.sa_flags = 0; \
66-
sigaction(__sig_num, &sa_default, nullptr); \
64+
# define __FINALLY \
65+
{ \
66+
struct sigaction sa_default{}; \
67+
sa_default.sa_handler = SIG_DFL; \
68+
sigemptyset(&sa_default.sa_mask); \
69+
sa_default.sa_flags = 0; \
70+
for (int i = 1; i < NSIG; ++i) \
71+
{ \
72+
if (i == SIGKILL || i == SIGSTOP) \
73+
continue; \
74+
sigaction(i, &sa_default, nullptr); \
75+
} \
6776
}
6877

6978
# define __ENDTRY \
@@ -255,12 +264,28 @@ TEST_SUITE("tlx/Containers")
255264
CHECK((arr3[0].value == 0 && arr3[1].value == 0));
256265

257266
// while, if you run unittest with Xcode debugger, the debugger still raise exception
258-
__TRY(SIGILL)
267+
__TRY
259268
{
260269
// pod vector, no auto fill when dtor is trivial
261270
tlx::pod_vector<NonTrivalCtor1> arr4;
262271
arr4.resize(2);
263272
CHECK((arr4[0].value != 123 && arr4[1].value != 123 && arr4.size() == 2));
273+
274+
tlx::pod_vector<TrivalCtor1> arr5;
275+
arr5.resize(2);
276+
277+
#ifndef __APPLE__
278+
CHECK((arr5[0].value != 0 && arr5[1].value != 0));
279+
#endif
280+
// we can safe access initialized member without exception catch
281+
arr5.resize(4, TrivalCtor1{39});
282+
CHECK((arr5[2].value == 39 && arr5[3].value == 39));
283+
284+
arr5.resize(128, TrivalCtor1{66});
285+
CHECK((arr5[2].value == 39 && arr5[3].value == 39));
286+
287+
CHECK((arr5[10].value == 66 && arr5[22].value == 66));
288+
264289
AXLOGI("Access uninitialzed object membmer done (non optimized build or non-Apple platforms)");
265290
}
266291
__CATCH
@@ -273,21 +298,6 @@ TEST_SUITE("tlx/Containers")
273298
}
274299
__ENDTRY;
275300

276-
tlx::pod_vector<TrivalCtor1> arr5;
277-
arr5.resize(2);
278-
279-
#ifndef __APPLE__
280-
CHECK((arr5[0].value != 0 && arr5[1].value != 0));
281-
#endif
282-
// we can safe access initialized member without exception catch
283-
arr5.resize(4, TrivalCtor1{39});
284-
CHECK((arr5[2].value == 39 && arr5[3].value == 39));
285-
286-
arr5.resize(128, TrivalCtor1{66});
287-
CHECK((arr5[2].value == 39 && arr5[3].value == 39));
288-
289-
CHECK((arr5[10].value == 66 && arr5[22].value == 66));
290-
291301
// shoud report compile error, non trivial dtors types can't use tlx::pod_vector
292302
// tlx::pod_vector<NonTrivialDtor1> arr6;
293303

0 commit comments

Comments
 (0)