-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_tests.zig
More file actions
269 lines (216 loc) · 7.83 KB
/
async_tests.zig
File metadata and controls
269 lines (216 loc) · 7.83 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
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
const std = @import("std");
const zig_test = @import("zig-test");
/// Example: Basic async test
pub fn example_basic_async_test() !void {
const allocator = std.heap.page_allocator;
// Create async test executor
var executor = zig_test.AsyncTestExecutor.init(allocator, .{
.default_timeout_ms = 5000,
.concurrent = false,
.verbose = true,
});
defer executor.deinit();
// Define an async test function
const asyncTest1 = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
// Simulate async work
std.Thread.sleep(100 * std.time.ns_per_ms);
std.debug.print("Async test 1 completed\n", .{});
}
}.run;
// Register the test
try executor.registerTest("async_test_1", asyncTest1);
// Execute all tests
const results = try executor.executeAll();
defer allocator.free(results);
// Print results
for (results) |result| {
std.debug.print("Test '{s}': {s}\n", .{ result.name, @tagName(result.status) });
}
// Get statistics
const stats = zig_test.AsyncTestExecutor.getStats(results);
std.debug.print("{}\n", .{stats});
}
/// Example: Async test with timeout
pub fn example_async_timeout() !void {
const allocator = std.heap.page_allocator;
var executor = zig_test.AsyncTestExecutor.init(allocator, .{
.default_timeout_ms = 100, // Short timeout
.verbose = true,
});
defer executor.deinit();
// Test that will timeout
const slowTest = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.Thread.sleep(500 * std.time.ns_per_ms); // Takes too long
}
}.run;
try executor.registerTest("slow_test", slowTest);
const results = try executor.executeAll();
defer allocator.free(results);
for (results) |*result| {
std.debug.print("Test '{s}': {s}\n", .{ result.name, @tagName(result.status) });
if (result.error_msg) |msg| {
std.debug.print(" Error: {s}\n", .{msg});
}
result.deinit();
}
}
/// Example: Concurrent async tests
pub fn example_concurrent_async() !void {
const allocator = std.heap.page_allocator;
var executor = zig_test.AsyncTestExecutor.init(allocator, .{
.concurrent = true,
.max_concurrent = 3,
.verbose = true,
});
defer executor.deinit();
// Create multiple async tests
const asyncTest = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.Thread.sleep(50 * std.time.ns_per_ms);
}
}.run;
try executor.registerTest("concurrent_1", asyncTest);
try executor.registerTest("concurrent_2", asyncTest);
try executor.registerTest("concurrent_3", asyncTest);
try executor.registerTest("concurrent_4", asyncTest);
try executor.registerTest("concurrent_5", asyncTest);
const start = std.time.nanoTimestamp();
const results = try executor.executeAll();
defer allocator.free(results);
const duration = std.time.nanoTimestamp() - start;
std.debug.print("Total duration: {d:.2}ms\n", .{
@as(f64, @floatFromInt(duration)) / 1_000_000.0,
});
const stats = zig_test.AsyncTestExecutor.getStats(results);
std.debug.print("{}\n", .{stats});
}
/// Example: Async tests with hooks
pub fn example_async_hooks() !void {
const allocator = std.heap.page_allocator;
var hooks = zig_test.AsyncHooksManager.init(allocator);
// Setup hooks
const beforeHook = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.debug.print("Before hook executed\n", .{});
std.Thread.sleep(10 * std.time.ns_per_ms);
}
}.run;
const afterHook = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.debug.print("After hook executed\n", .{});
std.Thread.sleep(10 * std.time.ns_per_ms);
}
}.run;
hooks.before_each = beforeHook;
hooks.after_each = afterHook;
// Run hooks
try hooks.runBeforeEach();
std.debug.print("Running test...\n", .{});
try hooks.runAfterEach();
}
/// Example: Using itAsync in suite
pub fn example_suite_async() !void {
const allocator = std.heap.page_allocator;
// Register async tests using the suite API
const asyncTest1 = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.Thread.sleep(50 * std.time.ns_per_ms);
std.debug.print("Suite async test 1 passed\n", .{});
}
}.run;
const asyncTest2 = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.Thread.sleep(50 * std.time.ns_per_ms);
std.debug.print("Suite async test 2 passed\n", .{});
}
}.run;
// Register async tests
try zig_test.itAsync(allocator, "async suite test 1", asyncTest1);
try zig_test.itAsync(allocator, "async suite test 2", asyncTest2);
// With custom timeout
try zig_test.itAsyncTimeout(allocator, "async with timeout", asyncTest1, 1000);
std.debug.print("Async tests registered successfully\n", .{});
}
/// Example: Mixed sync and async tests
pub fn example_mixed_tests() !void {
const allocator = std.heap.page_allocator;
const syncTest = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.debug.print("Sync test executed\n", .{});
}
}.run;
const asyncTest = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.Thread.sleep(50 * std.time.ns_per_ms);
std.debug.print("Async test executed\n", .{});
}
}.run;
// Register both types
try zig_test.it(allocator, "sync test", syncTest);
try zig_test.itAsync(allocator, "async test", asyncTest);
std.debug.print("Mixed sync/async tests registered\n", .{});
}
/// Example: Error handling in async tests
pub fn example_async_errors() !void {
const allocator = std.heap.page_allocator;
var executor = zig_test.AsyncTestExecutor.init(allocator, .{
.verbose = true,
});
defer executor.deinit();
// Test that passes
const passingTest = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.Thread.sleep(10 * std.time.ns_per_ms);
}
}.run;
// Test that fails
const failingTest = struct {
fn run(alloc: std.mem.Allocator) !void {
_ = alloc;
std.Thread.sleep(10 * std.time.ns_per_ms);
return error.AsyncTestFailed;
}
}.run;
try executor.registerTest("passing_test", passingTest);
try executor.registerTest("failing_test", failingTest);
const results = try executor.executeAll();
defer allocator.free(results);
for (results) |*result| {
std.debug.print("Test '{s}': {s}\n", .{ result.name, @tagName(result.status) });
if (result.error_msg) |msg| {
std.debug.print(" Error: {s}\n", .{msg});
}
result.deinit();
}
const stats = zig_test.AsyncTestExecutor.getStats(results);
std.debug.print("{}\n", .{stats});
}
pub fn main() !void {
std.debug.print("\n=== Basic Async Test ===\n", .{});
try example_basic_async_test();
std.debug.print("\n=== Async Timeout ===\n", .{});
try example_async_timeout();
std.debug.print("\n=== Concurrent Async Tests ===\n", .{});
try example_concurrent_async();
std.debug.print("\n=== Async Hooks ===\n", .{});
try example_async_hooks();
std.debug.print("\n=== Suite Async Tests ===\n", .{});
try example_suite_async();
std.debug.print("\n=== Mixed Sync/Async Tests ===\n", .{});
try example_mixed_tests();
std.debug.print("\n=== Async Error Handling ===\n", .{});
try example_async_errors();
std.debug.print("\n✅ All async test examples completed!\n", .{});
}