]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/thread_test.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / tests / unit / thread_test.cc
1 /*
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.
6 *
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,
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
16 * under the License.
17 */
18
19 /*
20 * Copyright (C) 2015 Cloudius Systems, Ltd.
21 */
22
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>
33
34 using namespace seastar;
35 using namespace std::chrono_literals;
36
37 SEASTAR_TEST_CASE(test_thread_1) {
38 return do_with(sstring(), [] (sstring& x) {
39 auto t1 = new thread([&x] {
40 x = "abc";
41 });
42 return t1->join().then([&x, t1] {
43 BOOST_REQUIRE_EQUAL(x, "abc");
44 delete t1;
45 });
46 });
47 }
48
49 SEASTAR_TEST_CASE(test_thread_2) {
50 struct tmp {
51 std::vector<thread> threads;
52 semaphore sem1{0};
53 semaphore sem2{0};
54 int counter = 0;
55 void thread_fn() {
56 sem1.wait(1).get();
57 ++counter;
58 sem2.signal(1);
59 }
60 };
61 return do_with(tmp(), [] (tmp& x) {
62 auto n = 10;
63 for (int i = 0; i < n; ++i) {
64 x.threads.emplace_back(std::bind(&tmp::thread_fn, &x));
65 }
66 BOOST_REQUIRE_EQUAL(x.counter, 0);
67 x.sem1.signal(n);
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));
71 });
72 });
73 }
74
75 SEASTAR_TEST_CASE(test_thread_async) {
76 sstring x = "x";
77 sstring y = "y";
78 auto concat = [] (sstring x, sstring y) {
79 sleep(10ms).get();
80 return x + y;
81 };
82 return async(concat, x, y).then([] (sstring xy) {
83 BOOST_REQUIRE_EQUAL(xy, "xy");
84 });
85 }
86
87 SEASTAR_TEST_CASE(test_thread_async_immed) {
88 return async([] { return 3; }).then([] (int three) {
89 BOOST_REQUIRE_EQUAL(three, 3);
90 });
91 }
92
93 SEASTAR_TEST_CASE(test_thread_async_nested) {
94 return async([] {
95 return async([] {
96 return 3;
97 }).get0();
98 }).then([] (int three) {
99 BOOST_REQUIRE_EQUAL(three, 3);
100 });
101 }
102
103 void compute(float& result, bool& done, uint64_t& ctr) {
104 while (!done) {
105 for (int n = 0; n < 10000; ++n) {
106 result += 1 / (result + 1);
107 ++ctr;
108 }
109 thread::yield();
110 }
111 }
112
113 #if defined(SEASTAR_ASAN_ENABLED) && defined(SEASTAR_HAVE_ASAN_FIBER_SUPPORT)
114 volatile int force_write;
115 volatile void* shut_up_gcc;
116
117 [[gnu::noinline]]
118 void throw_exception() {
119 volatile char buf[1024];
120 shut_up_gcc = &buf;
121 for (int i = 0; i < 1024; i++) {
122 buf[i] = force_write;
123 }
124 throw 1;
125 }
126
127 [[gnu::noinline]]
128 void use_stack() {
129 volatile char buf[2 * 1024];
130 shut_up_gcc = &buf;
131 for (int i = 0; i < 2 * 1024; i++) {
132 buf[i] = force_write;
133 }
134 }
135
136 SEASTAR_TEST_CASE(test_asan_false_positive) {
137 return async([] {
138 try {
139 throw_exception();
140 } catch (...) {
141 use_stack();
142 }
143 });
144 }
145 #endif
146
147 SEASTAR_THREAD_TEST_CASE_EXPECTED_FAILURES(abc, 2) {
148 BOOST_TEST(false);
149 BOOST_TEST(false);
150 }