Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Reactor.ut.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Reactor.hpp"
18
22
23#include <boost/test/unit_test.hpp>
24
25using namespace std;
26using namespace toolbox;
27
28namespace {
29
30struct TestHandler : RefCount<TestHandler, ThreadUnsafePolicy> {
31 void on_input(CyclTime /*now*/, int fd, unsigned /*events*/)
32 {
33 char buf[4];
34 os::recv(fd, buf, 4, 0);
35 if (strcmp(buf, "foo") == 0) {
36 ++matches;
37 }
38 }
39 int matches{};
40};
41
42} // namespace
43
45
47{
48 using namespace literals::chrono_literals;
49
50 Reactor r{1024};
52
54 const auto sub = r.subscribe(*socks.second, EpollIn, bind<&TestHandler::on_input>(h.get()));
55
56 const auto now = CyclTime::now();
57 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 0);
58 BOOST_CHECK_EQUAL(h->matches, 0);
59
60 socks.first.send("foo", 4, 0);
61 socks.first.send("foo", 4, 0);
62 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 1);
63 BOOST_CHECK_EQUAL(h->matches, 1);
64 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 1);
65 BOOST_CHECK_EQUAL(h->matches, 2);
66
67 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 0);
68 BOOST_CHECK_EQUAL(h->matches, 2);
69
70 socks.first.send("foo", 4, 0);
71 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 1);
72 BOOST_CHECK_EQUAL(h->matches, 3);
73
74 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 0);
75 BOOST_CHECK_EQUAL(h->matches, 3);
76}
77
79{
80 using namespace literals::chrono_literals;
81
82 Reactor r{1024};
84
86 auto sub = r.subscribe(*socks.second, EpollIn | EpollEt, bind<&TestHandler::on_input>(h.get()));
87
88 const auto now = CyclTime::now();
89 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 0);
90 BOOST_CHECK_EQUAL(h->matches, 0);
91
92 socks.first.send("foo", 4, 0);
93 socks.first.send("foo", 4, 0);
94 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 1);
95 BOOST_CHECK_EQUAL(h->matches, 1);
96
97 // No notification for second message.
98 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 0);
99 BOOST_CHECK_EQUAL(h->matches, 1);
100
101 // Revert to level-triggered.
102 sub.set_events(EpollIn);
103 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 1);
104 BOOST_CHECK_EQUAL(h->matches, 2);
105
106 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 0);
107 BOOST_CHECK_EQUAL(h->matches, 2);
108
109 socks.first.send("foo", 4, 0);
110 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 1);
111 BOOST_CHECK_EQUAL(h->matches, 3);
112
113 BOOST_CHECK_EQUAL(r.poll(now, 0ms), 0);
114 BOOST_CHECK_EQUAL(h->matches, 3);
115}
116
118{
119 int i{0};
120 auto fn = [&i](CyclTime) { ++i; };
121
122 Reactor r{1024};
123
124 Hook h{bind(&fn)};
125 r.add_hook(h);
126
127 BOOST_CHECK_EQUAL(r.poll(CyclTime::now(), 0ms), 0);
129}
130
BOOST_CHECK_EQUAL(v.size(), 10U)
BOOST_AUTO_TEST_CASE(ReactorLevelCase)
Base class for atomic referenced counted objects.
Definition RefCount.hpp:61
STL namespace.
@ EpollIn
The associated file is available for read(2) operations.
Definition Epoll.hpp:115
std::pair< IoSock, IoSock > socketpair(ProtocolT protocol)
Definition IoSock.hpp:90
ssize_t recv(int sockfd, void *buf, std::size_t len, int flags, std::error_code &ec) noexcept
Receive a message from a socket.
Definition Socket.hpp:354
constexpr auto bind() noexcept
Definition Slot.hpp:92