Repository navigation
Expand file tree
/
Copy pathWorkerPoolTests.cpp
More file actions
110 lines (82 loc) · 2.59 KB
/
Copy pathWorkerPoolTests.cpp
File metadata and controls
110 lines (82 loc) · 2.59 KB
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
#include "stdafx.h"
#include "Disruptor/BasicExecutor.h"
#include "Disruptor/FatalExceptionHandler.h"
#include "Disruptor/RoundRobinThreadAffinedTaskScheduler.h"
#include "Disruptor/WorkerPool.h"
namespace Disruptor
{
namespace Tests
{
class AtomicLong
{
public:
AtomicLong() : m_value(0)
{}
AtomicLong(const AtomicLong& other)
: m_value(other.value())
{}
void operator=(const AtomicLong& other)
{
m_value = other.value();
}
std::int64_t value() const
{
return m_value;
}
void increment()
{
++m_value;
}
private:
std::atomic< std::int64_t > m_value;
};
class AtomicLongWorkHandler : public IWorkHandler< AtomicLong >
{
public:
void onEvent(AtomicLong& evt) override
{
evt.increment();
}
};
} // namespace Tests
} // namespace Disruptor
using namespace Disruptor;
using namespace Disruptor::Tests;
BOOST_AUTO_TEST_SUITE(WorkerPoolTests)
BOOST_AUTO_TEST_CASE(ShouldProcessEachMessageByOnlyOneWorker)
{
auto taskScheduler = std::make_shared< RoundRobinThreadAffinedTaskScheduler >();
taskScheduler->start(2);
WorkerPool< AtomicLong > pool
(
[] { return AtomicLong(); },
std::make_shared< FatalExceptionHandler< AtomicLong > >(),
{ std::make_shared< AtomicLongWorkHandler >(), std::make_shared< AtomicLongWorkHandler >() }
);
auto ringBuffer = pool.start(std::make_shared< BasicExecutor >(taskScheduler));
ringBuffer->next();
ringBuffer->next();
ringBuffer->publish(0);
ringBuffer->publish(1);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
BOOST_CHECK_EQUAL((*ringBuffer)[0].value(), 1L);
BOOST_CHECK_EQUAL((*ringBuffer)[1].value(), 1L);
}
BOOST_AUTO_TEST_CASE(ShouldProcessOnlyOnceItHasBeenPublished)
{
auto taskScheduler = std::make_shared< RoundRobinThreadAffinedTaskScheduler >();
taskScheduler->start(2);
WorkerPool< AtomicLong > pool
(
[] { return AtomicLong(); },
std::make_shared< FatalExceptionHandler< AtomicLong > >(),
{ std::make_shared< AtomicLongWorkHandler >(), std::make_shared< AtomicLongWorkHandler >() }
);
auto ringBuffer = pool.start(std::make_shared< BasicExecutor >(taskScheduler));
ringBuffer->next();
ringBuffer->next();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
BOOST_CHECK_EQUAL((*ringBuffer)[0].value(), 0L);
BOOST_CHECK_EQUAL((*ringBuffer)[1].value(), 0L);
}
BOOST_AUTO_TEST_SUITE_END()