aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/executor/blockingthreadstackexecutor_test.cpp
blob: 751f4356e5abdeecb8d85ec6957d4e7369f6ff8e (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright Vespa.ai. 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/testkit/time_bomb.h>
#include <vespa/vespalib/util/blockingthreadstackexecutor.h>
#include <vespa/vespalib/util/executor.h>
#include <vespa/vespalib/util/backtrace.h>
#include <vespa/vespalib/util/size_literals.h>
#include <thread>

using namespace vespalib;

constexpr vespalib::duration waitTime = 30s;

class MyTask : public Executor::Task
{
private:
    Gate &_entryGate;
    CountDownLatch &_exitLatch;

public:
    MyTask(Gate &entryGate, CountDownLatch &exitLatch)
        : _entryGate(entryGate),
          _exitLatch(exitLatch)
    {}
    void run() override {
        _entryGate.await(waitTime);
        _exitLatch.countDown();
    }
    static Task::UP create(Gate &entryGate, CountDownLatch &exitLatch) {
        return std::make_unique<MyTask>(entryGate, exitLatch);
    }
};

void
blockedExecute(BlockingThreadStackExecutor *executor, Gate *workersEntryGate, CountDownLatch *workersExitLatch, Gate *exitGate)
{
    executor->execute(MyTask::create(*workersEntryGate, *workersExitLatch)); // this should be a blocking call
    exitGate->countDown();
}

using ThreadUP = std::unique_ptr<std::thread>;

struct Fixture
{
    BlockingThreadStackExecutor executor;
    Gate workersEntryGate;
    CountDownLatch workersExitLatch;
    Gate blockedExecuteGate;

    Fixture(uint32_t taskLimit, uint32_t tasksToWaitFor)
        : executor(1, taskLimit),
          workersEntryGate(),
          workersExitLatch(tasksToWaitFor),
          blockedExecuteGate()
    {}
    void execute(size_t numTasks) {
        for (size_t i = 0; i < numTasks; ++i) {
            executor.execute(MyTask::create(workersEntryGate, workersExitLatch));
        }
    }
    void updateTaskLimit(uint32_t taskLimit) {
        executor.setTaskLimit(taskLimit);
    }
    void openForWorkers() {
        workersEntryGate.countDown();
    }
    void waitForWorkers() {
        workersExitLatch.await(waitTime);
    }
    void assertExecuteIsBlocked() {
        blockedExecuteGate.await(10ms);
        EXPECT_EQUAL(1u, blockedExecuteGate.getCount());
    }
    void waitForExecuteIsFinished() {
        blockedExecuteGate.await(waitTime);
        EXPECT_EQUAL(0u, blockedExecuteGate.getCount());
    }
    ThreadUP blockedExecuteThread() {
        return std::make_unique<std::thread>(blockedExecute, &executor, &workersEntryGate, &workersExitLatch, &blockedExecuteGate);
    }
    void blockedExecuteAndWaitUntilFinished() {
        ThreadUP thread = blockedExecuteThread();
        TEST_DO(assertExecuteIsBlocked());
        openForWorkers();
        TEST_DO(waitForExecuteIsFinished());
        thread->join();
        waitForWorkers();
    }
};

TEST_F("require that execute() blocks when task limits is reached", Fixture(3, 4))
{
    f.execute(3);
    f.blockedExecuteAndWaitUntilFinished();
}

TEST_F("require that task limit can be increased", Fixture(3, 5))
{
    f.execute(3);
    f.updateTaskLimit(4);
    f.execute(1);
    f.blockedExecuteAndWaitUntilFinished();
}

TEST_F("require that task limit can be decreased", Fixture(3, 3))
{
    f.execute(2);
    f.updateTaskLimit(2);
    f.blockedExecuteAndWaitUntilFinished();
}

vespalib::string get_worker_stack_trace(BlockingThreadStackExecutor &executor) {
    struct StackTraceTask : public Executor::Task {
        vespalib::string &trace;
        explicit StackTraceTask(vespalib::string &t) : trace(t) {}
        void run() override { trace = getStackTrace(0); }
    };
    vespalib::string trace;
    executor.execute(std::make_unique<StackTraceTask>(trace));
    executor.sync();
    return trace;
}

VESPA_THREAD_STACK_TAG(my_stack_tag);

TEST_F("require that executor has appropriate default thread stack tag", BlockingThreadStackExecutor(1, 10)) {
    vespalib::string trace = get_worker_stack_trace(f1);
    if (!EXPECT_TRUE(trace.find("unnamed_blocking_executor") != vespalib::string::npos)) {
        fprintf(stderr, "%s\n", trace.c_str());
    }
}

TEST_F("require that executor thread stack tag can be set", BlockingThreadStackExecutor(1, 10, my_stack_tag)) {
    vespalib::string trace = get_worker_stack_trace(f1);
    if (!EXPECT_TRUE(trace.find("my_stack_tag") != vespalib::string::npos)) {
        fprintf(stderr, "%s\n", trace.c_str());
    }
}

TEST_F("require that tasks posted from internal worker thread will not block executor", TimeBomb(60)) {
    size_t cnt = 0;
    Gate fork_done;
    BlockingThreadStackExecutor executor(1, 10);
    struct IncTask : Executor::Task {
        size_t &cnt;
        IncTask(size_t &cnt_in) : cnt(cnt_in) {}
        void run() override { ++cnt; }
    };
    struct ForkTask : Executor::Task {
        Executor &executor;
        Gate &fork_done;
        size_t &cnt;
        ForkTask(Executor &executor_in, Gate &fork_done_in, size_t &cnt_in)
            : executor(executor_in), fork_done(fork_done_in), cnt(cnt_in) {}
        void run() override {
            for (size_t i = 0; i < 32; ++i) {
                executor.execute(std::make_unique<IncTask>(cnt));
            }
            fork_done.countDown();
        }
    };
    // post 32 internal tasks on a blocking executor with tasklimit 10
    executor.execute(std::make_unique<ForkTask>(executor, fork_done, cnt));
    fork_done.await();
    executor.sync();
    EXPECT_EQUAL(cnt, 32u);
}

TEST_MAIN() { TEST_RUN_ALL(); }