aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--testes/coroutine.lua44
1 files changed, 43 insertions, 1 deletions
diff --git a/testes/coroutine.lua b/testes/coroutine.lua
index 461e0377..76c9d6e6 100644
--- a/testes/coroutine.lua
+++ b/testes/coroutine.lua
@@ -136,6 +136,10 @@ do
136 assert(coroutine.status(co) == "dead") 136 assert(coroutine.status(co) == "dead")
137 local st, msg = coroutine.close(co) 137 local st, msg = coroutine.close(co)
138 assert(st and msg == nil) 138 assert(st and msg == nil)
139 -- also ok to close it again
140 st, msg = coroutine.close(co)
141 assert(st and msg == nil)
142
139 143
140 -- cannot close the running coroutine 144 -- cannot close the running coroutine
141 local st, msg = pcall(coroutine.close, coroutine.running()) 145 local st, msg = pcall(coroutine.close, coroutine.running())
@@ -149,6 +153,22 @@ do
149 assert(not st and string.find(msg, "normal")) 153 assert(not st and string.find(msg, "normal"))
150 end))() 154 end))()
151 155
156 -- cannot close a coroutine while closing it
157 do
158 local co
159 co = coroutine.create(
160 function()
161 local x <close> = func2close(function()
162 coroutine.close(co) -- try to close it again
163 end)
164 coroutine.yield(20)
165 end)
166 local st, msg = coroutine.resume(co)
167 assert(st and msg == 20)
168 st, msg = coroutine.close(co)
169 assert(not st and string.find(msg, "running coroutine"))
170 end
171
152 -- to-be-closed variables in coroutines 172 -- to-be-closed variables in coroutines
153 local X 173 local X
154 174
@@ -158,6 +178,9 @@ do
158 assert(not st and msg == 100) 178 assert(not st and msg == 100)
159 st, msg = coroutine.close(co) 179 st, msg = coroutine.close(co)
160 assert(not st and msg == 100) 180 assert(not st and msg == 100)
181 -- after closing, no more errors
182 st, msg = coroutine.close(co)
183 assert(st and msg == nil)
161 184
162 co = coroutine.create(function () 185 co = coroutine.create(function ()
163 local x <close> = func2close(function (self, err) 186 local x <close> = func2close(function (self, err)
@@ -189,6 +212,9 @@ do
189 local st, msg = coroutine.close(co) 212 local st, msg = coroutine.close(co)
190 assert(st == false and coroutine.status(co) == "dead" and msg == 200) 213 assert(st == false and coroutine.status(co) == "dead" and msg == 200)
191 assert(x == 200) 214 assert(x == 200)
215 -- after closing, no more errors
216 st, msg = coroutine.close(co)
217 assert(st and msg == nil)
192end 218end
193 219
194do 220do
@@ -419,7 +445,7 @@ do
419 445
420 local X = false 446 local X = false
421 A = coroutine.wrap(function() 447 A = coroutine.wrap(function()
422 local _ <close> = setmetatable({}, {__close = function () X = true end}) 448 local _ <close> = func2close(function () X = true end)
423 return pcall(A, 1) 449 return pcall(A, 1)
424 end) 450 end)
425 st, res = A() 451 st, res = A()
@@ -427,6 +453,22 @@ do
427end 453end
428 454
429 455
456-- bug in 5.4.1
457do
458 -- coroutine ran close metamethods with invalid status during a
459 -- reset.
460 local co
461 co = coroutine.wrap(function()
462 local x <close> = func2close(function() return pcall(co) end)
463 error(111)
464 end)
465 local st, errobj = pcall(co)
466 assert(not st and errobj == 111)
467 st, errobj = pcall(co)
468 assert(not st and string.find(errobj, "dead coroutine"))
469end
470
471
430-- attempt to resume 'normal' coroutine 472-- attempt to resume 'normal' coroutine
431local co1, co2 473local co1, co2
432co1 = coroutine.create(function () return co2() end) 474co1 = coroutine.create(function () return co2() end)