forked from Abc-Arbitrage/Disruptor-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequenceReportingCallbackTests.cpp
More file actions
90 lines (66 loc) · 2.87 KB
/
Copy pathSequenceReportingCallbackTests.cpp
File metadata and controls
90 lines (66 loc) · 2.87 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
#include "stdafx.h"
#include "Disruptor/BatchEventProcessor.h"
#include "Disruptor/ISequenceReportingEventHandler.h"
#include "Disruptor/RingBuffer.h"
#include "Disruptor.TestTools/ManualResetEvent.h"
#include "StubEvent.h"
namespace Disruptor
{
namespace Tests
{
struct SequenceReportingCallbackTestsFixture
{
std::shared_ptr< ManualResetEvent > m_callbackSignal = std::make_shared< ManualResetEvent >(false);
std::shared_ptr< ManualResetEvent > m_onEndOfBatchSignal = std::make_shared< ManualResetEvent >(false);
};
class TestSequenceReportingEventHandler : public ISequenceReportingEventHandler< StubEvent >
{
public:
TestSequenceReportingEventHandler(const std::shared_ptr< ManualResetEvent >& callbackSignal,
const std::shared_ptr< ManualResetEvent >& onEndOfBatchSignal)
: m_callbackSignal(callbackSignal)
, m_onEndOfBatchSignal(onEndOfBatchSignal)
{
}
void setSequenceCallback(const std::shared_ptr< ISequence >& sequenceTrackerCallback) override
{
m_sequenceCallback = sequenceTrackerCallback;
}
void onEvent(StubEvent& /*evt*/, std::int64_t sequence, bool endOfBatch) override
{
m_sequenceCallback->setValue(sequence);
m_callbackSignal->set();
if (endOfBatch)
{
m_onEndOfBatchSignal->waitOne();
}
}
private:
std::shared_ptr< ISequence > m_sequenceCallback;
std::shared_ptr< ManualResetEvent > m_callbackSignal;
std::shared_ptr< ManualResetEvent > m_onEndOfBatchSignal;
};
} // namespace Tests
} // namespace Disruptor
using namespace Disruptor;
using namespace Disruptor::Tests;
BOOST_FIXTURE_TEST_SUITE(SequenceReportingCallbackTests, SequenceReportingCallbackTestsFixture)
BOOST_AUTO_TEST_CASE(ShouldReportProgressByUpdatingSequenceViaCallback)
{
auto ringBuffer = RingBuffer< StubEvent >::createMultiProducer([] { return StubEvent(-1); }, 16);
auto sequenceBarrier = ringBuffer->newBarrier();
auto handler = std::make_shared< TestSequenceReportingEventHandler >(m_callbackSignal, m_onEndOfBatchSignal);
auto batchEventProcessor = std::make_shared< BatchEventProcessor< StubEvent > >(ringBuffer, sequenceBarrier, handler);
ringBuffer->addGatingSequences({ batchEventProcessor->sequence() });
std::thread thread([&] { batchEventProcessor->run(); });
BOOST_CHECK_EQUAL(-1L, batchEventProcessor->sequence()->value());
ringBuffer->publish(ringBuffer->next());
m_callbackSignal->waitOne();
BOOST_CHECK_EQUAL(0L, batchEventProcessor->sequence()->value());
m_onEndOfBatchSignal->set();
BOOST_CHECK_EQUAL(0L, batchEventProcessor->sequence()->value());
batchEventProcessor->halt();
if (thread.joinable())
thread.join();
}
BOOST_AUTO_TEST_SUITE_END()