forked from Abc-Arbitrage/Disruptor-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisruptorFixture.cpp
More file actions
163 lines (135 loc) · 6.09 KB
/
DisruptorFixture.cpp
File metadata and controls
163 lines (135 loc) · 6.09 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
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
#include "stdafx.h"
#include "DisruptorFixture.h"
namespace Disruptor
{
namespace Tests
{
DisruptorFixture::DisruptorFixture()
{
m_lastPublishedEvent = nullptr;
m_ringBuffer = nullptr;
m_delayedEventHandlers.clear();
m_testWorkHandlers.clear();
m_executor = std::make_shared< StubExecutor >();
m_disruptor = std::make_shared< disruptor< TestEvent > >([] { return TestEvent(); }, 4, m_executor);
}
DisruptorFixture::~DisruptorFixture()
{
for (auto&& delayedEventHandler : m_delayedEventHandlers)
{
delayedEventHandler->stopWaiting();
}
for (auto&& testWorkHandler : m_testWorkHandlers)
{
testWorkHandler->stopWaiting();
}
m_disruptor->halt();
m_executor->joinAllThreads();
}
DisruptorFixture::TempEventHandler::TempEventHandler(const std::shared_ptr< disruptor< TestEvent > >& disruptor,
const std::vector< std::shared_ptr< std::int64_t > >& remainingCapacity)
: m_disruptor(disruptor)
, m_remainingCapacity(remainingCapacity)
{
}
void DisruptorFixture::TempEventHandler::onEvent(TestEvent& /*data*/, std::int64_t /*sequence*/, bool /*endOfBatch*/)
{
*m_remainingCapacity[0] = m_disruptor->ringBuffer()->getRemainingCapacity();
}
DisruptorFixture::EventProcessorFactory::EventProcessorFactory(const std::shared_ptr< disruptor< TestEvent > >& disruptor,
const std::shared_ptr< IEventHandler< TestEvent > >& eventHandler,
std::int32_t sequenceLength)
: m_disruptor(disruptor)
, m_eventHandler(eventHandler)
, m_sequenceLength(sequenceLength)
{
}
std::shared_ptr< IEventProcessor > DisruptorFixture::EventProcessorFactory::createEventProcessor(const std::shared_ptr< RingBuffer< TestEvent > >& ringBuffer,
const std::vector< std::shared_ptr< ISequence > >& barrierSequences)
{
BOOST_CHECK_MESSAGE((size_t)m_sequenceLength == barrierSequences.size(), "Should not have had any barrier sequences");
return std::make_shared< BatchEventProcessor< TestEvent > >(m_disruptor->ringBuffer(), ringBuffer->newBarrier(barrierSequences), m_eventHandler);
}
std::shared_ptr< TestWorkHandler > DisruptorFixture::createTestWorkHandler()
{
auto testWorkHandler = std::make_shared< TestWorkHandler >();
m_testWorkHandlers.push_back(testWorkHandler);
return testWorkHandler;
}
void DisruptorFixture::ensureTwoEventsProcessedAccordingToDependencies(const std::shared_ptr< CountdownEvent >& countDownLatch,
const std::initializer_list< std::shared_ptr< DelayedEventHandler > >& dependencies)
{
publishEvent();
publishEvent();
for (auto&& dependency : dependencies)
{
assertThatCountDownLatchEquals(countDownLatch, 2L);
dependency->processEvent();
dependency->processEvent();
}
assertThatCountDownLatchIsZero(countDownLatch);
}
void DisruptorFixture::assertProducerReaches(const std::shared_ptr< StubPublisher >& stubPublisher, std::int32_t expectedPublicationCount, bool strict)
{
auto loopStart = ClockConfig::Clock::now();
while (stubPublisher->getPublicationCount() < expectedPublicationCount && ClockConfig::Clock::now() - loopStart < std::chrono::milliseconds(5))
{
std::this_thread::yield();
}
if (strict)
{
BOOST_CHECK_EQUAL(stubPublisher->getPublicationCount(), expectedPublicationCount);
}
else
{
auto actualPublicationCount = stubPublisher->getPublicationCount();
BOOST_CHECK_MESSAGE(actualPublicationCount >= expectedPublicationCount,
"Producer reached unexpected count. Expected at least " << expectedPublicationCount << " but only reached " << actualPublicationCount);
}
}
DisruptorFixture::EventTranslator::EventTranslator(DisruptorFixture& disruptorTests)
: m_disruptorTests(disruptorTests)
{
}
void DisruptorFixture::EventTranslator::translateTo(TestEvent& eventData, std::int64_t /*sequence*/)
{
m_disruptorTests.m_lastPublishedEvent = &eventData;
}
TestEvent& DisruptorFixture::publishEvent()
{
if (m_ringBuffer == nullptr)
{
m_ringBuffer = m_disruptor->start();
for (auto&& eventHandler : m_delayedEventHandlers)
{
eventHandler->awaitStart();
}
}
m_disruptor->publishEvent(std::make_shared< EventTranslator >(*this));
return *m_lastPublishedEvent;
}
std::exception DisruptorFixture::waitFor(const std::shared_ptr< AtomicReference< std::exception > >& reference)
{
while (!reference->read())
{
std::this_thread::yield();
}
return reference->read().get();
}
std::shared_ptr< DelayedEventHandler > DisruptorFixture::createDelayedEventHandler()
{
auto delayedEventHandler = std::make_shared< DelayedEventHandler >();
m_delayedEventHandlers.push_back(delayedEventHandler);
return delayedEventHandler;
}
void DisruptorFixture::assertThatCountDownLatchEquals(const std::shared_ptr< CountdownEvent >& countDownLatch, std::int64_t expectedCountDownValue) const
{
BOOST_CHECK_EQUAL(countDownLatch->currentCount(), expectedCountDownValue);
}
void DisruptorFixture::assertThatCountDownLatchIsZero(const std::shared_ptr< CountdownEvent >& countDownLatch) const
{
auto released = countDownLatch->wait(std::chrono::seconds(m_timeoutInSeconds));
BOOST_CHECK_MESSAGE(released == true, "Batch handler did not receive entries: " << countDownLatch->currentCount());
}
} // namespace Tests
} // namespace Disruptor