summaryrefslogtreecommitdiffstats
path: root/util/system/atexit_ut.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/atexit_ut.cpp
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/atexit_ut.cpp')
-rw-r--r--util/system/atexit_ut.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/util/system/atexit_ut.cpp b/util/system/atexit_ut.cpp
new file mode 100644
index 00000000000..953f4328110
--- /dev/null
+++ b/util/system/atexit_ut.cpp
@@ -0,0 +1,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);