summaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/tests/shutdownguard/shutdownguard_test.cpp
blob: 79777cdd53f5e72a782cb42d39b1f665da3ef1d7 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/shutdownguard.h>
#include <thread>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>

using namespace vespalib;

TEST_SETUP(Test);

int
Test::Main()
{
    TEST_INIT("shutdownguard_test");
    {
        ShutdownGuard farFuture(1000000s);
        std::this_thread::sleep_for(20ms);
    }
    EXPECT_TRUE(true);
    pid_t child = fork();
    if (child == 0) {
        ShutdownGuard soon(30ms);
        for (int i = 0; i < 1000; ++i) {
            std::this_thread::sleep_for(20ms);
        }
        std::_Exit(0);
    }
    for (int i = 0; i < 1000; ++i) {
        std::this_thread::sleep_for(20ms);
        int stat = 0;
        if (waitpid(child, &stat, WNOHANG) == child) {
            EXPECT_TRUE(WIFEXITED(stat));
            EXPECT_EQUAL(1, WEXITSTATUS(stat));
            break;
        }
        EXPECT_TRUE(i < 800);
    }
    TEST_DONE();
}