aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/testkit-mt/testkit-mt_test.cpp
blob: 385a69eba57c0945fb89f502ceaa3f7a2e0f99d9 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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>

using namespace vespalib;

void checkThreads(size_t thread_id, size_t num_threads, std::vector<size_t> &state) {
    if (thread_id == 0) {
        for (size_t i = 0; i < num_threads; ++i) {
            state.push_back(num_threads);
        }
    }
    TEST_BARRIER();
    ASSERT_EQUAL(num_threads, state.size());
    state[thread_id] = thread_id;
    TEST_BARRIER();
    if (thread_id == 0) {
        for (size_t i = 0; i < num_threads; ++i) {
            EXPECT_EQUAL(i, state[i]);
        }
    }
}

TEST_MT("multi-threaded test without fixtures", 100) {
    static std::vector<size_t> state;
    TEST_DO(checkThreads(thread_id, num_threads, state));
}

TEST_MT_F("multi-threaded test with 1 fixture", 100, std::vector<size_t>()) {
    EXPECT_EQUAL(&f, &f1);
    TEST_DO(checkThreads(thread_id, num_threads, f1));
}

TEST_MT_FF("multi-threaded test with 2 fixtures", 100, std::vector<size_t>(), size_t(5)) {
    EXPECT_EQUAL(5u, f2);
    TEST_DO(checkThreads(thread_id, num_threads, f1));
}

TEST_MT_FFF("multi-threaded test with 3 fixtures", 100, std::vector<size_t>(), size_t(5), size_t(10)) {
    EXPECT_EQUAL(5u, f2);
    EXPECT_EQUAL(10u, f3);
    TEST_DO(checkThreads(thread_id, num_threads, f1));
}

TEST_MT_F("let fixture pick up thread count", 14, size_t(num_threads)) {
    EXPECT_EQUAL(num_threads, f1);
}

TEST_MT_FF("let fixtures pick up thread count", 14, size_t(num_threads), size_t(num_threads)) {
    EXPECT_EQUAL(num_threads, f1);
    EXPECT_EQUAL(num_threads, f2);
}

TEST_MT_FFF("let fixturess pick up thread count", 14, size_t(num_threads),
            size_t(num_threads), size_t(num_threads))
{
    EXPECT_EQUAL(num_threads, f1);
    EXPECT_EQUAL(num_threads, f2);
    EXPECT_EQUAL(num_threads, f3);
}

IGNORE_TEST_MT("partial unwind breaks barrier", 10) {
    if (thread_id == 5) {
        TEST_FATAL("partial unwind");
    }
    TEST_BARRIER();
}

IGNORE_TEST_MT("ignore multithreaded test with no fixtures", 10) {
    EXPECT_TRUE(true);
}

IGNORE_TEST_MT_F("ignore multithreaded test with 1 fixture", 10, int(5)) {
    EXPECT_EQUAL(5, f1);
}

IGNORE_TEST_MT_FF("ignore multithreaded test with 2 fixtures", 10, int(5), int(10)) {
    EXPECT_EQUAL(5, f1);
    EXPECT_EQUAL(10, f2);
}

IGNORE_TEST_MT_FFF("ignore multithreaded test with 3 fixtures", 10, int(5), int(10), int(15)) {
    EXPECT_EQUAL(5, f1);
    EXPECT_EQUAL(10, f2);
    EXPECT_EQUAL(15, f3);
}

TEST_MAIN() { TEST_RUN_ALL(); }