aboutsummaryrefslogtreecommitdiffstats
path: root/vbench/src/tests/handler_thread/handler_thread_test.cpp
blob: bd36556efa27f933885f121574aa8dfbecdf274a (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>
#include <vbench/test/all.h>
#include <vespa/vespalib/util/time.h>

using namespace vbench;

struct MyHandler : Handler<int> {
    std::vector<int> values;
    ~MyHandler() override;
    void handle(std::unique_ptr<int> value) override {
        values.push_back(*value);
        std::this_thread::sleep_for(10ms);
    }
};

MyHandler::~MyHandler() = default;

VESPA_THREAD_STACK_TAG(test_thread);

TEST("handler thread") {
    MyHandler handler;
    HandlerThread<int> th(handler, test_thread);
    th.handle(std::unique_ptr<int>(new int(1)));
    th.handle(std::unique_ptr<int>(new int(2)));
    th.handle(std::unique_ptr<int>(new int(3)));
    th.join();
    th.handle(std::unique_ptr<int>(new int(4)));
    th.handle(std::unique_ptr<int>(new int(5)));
    ASSERT_EQUAL(3u, handler.values.size());
    EXPECT_EQUAL(1, handler.values[0]);
    EXPECT_EQUAL(2, handler.values[1]);
    EXPECT_EQUAL(3, handler.values[2]);
}

TEST_MAIN() { TEST_RUN_ALL(); }