(import test) (import (chicken blob)) (import (srfi 4)) (import (srfi 18)) (import timed-resource) (cond-expand (windows) (unix (define-constant ACTIVE-SECONDS 1.0) (define +opened+ 0) ;For Testing Only (define +closed+ 0) ;For Testing Only (define random-blob (let ((tr-random-dev (make-timed-resource (lambda () (set! +opened+ (add1 +opened+)) ;For Testing Only (open-input-file "/dev/random" #:binary)) (lambda (port) (set! +closed+ (add1 +closed+)) ;For Testing Only (close-output-port port)) ACTIVE-SECONDS))) (lambda (#!optional (bits 128)) (let ((bytes (inexact->exact (floor (/ (+ bits 7) 8))))) (with-timed-resource tr-random-dev (lambda (port) (u8vector->blob (read-u8vector bytes port))))) ) ) ) (test-group "Simple Timed Resource" (let ((x (random-blob))) (test-assert (blob? x)) (test 16 (blob-size x)) ) ;Wait a little longer than the timeout (thread-sleep! (inexact->exact (round (+ ACTIVE-SECONDS 0.5)))) ;Better be closed now! (test "Resource Closed" +closed+ +opened+) ) ) ) (test-exit)