File: condition_variable_test.cc

package info (click to toggle)
seastar 25.05.0-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 7,256 kB
  • sloc: cpp: 89,250; python: 5,066; ansic: 3,452; sh: 1,272; xml: 177; makefile: 9
file content (396 lines) | stat: -rw-r--r-- 10,491 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * This file is open source software, licensed to you under the terms
 * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership.  You may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*
 * Copyright (C) 2015 Cloudius Systems, Ltd.
 */

#include <seastar/core/thread.hh>
#include <seastar/core/do_with.hh>
#include <seastar/testing/test_case.hh>
#include <seastar/testing/thread_test_case.hh>
#include <seastar/core/sstring.hh>
#include <seastar/core/condition-variable.hh>
#include <seastar/core/do_with.hh>
#include <seastar/core/loop.hh>
#include <seastar/core/map_reduce.hh>
#include <seastar/core/sleep.hh>
#include <seastar/core/shared_mutex.hh>
#include <seastar/core/when_all.hh>
#include <seastar/core/when_any.hh>
#include <seastar/core/with_timeout.hh>
#include <boost/range/irange.hpp>

using namespace seastar;
using namespace std::chrono_literals;
using steady_clock = std::chrono::steady_clock;

SEASTAR_THREAD_TEST_CASE(test_condition_variable_signal_consume) {
    condition_variable cv;

    cv.signal();
    auto f = cv.wait();

    BOOST_REQUIRE_EQUAL(f.available(), true);
    f.get();

    auto f2 = cv.wait();

    BOOST_REQUIRE_EQUAL(f2.available(), false);

    cv.signal();

    with_timeout(steady_clock::now() + 10ms, std::move(f2)).get();

    std::vector<future<>> waiters;
    waiters.emplace_back(cv.wait());
    waiters.emplace_back(cv.wait());
    waiters.emplace_back(cv.wait());

    BOOST_REQUIRE_EQUAL(std::count_if(waiters.begin(), waiters.end(), std::mem_fn(&future<>::available)), 0u);

    cv.signal();

    BOOST_REQUIRE_EQUAL(std::count_if(waiters.begin(), waiters.end(), std::mem_fn(&future<>::available)), 1u);
    // FIFO
    BOOST_REQUIRE_EQUAL(waiters.front().available(), true);

    cv.broadcast();

    BOOST_REQUIRE_EQUAL(std::count_if(waiters.begin(), waiters.end(), std::mem_fn(&future<>::available)), 3u);
}

SEASTAR_THREAD_TEST_CASE(test_condition_variable_pred) {
    condition_variable cv;
    bool ready = false;

    try {
        cv.wait(100ms, [&] { return ready; }).get();
        BOOST_FAIL("should not reach");
    } catch (condition_variable_timed_out&) {
        // ok
    } catch (...) {
        BOOST_FAIL("should not reach");
    }
    // should not affect outcome.
    cv.signal();

    try {
        cv.wait(100ms, [&] { return ready; }).get();
        BOOST_FAIL("should not reach");
    } catch (condition_variable_timed_out&) {
        // ok
    } catch (...) {
        BOOST_FAIL("should not reach");
    }

}

SEASTAR_THREAD_TEST_CASE(test_condition_variable_signal_break) {
    condition_variable cv;

    std::vector<future<>> waiters;
    waiters.emplace_back(cv.wait());
    waiters.emplace_back(cv.wait());
    waiters.emplace_back(cv.wait());

    BOOST_REQUIRE_EQUAL(std::count_if(waiters.begin(), waiters.end(), std::mem_fn(&future<>::available)), 0u);

    cv.broken();

    for (auto& f : waiters) {
        try {
            f.get();
        } catch (broken_condition_variable&) {
            // ok
            continue;
        }
        BOOST_FAIL("should not reach");
    }

    try {
        auto f = cv.wait();
        f.get();
        BOOST_FAIL("should not reach");
    } catch (broken_condition_variable&) {
        // ok
    } catch (...) {
        BOOST_FAIL("should not reach");
    }
}

SEASTAR_THREAD_TEST_CASE(test_condition_variable_timeout) {
    condition_variable cv;

    auto f = cv.wait(100ms);
    BOOST_REQUIRE_EQUAL(f.available(), false);

    sleep(200ms).get();
    BOOST_REQUIRE_EQUAL(f.available(), true);

    try {
        f.get();
        BOOST_FAIL("should not reach");
    } catch (condition_variable_timed_out&) {
        // ok
    } catch (...) {
        BOOST_FAIL("should not reach");
    }
}

SEASTAR_THREAD_TEST_CASE(test_condition_variable_pred_wait) {
    condition_variable cv;

    bool ready = false;

    timer<> t;
    t.set_callback([&] { ready = true; cv.signal(); });
    t.arm(100ms);

    cv.wait([&] { return ready; }).get();

    ready = false;

    try {
        cv.wait(10ms, [&] { return ready; }).get();
        BOOST_FAIL("should not reach");
    } catch (timed_out_error&) {
        BOOST_FAIL("should not reach");
    } catch (condition_variable_timed_out&) {
        // ok
    } catch (...) {
        BOOST_FAIL("should not reach");
    }

    ready = true;
    cv.signal();

    cv.wait(10ms, [&] { return ready; }).get();

    for (int i = 0; i < 2; ++i) {
        ready = false;
        t.set_callback([&] { cv.broadcast();});
        t.arm_periodic(10ms);

        try {
            cv.wait(300ms, [&] { return ready; }).get();
            BOOST_FAIL("should not reach");
        } catch (timed_out_error&) {
            BOOST_FAIL("should not reach");
        } catch (condition_variable_timed_out&) {
            // ok
        } catch (...) {
            BOOST_FAIL("should not reach");
        }
        t.cancel();
        cv.signal();
    }

    ready = true;
    cv.signal();

    cv.wait([&] { return ready; }).get();
    // signal state should remain on
    cv.wait().get();
}

SEASTAR_THREAD_TEST_CASE(test_condition_variable_has_waiter) {
    condition_variable cv;

    BOOST_REQUIRE_EQUAL(cv.has_waiters(), false);

    auto f = cv.wait();
    BOOST_REQUIRE_EQUAL(cv.has_waiters(), true);

    cv.signal();
    f.get();
    BOOST_REQUIRE_EQUAL(cv.has_waiters(), false);
}

SEASTAR_TEST_CASE(test_condition_variable_signal_consume_coroutine) {
    condition_variable cv;

    cv.signal();
    co_await with_timeout(steady_clock::now() + 10ms, [&]() -> future<> {
        co_await cv.when();
    }());

    try {
        co_await with_timeout(steady_clock::now() + 10ms, [&]() -> future<> {
            co_await cv.when();
        }());
        BOOST_FAIL("should not reach");
    } catch (condition_variable_timed_out&) {
        BOOST_FAIL("should not reach");
    } catch (timed_out_error&) {
        // ok
    } catch (...) {
        BOOST_FAIL("should not reach");
    }

    try {
        co_await with_timeout(steady_clock::now() + 10s, [&]() -> future<> {
            co_await cv.when(100ms);
        }());
        BOOST_FAIL("should not reach");
    } catch (timed_out_error&) {
        BOOST_FAIL("should not reach");
    } catch (condition_variable_timed_out&) {
        // ok
    } catch (...) {
        BOOST_FAIL("should not reach");
    }

}

SEASTAR_TEST_CASE(test_condition_variable_pred_when) {
    condition_variable cv;

    bool ready = false;

    timer<> t;
    t.set_callback([&] { ready = true; cv.signal(); });
    t.arm(100ms);

    co_await cv.when([&] { return ready; });

    ready = false;

    try {
        co_await cv.when(10ms, [&] { return ready; });
        BOOST_FAIL("should not reach");
    } catch (timed_out_error&) {
        BOOST_FAIL("should not reach");
    } catch (condition_variable_timed_out&) {
        // ok
    } catch (...) {
        BOOST_FAIL("should not reach");
    }

    ready = true;
    cv.signal();

    co_await cv.when(10ms, [&] { return ready; });

    for (int i = 0; i < 2; ++i) {
        ready = false;
        t.set_callback([&] { cv.broadcast();});
        t.arm_periodic(10ms);

        try {
            co_await cv.when(300ms, [&] { return ready; });
            BOOST_FAIL("should not reach");
        } catch (timed_out_error&) {
            BOOST_FAIL("should not reach");
        } catch (condition_variable_timed_out&) {
            // ok
        } catch (...) {
            BOOST_FAIL("should not reach");
        }
        t.cancel();
        cv.signal();
    }

    ready = true;
    cv.signal();

    co_await cv.when([&] { return ready; });
    // signal state should remain on
    co_await cv.when();
}



SEASTAR_TEST_CASE(test_condition_variable_when_signal) {
    condition_variable cv;

    bool ready = false;

    timer<> t;
    t.set_callback([&] { cv.signal(); ready = true; });
    t.arm(100ms);

    co_await cv.when();
    // ensure we did not resume before timer ran fully
    BOOST_REQUIRE_EQUAL(ready, true);
}

SEASTAR_TEST_CASE(test_condition_variable_when_timeout) {
    condition_variable cv;

    bool ready = false;

    // create "background" fiber
    auto f = [&]() -> future<> {
        try {
            co_await cv.when(100ms, [&] { return ready; });
        } catch (timed_out_error&) {
            BOOST_FAIL("should not reach");
        } catch (condition_variable_timed_out&) {
            BOOST_FAIL("should not reach");
        } catch (...) {
            BOOST_FAIL("should not reach");
        }
    }();

    // ensure we wake up waiter before timeuot
    ready = true;
    cv.signal();

    // now busy-spin until the timer should be expired
    while (cv.has_waiters()) {
    }

    // he should not have run yet...
    BOOST_REQUIRE_EQUAL(f.available(), false);
    // now, if the code is broken, the timer will run once we switch out,
    // and cause the wait to time out, even though it did not. -> assert

    co_await std::move(f);
}

// Check that exception from a predicate is propogated to the waiter
SEASTAR_THREAD_TEST_CASE(test_condition_variable_wait_predicate_throws) {
    condition_variable cv;

    auto f = cv.wait([i = 1] () mutable {
                     if (i == 0) {
                        throw std::runtime_error("Predicate error");
                     }
                     i--;
                     return false;
             });
    cv.signal();
    BOOST_REQUIRE_THROW(f.get(), std::runtime_error);
}

// Check that exception from a predicate is propogated to the waiter
SEASTAR_TEST_CASE(test_condition_variable_when_predicate_throws) {
    condition_variable cv;

    (void)sleep(100ms).then([&] { cv.signal(); });

    BOOST_REQUIRE_THROW(co_await
              cv.when([i = 1] () mutable {
                     if (i == 0) {
                        throw std::runtime_error("Predicate error");
                     }
                     i--;
                     return false;
             }), std::runtime_error);
}