summaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/testkit/time_bomb.cpp
blob: 9d4b2ea30c8b5d5d016660161008f041fb57503a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "time_bomb.h"
#include <vespa/log/log.h>
LOG_SETUP(".vespalib.testkit.time_bomb");

namespace vespalib {

namespace {

void bomb(Gate &gate, vespalib::duration timeout) {
    if (timeout > 5s) {
        if (gate.await(timeout - 5s)) {
            return;
        }
    }
    size_t countdown = std::min(count_s(timeout), 5l);
    while (countdown > 0) {
        fprintf(stderr, "...%zu...\n", countdown--);
        if (gate.await(1s)) {
            return;
        }
    }
    fprintf(stderr, "BOOM!\n");
    LOG_ABORT("should not be reached");
}

} // namespace vespalib::<unnamed>

TimeBomb::TimeBomb(duration timeout)
    : _gate(),
      _thread(bomb, std::ref(_gate), timeout)
{
}

TimeBomb::~TimeBomb()
{
    _gate.countDown();
    _thread.join();
}

} // namespace vespalib