aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/atexit_ut.cpp
blob: d577b27a2a5ee0584b8c6d24915c92363a6c3f5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <library/cpp/testing/unittest/registar.h>

#include "atexit.h"
 
#include <errno.h>

#ifdef _win_ 
// not implemented
#else 
    #include <sys/types.h>
    #include <sys/wait.h>
#endif //_win_ 
 
#include <stdio.h> 
 
#ifdef _win_ 
// not implemented
#else 
struct TAtExitParams { 
    TAtExitParams(int fd_, const char* str_) 
        : fd(fd_) 
        , str(str_) 
    { 
    } 
 
    int fd; 
    const char* str; 
}; 
 
void MyAtExitFunc(void* ptr) {
    THolder<TAtExitParams> params{static_cast<TAtExitParams*>(ptr)};
    if (write(params->fd, params->str, strlen(params->str)) < 0) {
        abort();
    }
} 
#endif 
 
class TAtExitTest: public TTestBase {
    UNIT_TEST_SUITE(TAtExitTest);
    UNIT_TEST(TestAtExit)
    UNIT_TEST_SUITE_END(); 
 
    void TestAtExit() { 
#ifdef _win_ 
// not implemented
#else 
        int ret; 
        int pipefd[2]; 

        ret = pipe(pipefd); 
        UNIT_ASSERT(ret == 0); 
 
        pid_t pid = fork(); 
 
        if (pid < 0) { 
            UNIT_ASSERT(0); 
        } 
 
        if (pid > 0) { 
            char data[1024]; 
            int last = 0; 
 
            close(pipefd[1]); 
 
            while (read(pipefd[0], data + last++, 1) > 0 && last < 1024) { 
            } 
            data[--last] = 0; 

            UNIT_ASSERT(strcmp(data, "High prio\nMiddle prio\nLow-middle prio\nLow prio\nVery low prio\n") == 0); 
        } else { 
            close(pipefd[0]); 
 
            AtExit(MyAtExitFunc, new TAtExitParams(pipefd[1], "Low prio\n"), 3); 
            AtExit(MyAtExitFunc, new TAtExitParams(pipefd[1], "Middle prio\n"), 5); 
            AtExit(MyAtExitFunc, new TAtExitParams(pipefd[1], "High prio\n"), 7); 
            AtExit(MyAtExitFunc, new TAtExitParams(pipefd[1], "Very low prio\n"), 1); 
            AtExit(MyAtExitFunc, new TAtExitParams(pipefd[1], "Low-middle prio\n"), 4); 
 
            exit(0); 
        } 
#endif //_win_ 
    } 
}; 
 
UNIT_TEST_SUITE_REGISTRATION(TAtExitTest);