summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/thread/thread_test.cpp
blob: f7e8753fd86df0faa43d8a2dbaef206a23447e64 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/vespalib/util/thread.h>

using namespace vespalib;

VESPA_THREAD_STACK_TAG(test_agent_thread);

struct Agent : public Runnable {
    bool was_run;
    Agent() : was_run(false) {}
    void run() override {
        was_run = true;
    }
};

void my_fun(bool *was_run) {
    *was_run = true;
}

TEST("run vespalib::Runnable with init function") {
    Agent agent;
    {
        auto thread = Thread::start(agent, test_agent_thread);
    }
    EXPECT_TRUE(agent.was_run);
}

TEST("run custom function") {
    bool was_run = false;
    {
        auto thread = Thread::start(my_fun, &was_run);
    }
    EXPECT_TRUE(was_run);
}

TEST("join multiple times (including destructor)") {
    bool was_run = false;
    {
        auto thread = Thread::start(my_fun, &was_run);
        thread.join();
        thread.join();
        thread.join();
    }
    EXPECT_TRUE(was_run);
}

TEST_MAIN() { TEST_RUN_ALL(); }