-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrency.spec.lua
95 lines (78 loc) · 2.22 KB
/
concurrency.spec.lua
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
mtt.register("Promise.all (success)", function(callback)
local p1 = Promise.new(function(resolve)
resolve(5)
end)
local p2 = Promise.new(function(resolve)
resolve(10)
end)
Promise.all(p1, p2):next(function(values)
assert(#values == 2)
assert(values[1] == 5)
assert(values[2] == 10)
callback()
end):catch(function(err)
callback(err)
end)
end)
mtt.register("Promise.all (error)", function(callback)
local p1 = Promise.new(function(resolve)
resolve(5)
end)
local p2 = Promise.new(function(_, reject)
reject("stuff")
end)
Promise.all(p1, p2):next(function()
callback("unexpected success")
end):catch(function(err)
assert(err[1] == nil)
assert(err[2] == "stuff")
callback()
end)
end)
mtt.register("Promise.race (success)", function(callback)
local p1 = Promise.resolve(5)
local p2 = Promise.new()
Promise.race(p1, p2):next(function(v)
assert(v == 5)
callback()
end)
end)
mtt.register("Promise.race (error)", function(callback)
local p1 = Promise.reject("whatever")
local p2 = Promise.new()
Promise.race(p1, p2):next(function()
callback("unexpected success")
end):catch(function(err)
assert(err == "whatever")
callback()
end)
end)
mtt.register("Promise.race (timeout error)", function(callback)
local p = Promise.new()
local to = Promise.after(0.1, nil, "whatever")
Promise.race(p, to):next(function()
callback("unexpected success")
end):catch(function(err)
assert(err == "whatever")
callback()
end)
end)
mtt.register("Promise.any (success)", function(callback)
local p1 = Promise.resolve(5)
local p2 = Promise.reject("stuff")
Promise.any(p1, p2):next(function(v)
assert(v == 5)
callback()
end)
end)
mtt.register("Promise.any (error)", function(callback)
local p1 = Promise.reject("stuff1")
local p2 = Promise.reject("stuff2")
Promise.any(p1, p2):next(function()
callback("unexpected success")
end):catch(function(errors)
assert(errors[1] == "stuff1")
assert(errors[2] == "stuff2")
callback()
end)
end)