1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; While most of SBCL is derived from the CMU CL system, the test
5 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; This software is in the public domain and is provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
10 ;;;; more information.
12 (in-package "CL-USER")
14 (use-package :test-util
)
16 (with-test (:name
(:timer
:relative
))
17 (let* ((has-run-p nil
)
18 (timer (make-timer (lambda () (setq has-run-p t
))
19 :name
"simple timer")))
20 (schedule-timer timer
0.5)
22 (assert (not has-run-p
))
25 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl
::*schedule
*))))))
27 (with-test (:name
(:timer
:absolute
))
28 (let* ((has-run-p nil
)
29 (timer (make-timer (lambda () (setq has-run-p t
))
30 :name
"simple timer")))
31 (schedule-timer timer
(+ 1/2 (get-universal-time)) :absolute-p t
)
33 (assert (not has-run-p
))
36 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl
::*schedule
*))))))
39 (with-test (:name
(:timer
:other-thread
))
40 (let* ((thread (sb-thread:make-thread
(lambda () (sleep 2))))
41 (timer (make-timer (lambda ()
42 (assert (eq thread sb-thread
:*current-thread
*)))
44 (schedule-timer timer
0.1)))
47 (with-test (:name
(:timer
:new-thread
))
48 (let* ((original-thread sb-thread
:*current-thread
*)
51 (assert (not (eq original-thread
52 sb-thread
:*current-thread
*))))
54 (schedule-timer timer
0.1)))
56 (with-test (:name
(:timer
:repeat-and-unschedule
))
60 (make-timer (lambda ()
61 (when (= 5 (incf run-count
))
62 (unschedule-timer timer
)))))
63 (schedule-timer timer
0 :repeat-interval
0.2)
64 (assert (timer-scheduled-p timer
:delta
0.3))
66 (assert (= 5 run-count
))
67 (assert (not (timer-scheduled-p timer
)))
68 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl
::*schedule
*))))))
70 (with-test (:name
(:timer
:reschedule
))
71 (let* ((has-run-p nil
)
72 (timer (make-timer (lambda ()
73 (setq has-run-p t
)))))
74 (schedule-timer timer
0.2)
75 (schedule-timer timer
0.3)
78 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl
::*schedule
*))))))
80 (with-test (:name
(:timer
:stress
))
81 (let ((time (1+ (get-universal-time))))
83 (schedule-timer (make-timer (lambda ())) time
:absolute-p t
))
85 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl
::*schedule
*))))))
87 (defmacro raises-timeout-p
(&body body
)
88 `(handler-case (progn (progn ,@body
) nil
)
89 (sb-ext:timeout
() t
)))
91 (with-test (:name
(:with-timeout
:timeout
))
92 (assert (raises-timeout-p
93 (sb-ext:with-timeout
0.2
96 (with-test (:name
(:with-timeout
:fall-through
))
97 (assert (not (raises-timeout-p
98 (sb-ext:with-timeout
0.3
101 (with-test (:name
(:with-timeout
:nested-timeout-smaller
))
102 (assert(raises-timeout-p
103 (sb-ext:with-timeout
10
104 (sb-ext:with-timeout
0.5
107 (with-test (:name
(:with-timeout
:nested-timeout-bigger
))
108 (assert(raises-timeout-p
109 (sb-ext:with-timeout
0.5
110 (sb-ext:with-timeout
2
114 (with-test (:name
(:with-timeout
:many-at-the-same-time
))
116 (sb-thread:make-thread
118 (sb-ext:with-timeout
0.5