Commit | Line | Data |
---|---|---|
b046219e JG |
1 | ;;;; srfi-18.test --- Test suite for Guile's SRFI-18 functions. -*- scheme -*- |
2 | ;;;; Julian Graham, 2007-10-26 | |
3 | ;;;; | |
a850c3cc | 4 | ;;;; Copyright (C) 2007, 2008, 2012 Free Software Foundation, Inc. |
b046219e | 5 | ;;;; |
53befeb7 NJ |
6 | ;;;; This library is free software; you can redistribute it and/or |
7 | ;;;; modify it under the terms of the GNU Lesser General Public | |
8 | ;;;; License as published by the Free Software Foundation; either | |
9 | ;;;; version 3 of the License, or (at your option) any later version. | |
b046219e | 10 | ;;;; |
53befeb7 | 11 | ;;;; This library is distributed in the hope that it will be useful, |
b046219e | 12 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
53befeb7 NJ |
13 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | ;;;; Lesser General Public License for more details. | |
b046219e | 15 | ;;;; |
53befeb7 NJ |
16 | ;;;; You should have received a copy of the GNU Lesser General Public |
17 | ;;;; License along with this library; if not, write to the Free Software | |
18 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
b046219e JG |
19 | |
20 | (define-module (test-suite test-srfi-18) | |
d0b6ad34 | 21 | #:use-module (test-suite lib)) |
b046219e | 22 | |
30a5e062 AW |
23 | ;; two expressions so that the srfi-18 import is in effect for expansion |
24 | ;; of the rest | |
25 | (if (provided? 'threads) | |
26 | (use-modules (srfi srfi-18))) | |
27 | ||
a850c3cc AW |
28 | (cond |
29 | ((provided? 'threads) | |
30 | (with-test-prefix "current-thread" | |
70eca635 | 31 | |
a850c3cc AW |
32 | (pass-if "current-thread eq current-thread" |
33 | (eq? (current-thread) (current-thread)))) | |
b046219e | 34 | |
a850c3cc | 35 | (with-test-prefix "thread?" |
b046219e | 36 | |
a850c3cc AW |
37 | (pass-if "current-thread is thread" |
38 | (thread? (current-thread))) | |
b046219e | 39 | |
a850c3cc AW |
40 | (pass-if "foo not thread" |
41 | (not (thread? 'foo)))) | |
b046219e | 42 | |
a850c3cc | 43 | (with-test-prefix "make-thread" |
b046219e | 44 | |
a850c3cc AW |
45 | (pass-if "make-thread creates new thread" |
46 | (let* ((n (length (all-threads))) | |
47 | (t (make-thread (lambda () 'foo) 'make-thread-1)) | |
48 | (r (> (length (all-threads)) n))) | |
49 | (thread-terminate! t) r))) | |
b046219e | 50 | |
a850c3cc | 51 | (with-test-prefix "thread-name" |
b046219e | 52 | |
a850c3cc AW |
53 | (pass-if "make-thread with name binds name" |
54 | (let* ((t (make-thread (lambda () 'foo) 'thread-name-1)) | |
55 | (r (eq? (thread-name t) 'thread-name-1))) | |
56 | (thread-terminate! t) r)) | |
b046219e | 57 | |
a850c3cc AW |
58 | (pass-if "make-thread without name does not bind name" |
59 | (let* ((t (make-thread (lambda () 'foo))) | |
60 | (r (not (thread-name t)))) | |
61 | (thread-terminate! t) r))) | |
b046219e | 62 | |
a850c3cc | 63 | (with-test-prefix "thread-specific" |
b046219e | 64 | |
a850c3cc AW |
65 | (pass-if "thread-specific is initially #f" |
66 | (let* ((t (make-thread (lambda () 'foo) 'thread-specific-1)) | |
67 | (r (not (thread-specific t)))) | |
68 | (thread-terminate! t) r)) | |
b046219e | 69 | |
a850c3cc AW |
70 | (pass-if "thread-specific-set! can set value" |
71 | (let ((t (make-thread (lambda () 'foo) 'thread-specific-2))) | |
72 | (thread-specific-set! t "hello") | |
73 | (let ((r (equal? (thread-specific t) "hello"))) | |
74 | (thread-terminate! t) r)))) | |
b046219e | 75 | |
a850c3cc | 76 | (with-test-prefix "thread-start!" |
b046219e | 77 | |
a850c3cc AW |
78 | (pass-if "thread activates only after start" |
79 | (let* ((started #f) | |
80 | (m (make-mutex 'thread-start-mutex)) | |
81 | (t (make-thread (lambda () (set! started #t)) 'thread-start-1))) | |
82 | (and (not started) (thread-start! t) (thread-join! t) started)))) | |
b046219e | 83 | |
a850c3cc | 84 | (with-test-prefix "thread-yield!" |
b046219e | 85 | |
a850c3cc AW |
86 | (pass-if "thread yield suceeds" |
87 | (thread-yield!) #t)) | |
b046219e | 88 | |
a850c3cc | 89 | (with-test-prefix "thread-sleep!" |
b046219e | 90 | |
a850c3cc AW |
91 | (pass-if "thread sleep with time" |
92 | (let ((future-time (seconds->time (+ (time->seconds (current-time)) 2)))) | |
93 | (unspecified? (thread-sleep! future-time)))) | |
b046219e | 94 | |
a850c3cc AW |
95 | (pass-if "thread sleep with number" |
96 | (let ((old-secs (car (current-time)))) | |
97 | (unspecified? (thread-sleep! (+ (time->seconds (current-time))))))) | |
b046219e | 98 | |
a850c3cc AW |
99 | (pass-if "thread does not sleep on past time" |
100 | (let ((past-time (seconds->time (- (time->seconds (current-time)) 2)))) | |
101 | (unspecified? (thread-sleep! past-time))))) | |
b046219e | 102 | |
a850c3cc | 103 | (with-test-prefix "thread-terminate!" |
b046219e | 104 | |
a850c3cc AW |
105 | (pass-if "termination destroys non-started thread" |
106 | (let ((t (make-thread (lambda () 'nothing) 'thread-terminate-1)) | |
107 | (num-threads (length (all-threads))) | |
108 | (success #f)) | |
109 | (thread-terminate! t) | |
110 | (with-exception-handler | |
111 | (lambda (obj) (set! success (terminated-thread-exception? obj))) | |
112 | (lambda () (thread-join! t))) | |
113 | success)) | |
114 | ||
115 | (pass-if "termination destroys started thread" | |
116 | (let* ((m1 (make-mutex 'thread-terminate-2a)) | |
117 | (m2 (make-mutex 'thread-terminate-2b)) | |
118 | (c (make-condition-variable 'thread-terminate-2)) | |
119 | (t (make-thread (lambda () | |
120 | (mutex-lock! m1) | |
121 | (condition-variable-signal! c) | |
122 | (mutex-unlock! m1) | |
123 | (mutex-lock! m2)) | |
124 | 'thread-terminate-2)) | |
125 | (success #f)) | |
126 | (mutex-lock! m1) | |
127 | (mutex-lock! m2) | |
128 | (thread-start! t) | |
129 | (mutex-unlock! m1 c) | |
130 | (thread-terminate! t) | |
131 | (with-exception-handler | |
132 | (lambda (obj) (set! success (terminated-thread-exception? obj))) | |
133 | (lambda () (thread-join! t))) | |
134 | success))) | |
135 | ||
136 | (with-test-prefix "thread-join!" | |
137 | ||
138 | (pass-if "join receives result of thread" | |
139 | (let ((t (make-thread (lambda () 'foo) 'thread-join-1))) | |
140 | (thread-start! t) | |
141 | (eq? (thread-join! t) 'foo))) | |
142 | ||
143 | (pass-if "join receives timeout val if timeout expires" | |
144 | (let* ((m (make-mutex 'thread-join-2)) | |
145 | (t (make-thread (lambda () (mutex-lock! m)) 'thread-join-2))) | |
146 | (mutex-lock! m) | |
147 | (thread-start! t) | |
148 | (let ((r (thread-join! t (current-time) 'bar))) | |
149 | (thread-terminate! t) | |
150 | (eq? r 'bar)))) | |
151 | ||
152 | (pass-if "join throws exception on timeout without timeout val" | |
153 | (let* ((m (make-mutex 'thread-join-3)) | |
154 | (t (make-thread (lambda () (mutex-lock! m)) 'thread-join-3)) | |
155 | (success #f)) | |
156 | (mutex-lock! m) | |
157 | (thread-start! t) | |
158 | (with-exception-handler | |
159 | (lambda (obj) (set! success (join-timeout-exception? obj))) | |
160 | (lambda () (thread-join! t (current-time)))) | |
161 | (thread-terminate! t) | |
162 | success)) | |
163 | ||
164 | (pass-if "join waits on timeout" | |
165 | (let ((t (make-thread (lambda () (sleep 1) 'foo) 'thread-join-4))) | |
166 | (thread-start! t) | |
167 | (eq? (thread-join! t (+ (time->seconds (current-time)) 2)) 'foo)))) | |
168 | ||
169 | (with-test-prefix "mutex?" | |
170 | ||
171 | (pass-if "make-mutex creates mutex" | |
172 | (mutex? (make-mutex))) | |
173 | ||
174 | (pass-if "symbol not mutex" | |
175 | (not (mutex? 'foo)))) | |
176 | ||
177 | (with-test-prefix "mutex-name" | |
178 | ||
179 | (pass-if "make-mutex with name binds name" | |
180 | (let* ((m (make-mutex 'mutex-name-1))) | |
181 | (eq? (mutex-name m) 'mutex-name-1))) | |
182 | ||
183 | (pass-if "make-mutex without name does not bind name" | |
184 | (let* ((m (make-mutex))) | |
185 | (not (mutex-name m))))) | |
186 | ||
187 | (with-test-prefix "mutex-specific" | |
188 | ||
189 | (pass-if "mutex-specific is initially #f" | |
190 | (let ((m (make-mutex 'mutex-specific-1))) | |
191 | (not (mutex-specific m)))) | |
192 | ||
193 | (pass-if "mutex-specific-set! can set value" | |
194 | (let ((m (make-mutex 'mutex-specific-2))) | |
195 | (mutex-specific-set! m "hello") | |
196 | (equal? (mutex-specific m) "hello")))) | |
197 | ||
198 | (with-test-prefix "mutex-state" | |
199 | ||
200 | (pass-if "mutex state is initially not-abandoned" | |
201 | (let ((m (make-mutex 'mutex-state-1))) | |
202 | (eq? (mutex-state m) 'not-abandoned))) | |
203 | ||
204 | (pass-if "mutex state of locked, owned mutex is owner thread" | |
205 | (let ((m (make-mutex 'mutex-state-2))) | |
206 | (mutex-lock! m) | |
207 | (eq? (mutex-state m) (current-thread)))) | |
b046219e | 208 | |
a850c3cc AW |
209 | (pass-if "mutex state of locked, unowned mutex is not-owned" |
210 | (let ((m (make-mutex 'mutex-state-3))) | |
211 | (mutex-lock! m #f #f) | |
212 | (eq? (mutex-state m) 'not-owned))) | |
213 | ||
214 | (pass-if "mutex state of unlocked, abandoned mutex is abandoned" | |
215 | (let* ((m (make-mutex 'mutex-state-4)) | |
216 | (t (make-thread (lambda () (mutex-lock! m))))) | |
217 | (thread-start! t) | |
218 | (thread-join! t) | |
219 | (eq? (mutex-state m) 'abandoned)))) | |
220 | ||
221 | (with-test-prefix "mutex-lock!" | |
b046219e | 222 | |
a850c3cc AW |
223 | (pass-if "mutex-lock! returns true on successful lock" |
224 | (let* ((m (make-mutex 'mutex-lock-1))) | |
225 | (mutex-lock! m))) | |
226 | ||
227 | (pass-if "mutex-lock! returns false on timeout" | |
228 | (let* ((m (make-mutex 'mutex-lock-2)) | |
229 | (t (make-thread (lambda () (mutex-lock! m (current-time) #f))))) | |
230 | (mutex-lock! m) | |
231 | (thread-start! t) | |
232 | (not (thread-join! t)))) | |
233 | ||
234 | (pass-if "mutex-lock! returns true when lock obtained within timeout" | |
235 | (let* ((m (make-mutex 'mutex-lock-3)) | |
236 | (t (make-thread (lambda () | |
237 | (mutex-lock! m (+ (time->seconds (current-time)) | |
238 | 100) | |
239 | #f))))) | |
240 | (mutex-lock! m) | |
241 | (thread-start! t) | |
242 | (mutex-unlock! m) | |
243 | (thread-join! t))) | |
244 | ||
245 | (pass-if "can lock mutex for non-current thread" | |
246 | (let* ((m1 (make-mutex 'mutex-lock-4a)) | |
247 | (m2 (make-mutex 'mutex-lock-4b)) | |
248 | (t (make-thread (lambda () (mutex-lock! m1)) 'mutex-lock-4))) | |
249 | (mutex-lock! m1) | |
250 | (thread-start! t) | |
251 | (mutex-lock! m2 #f t) | |
252 | (let ((success (eq? (mutex-state m2) t))) | |
253 | (thread-terminate! t) success))) | |
254 | ||
255 | (pass-if "locking abandoned mutex throws exception" | |
256 | (let* ((m (make-mutex 'mutex-lock-5)) | |
257 | (t (make-thread (lambda () (mutex-lock! m)) 'mutex-lock-5)) | |
258 | (success #f)) | |
259 | (thread-start! t) | |
260 | (thread-join! t) | |
261 | (with-exception-handler | |
262 | (lambda (obj) (set! success (abandoned-mutex-exception? obj))) | |
263 | (lambda () (mutex-lock! m))) | |
264 | (and success (eq? (mutex-state m) (current-thread))))) | |
265 | ||
266 | (pass-if "sleeping threads notified of abandonment" | |
267 | (let* ((m1 (make-mutex 'mutex-lock-6a)) | |
268 | (m2 (make-mutex 'mutex-lock-6b)) | |
269 | (c (make-condition-variable 'mutex-lock-6)) | |
270 | (t (make-thread (lambda () | |
271 | (mutex-lock! m1) | |
272 | (mutex-lock! m2) | |
273 | (condition-variable-signal! c)))) | |
274 | (success #f)) | |
275 | (mutex-lock! m1) | |
276 | (thread-start! t) | |
277 | (with-exception-handler | |
278 | (lambda (obj) (set! success (abandoned-mutex-exception? obj))) | |
279 | (lambda () (mutex-unlock! m1 c) (mutex-lock! m2))) | |
280 | success))) | |
281 | ||
282 | (with-test-prefix "mutex-unlock!" | |
b046219e | 283 | |
a850c3cc AW |
284 | (pass-if "unlock changes mutex state" |
285 | (let* ((m (make-mutex 'mutex-unlock-1))) | |
286 | (mutex-lock! m) | |
287 | (mutex-unlock! m) | |
288 | (eq? (mutex-state m) 'not-abandoned))) | |
289 | ||
290 | (pass-if "can unlock from any thread" | |
291 | (let* ((m (make-mutex 'mutex-unlock-2)) | |
292 | (t (make-thread (lambda () (mutex-unlock! m)) 'mutex-unlock-2))) | |
293 | (mutex-lock! m) | |
294 | (thread-start! t) | |
295 | (thread-join! t) | |
296 | (eq? (mutex-state m) 'not-abandoned))) | |
297 | ||
298 | (pass-if "mutex unlock is true when condition is signalled" | |
299 | (let* ((m (make-mutex 'mutex-unlock-3)) | |
300 | (c (make-condition-variable 'mutex-unlock-3)) | |
301 | (t (make-thread (lambda () | |
302 | (mutex-lock! m) | |
303 | (condition-variable-signal! c) | |
304 | (mutex-unlock! m))))) | |
305 | (mutex-lock! m) | |
306 | (thread-start! t) | |
307 | (mutex-unlock! m c))) | |
308 | ||
309 | (pass-if "mutex unlock is false when condition times out" | |
310 | (let* ((m (make-mutex 'mutex-unlock-4)) | |
311 | (c (make-condition-variable 'mutex-unlock-4))) | |
312 | (mutex-lock! m) | |
313 | (not (mutex-unlock! m c (+ (time->seconds (current-time)) 1)))))) | |
314 | ||
315 | (with-test-prefix "condition-variable?" | |
316 | ||
317 | (pass-if "make-condition-variable creates condition variable" | |
318 | (condition-variable? (make-condition-variable))) | |
319 | ||
320 | (pass-if "symbol not condition variable" | |
321 | (not (condition-variable? 'foo)))) | |
322 | ||
323 | (with-test-prefix "condition-variable-name" | |
324 | ||
325 | (pass-if "make-condition-variable with name binds name" | |
326 | (let* ((c (make-condition-variable 'condition-variable-name-1))) | |
327 | (eq? (condition-variable-name c) 'condition-variable-name-1))) | |
328 | ||
329 | (pass-if "make-condition-variable without name does not bind name" | |
330 | (let* ((c (make-condition-variable))) | |
331 | (not (condition-variable-name c))))) | |
332 | ||
333 | (with-test-prefix "condition-variable-specific" | |
334 | ||
335 | (pass-if "condition-variable-specific is initially #f" | |
336 | (let ((c (make-condition-variable 'condition-variable-specific-1))) | |
337 | (not (condition-variable-specific c)))) | |
338 | ||
339 | (pass-if "condition-variable-specific-set! can set value" | |
340 | (let ((c (make-condition-variable 'condition-variable-specific-1))) | |
341 | (condition-variable-specific-set! c "hello") | |
342 | (equal? (condition-variable-specific c) "hello")))) | |
343 | ||
344 | (with-test-prefix "condition-variable-signal!" | |
b046219e | 345 | |
a850c3cc AW |
346 | (pass-if "condition-variable-signal! wakes up single thread" |
347 | (let* ((m (make-mutex 'condition-variable-signal-1)) | |
348 | (c (make-condition-variable 'condition-variable-signal-1)) | |
349 | (t (make-thread (lambda () | |
350 | (mutex-lock! m) | |
351 | (condition-variable-signal! c) | |
352 | (mutex-unlock! m))))) | |
353 | (mutex-lock! m) | |
354 | (thread-start! t) | |
355 | (mutex-unlock! m c)))) | |
356 | ||
357 | (with-test-prefix "condition-variable-broadcast!" | |
358 | ||
359 | (pass-if "condition-variable-broadcast! wakes up multiple threads" | |
360 | (let* ((sem 0) | |
361 | (c1 (make-condition-variable 'condition-variable-broadcast-1-a)) | |
362 | (m1 (make-mutex 'condition-variable-broadcast-1-a)) | |
363 | (c2 (make-condition-variable 'condition-variable-broadcast-1-b)) | |
364 | (m2 (make-mutex 'condition-variable-broadcast-1-b)) | |
365 | (inc-sem! (lambda () | |
366 | (mutex-lock! m1) | |
367 | (set! sem (+ sem 1)) | |
368 | (condition-variable-broadcast! c1) | |
369 | (mutex-unlock! m1))) | |
370 | (dec-sem! (lambda () | |
371 | (mutex-lock! m1) | |
372 | (while (eqv? sem 0) (wait-condition-variable c1 m1)) | |
373 | (set! sem (- sem 1)) | |
374 | (mutex-unlock! m1))) | |
375 | (t1 (make-thread (lambda () | |
376 | (mutex-lock! m2) | |
377 | (inc-sem!) | |
378 | (mutex-unlock! m2 c2) | |
379 | (inc-sem!)))) | |
380 | (t2 (make-thread (lambda () | |
381 | (mutex-lock! m2) | |
382 | (inc-sem!) | |
383 | (mutex-unlock! m2 c2) | |
384 | (inc-sem!))))) | |
385 | (thread-start! t1) | |
386 | (thread-start! t2) | |
387 | (dec-sem!) | |
388 | (dec-sem!) | |
389 | (mutex-lock! m2) | |
390 | (condition-variable-broadcast! c2) | |
391 | (mutex-unlock! m2) | |
392 | (dec-sem!) | |
393 | (dec-sem!)))) | |
394 | ||
395 | (with-test-prefix "time?" | |
396 | ||
397 | (pass-if "current-time is time" (time? (current-time))) | |
398 | (pass-if "number is not time" (not (time? 123))) | |
399 | (pass-if "symbol not time" (not (time? 'foo)))) | |
400 | ||
401 | (with-test-prefix "time->seconds" | |
402 | ||
403 | (pass-if "time->seconds makes time into rational" | |
404 | (rational? (time->seconds (current-time)))) | |
405 | ||
406 | (pass-if "time->seconds is reversible" | |
407 | (let ((t (current-time))) | |
408 | (equal? t (seconds->time (time->seconds t)))))) | |
409 | ||
410 | (with-test-prefix "seconds->time" | |
411 | ||
412 | (pass-if "seconds->time makes rational into time" | |
413 | (time? (seconds->time 123.456))) | |
414 | ||
415 | (pass-if "seconds->time is reversible" | |
416 | (let ((t (time->seconds (current-time)))) | |
417 | (equal? t (time->seconds (seconds->time t)))))) | |
418 | ||
419 | (with-test-prefix "current-exception-handler" | |
420 | ||
421 | (pass-if "current handler returned at top level" | |
422 | (procedure? (current-exception-handler))) | |
423 | ||
424 | (pass-if "specified handler set under with-exception-handler" | |
425 | (let ((h (lambda (key . args) 'nothing))) | |
426 | (with-exception-handler h (lambda () (eq? (current-exception-handler) | |
427 | h))))) | |
428 | ||
429 | (pass-if "multiple levels of handler nesting" | |
430 | (let ((h (lambda (key . args) 'nothing)) | |
431 | (i (current-exception-handler))) | |
432 | (and (with-exception-handler h (lambda () | |
433 | (eq? (current-exception-handler) h))) | |
434 | (eq? (current-exception-handler) i)))) | |
435 | ||
436 | (pass-if "exception handler installation is thread-safe" | |
437 | (let* ((h1 (current-exception-handler)) | |
438 | (h2 (lambda (key . args) 'nothing-2)) | |
439 | (m (make-mutex 'current-exception-handler-4)) | |
440 | (c (make-condition-variable 'current-exception-handler-4)) | |
441 | (t (make-thread (lambda () | |
442 | (with-exception-handler | |
443 | h2 (lambda () | |
444 | (mutex-lock! m) | |
445 | (condition-variable-signal! c) | |
446 | (wait-condition-variable c m) | |
447 | (and (eq? (current-exception-handler) h2) | |
448 | (mutex-unlock! m))))) | |
449 | 'current-exception-handler-4))) | |
450 | (mutex-lock! m) | |
451 | (thread-start! t) | |
452 | (wait-condition-variable c m) | |
453 | (and (eq? (current-exception-handler) h1) | |
454 | (condition-variable-signal! c) | |
455 | (mutex-unlock! m) | |
456 | (thread-join! t))))) | |
457 | ||
458 | (with-test-prefix "uncaught-exception-reason" | |
459 | ||
460 | (pass-if "initial handler captures top level exception" | |
461 | (let ((t (make-thread (lambda () (raise 'foo)))) | |
462 | (success #f)) | |
463 | (thread-start! t) | |
464 | (with-exception-handler | |
465 | (lambda (obj) | |
466 | (and (uncaught-exception? obj) | |
467 | (eq? (uncaught-exception-reason obj) 'foo) | |
468 | (set! success #t))) | |
469 | (lambda () (thread-join! t))) | |
470 | success)) | |
471 | ||
472 | (pass-if "initial handler captures non-SRFI-18 throw" | |
473 | (let ((t (make-thread (lambda () (throw 'foo)))) | |
474 | (success #f)) | |
475 | (thread-start! t) | |
476 | (with-exception-handler | |
477 | (lambda (obj) | |
478 | (and (uncaught-exception? obj) | |
479 | (eq? (uncaught-exception-reason obj) 'foo) | |
480 | (set! success #t))) | |
481 | (lambda () (thread-join! t))) | |
482 | success))))) |