-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.zig
More file actions
207 lines (171 loc) · 5.64 KB
/
parallel.zig
File metadata and controls
207 lines (171 loc) · 5.64 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
const std = @import("std");
const suite = @import("suite.zig");
const reporter = @import("reporter.zig");
const compat = @import("compat.zig");
/// Options for parallel test execution
pub const ParallelOptions = struct {
/// Number of worker threads (null = auto-detect CPU count)
n_jobs: ?usize = null,
/// Whether parallel execution is enabled
enabled: bool = false,
};
/// Result from running a test in parallel
pub const TestResult = struct {
test_case: *suite.TestCase,
suite_name: []const u8,
success: bool,
mutex: compat.Mutex = .{},
};
/// Context for parallel test execution
pub const ParallelContext = struct {
allocator: std.mem.Allocator,
reporter: *reporter.Reporter,
results: std.ArrayList(TestResult),
mutex: compat.Mutex = .{},
pub fn init(allocator: std.mem.Allocator, rep: *reporter.Reporter) ParallelContext {
return .{
.allocator = allocator,
.reporter = rep,
.results = .empty,
};
}
pub fn deinit(self: *ParallelContext) void {
self.results.deinit(self.allocator);
}
};
/// Task for executing a single test
pub const TestTask = struct {
context: *ParallelContext,
test_case: *suite.TestCase,
suite_name: []const u8,
pub fn run(self: *TestTask) void {
// Execute the test
const success = self.executeTest();
// Store result in thread-safe manner
self.context.mutex.lock();
defer self.context.mutex.unlock();
self.context.results.append(self.context.allocator, .{
.test_case = self.test_case,
.suite_name = self.suite_name,
.success = success,
}) catch |err| {
std.debug.print("Error appending result: {any}\n", .{err});
};
}
fn executeTest(self: *TestTask) bool {
self.test_case.status = .running;
// Call the test function
self.test_case.test_fn(self.context.allocator) catch |err| {
self.test_case.status = .failed;
const err_msg = std.fmt.allocPrint(
self.context.allocator,
"Test failed: {any}",
.{err},
) catch "Out of memory";
self.test_case.error_message = err_msg;
return false;
};
self.test_case.status = .passed;
return true;
}
};
/// Run tests in parallel using thread pool
pub fn runTestsParallel(
allocator: std.mem.Allocator,
test_registry: *suite.TestRegistry,
rep: *reporter.Reporter,
options: ParallelOptions,
) !bool {
if (!options.enabled) {
return error.ParallelNotEnabled;
}
// Create parallel context
var context = ParallelContext.init(allocator, rep);
defer context.deinit();
// Count total tests
var total_tests: usize = 0;
for (test_registry.root_suites.items) |test_suite| {
total_tests += test_suite.tests.items.len;
}
try rep.onRunStart(total_tests);
// Collect tasks and spawn threads
var threads = std.ArrayList(std.Thread).empty;
defer threads.deinit(allocator);
for (test_registry.root_suites.items) |test_suite| {
try rep.onSuiteStart(test_suite.name);
for (test_suite.tests.items) |*test_case| {
if (test_case.skip) {
test_case.status = .skipped;
continue;
}
// Create task
const task = try allocator.create(TestTask);
task.* = .{
.context = &context,
.test_case = test_case,
.suite_name = test_suite.name,
};
// Spawn thread for task
const thread = try std.Thread.spawn(.{}, runTestTask, .{task});
try threads.append(allocator, thread);
}
}
// Wait for all threads to complete
for (threads.items) |thread| {
thread.join();
}
// Report results (now that all tests are done)
var all_passed = true;
for (test_registry.root_suites.items) |test_suite| {
for (test_suite.tests.items) |*test_case| {
try rep.onTestEnd(test_case);
if (test_case.status == .failed) {
all_passed = false;
}
}
try rep.onSuiteEnd(test_suite.name);
}
// Create results summary
var results = reporter.TestResults.init(allocator);
defer results.deinit();
for (test_registry.root_suites.items) |test_suite| {
for (test_suite.tests.items) |test_case| {
results.total += 1;
switch (test_case.status) {
.passed => results.passed += 1,
.failed => results.failed += 1,
.skipped => results.skipped += 1,
else => {},
}
}
}
try rep.onRunEnd(&results);
return all_passed;
}
fn runTestTask(task: *TestTask) void {
task.run();
}
// Tests
test "ParallelOptions default values" {
const options = ParallelOptions{};
try std.testing.expectEqual(@as(?usize, null), options.n_jobs);
try std.testing.expectEqual(false, options.enabled);
}
test "ParallelOptions with custom values" {
const options = ParallelOptions{
.n_jobs = 4,
.enabled = true,
};
try std.testing.expectEqual(@as(?usize, 4), options.n_jobs);
try std.testing.expectEqual(true, options.enabled);
}
test "ParallelContext initialization" {
const allocator = std.testing.allocator;
var rep = reporter.Reporter{
.vtable = undefined,
.allocator = allocator,
};
var context = ParallelContext.init(allocator, &rep);
defer context.deinit();
try std.testing.expectEqual(@as(usize, 0), context.results.items.len);
}