diff options
Diffstat (limited to 'testes/coroutine.lua')
-rw-r--r-- | testes/coroutine.lua | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/testes/coroutine.lua b/testes/coroutine.lua index 7d42eadd..5674a4dd 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua | |||
@@ -119,6 +119,51 @@ end | |||
119 | assert(#a == 25 and a[#a] == 97) | 119 | assert(#a == 25 and a[#a] == 97) |
120 | x, a = nil | 120 | x, a = nil |
121 | 121 | ||
122 | |||
123 | -- coroutine kill | ||
124 | do | ||
125 | -- ok to kill a dead coroutine | ||
126 | local co = coroutine.create(print) | ||
127 | assert(coroutine.resume(co, "testing 'coroutine.kill'")) | ||
128 | assert(coroutine.status(co) == "dead") | ||
129 | assert(coroutine.kill(co)) | ||
130 | |||
131 | -- cannot kill the running coroutine | ||
132 | local st, msg = pcall(coroutine.kill, coroutine.running()) | ||
133 | assert(not st and string.find(msg, "running")) | ||
134 | |||
135 | local main = coroutine.running() | ||
136 | |||
137 | -- cannot kill a "normal" coroutine | ||
138 | ;(coroutine.wrap(function () | ||
139 | local st, msg = pcall(coroutine.kill, main) | ||
140 | assert(not st and string.find(msg, "normal")) | ||
141 | end))() | ||
142 | |||
143 | -- to-be-closed variables in coroutines | ||
144 | local X | ||
145 | co = coroutine.create(function () | ||
146 | local *toclose x = function (err) assert(err == nil); X = false end | ||
147 | X = true | ||
148 | coroutine.yield() | ||
149 | end) | ||
150 | coroutine.resume(co) | ||
151 | assert(X) | ||
152 | assert(coroutine.kill(co)) | ||
153 | assert(not X and coroutine.status(co) == "dead") | ||
154 | |||
155 | -- error killing a coroutine | ||
156 | co = coroutine.create(function() | ||
157 | local *toclose x = function (err) assert(err == nil); error(111) end | ||
158 | coroutine.yield() | ||
159 | end) | ||
160 | coroutine.resume(co) | ||
161 | local st, msg = coroutine.kill(co) | ||
162 | assert(not st and coroutine.status(co) == "dead" and msg == 111) | ||
163 | |||
164 | end | ||
165 | |||
166 | |||
122 | -- yielding across C boundaries | 167 | -- yielding across C boundaries |
123 | 168 | ||
124 | co = coroutine.wrap(function() | 169 | co = coroutine.wrap(function() |