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
|
// Copyright 2016 Yahoo Inc. 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>
using namespace vbench;
struct MyHandler : public Handler<int> {
int value;
MyHandler() : value(-1) {}
virtual void handle(std::unique_ptr<int> v) override { value = (v.get() != 0) ? *v : 0; }
};
struct Fetcher : public vespalib::Runnable {
Provider<int> &provider;
Handler<int> &handler;
Fetcher(Provider<int> &p, Handler<int> &h) : provider(p), handler(h) {}
virtual void run() override { handler.handle(provider.provide()); }
};
TEST("dispatcher") {
MyHandler dropped;
MyHandler handler1;
MyHandler handler2;
Dispatcher<int> dispatcher(dropped);
Fetcher fetcher1(dispatcher, handler1);
Fetcher fetcher2(dispatcher, handler2);
vespalib::Thread thread1(fetcher1);
vespalib::Thread thread2(fetcher2);
thread1.start();
EXPECT_TRUE(dispatcher.waitForThreads(1, 512));
thread2.start();
EXPECT_TRUE(dispatcher.waitForThreads(2, 512));
EXPECT_EQUAL(-1, dropped.value);
EXPECT_EQUAL(-1, handler1.value);
EXPECT_EQUAL(-1, handler2.value);
dispatcher.handle(std::unique_ptr<int>(new int(1)));
dispatcher.handle(std::unique_ptr<int>(new int(2)));
dispatcher.handle(std::unique_ptr<int>(new int(3)));
thread1.join();
thread2.join();
EXPECT_EQUAL(3, dropped.value);
EXPECT_EQUAL(2, handler1.value);
EXPECT_EQUAL(1, handler2.value);
dispatcher.close();
{
dispatcher.handle(std::unique_ptr<int>(new int(4)));
EXPECT_EQUAL(3, dropped.value);
MyHandler handler3;
Fetcher fetcher3(dispatcher, handler3);
EXPECT_EQUAL(-1, handler3.value);
fetcher3.run();
EXPECT_EQUAL(0, handler3.value);
}
}
TEST_FF("dispatcher poll timeout", MyHandler(), Dispatcher<int>(f1)) {
EXPECT_FALSE(f2.waitForThreads(1, 2));
}
TEST_MAIN() { TEST_RUN_ALL(); }
|