Repository navigation
Expand file tree
/
Copy pathEventPollerTests.cpp
More file actions
112 lines (83 loc) · 3.23 KB
/
Copy pathEventPollerTests.cpp
File metadata and controls
112 lines (83 loc) · 3.23 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
#include "stdafx.h"
#include "Disruptor/ArgumentOutOfRangeException.h"
#include "Disruptor/BusySpinWaitStrategy.h"
#include "Disruptor/EventPoller.h"
#include "Disruptor/Sequence.h"
#include "Disruptor/SleepingWaitStrategy.h"
#include "Disruptor/RingBuffer.h"
#include "DataProviderMock.h"
#include "SequencerMock.h"
using namespace Disruptor;
using namespace Disruptor::Tests;
BOOST_AUTO_TEST_SUITE(EventPollerTests)
BOOST_AUTO_TEST_CASE(ShouldPollForEvents)
{
typedef std::int32_t TestDataType;
auto pollSequence = std::make_shared< Sequence >();
auto bufferSequence = std::make_shared< Sequence >();
auto gatingSequence = std::make_shared< Sequence >();
auto sequencerMock = std::make_shared< testing::NiceMock< SequencerMock< TestDataType > > >();
auto handled = false;
auto handler = [&](TestDataType&, std::int64_t, bool) -> bool
{
handled = true;
return false;
};
auto providerMock = std::make_shared< testing::NiceMock< DataProviderMock< TestDataType > > >();
auto poller = EventPoller< TestDataType >::newInstance(providerMock, sequencerMock, pollSequence, bufferSequence, { gatingSequence });
auto event = std::int32_t(42);
auto states = PollState::Idle;
EXPECT_CALL(*sequencerMock, cursor()).WillRepeatedly(testing::Invoke([&]()
{
switch (states)
{
case PollState::Processing:
return 0L;
case PollState::Gating:
return 0L;
case PollState::Idle:
return -1L;
default:
DISRUPTOR_THROW_ARGUMENT_OUT_OF_RANGE_EXCEPTION(states);
}
}));
EXPECT_CALL(*sequencerMock, getHighestPublishedSequence(0L, -1L)).WillRepeatedly(testing::Return(-1L));
EXPECT_CALL(*sequencerMock, getHighestPublishedSequence(0L, 0L)).WillRepeatedly(testing::Return(0L));
EXPECT_CALL(*providerMock, indexer(0)).WillRepeatedly(testing::ReturnRef(event));
// Initial State - nothing published.
states = PollState::Idle;
BOOST_CHECK_EQUAL(poller->poll(handler), PollState::Idle);
// Publish Event.
states = PollState::Gating;
bufferSequence->incrementAndGet();
BOOST_CHECK_EQUAL(poller->poll(handler), PollState::Gating);
states = PollState::Processing;
gatingSequence->incrementAndGet();
BOOST_CHECK_EQUAL(poller->poll(handler), PollState::Processing);
BOOST_CHECK_EQUAL(handled, true);
}
BOOST_AUTO_TEST_CASE(ShouldSuccessfullyPollWhenBufferIsFull)
{
typedef std::int32_t DataType;
auto handled = 0;
auto handler = [&](std::vector< DataType >&, std::int64_t, bool) -> bool
{
handled++;
return true;
};
auto factory = [] { return std::vector< DataType >(1); };
auto ringBuffer = RingBuffer< std::vector< DataType > >::createMultiProducer(factory, 0x4, std::make_shared< SleepingWaitStrategy >());
auto poller = ringBuffer->newPoller({});
ringBuffer->addGatingSequences({ poller->sequence() });
const auto count = 4;
for (auto i = 1; i <= count; ++i)
{
auto next = ringBuffer->next();
(*ringBuffer)[next][0] = i; //sucks
ringBuffer->publish(next);
}
// think of another thread
poller->poll(handler);
BOOST_CHECK_EQUAL(handled, 4);
}
BOOST_AUTO_TEST_SUITE_END()