| ... | @@ -700,3 +700,69 @@ test "returning a const error from async function" { | ... | @@ -700,3 +700,69 @@ test "returning a const error from async function" { |
| 700 | }; | 700 | }; |
| 701 | S.doTheTest(); | 701 | S.doTheTest(); |
| 702 | } | 702 | } |
| | 703 | |
| | 704 | test "async/await typical usage" { |
| | 705 | inline for ([_]bool{false, true}) |b1| { |
| | 706 | inline for ([_]bool{false, true}) |b2| { |
| | 707 | testAsyncAwaitTypicalUsage(b1, b2).doTheTest(); |
| | 708 | } |
| | 709 | } |
| | 710 | } |
| | 711 | |
| | 712 | fn testAsyncAwaitTypicalUsage(comptime simulate_fail_download: bool, comptime simulate_fail_file: bool) type { |
| | 713 | return struct { |
| | 714 | fn doTheTest() void { |
| | 715 | _ = async amainWrap(); |
| | 716 | resume global_file_frame; |
| | 717 | resume global_download_frame; |
| | 718 | } |
| | 719 | fn amainWrap() void { |
| | 720 | if (amain()) |_| { |
| | 721 | expect(!simulate_fail_download); |
| | 722 | expect(!simulate_fail_file); |
| | 723 | } else |e| switch (e) { |
| | 724 | error.NoResponse => expect(simulate_fail_download), |
| | 725 | error.FileNotFound => expect(simulate_fail_file), |
| | 726 | else => @panic("test failure"), |
| | 727 | } |
| | 728 | } |
| | 729 | |
| | 730 | fn amain() !void { |
| | 731 | const allocator = std.heap.direct_allocator; // TODO once we have the debug allocator, use that, so that this can detect leaks |
| | 732 | var download_frame = async fetchUrl(allocator, "https://example.com/"); |
| | 733 | errdefer cancel download_frame; |
| | 734 | |
| | 735 | var file_frame = async readFile(allocator, "something.txt"); |
| | 736 | errdefer cancel file_frame; |
| | 737 | |
| | 738 | const download_text = try await download_frame; |
| | 739 | defer allocator.free(download_text); |
| | 740 | |
| | 741 | const file_text = try await file_frame; |
| | 742 | defer allocator.free(file_text); |
| | 743 | |
| | 744 | expect(std.mem.eql(u8, "expected download text", download_text)); |
| | 745 | expect(std.mem.eql(u8, "expected file text", file_text)); |
| | 746 | } |
| | 747 | |
| | 748 | var global_download_frame: anyframe = undefined; |
| | 749 | fn fetchUrl(allocator: *std.mem.Allocator, url: []const u8) anyerror![]u8 { |
| | 750 | global_download_frame = @frame(); |
| | 751 | const result = try std.mem.dupe(allocator, u8, "expected download text"); |
| | 752 | errdefer allocator.free(result); |
| | 753 | suspend; |
| | 754 | if (simulate_fail_download) return error.NoResponse; |
| | 755 | return result; |
| | 756 | } |
| | 757 | |
| | 758 | var global_file_frame: anyframe = undefined; |
| | 759 | fn readFile(allocator: *std.mem.Allocator, filename: []const u8) anyerror![]u8 { |
| | 760 | global_file_frame = @frame(); |
| | 761 | const result = try std.mem.dupe(allocator, u8, "expected file text"); |
| | 762 | errdefer allocator.free(result); |
| | 763 | suspend; |
| | 764 | if (simulate_fail_file) return error.FileNotFound; |
| | 765 | return result; |
| | 766 | } |
| | 767 | }; |
| | 768 | } |