OILS / mycpp / gc_iolib_test.cc View on Github | oilshell.org

109 lines, 61 significant
1#include "mycpp/gc_iolib.h"
2
3#include <unistd.h>
4
5#include "mycpp/gc_alloc.h" // gHeap
6#include "vendor/greatest.h"
7
8TEST signal_test() {
9 iolib::SignalSafe* signal_safe = iolib::InitSignalSafe();
10
11 {
12 List<int>* q = signal_safe->TakePendingSignals();
13 ASSERT(q != nullptr);
14 ASSERT_EQ(0, len(q));
15 signal_safe->ReuseEmptyList(q);
16 }
17
18 pid_t mypid = getpid();
19
20 iolib::RegisterSignalInterest(SIGUSR1);
21 iolib::RegisterSignalInterest(SIGUSR2);
22
23 kill(mypid, SIGUSR1);
24 ASSERT_EQ(SIGUSR1, signal_safe->LastSignal());
25
26 kill(mypid, SIGUSR2);
27 ASSERT_EQ(SIGUSR2, signal_safe->LastSignal());
28
29 {
30 List<int>* q = signal_safe->TakePendingSignals();
31 ASSERT(q != nullptr);
32 ASSERT_EQ(2, len(q));
33 ASSERT_EQ(SIGUSR1, q->at(0));
34 ASSERT_EQ(SIGUSR2, q->at(1));
35
36 q->clear();
37 signal_safe->ReuseEmptyList(q);
38 }
39
40 iolib::sigaction(SIGUSR1, SIG_IGN);
41 kill(mypid, SIGUSR1);
42 {
43 List<int>* q = signal_safe->TakePendingSignals();
44 ASSERT(q != nullptr);
45 ASSERT(len(q) == 0);
46 signal_safe->ReuseEmptyList(q);
47 }
48 iolib::sigaction(SIGUSR2, SIG_IGN);
49
50 iolib::RegisterSignalInterest(SIGWINCH);
51
52 kill(mypid, SIGWINCH);
53 ASSERT_EQ(iolib::UNTRAPPED_SIGWINCH, signal_safe->LastSignal());
54
55 signal_safe->SetSigWinchCode(SIGWINCH);
56
57 kill(mypid, SIGWINCH);
58 ASSERT_EQ(SIGWINCH, signal_safe->LastSignal());
59 {
60 List<int>* q = signal_safe->TakePendingSignals();
61 ASSERT(q != nullptr);
62 ASSERT_EQ(2, len(q));
63 ASSERT_EQ(SIGWINCH, q->at(0));
64 ASSERT_EQ(SIGWINCH, q->at(1));
65 }
66
67 PASS();
68}
69
70TEST signal_safe_test() {
71 iolib::SignalSafe signal_safe;
72
73 List<int>* received = signal_safe.TakePendingSignals();
74
75 // We got now signals
76 ASSERT_EQ_FMT(0, len(received), "%d");
77
78 // The existing queue is of length 0
79 ASSERT_EQ_FMT(0, len(signal_safe.pending_signals_), "%d");
80
81 // Capacity is a ROUND NUMBER from the allocator's POV
82 // There's no convenient way to test the obj_len we pass to gHeap.Allocate,
83 // but it should be (1022 + 2) * 4.
84 ASSERT_EQ_FMT(1022, signal_safe.pending_signals_->capacity_, "%d");
85
86 // Register too many signals
87 for (int i = 0; i < iolib::kMaxPendingSignals + 10; ++i) {
88 signal_safe.UpdateFromSignalHandler(SIGINT);
89 }
90
91 PASS();
92}
93
94GREATEST_MAIN_DEFS();
95
96int main(int argc, char** argv) {
97 gHeap.Init();
98
99 GREATEST_MAIN_BEGIN();
100
101 RUN_TEST(signal_test);
102 RUN_TEST(signal_safe_test);
103
104 gHeap.CleanProcessExit();
105
106 GREATEST_MAIN_END(); /* display results */
107
108 return 0;
109}