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 (defmacro raises-timeout-p (&body body)
17 `(handler-case (progn (progn ,@body) nil)
18 (sb-ext:timeout () t)))
20 (with-test (:name (:timer :relative))
21 (let* ((has-run-p nil)
22 (timer (make-timer (lambda () (setq has-run-p t))
23 :name "simple timer")))
24 (schedule-timer timer 0.5)
26 (assert (not has-run-p))
29 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
31 (with-test (:name (:timer :absolute))
32 (let* ((has-run-p nil)
33 (timer (make-timer (lambda () (setq has-run-p t))
34 :name "simple timer")))
35 (schedule-timer timer (+ 1/2 (get-universal-time)) :absolute-p t)
37 (assert (not has-run-p))
40 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
43 (with-test (:name (:timer :other-thread))
44 (let* ((thread (sb-thread:make-thread (lambda () (sleep 2))))
45 (timer (make-timer (lambda ()
46 (assert (eq thread sb-thread:*current-thread*)))
48 (schedule-timer timer 0.1)))
51 (with-test (:name (:timer :new-thread))
52 (let* ((original-thread sb-thread:*current-thread*)
55 (assert (not (eq original-thread
56 sb-thread:*current-thread*))))
58 (schedule-timer timer 0.1)))
60 (with-test (:name (:timer :repeat-and-unschedule))
64 (make-timer (lambda ()
65 (when (= 5 (incf run-count))
66 (unschedule-timer timer)))))
67 (schedule-timer timer 0 :repeat-interval 0.2)
68 (assert (timer-scheduled-p timer :delta 0.3))
70 (assert (= 5 run-count))
71 (assert (not (timer-scheduled-p timer)))
72 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
74 (with-test (:name (:timer :reschedule))
75 (let* ((has-run-p nil)
76 (timer (make-timer (lambda ()
77 (setq has-run-p t)))))
78 (schedule-timer timer 0.2)
79 (schedule-timer timer 0.3)
82 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
84 (with-test (:name (:timer :stress))
85 (let ((time (1+ (get-universal-time))))
87 (schedule-timer (make-timer (lambda ())) time :absolute-p t))
89 (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*))))))
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
113 (defun wait-for-threads (threads)
114 (loop while (some #'sb-thread:thread-alive-p threads) do (sleep 0.01)))
117 (with-test (:name (:with-timeout :many-at-the-same-time))
119 (let ((threads (loop repeat 10 collect
120 (sb-thread:make-thread
123 (sb-ext:with-timeout 0.5
126 (format t "~%not ok~%"))
129 (assert (not (raises-timeout-p
130 (sb-ext:with-timeout 20
131 (wait-for-threads threads)))))
135 (with-test (:name (:with-timeout :dead-thread))
136 (sb-thread:make-thread
138 (let ((timer (make-timer (lambda ()))))
139 (schedule-timer timer 3)
145 (defun random-type (n)
146 `(integer ,(random n) ,(+ n (random n))))
148 ;;; FIXME: Since timeouts do not work on Windows this would loop
151 (with-test (:name '(:hash-cache :interrupt))
152 (let* ((type1 (random-type 500))
153 (type2 (random-type 500))
154 (wanted (subtypep type1 type2)))
157 (sb-ext:schedule-timer (sb-ext:make-timer
159 (assert (eq wanted (subtypep type1 type2)))
163 (assert (eq wanted (subtypep type1 type2))))))))