Repository navigation
Expand file tree
/
Copy pathtest_executor.cpp
More file actions
172 lines (161 loc) · 5.69 KB
/
Copy pathtest_executor.cpp
File metadata and controls
172 lines (161 loc) · 5.69 KB
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
#include <deep_jit/utils/executor.hpp>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std::chrono_literals;
using deep_jit::IOTimeoutError;
using deep_jit::ThreadExecutor;
thread_local int worker_marker = 0;
template<typename F>
void expect_timeout(F&& f) {
try {
f();
assert(false && "expected a timeout");
} catch (const IOTimeoutError&) {}
}
void test_results_and_errors() {
ThreadExecutor executor;
auto id = executor.run([] { return std::this_thread::get_id(); });
assert(id != std::this_thread::get_id());
assert(executor.run([] { return std::this_thread::get_id(); }) == id);
assert(executor.run([p = std::make_unique<int>(42)] { return *p; }) == 42);
auto value = executor.run([] { return std::make_unique<int>(17); });
assert(*value == 17);
int count = 0;
auto increment = [&] { ++count; };
executor.run(increment, 0ms);
assert(count == 1);
assert(&executor.run([&]() -> int& { return count; }) == &count);
try {
executor.run([] {
worker_marker = 77;
throw std::runtime_error("injected I/O failure");
});
assert(false);
} catch (const std::runtime_error& error) {
assert(std::string(error.what()) == "injected I/O failure");
}
assert(executor.run([] { return worker_marker; }) == 0);
}
void test_independent_timeouts() {
ThreadExecutor executor;
std::promise<void> started, release, finished;
auto gate = release.get_future().share();
auto old_worker = std::async(std::launch::async, [&] {
expect_timeout([&] {
executor.run([&, gate] {
started.set_value();
gate.wait();
finished.set_value();
}, 100ms);
});
});
started.get_future().get();
std::vector<std::future<int>> callers;
for (int i = 0; i < 4; ++i) {
callers.push_back(std::async(std::launch::async, [&executor, i] {
return executor.run([i] {
std::this_thread::sleep_for(60ms);
return i;
}, 200ms);
}));
}
old_worker.get();
for (int i = 0; i < 4; ++i)
assert(callers[i].get() == i);
release.set_value();
finished.get_future().get();
assert(executor.run([] { return 42; }) == 42);
}
void test_capture_destruction() {
ThreadExecutor executor;
std::promise<void> release, destroyed;
auto gate = release.get_future().share();
const auto caller = std::this_thread::get_id();
auto deleter = [&, gate, caller](int* pointer) {
assert(std::this_thread::get_id() != caller);
gate.wait();
delete pointer;
destroyed.set_value();
};
expect_timeout([&] {
executor.run([p = std::unique_ptr<int, decltype(deleter)>(new int(7), deleter)] {
return *p;
}, 50ms);
});
assert(executor.run([] { return 23; }) == 23);
release.set_value();
destroyed.get_future().get();
}
void test_timeout_completion_race() {
ThreadExecutor executor;
for (int i = 0; i < 150; ++i) {
try {
executor.run([] { std::this_thread::sleep_for(1ms); }, 1ms);
} catch (const IOTimeoutError&) {}
assert(executor.run([i] { return i; }, 500ms) == i);
}
}
void test_consecutive_timeouts() {
ThreadExecutor executor;
std::promise<void> release;
auto gate = release.get_future().share();
std::vector<std::future<void>> finished;
for (int i = 0; i < 3; ++i) {
std::promise<void> completion;
finished.push_back(completion.get_future());
const auto start = std::chrono::steady_clock::now();
expect_timeout([&] {
executor.run([gate, completion = std::move(completion)]() mutable {
gate.wait();
completion.set_value();
}, 40ms);
});
assert(std::chrono::steady_clock::now() - start >= 40ms);
}
assert(executor.run([] { return 11; }) == 11);
release.set_value();
for (auto& future : finished)
future.get();
}
void test_run_in_iothread() {
const auto worker = deep_jit::run_in_iothread([] { return std::this_thread::get_id(); });
assert(worker != std::this_thread::get_id());
assert(deep_jit::run_in_iothread([worker] {
assert(std::this_thread::get_id() == worker);
return deep_jit::run_in_iothread([p = std::make_unique<int>(42)] { return *p; });
}) == 42);
try {
deep_jit::run_in_iothread([] {
deep_jit::run_in_iothread([] { throw std::runtime_error("nested failure"); });
});
assert(false);
} catch (const std::runtime_error& error) {
assert(std::string(error.what()) == "nested failure");
}
assert(deep_jit::run_in_iothread([] { return 7; }) == 7);
}
int main(int argc, char** argv) {
if (argc > 1) {
if (std::string_view(argv[1]) == "disable-fallback") {
const auto caller = std::this_thread::get_id();
assert(deep_jit::detail::disable_cache_fallback());
assert(deep_jit::run_in_iothread([] { return std::this_thread::get_id(); }) == caller);
assert(not deep_jit::detail::in_iothread);
return 0;
}
// The process must exit without joining this permanently blocked worker.
std::promise<void> release;
auto gate = release.get_future().share();
ThreadExecutor executor;
expect_timeout([&] { executor.run([gate] { gate.wait(); }, 20ms); });
return 0;
}
test_results_and_errors();
test_independent_timeouts();
test_capture_destruction();
test_timeout_completion_race();
test_consecutive_timeouts();
test_run_in_iothread();
std::cout << "Executor tests passed\n";
}