2018-02-28 15:22:43 -08:00
|
|
|
const std = @import("std");
|
|
|
|
const assert = std.debug.assert;
|
|
|
|
|
|
|
|
var x: i32 = 1;
|
|
|
|
|
|
|
|
test "create a coroutine and cancel it" {
|
2018-03-21 16:56:41 -07:00
|
|
|
const p = try async<std.debug.global_allocator> simpleAsyncFn();
|
2018-02-28 15:22:43 -08:00
|
|
|
cancel p;
|
|
|
|
assert(x == 2);
|
|
|
|
}
|
|
|
|
|
2018-02-28 15:56:26 -08:00
|
|
|
async fn simpleAsyncFn() void {
|
2018-02-28 15:22:43 -08:00
|
|
|
x += 1;
|
|
|
|
suspend;
|
|
|
|
x += 1;
|
|
|
|
}
|
2018-02-28 19:18:48 -08:00
|
|
|
|
|
|
|
test "coroutine suspend, resume, cancel" {
|
|
|
|
seq('a');
|
2018-03-21 16:56:41 -07:00
|
|
|
const p = try async<std.debug.global_allocator> testAsyncSeq();
|
2018-02-28 19:18:48 -08:00
|
|
|
seq('c');
|
|
|
|
resume p;
|
|
|
|
seq('f');
|
|
|
|
cancel p;
|
|
|
|
seq('g');
|
|
|
|
|
|
|
|
assert(std.mem.eql(u8, points, "abcdefg"));
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn testAsyncSeq() void {
|
|
|
|
defer seq('e');
|
|
|
|
|
|
|
|
seq('b');
|
|
|
|
suspend;
|
|
|
|
seq('d');
|
|
|
|
}
|
|
|
|
var points = []u8{0} ** "abcdefg".len;
|
|
|
|
var index: usize = 0;
|
|
|
|
|
|
|
|
fn seq(c: u8) void {
|
|
|
|
points[index] = c;
|
|
|
|
index += 1;
|
|
|
|
}
|
2018-02-28 19:26:26 -08:00
|
|
|
|
|
|
|
test "coroutine suspend with block" {
|
2018-03-21 16:56:41 -07:00
|
|
|
const p = try async<std.debug.global_allocator> testSuspendBlock();
|
2018-02-28 19:26:26 -08:00
|
|
|
std.debug.assert(!result);
|
|
|
|
resume a_promise;
|
|
|
|
std.debug.assert(result);
|
|
|
|
cancel p;
|
|
|
|
}
|
|
|
|
|
|
|
|
var a_promise: promise = undefined;
|
|
|
|
var result = false;
|
|
|
|
|
|
|
|
async fn testSuspendBlock() void {
|
|
|
|
suspend |p| {
|
|
|
|
a_promise = p;
|
|
|
|
}
|
|
|
|
result = true;
|
|
|
|
}
|
2018-03-01 12:46:35 -08:00
|
|
|
|
|
|
|
var await_a_promise: promise = undefined;
|
|
|
|
var await_final_result: i32 = 0;
|
|
|
|
|
|
|
|
test "coroutine await" {
|
|
|
|
await_seq('a');
|
2018-03-21 16:56:41 -07:00
|
|
|
const p = async<std.debug.global_allocator> await_amain() catch unreachable;
|
2018-03-01 12:46:35 -08:00
|
|
|
await_seq('f');
|
|
|
|
resume await_a_promise;
|
|
|
|
await_seq('i');
|
|
|
|
assert(await_final_result == 1234);
|
|
|
|
assert(std.mem.eql(u8, await_points, "abcdefghi"));
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn await_amain() void {
|
|
|
|
await_seq('b');
|
|
|
|
const p = async await_another() catch unreachable;
|
|
|
|
await_seq('e');
|
|
|
|
await_final_result = await p;
|
|
|
|
await_seq('h');
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn await_another() i32 {
|
|
|
|
await_seq('c');
|
|
|
|
suspend |p| {
|
|
|
|
await_seq('d');
|
|
|
|
await_a_promise = p;
|
|
|
|
}
|
|
|
|
await_seq('g');
|
|
|
|
return 1234;
|
|
|
|
}
|
|
|
|
|
|
|
|
var await_points = []u8{0} ** "abcdefghi".len;
|
|
|
|
var await_seq_index: usize = 0;
|
|
|
|
|
|
|
|
fn await_seq(c: u8) void {
|
|
|
|
await_points[await_seq_index] = c;
|
|
|
|
await_seq_index += 1;
|
|
|
|
}
|
2018-03-01 13:17:38 -08:00
|
|
|
|
|
|
|
|
|
|
|
var early_final_result: i32 = 0;
|
|
|
|
|
|
|
|
test "coroutine await early return" {
|
|
|
|
early_seq('a');
|
2018-03-21 16:56:41 -07:00
|
|
|
const p = async<std.debug.global_allocator> early_amain() catch unreachable;
|
2018-03-01 13:17:38 -08:00
|
|
|
early_seq('f');
|
|
|
|
assert(early_final_result == 1234);
|
|
|
|
assert(std.mem.eql(u8, early_points, "abcdef"));
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn early_amain() void {
|
|
|
|
early_seq('b');
|
|
|
|
const p = async early_another() catch unreachable;
|
|
|
|
early_seq('d');
|
|
|
|
early_final_result = await p;
|
|
|
|
early_seq('e');
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn early_another() i32 {
|
|
|
|
early_seq('c');
|
|
|
|
return 1234;
|
|
|
|
}
|
|
|
|
|
|
|
|
var early_points = []u8{0} ** "abcdef".len;
|
|
|
|
var early_seq_index: usize = 0;
|
|
|
|
|
|
|
|
fn early_seq(c: u8) void {
|
|
|
|
early_points[early_seq_index] = c;
|
|
|
|
early_seq_index += 1;
|
|
|
|
}
|
2018-03-09 18:05:54 -08:00
|
|
|
|
|
|
|
test "coro allocation failure" {
|
|
|
|
var failing_allocator = std.debug.FailingAllocator.init(std.debug.global_allocator, 0);
|
2018-03-21 16:56:41 -07:00
|
|
|
if (async<&failing_allocator.allocator> asyncFuncThatNeverGetsRun()) {
|
2018-03-09 18:05:54 -08:00
|
|
|
@panic("expected allocation failure");
|
|
|
|
} else |err| switch (err) {
|
|
|
|
error.OutOfMemory => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn asyncFuncThatNeverGetsRun() void {
|
|
|
|
@panic("coro frame allocation should fail");
|
|
|
|
}
|
2018-03-21 16:56:41 -07:00
|
|
|
|
|
|
|
test "async function with dot syntax" {
|
|
|
|
const S = struct {
|
|
|
|
var y: i32 = 1;
|
|
|
|
async fn foo() void {
|
|
|
|
y += 1;
|
|
|
|
suspend;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const p = try async<std.debug.global_allocator> S.foo();
|
|
|
|
cancel p;
|
|
|
|
assert(S.y == 2);
|
|
|
|
}
|
2018-03-22 13:56:03 -07:00
|
|
|
|
|
|
|
test "async fn pointer in a struct field" {
|
|
|
|
var data: i32 = 1;
|
|
|
|
const Foo = struct {
|
|
|
|
bar: async<&std.mem.Allocator> fn(&i32) void,
|
|
|
|
};
|
|
|
|
var foo = Foo {
|
|
|
|
.bar = simpleAsyncFn2,
|
|
|
|
};
|
|
|
|
const p = (async<std.debug.global_allocator> foo.bar(&data)) catch unreachable;
|
|
|
|
assert(data == 2);
|
|
|
|
cancel p;
|
|
|
|
assert(data == 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
async<&std.mem.Allocator> fn simpleAsyncFn2(y: &i32) void {
|
|
|
|
defer *y += 2;
|
|
|
|
*y += 1;
|
|
|
|
suspend;
|
|
|
|
}
|
2018-03-24 15:21:51 -07:00
|
|
|
|
|
|
|
test "async fn with inferred error set" {
|
|
|
|
const p = (async<std.debug.global_allocator> failing()) catch unreachable;
|
|
|
|
resume p;
|
|
|
|
cancel p;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn failing() !void {
|
|
|
|
suspend;
|
|
|
|
return error.Fail;
|
|
|
|
}
|