From b48b362e3f31b1d35e6fa39bd5f7ef7b8e8e9488 Mon Sep 17 00:00:00 2001 From: Jeff Mickey Date: Thu, 8 Sep 2016 14:16:08 -0700 Subject: [PATCH] tests: Add sleep order testing --- tests/basic.scm | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/basic.scm b/tests/basic.scm index 31d392ec..57bff2ba 100644 --- a/tests/basic.scm +++ b/tests/basic.scm @@ -18,7 +18,8 @@ ;;;; (define-module (tests basic) - #:use-module (fibers)) + #:use-module (fibers) + #:use-module (srfi srfi-1)) (define failed? #f) @@ -96,6 +97,30 @@ ;; sleep wakeup order +(define sleep-list-mtx (make-mutex)) + +(define sleep-list '()) + +(define (add-order n) + (lock-mutex sleep-list-mtx) + (set! sleep-list (cons n sleep-list)) + (unlock-mutex sleep-list-mtx)) + +(define *randstate* (random-state-from-platform)) + +;; TODO(codemac): Curerntly this does not pass, as the "spawn time" +;; for each sleep isn't actually taken into account here. Ideally we +;; can make these sleeps small enough that the test goes quickly +;; without actually drifting across when each spawn-fiber is called. +(assert-run-fibers-terminates + (do-times + 1000 + (let* ((nr (random 100 *randstate*)) + (n (exact->inexact (/ nr 1000)))) + (spawn-fiber (lambda () (sleep n) (add-order n)))))) + +(assert-equal (sort sleep-list <) (reverse sleep-list)) + ;; fib using channels ;; sleep durations