|
1 | 1 | -- https://github.com/ocamllabs/ocaml-effects-tutorial/blob/master/sources/solved/async_await.ml
|
2 | 2 |
|
3 |
| -local eff = require('eff') |
| 3 | +local eff = require('src/eff') |
4 | 4 | local Eff, perform, handler = eff.Eff, eff.perform, eff.handler
|
5 |
| -local inspect = require('inspect') |
6 |
| - |
7 |
| -local imut |
8 |
| -do |
9 |
| - table.move = table.move |
10 |
| - or function(src, from, to, on, dst) |
11 |
| - for i = from, to do |
12 |
| - dst[i + on - 1] = src[i] |
13 |
| - end |
14 |
| - |
15 |
| - return dst |
16 |
| - end |
17 |
| - |
18 |
| - local cp = function(t) |
19 |
| - return table.move(t, 1, #t, 1, {}) |
20 |
| - end |
21 |
| - |
22 |
| - local cons = function(e, t) |
23 |
| - local ret = cp(t) |
24 |
| - table.insert(ret, e) |
25 |
| - return ret |
26 |
| - end |
27 |
| - |
28 |
| - local rev = function(t) |
29 |
| - local ret = {} |
30 |
| - |
31 |
| - for i = #t, 1, -1 do |
32 |
| - table.insert(ret, t[i]) |
33 |
| - end |
34 |
| - |
35 |
| - return ret |
36 |
| - end |
37 |
| - |
38 |
| - imut = { |
39 |
| - cp = cp, |
40 |
| - cons = cons, |
41 |
| - rev = rev |
42 |
| - } |
43 |
| -end |
44 | 5 |
|
45 |
| -local ref |
46 |
| -do |
47 |
| - local newref = function() |
48 |
| - return setmetatable({ |
49 |
| - content = nil, |
50 |
| - get = function(self) |
51 |
| - return self.content |
52 |
| - end |
53 |
| - }, { |
54 |
| - __call = function(self, v) |
55 |
| - self.content = v |
56 |
| - return self |
57 |
| - end, |
58 |
| - __bnot = function(self) |
59 |
| - return self.content |
60 |
| - end}) |
61 |
| - end |
62 |
| - |
63 |
| - ref = function(v) return newref()(v) end |
64 |
| -end |
| 6 | +local imut = require('spec/utils/imut') |
| 7 | +local ref = require('spec/utils/ref') |
| 8 | +local randoms = require('spec/utils/SEED') |
| 9 | +randoms.init() |
65 | 10 |
|
66 | 11 | local Waiting = function(conts)
|
67 | 12 | return { conts, cls = "waiting" }
|
@@ -142,27 +87,53 @@ local run = function(main)
|
142 | 87 | return fork(ref(Waiting{}), main)
|
143 | 88 | end
|
144 | 89 |
|
145 |
| -local main = function() |
146 |
| - local task = function(name) |
147 |
| - return function() |
148 |
| - print(("Starting %s"):format(name)) |
149 |
| - local v = math.random(100) |
150 |
| - print(("Yielding %s"):format(name)) |
151 |
| - yield() |
152 |
| - print(("Ending %s with %d"):format(name, v)) |
153 |
| - return v |
| 90 | +insulate("async await test", function() |
| 91 | + randomize(false) |
| 92 | + |
| 93 | + spy.on(_G, "print") |
| 94 | + |
| 95 | + local main = function() |
| 96 | + local task = function(name) |
| 97 | + return function() |
| 98 | + print(("Starting %s"):format(name)) |
| 99 | + local v = math.random(100) |
| 100 | + print(("Yielding %s"):format(name)) |
| 101 | + yield() |
| 102 | + print(("Ending %s with %d"):format(name, v)) |
| 103 | + return v |
| 104 | + end |
154 | 105 | end
|
155 |
| - end |
156 | 106 |
|
157 |
| - local pa = async(task "a") |
158 |
| - local pb = async(task "b") |
159 |
| - local pc = async(function() |
160 |
| - return await(pa) + await(pb) |
161 |
| - end) |
| 107 | + local pa = async(task "a") |
162 | 108 |
|
163 |
| - print(("sum is %d"):format(await(pc))) |
164 |
| - assert(await(pa) + await(pb) == await(pc)) |
165 |
| -end |
| 109 | + local pb = async(task "b") |
| 110 | + local pc = async(function() |
| 111 | + return await(pa) + await(pb) |
| 112 | + end) |
166 | 113 |
|
167 |
| -run(main) |
| 114 | + print(("sum is %d"):format(await(pc))) |
| 115 | + assert(await(pa) + await(pb) == await(pc)) |
| 116 | + end |
| 117 | + |
| 118 | + describe("run", function() |
| 119 | + run(main) |
| 120 | + |
| 121 | + it("check first resuming a", function() |
| 122 | + assert.spy(print).was_called_with("Starting a") |
| 123 | + assert.spy(print).was_called_with("Yielding a") |
| 124 | + end) |
| 125 | + |
| 126 | + it("check first resuming b", function() |
| 127 | + assert.spy(print).was_called_with("Starting b") |
| 128 | + assert.spy(print).was_called_with("Yielding b") |
| 129 | + end) |
| 130 | + |
| 131 | + it("check task return value", function() |
| 132 | + assert.spy(print).was_called_with(("Ending %s with %d"):format("a", randoms[1])) |
| 133 | + assert.spy(print).was_called_with(("Ending %s with %d"):format("b", randoms[2])) |
| 134 | + assert.spy(print).was_called_with(("sum is %d"):format(randoms[1] + randoms[2])) |
| 135 | + assert.spy(print).was_called(7) |
| 136 | + end) |
| 137 | + end) |
| 138 | +end) |
168 | 139 |
|
0 commit comments