3 (use-package :test-util
)
5 (defmacro assert-timeout
((expected-message) form
)
6 (let ((ok (gensym "OK")))
11 (assert (string= ,expected-message
12 (princ-to-string condition
)))
14 (error "No timeout from form:~% ~S" ',form
)))))
16 (defun run-sleep (seconds)
17 (sb-ext:run-program
"sleep" (list (format nil
"~D" seconds
))
20 (with-test (:name
(sb-sys:decode-timeout
:large-values
:lp-1727789
))
21 (flet ((test (seconds)
22 (assert (not (sb-sys:decode-timeout seconds
)))))
24 (test (1+ sb-kernel
:internal-seconds-limit
))
25 (test (float (1+ sb-kernel
:internal-seconds-limit
) 1.0f0
))
26 (test (float (1+ sb-kernel
:internal-seconds-limit
) 1.0d0
))))
28 (with-test (:name
(sb-sys:with-deadline
:large-values
:lp-1727789
))
29 (flet ((test (seconds)
30 (let ((sem (sb-thread:make-semaphore
:count
0)))
31 (assert-timeout ("A deadline was reached after 0 seconds.")
32 (sb-sys:with-deadline
(:seconds seconds
)
33 (sb-sys:with-deadline
(:seconds
0)
34 (sb-thread:wait-on-semaphore sem
)))))))
35 (test (1+ most-positive-fixnum
))
36 (test (1+ sb-kernel
:internal-seconds-limit
))
37 (test (float (1+ sb-kernel
:internal-seconds-limit
) 1.0f0
))
38 (test (float (1+ sb-kernel
:internal-seconds-limit
) 1.0d0
))))
40 (with-test (:name
(:deadline sb-ext
:run-program
:trivial
) :fails-on
:win32
)
41 (assert-timeout ("A deadline was reached after 1 second.")
42 (sb-sys:with-deadline
(:seconds
1)
45 (with-test (:name
(:deadline sb-sys
:defer-deadline
1) :fails-on
:win32
)
47 (assert-timeout ("A deadline was reached after 0.1 seconds.")
48 (handler-bind ((sb-sys:deadline-timeout
52 (sb-sys:defer-deadline
0.1 c
)))))
53 (sb-sys:with-deadline
(:seconds
1)
57 (with-test (:name
(:deadline sb-sys
:defer-deadline
2) :fails-on
:win32
)
61 (handler-bind ((sb-sys:deadline-timeout
64 (sb-sys:defer-deadline
0.1 c
))))
65 (sb-sys:with-deadline
(:seconds
1)
67 (sb-sys:deadline-timeout
(c)
70 (assert (not final
))))
72 (with-test (:name
(:deadline sb-sys
:defer-deadline
3) :fails-on
:win32
)
74 (assert-timeout ("A deadline was reached after 0.1 seconds.")
75 (handler-bind ((sb-sys:deadline-timeout
77 (declare (ignore condition
))
80 (invoke-restart 'sb-sys
:defer-deadline
)))))
81 (sb-sys:with-deadline
(:seconds
.1)
85 (with-test (:name
(:deadline sb-sys
:cancel-deadline
) :fails-on
:win32
)
89 (handler-bind ((sb-sys:deadline-timeout
92 (sb-sys:cancel-deadline c
))))
93 (sb-sys:with-deadline
(:seconds
1)
95 (sb-sys:deadline-timeout
(c)
98 (assert (not final
))))
100 (with-test (:name
(:deadline sb-thread
:grab-mutex
)
101 :skipped-on
(not :sb-thread
))
102 (assert-timeout ("A deadline was reached after 1 second.")
103 (let ((lock (sb-thread:make-mutex
))
105 (make-join-thread (lambda ()
106 (sb-thread:grab-mutex lock
)
109 (loop while waitp do
(sleep 0.01))
110 (sb-sys:with-deadline
(:seconds
1)
111 (sb-thread:grab-mutex lock
)))))
113 (with-test (:name
(:deadline sb-thread
:wait-on-semaphore
)
114 :skipped-on
(not :sb-thread
))
115 (assert-timeout ("A deadline was reached after 1 second.")
116 (let ((sem (sb-thread:make-semaphore
:count
0)))
117 (sb-sys:with-deadline
(:seconds
1)
118 (sb-thread:wait-on-semaphore sem
)))))
120 (with-test (:name
(:deadline sb-thread
:join-thread
)
121 :skipped-on
(not :sb-thread
)
123 (assert-timeout ("A deadline was reached after 1 second.")
124 (sb-sys:with-deadline
(:seconds
1)
125 (sb-thread:join-thread
126 (make-kill-thread (lambda () (loop (sleep 1))))))))
128 (with-test (:name
(:deadline
:futex-wait-eintr
)
129 :skipped-on
(not :sb-thread
)
131 (let ((lock (sb-thread:make-mutex
))
133 (make-join-thread (lambda ()
134 (sb-thread:grab-mutex lock
)
137 (loop while waitp do
(sleep 0.01))
138 (let ((thread (make-join-thread
140 (let ((start (get-internal-real-time)))
142 (sb-sys:with-deadline
(:seconds
1)
143 (sb-thread:grab-mutex lock
))
144 (sb-sys:deadline-timeout
(x)
146 (let ((end (get-internal-real-time)))
147 (float (/ (- end start
)
148 internal-time-units-per-second
)
151 (sb-thread:interrupt-thread thread
(lambda () 42))
152 (let ((seconds-passed (sb-thread:join-thread thread
)))
153 (assert (< seconds-passed
1.2))))))
157 (with-test (:name
(sb-sys:with-deadline sleep
:smoke
))
158 (assert-timeout ("A deadline was reached after 0.1 seconds.")
159 (sb-sys:with-deadline
(:seconds
.1) (sleep 1)))
162 (sb-sys:with-deadline
(:seconds
.2) (sleep .1))
163 sb-sys
:deadline-timeout
))
165 (with-test (:name
(sb-sys:with-deadline sleep
:long-sleep
))
166 (assert-timeout ("A deadline was reached after 0.1 seconds.")
167 (sb-sys:with-deadline
(:seconds
.1)
168 (sleep (1+ sb-kernel
:internal-seconds-limit
)))))
170 (with-test (:name
(sb-sys:with-deadline sleep
:no-sleep
))
171 ;; When SLEEP is called in the context of an expired deadline, the
172 ;; DEADLINE-TIMEOUT must be signaled even if there is no sleeping to
174 (assert-timeout ("A deadline was reached after 0.1 seconds.")
175 (sb-sys:with-deadline
(:seconds
.1)
176 (let ((sb-impl::*deadline
* nil
)) (sleep .2))
179 (with-test (:name
(sb-sys:with-deadline sleep sb-sys
:defer-deadline
))
182 (handler-bind ((sb-sys:deadline-timeout
185 (sb-sys:defer-deadline
.1 condition
))))
186 (sb-sys:with-deadline
(:seconds
.1) (sleep .5)))
187 sb-sys
:deadline-timeout
)
190 (with-test (:name
(sb-sys:with-deadline sleep sb-sys
:cancel-deadline
))
192 (handler-bind ((sb-sys:deadline-timeout
194 (sb-sys:cancel-deadline condition
))))
195 (sb-sys:with-deadline
(:seconds
.1) (sleep 1)))
196 sb-sys
:deadline-timeout
))