X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=tests%2Ftimer.impure.lisp;h=3cea11c68394159106a317a5c96dd1414cdd6677;hb=e7ef1082bd3e7f3851de9d90c5dbddf226a71382;hp=8a664538bd0c4faa7ab5a71d23c6df2bc021a242;hpb=18aa51db1d51cb20eb35a8e34c36cd78446c6bdd;p=sbcl.git diff --git a/tests/timer.impure.lisp b/tests/timer.impure.lisp index 8a66453..3cea11c 100644 --- a/tests/timer.impure.lisp +++ b/tests/timer.impure.lisp @@ -13,7 +13,12 @@ (use-package :test-util) -(with-test (:name (:timer :relative)) +(defmacro raises-timeout-p (&body body) + `(handler-case (progn (progn ,@body) nil) + (sb-ext:timeout () t))) + +(with-test (:name (:timer :relative) + :fails-on '(and :sparc :linux)) (let* ((has-run-p nil) (timer (make-timer (lambda () (setq has-run-p t)) :name "simple timer"))) @@ -24,7 +29,8 @@ (assert has-run-p) (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*)))))) -(with-test (:name (:timer :absolute)) +(with-test (:name (:timer :absolute) + :fails-on '(and :sparc :linux)) (let* ((has-run-p nil) (timer (make-timer (lambda () (setq has-run-p t)) :name "simple timer"))) @@ -53,7 +59,8 @@ :thread t))) (schedule-timer timer 0.1))) -(with-test (:name (:timer :repeat-and-unschedule)) +(with-test (:name (:timer :repeat-and-unschedule) + :fails-on '(and :sparc :linux)) (let* ((run-count 0) timer) (setq timer @@ -84,10 +91,6 @@ (sleep 2) (assert (zerop (length (sb-impl::%pqueue-contents sb-impl::*schedule*)))))) -(defmacro raises-timeout-p (&body body) - `(handler-case (progn (progn ,@body) nil) - (sb-ext:timeout () t))) - (with-test (:name (:with-timeout :timeout)) (assert (raises-timeout-p (sb-ext:with-timeout 0.2 @@ -110,14 +113,26 @@ (sb-ext:with-timeout 2 (sleep 2)))))) +(defun wait-for-threads (threads) + (loop while (some #'sb-thread:thread-alive-p threads) do (sleep 0.01))) + #+sb-thread (with-test (:name (:with-timeout :many-at-the-same-time)) - (loop repeat 10 do - (sb-thread:make-thread - (lambda () - (sb-ext:with-timeout 0.5 - (sleep 5) - (assert nil)))))) + (let ((ok t)) + (let ((threads (loop repeat 10 collect + (sb-thread:make-thread + (lambda () + (handler-case + (sb-ext:with-timeout 0.5 + (sleep 5) + (setf ok nil) + (format t "~%not ok~%")) + (timeout () + ))))))) + (assert (not (raises-timeout-p + (sb-ext:with-timeout 20 + (wait-for-threads threads))))) + (assert ok)))) #+sb-thread (with-test (:name (:with-timeout :dead-thread)) @@ -128,3 +143,102 @@ (assert t)))) (sleep 6) (assert t)) + + +(defun random-type (n) + `(integer ,(random n) ,(+ n (random n)))) + +;;; FIXME: Since timeouts do not work on Windows this would loop +;;; forever. +#-win32 +(with-test (:name (:hash-cache :interrupt)) + (let* ((type1 (random-type 500)) + (type2 (random-type 500)) + (wanted (subtypep type1 type2))) + (dotimes (i 100) + (block foo + (sb-ext:schedule-timer (sb-ext:make-timer + (lambda () + (assert (eq wanted (subtypep type1 type2))) + (return-from foo))) + 0.05) + (loop + (assert (eq wanted (subtypep type1 type2)))))))) + +;;; Disabled. Hangs occasionally at least on x86. See comment before +;;; the next test case. +#+(and nil sb-thread) +(with-test (:name (:timer :parallel-unschedule)) + (let ((timer (sb-ext:make-timer (lambda () 42) :name "parallel schedulers")) + (other nil)) + (flet ((flop () + (sleep (random 0.01)) + (loop repeat 10000 + do (sb-ext:unschedule-timer timer)))) + (loop repeat 5 + do (mapcar #'sb-thread:join-thread + (loop for i from 1 upto 10 + collect (let* ((thread (sb-thread:make-thread #'flop + :name (format nil "scheduler ~A" i))) + (ticker (sb-ext:make-timer (lambda () 13) :thread (or other thread) + :name (format nil "ticker ~A" i)))) + (setf other thread) + (sb-ext:schedule-timer ticker 0 :repeat-interval 0.00001) + thread))))))) + +;;;; FIXME: OS X 10.4 doesn't like these being at all, and gives us a SIGSEGV +;;;; instead of using the Mach expection system! 10.5 on the other tends to +;;;; lose() where with interrupt already pending. :/ +;;;; +;;;; FIXME: This test also occasionally hangs on Linux/x86-64 at least. The +;;;; common feature is one thread in gc_stop_the_world, and another trying to +;;;; signal_interrupt_thread, but both (apparently) getting EAGAIN repeatedly. +;;;; Exactly how or why this is happening remains under investigation -- but +;;;; it seems plausible that the fast timers simply fill up the interrupt +;;;; queue completely. (On some occasions the process unwedges itself after +;;;; a few minutes, but not always.) +;;;; +;;;; FIXME: Another failure mode on Linux: recursive entries to +;;;; RUN-EXPIRED-TIMERS blowing the stack. +#+nil +(with-test (:name (:timer :schedule-stress)) + (flet ((test () + (let* ((slow-timers (loop for i from 1 upto 100 + collect (sb-ext:make-timer (lambda () 13) :name (format nil "slow ~A" i)))) + (fast-timer (sb-ext:make-timer (lambda () 42) :name "fast"))) + (sb-ext:schedule-timer fast-timer 0.0001 :repeat-interval 0.0001) + (dolist (timer slow-timers) + (sb-ext:schedule-timer timer (random 0.1) :repeat-interval (random 0.1))) + (dolist (timer slow-timers) + (sb-ext:unschedule-timer timer)) + (sb-ext:unschedule-timer fast-timer)))) + #+sb-thread + (mapcar #'sb-thread:join-thread (loop repeat 10 collect (sb-thread:make-thread #'test))) + #-sb-thread + (loop repeat 10 do (test)))) + +#+sb-thread +(with-test (:name (:timer :threaded-stress)) + (let ((barrier (sb-thread:make-semaphore)) + (goal 100)) + (flet ((wait-for-goal () + (let ((*n* 0)) + (declare (special *n*)) + (sb-thread:signal-semaphore barrier) + (loop until (eql *n* goal)))) + (one () + (declare (special *n*)) + (incf *n*))) + (let ((threads (list (sb-thread:make-thread #'wait-for-goal) + (sb-thread:make-thread #'wait-for-goal) + (sb-thread:make-thread #'wait-for-goal)))) + (sb-thread:wait-on-semaphore barrier) + (sb-thread:wait-on-semaphore barrier) + (sb-thread:wait-on-semaphore barrier) + (flet ((sched (thread) + (sb-thread:make-thread (lambda () + (loop repeat goal + do (sb-ext:schedule-timer (make-timer #'one :thread thread) 0.001)))))) + (dolist (thread threads) + (sched thread))) + (mapcar #'sb-thread:join-thread threads)))))