aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/testkit/time_bomb.h
blob: 2a88090d64e0a7266e063a0692c7fb16844cc443 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/util/gate.h>
#include <thread>

namespace vespalib {

/**
 * A TimeBomb is used to protect against deadlocked unit tests. A
 * TimeBomb is constructed with a given number of seconds. If the
 * TimeBomb is not destructed before the time runs out, it will abort
 * the program. The recommended way to use this class is as a fixture
 * for multi-threaded tests where the test may hang if something is
 * wrong.
 **/
class TimeBomb
{
private:
    Gate _gate;
    std::thread _thread;
public:
    TimeBomb(size_t seconds) : TimeBomb(from_s(seconds)) {}
    TimeBomb(vespalib::duration duration);
    ~TimeBomb(); // defuse the bomb
};

} // namespace vespalib