Debian Codegraph @

test-suite/tests/srfi-18.test

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