]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/thread_test.cc
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) 2015 Cloudius Systems, Ltd.
23 #include <seastar/core/thread.hh>
24 #include <seastar/core/do_with.hh>
25 #include <seastar/testing/test_case.hh>
26 #include <seastar/testing/thread_test_case.hh>
27 #include <seastar/core/sstring.hh>
28 #include <seastar/core/reactor.hh>
29 #include <seastar/core/semaphore.hh>
30 #include <seastar/core/do_with.hh>
31 #include <seastar/core/future-util.hh>
32 #include <seastar/core/sleep.hh>
34 using namespace seastar
;
35 using namespace std::chrono_literals
;
37 SEASTAR_TEST_CASE(test_thread_1
) {
38 return do_with(sstring(), [] (sstring
& x
) {
39 auto t1
= new thread([&x
] {
42 return t1
->join().then([&x
, t1
] {
43 BOOST_REQUIRE_EQUAL(x
, "abc");
49 SEASTAR_TEST_CASE(test_thread_2
) {
51 std::vector
<thread
> threads
;
61 return do_with(tmp(), [] (tmp
& x
) {
63 for (int i
= 0; i
< n
; ++i
) {
64 x
.threads
.emplace_back(std::bind(&tmp::thread_fn
, &x
));
66 BOOST_REQUIRE_EQUAL(x
.counter
, 0);
68 return x
.sem2
.wait(n
).then([&x
, n
] {
69 BOOST_REQUIRE_EQUAL(x
.counter
, n
);
70 return parallel_for_each(x
.threads
.begin(), x
.threads
.end(), std::mem_fn(&thread::join
));
75 SEASTAR_TEST_CASE(test_thread_async
) {
78 auto concat
= [] (sstring x
, sstring y
) {
82 return async(concat
, x
, y
).then([] (sstring xy
) {
83 BOOST_REQUIRE_EQUAL(xy
, "xy");
87 SEASTAR_TEST_CASE(test_thread_async_immed
) {
88 return async([] { return 3; }).then([] (int three
) {
89 BOOST_REQUIRE_EQUAL(three
, 3);
93 SEASTAR_TEST_CASE(test_thread_async_nested
) {
98 }).then([] (int three
) {
99 BOOST_REQUIRE_EQUAL(three
, 3);
103 void compute(float& result
, bool& done
, uint64_t& ctr
) {
105 for (int n
= 0; n
< 10000; ++n
) {
106 result
+= 1 / (result
+ 1);
113 #if defined(SEASTAR_ASAN_ENABLED) && defined(SEASTAR_HAVE_ASAN_FIBER_SUPPORT)
114 volatile int force_write
;
115 volatile void* shut_up_gcc
;
118 void throw_exception() {
119 volatile char buf
[1024];
121 for (int i
= 0; i
< 1024; i
++) {
122 buf
[i
] = force_write
;
129 volatile char buf
[2 * 1024];
131 for (int i
= 0; i
< 2 * 1024; i
++) {
132 buf
[i
] = force_write
;
136 SEASTAR_TEST_CASE(test_asan_false_positive
) {
147 SEASTAR_THREAD_TEST_CASE_EXPECTED_FAILURES(abc
, 2) {