-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_support.zig
More file actions
303 lines (242 loc) · 8.04 KB
/
async_support.zig
File metadata and controls
303 lines (242 loc) · 8.04 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const std = @import("std");
const compat = @import("compat.zig");
/// Async/Await Support for Zig Test Framework
///
/// NOTE: Zig's async/await is still experimental and subject to change.
/// This module provides basic utilities for async testing until Zig's
/// async stabilizes. For now, we provide thread-based async simulation.
/// Async test function signature
pub const AsyncTestFn = *const fn (allocator: std.mem.Allocator) anyerror!void;
/// Future-like type for async operations
pub fn Future(comptime T: type) type {
return struct {
result: ?Result = null,
completed: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
mutex: compat.Mutex = .{},
const Self = @This();
const Result = union(enum) {
value: T,
err: anyerror,
};
pub fn init() Self {
return .{};
}
/// Wait for the future to complete and get the result
pub fn await_value(self: *Self) !T {
while (!self.completed.load(.monotonic)) {
compat.sleep(1 * std.time.ns_per_ms);
}
self.mutex.lock();
defer self.mutex.unlock();
if (self.result) |result| {
return switch (result) {
.value => |v| v,
.err => |e| e,
};
}
return error.FutureNotCompleted;
}
/// Complete the future with a value
pub fn resolve(self: *Self, value: T) void {
self.mutex.lock();
self.result = .{ .value = value };
self.mutex.unlock();
self.completed.store(true, .monotonic);
}
/// Complete the future with an error
pub fn reject(self: *Self, err: anyerror) void {
self.mutex.lock();
self.result = .{ .err = err };
self.mutex.unlock();
self.completed.store(true, .monotonic);
}
/// Check if the future is completed
pub fn isCompleted(self: *Self) bool {
return self.completed.load(.monotonic);
}
};
}
/// Promise type for creating async operations
pub fn Promise(comptime T: type) type {
return struct {
future: Future(T),
const Self = @This();
pub fn init() Self {
return .{
.future = Future(T).init(),
};
}
pub fn resolve(self: *Self, value: T) void {
self.future.resolve(value);
}
pub fn reject(self: *Self, err: anyerror) void {
self.future.reject(err);
}
pub fn getFuture(self: *Self) *Future(T) {
return &self.future;
}
};
}
/// Run an async test function
pub fn runAsync(allocator: std.mem.Allocator, func: AsyncTestFn) !void {
const Context = struct {
allocator: std.mem.Allocator,
func: AsyncTestFn,
result: ?anyerror = null,
completed: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
fn run(ctx: *@This()) void {
if (ctx.func(ctx.allocator)) |_| {
ctx.result = null;
} else |err| {
ctx.result = err;
}
ctx.completed.store(true, .monotonic);
}
};
var context = Context{
.allocator = allocator,
.func = func,
};
const thread = try std.Thread.spawn(.{}, Context.run, .{&context});
thread.detach();
// Wait for completion
while (!context.completed.load(.monotonic)) {
compat.sleep(1 * std.time.ns_per_ms);
}
if (context.result) |err| {
return err;
}
}
/// Async executor for running multiple async operations concurrently
pub const AsyncExecutor = struct {
allocator: std.mem.Allocator,
threads: std.ArrayList(std.Thread),
const Self = @This();
pub fn init(allocator: std.mem.Allocator) Self {
return .{
.allocator = allocator,
.threads = std.ArrayList(std.Thread).empty,
};
}
pub fn deinit(self: *Self) void {
self.threads.deinit(self.allocator);
}
/// Spawn an async task
pub fn spawn(self: *Self, comptime func: anytype, args: anytype) !void {
const thread = try std.Thread.spawn(.{}, func, args);
try self.threads.append(self.allocator, thread);
}
/// Wait for all spawned tasks to complete
pub fn waitAll(self: *Self) void {
for (self.threads.items) |thread| {
thread.join();
}
self.threads.clearRetainingCapacity();
}
};
/// Delay for async operations
pub fn delay(ms: u64) void {
compat.sleep(ms * std.time.ns_per_ms);
}
/// Run multiple futures concurrently and wait for all to complete
pub fn all(comptime T: type, allocator: std.mem.Allocator, futures: [](*Future(T))) ![]T {
var results = try allocator.alloc(T, futures.len);
for (futures, 0..) |future, i| {
results[i] = try future.await_value();
}
return results;
}
/// Run multiple futures concurrently and return the first completed
pub fn race(comptime T: type, futures: [](*Future(T))) !T {
while (true) {
for (futures) |future| {
if (future.isCompleted()) {
return try future.await_value();
}
}
compat.sleep(1 * std.time.ns_per_ms);
}
}
/// Timeout wrapper for async operations
pub fn timeout(comptime T: type, future: *Future(T), timeout_ms: u64) !T {
const start = compat.milliTimestamp();
while (!future.isCompleted()) {
const elapsed = compat.milliTimestamp() - start;
if (elapsed >= timeout_ms) {
return error.Timeout;
}
compat.sleep(1 * std.time.ns_per_ms);
}
return try future.await_value();
}
// Tests
test "Future resolve and await" {
var future = Future(i32).init();
// Resolve in background thread
const ResolveContext = struct {
future: *Future(i32),
fn run(ctx: @This()) void {
delay(10); // Simulate async work
ctx.future.resolve(42);
}
};
const thread = try std.Thread.spawn(.{}, ResolveContext.run, .{ResolveContext{ .future = &future }});
thread.detach();
const result = try future.await_value();
try std.testing.expectEqual(@as(i32, 42), result);
}
test "Future reject with error" {
var future = Future(i32).init();
// Reject in background thread
const RejectContext = struct {
future: *Future(i32),
fn run(ctx: @This()) void {
delay(10);
ctx.future.reject(error.TestError);
}
};
const thread = try std.Thread.spawn(.{}, RejectContext.run, .{RejectContext{ .future = &future }});
thread.detach();
const result = future.await_value();
try std.testing.expectError(error.TestError, result);
}
test "Promise pattern" {
var promise = Promise([]const u8).init();
var future = promise.getFuture();
// Resolve promise in background
const PromiseContext = struct {
promise: *Promise([]const u8),
fn run(ctx: @This()) void {
delay(10);
ctx.promise.resolve("Hello, Async!");
}
};
const thread = try std.Thread.spawn(.{}, PromiseContext.run, .{PromiseContext{ .promise = &promise }});
thread.detach();
const result = try future.await_value();
try std.testing.expectEqualStrings("Hello, Async!", result);
}
test "AsyncExecutor spawn and wait" {
const allocator = std.testing.allocator;
var executor = AsyncExecutor.init(allocator);
defer executor.deinit();
const TaskContext = struct {
fn task1() void {
delay(10);
}
fn task2() void {
delay(10);
}
};
try executor.spawn(TaskContext.task1, .{});
try executor.spawn(TaskContext.task2, .{});
executor.waitAll();
// If we get here, all tasks completed
try std.testing.expect(true);
}
test "timeout with Future" {
var future = Future(i32).init();
// Never resolve the future
const result = timeout(i32, &future, 50);
try std.testing.expectError(error.Timeout, result);
}