2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
20 * Copyright (C) 2022 ScyllaDB
23 #include <seastar/core/thread.hh>
24 #include <seastar/testing/test_case.hh>
25 #include <seastar/testing/thread_test_case.hh>
26 #include <seastar/core/abortable_fifo.hh>
27 #include <seastar/util/later.hh>
28 #include <boost/range/irange.hpp>
30 using namespace seastar
;
32 SEASTAR_TEST_CASE(test_no_abortable_operations
) {
33 internal::abortable_fifo
<int> fifo
;
35 BOOST_REQUIRE(fifo
.empty());
36 BOOST_REQUIRE_EQUAL(fifo
.size(), 0u);
37 BOOST_REQUIRE(!bool(fifo
));
41 BOOST_REQUIRE(!fifo
.empty());
42 BOOST_REQUIRE_EQUAL(fifo
.size(), 1u);
43 BOOST_REQUIRE(bool(fifo
));
44 BOOST_REQUIRE_EQUAL(fifo
.front(), 1);
49 BOOST_REQUIRE(!fifo
.empty());
50 BOOST_REQUIRE_EQUAL(fifo
.size(), 3u);
51 BOOST_REQUIRE(bool(fifo
));
52 BOOST_REQUIRE_EQUAL(fifo
.front(), 1);
56 BOOST_REQUIRE(!fifo
.empty());
57 BOOST_REQUIRE_EQUAL(fifo
.size(), 2u);
58 BOOST_REQUIRE(bool(fifo
));
59 BOOST_REQUIRE_EQUAL(fifo
.front(), 2);
63 BOOST_REQUIRE(!fifo
.empty());
64 BOOST_REQUIRE_EQUAL(fifo
.size(), 1u);
65 BOOST_REQUIRE(bool(fifo
));
66 BOOST_REQUIRE_EQUAL(fifo
.front(), 3);
70 BOOST_REQUIRE(fifo
.empty());
71 BOOST_REQUIRE_EQUAL(fifo
.size(), 0u);
72 BOOST_REQUIRE(!bool(fifo
));
74 return make_ready_future
<>();
77 SEASTAR_THREAD_TEST_CASE(test_abortable_operations
) {
78 std::vector
<int> expired
;
81 void operator()(int& v
) noexcept
{ e
.push_back(v
); }
84 internal::abortable_fifo
<int, my_expiry
> fifo(my_expiry
{expired
});
87 fifo
.push_back(1, as
);
89 BOOST_REQUIRE(!fifo
.empty());
90 BOOST_REQUIRE_EQUAL(fifo
.size(), 1u);
91 BOOST_REQUIRE(bool(fifo
));
92 BOOST_REQUIRE_EQUAL(fifo
.front(), 1);
97 BOOST_REQUIRE(fifo
.empty());
98 BOOST_REQUIRE_EQUAL(fifo
.size(), 0u);
99 BOOST_REQUIRE(!bool(fifo
));
100 BOOST_REQUIRE_EQUAL(expired
.size(), 1u);
101 BOOST_REQUIRE_EQUAL(expired
[0], 1);
107 fifo
.push_back(2, as
);
113 BOOST_REQUIRE(!fifo
.empty());
114 BOOST_REQUIRE_EQUAL(fifo
.size(), 2u);
115 BOOST_REQUIRE(bool(fifo
));
116 BOOST_REQUIRE_EQUAL(expired
.size(), 1u);
117 BOOST_REQUIRE_EQUAL(expired
[0], 2);
118 BOOST_REQUIRE_EQUAL(fifo
.front(), 1);
120 BOOST_REQUIRE_EQUAL(fifo
.size(), 1u);
121 BOOST_REQUIRE_EQUAL(fifo
.front(), 3);
123 BOOST_REQUIRE_EQUAL(fifo
.size(), 0u);
127 abort_source as1
, as2
;
129 fifo
.push_back(1, as1
);
130 fifo
.push_back(2, as1
);
132 fifo
.push_back(4, as2
);
137 BOOST_REQUIRE(!fifo
.empty());
138 BOOST_REQUIRE_EQUAL(fifo
.size(), 2u);
139 BOOST_REQUIRE(bool(fifo
));
140 BOOST_REQUIRE_EQUAL(expired
.size(), 2u);
141 std::sort(expired
.begin(), expired
.end());
142 BOOST_REQUIRE_EQUAL(expired
[0], 1);
143 BOOST_REQUIRE_EQUAL(expired
[1], 2);
144 BOOST_REQUIRE_EQUAL(fifo
.front(), 3);
146 BOOST_REQUIRE_EQUAL(fifo
.size(), 1u);
147 BOOST_REQUIRE_EQUAL(fifo
.front(), 4);
149 BOOST_REQUIRE_EQUAL(fifo
.size(), 0u);
156 fifo
.push_back(2, as
);
157 fifo
.push_back(3, as
);
158 fifo
.push_back(4, as
);
163 BOOST_REQUIRE(!fifo
.empty());
164 BOOST_REQUIRE_EQUAL(fifo
.size(), 1u);
165 BOOST_REQUIRE(bool(fifo
));
166 BOOST_REQUIRE_EQUAL(expired
.size(), 3u);
167 std::sort(expired
.begin(), expired
.end());
168 BOOST_REQUIRE_EQUAL(expired
[0], 2);
169 BOOST_REQUIRE_EQUAL(expired
[1], 3);
170 BOOST_REQUIRE_EQUAL(expired
[2], 4);
171 BOOST_REQUIRE_EQUAL(fifo
.front(), 1);
173 BOOST_REQUIRE_EQUAL(fifo
.size(), 0u);
179 fifo
.push_back(2, as
);
180 fifo
.push_back(3, as
);
181 fifo
.push_back(4, as
);
187 BOOST_REQUIRE_EQUAL(fifo
.size(), 2u);
188 BOOST_REQUIRE_EQUAL(fifo
.front(), 1);
190 BOOST_REQUIRE_EQUAL(fifo
.size(), 1u);
191 BOOST_REQUIRE_EQUAL(fifo
.front(), 5);
193 BOOST_REQUIRE_EQUAL(fifo
.size(), 0u);