forked from Abc-Arbitrage/Disruptor-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLifecycleAwareTests.cpp
More file actions
80 lines (58 loc) · 2.15 KB
/
LifecycleAwareTests.cpp
File metadata and controls
80 lines (58 loc) · 2.15 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
#include "stdafx.h"
#include "Disruptor/BatchEventProcessor.h"
#include "Disruptor/IEventHandler.h"
#include "Disruptor/IgnoreExceptionHandler.h"
#include "Disruptor/ILifecycleAware.h"
#include "Disruptor/RingBuffer.h"
#include "Disruptor.TestTools/ManualResetEvent.h"
#include "StubEvent.h"
#include "StubExecutor.h"
using namespace Disruptor;
using namespace ::Disruptor::Tests;
BOOST_AUTO_TEST_SUITE(LifecycleAwareTests)
class LifecycleAwareEventHandler : public IEventHandler< StubEvent >, public ILifecycleAware
{
public:
std::int32_t startCounter() const { return m_startCounter; }
std::int32_t shutdownCounter() const { return m_shutdownCounter; }
LifecycleAwareEventHandler(ManualResetEvent& startSignal, ManualResetEvent& shutdownSignal)
: m_startSignal(startSignal)
, m_shutdownSignal(shutdownSignal)
, m_startCounter(0)
, m_shutdownCounter(0)
{}
void onEvent(StubEvent&, std::int64_t, bool) override
{}
void onStart() override
{
++m_startCounter;
m_startSignal.set();
}
void onShutdown() override
{
++m_shutdownCounter;
m_shutdownSignal.set();
}
private:
ManualResetEvent& m_startSignal;
ManualResetEvent& m_shutdownSignal;
std::int32_t m_startCounter;
std::int32_t m_shutdownCounter;
};
BOOST_AUTO_TEST_CASE(ShouldNotifyOfBatchProcessorLifecycle)
{
ManualResetEvent startSignal(false);
ManualResetEvent shutdownSignal(false);
auto ringBuffer = std::make_shared< RingBuffer< StubEvent > >([] { return StubEvent(0); }, 16);
auto sequenceBarrier = ringBuffer->newBarrier();
auto eventHandler = std::make_shared< LifecycleAwareEventHandler >(startSignal, shutdownSignal);
auto batchEventProcessor = std::make_shared< BatchEventProcessor< StubEvent > >(ringBuffer, sequenceBarrier, eventHandler);
auto thread = std::thread([&] { batchEventProcessor->run(); });
startSignal.waitOne();
batchEventProcessor->halt();
shutdownSignal.waitOne();
thread.join();
BOOST_CHECK_EQUAL(eventHandler->startCounter(), 1);
BOOST_CHECK_EQUAL(eventHandler->shutdownCounter(), 1);
}
BOOST_AUTO_TEST_SUITE_END()