123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- (define-test-suite rendezvous-channels-tests)
- (define-test-case channel-1 rendezvous-channels-tests
- (let ((channel (make-channel)))
- (spawn
- (lambda ()
- (sleep 500)
- (send channel 'message)))
- (check (receive channel) => 'message)))
- (define-test-case channel-2 rendezvous-channels-tests
- (let ((channel (make-channel)))
- (spawn
- (lambda ()
- (send channel 'message)))
- (sleep 500)
- (check (receive channel) => 'message)))
- (define-test-case channel-3 rendezvous-channels-tests
- (do ((i 0 (+ 1 i)))
- ((= i 100)) ; detect races more reliably
- (let ((channel (make-channel)))
- (spawn
- (lambda ()
- (let loop ((i 0))
- (if (not (= i 1000))
- (begin
- (send channel i)
- (loop (+ 1 i)))))))
- (spawn
- (lambda ()
- ;; (sleep 500)
- (let loop ((i 1000))
- (if (not (= i 2000))
- (begin
- (send channel i)
- (loop (+ 1 i)))))))
-
- (let loop ((count 0)
- (values '()))
- (if (= count 2000)
- (check values
- (=> lset=)
- (iota 2000))
- (loop (+ 1 count)
- (cons (receive channel) values)))))))
- (define-test-case select-1 rendezvous-channels-tests
- (let ((channel-1 (make-channel))
- (channel-2 (make-channel)))
- (spawn
- (lambda ()
- (let loop ((i 0))
- (if (not (= i 1000))
- (begin
- (send channel-1 i)
- ;;(sleep 1000)
- (loop (+ 1 i)))))))
- (spawn
- (lambda ()
- ;; (sleep 500)
- (let loop ((i 1000))
- (if (not (= i 2000))
- (begin
- (send channel-2 i)
- (loop (+ 1 i)))))))
- (sleep 500)
- (let loop ((count 0)
- (values '()))
- (if (= count 2000)
- (check values
- (=> lset=)
- (iota 2000))
- (loop (+ 1 count)
- (cons (select (receive-rv channel-1)
- (receive-rv channel-2))
- values))))))
- (define-test-case wrap-1 rendezvous-channels-tests
- (let ((channel-1 (make-channel))
- (channel-2 (make-channel)))
- (spawn
- (lambda ()
- (let loop ((i 0))
- (if (not (= i 1000))
- (begin
- (send channel-1 i)
- ;;(sleep 1000)
- (loop (+ 1 i)))))))
- (spawn
- (lambda ()
- ;; (sleep 500)
- (let loop ((i 1000))
- (if (not (= i 2000))
- (begin
- (send channel-2 i)
- (loop (+ 1 i)))))))
- (sleep 500)
- (let loop ((count 0)
- (values '()))
- (if (= count 2000)
- (check values
- (=> lset=)
- (iota 2000))
- (let* ((val
- (select (wrap (receive-rv channel-1)
- (lambda (n)
- (cons 1 n)))
- (wrap (receive-rv channel-2)
- (lambda (n)
- (cons 2 n)))))
- (chan (car val))
- (n (cdr val)))
- (if (< n 1000)
- (check chan => 1)
- (check chan => 2))
- (loop (+ 1 count)
- (cons n values)))))))
- (define (make-rv-notifier rv ack-message nack-message callback)
- (with-nack
- (lambda (nack)
- (spawn
- (lambda ()
- (sync nack)
- (callback nack-message)))
- (wrap rv
- (lambda (ignore)
- (callback ack-message))))))
- (define-test-case with-nack-1 rendezvous-channels-tests
- (let ((channel-1 (make-channel))
- (channel-2 (make-channel))
- (message-1 #f)
- (message-2 #f))
- (spawn
- (lambda ()
- (sleep 500)
- (send channel-1 'ignore)))
- (let ((notifier-1
- (make-rv-notifier (receive-rv channel-1)
- "ch1" "not ch1"
- (lambda (message)
- (set! message-1 message))))
- (notifier-2
- (make-rv-notifier (receive-rv channel-2)
- "ch2" "not ch2"
- (lambda (message)
- (set! message-2 message)))))
- (select notifier-1 notifier-2)
- (sleep 500)
- (check message-1 => "ch1")
- (check message-2 => "not ch2")
- ;; kill off remaining thread
- (spawn
- (lambda ()
- (send channel-2 'ignore)))
- (select notifier-1 notifier-2))))
- (define-test-case with-nack-2 rendezvous-channels-tests
- (let ((channel-1 (make-channel))
- (channel-2 (make-channel))
- (message-1 #f)
- (message-2 #f))
- (spawn
- (lambda ()
- (send channel-1 'ignore)))
- (sleep 500)
- (let ((notifier-1
- (make-rv-notifier (receive-rv channel-1)
- "ch1" "not ch1"
- (lambda (message)
- (set! message-1 message))))
- (notifier-2
- (make-rv-notifier (receive-rv channel-2)
- "ch2" "not ch2"
- (lambda (message)
- (set! message-2 message)))))
- (select notifier-1 notifier-2)
- (sleep 500)
- (check message-1 => "ch1")
- (check message-2 => "not ch2")
- ;; kill off remaining thread
- (spawn
- (lambda ()
- (send channel-2 'ignore)))
- (select notifier-1 notifier-2))))
- (define-test-suite rendezvous-jars-tests)
- (define-test-case take rendezvous-jars-tests
- (let ((jar (make-jar)))
- (jar-put! jar 1)
- (check (jar-take jar) => 1)
- (jar-put! jar 2)
- (check (jar-take jar) => 2)
- (jar-put! jar 3)
- (check (jar-take jar) => 3)
- (jar-put! jar 4)
- (check (jar-take jar) => 4)))
- (define-test-case select rendezvous-jars-tests
- (let ((jar-1 (make-jar))
- (jar-2 (make-jar))
- (result-channel (make-channel)))
-
- (spawn
- (lambda ()
- (let ((contents (select (jar-take-rv jar-1)
- (jar-take-rv jar-2))))
- (send result-channel (cons 1 contents)))))
- (spawn
- (lambda ()
- (let ((contents (select (jar-take-rv jar-1)
- (jar-take-rv jar-2))))
- (send result-channel (cons 2 contents)))))
- (spawn
- (lambda ()
- (let ((contents (select (jar-take-rv jar-1)
- (jar-take-rv jar-2))))
- (send result-channel (cons 3 contents)))))
-
- (sleep 500)
- (jar-put! jar-1 17)
- (jar-put! jar-2 23)
- (let ((res-1 (receive result-channel))
- (res-2 (receive result-channel))
- (ensure
- (lambda (res)
- (check (member res '((1 . 17)
- (2 . 17)
- (3 . 17)
- (1 . 23)
- (2 . 23)
- (3 . 23)))))))
- ;; kill off remaining thread
- (jar-put! jar-1 #f)
- (receive result-channel)
- (ensure res-1)
- (ensure res-2))))
- (define-test-case multi rendezvous-jars-tests
- (let ((jar-1 (make-jar 1))
- (jar-2 (make-jar 2)))
- (spawn
- (lambda ()
- (let loop ((i 1))
- (if (< i 1000)
- (begin
- (check (jar-take jar-1) => (cons 1 (- i 1)))
- (jar-put! jar-2 (cons 2 i))
- (loop (+ i 1)))))))
- (jar-put! jar-2 (cons 2 0))
- (let loop ((i 0))
- (if (< i 1000)
- (begin
- (check (jar-take jar-2) => (cons 2 i))
- (jar-put! jar-1 (cons 1 i))
- (loop (+ i 1)))))))
- (define-test-suite rendezvous-placeholders-tests)
- (define-test-case placeholder-1 rendezvous-placeholders-tests
- (let ((placeholder-1 (make-placeholder))
- (placeholder-2 (make-placeholder))
- (results (make-channel)))
-
-
- (spawn
- (lambda ()
- (let ((contents (select (placeholder-value-rv placeholder-1)
- (placeholder-value-rv placeholder-1))))
- (send results contents))))
- (spawn
- (lambda ()
- (let ((contents (select (placeholder-value-rv placeholder-1)
- (placeholder-value-rv placeholder-2))))
- (send results contents))))
- (spawn
- (lambda ()
- (let ((contents (select (placeholder-value-rv placeholder-1)
- (placeholder-value-rv placeholder-2))))
- (send results contents))))
-
- (sleep 500)
- (placeholder-set! placeholder-1 17)
- (placeholder-set! placeholder-2 23)
- (let ((vals (list (receive results) (receive results) (receive results))))
- (check (lset<= vals '(17 23))))))
- (define-test-suite with-nack-tests)
- (define-test-case dummy with-nack-tests
- (let ((ch (make-channel)))
- (spawn
- (lambda ()
- (send ch 23)))
- (check
- (sync
- (with-nack (lambda (nack)
- (receive-rv ch))))
- => 23)))
- (define-test-case dummy-guard with-nack-tests
- (let ((ch (make-channel)))
- (spawn
- (lambda ()
- (send ch 23)))
- (check
- (sync
- (guard (lambda ()
- (with-nack (lambda (nack)
- (receive-rv ch))))))
- => 23)))
- (define-test-case no-nack-1 with-nack-tests
- (let ((ch (make-channel))
- (no #f))
- (spawn
- (lambda ()
- (sleep 500)
- (send ch 23)))
- (sync
- (wrap (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (set! no #t)))
- (receive-rv ch)))
- (lambda (value)
- (check value => 23))))
- (sleep 500)
- (check (not no))))
- (define-test-case no-nack-2 with-nack-tests
- (let ((ch (make-channel))
- (no #f))
- (spawn
- (lambda ()
- (sync
- (wrap (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (set! no 1)))
- (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (set! no 2)))
- (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (set! no 3)))
- (receive-rv ch)))))))
- (lambda (value)
- (check value => 10))))))
- (sleep 10)
- (send ch 10)
- (sleep 200)
- (check no => #f)))
- (define-test-case nack-1 with-nack-tests
- (let ((ch (make-channel))
- (results (make-channel)))
- (spawn
- (lambda ()
- (select
- (receive-rv ch)
- (wrap (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (send results 1)))
- (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (send results 2)))
- (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (send results 3)))
- (send-rv ch 'jo)))))))
- (lambda (value)
- (check #f))))))
- (sleep 10)
- (send ch 10)
- (let ((vals (list (receive results) (receive results) (receive results))))
- (check vals
- (=> lset=) '(1 2 3)))))
- (define-test-case no-nack-3 with-nack-tests
- (let ((ch (make-channel))
- (results (make-channel)))
- (spawn
- (lambda ()
- (select
- (send-rv ch 'tralala)
- (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (send results 1)))
- (guard (lambda ()
- (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (send results 2)))
- (send-rv ch 'jo-man)))))))
- (send-rv ch 'dudel-di-dudel))))
- (sleep 10)
- (check (memq (receive ch) '(tralala dudel-di-dudel)))
- (sleep 200)
- (check (list (receive results) (receive results))
- (=> lset=) '(1 2))))
- (define-test-case nack-2 with-nack-tests
- (let* ((ch-1 (make-channel))
- (ch-2 (make-channel))
- (rv-1 (receive-rv ch-1))
- (rv-2 (receive-rv ch-2))
- (results (make-channel)))
- (spawn
- (lambda ()
- (select
- (wrap (with-nack (lambda (nack)
- (choose
- (with-nack (lambda (nack)
- (spawn (lambda ()
- (sync nack)
- (send results 'nack-1)))
- rv-1))
- (with-nack (lambda (nack)
- (spawn (lambda ()
- (send results 'nack-2)))
- rv-2))
- rv-1)))
- (lambda (value)
- (send results 'rv-1)))
- (wrap rv-2
- (lambda (value)
- (send results 'rv-2))))))
- (sleep 10)
- (send ch-1 'jo)
- (sleep 200)
- (check
- (list (receive results) (receive results) (receive results))
- (=> lset=) '(nack-1 nack-2 rv-1))))
-
- (define (make-channels channels)
- (let loop ((res '()) (i channels))
- (if (= i 0)
- res
- (loop (cons (cons i (make-channel)) res) (- i 1)))))
- (define-test-case nack-3 with-nack-tests
- (let* ((channel-count 10)
- (channels (make-channels channel-count))
- (rvs (map (lambda (pair)
- (cons (car pair) (receive-rv (cdr pair))))
- channels))
- (results (make-channel)))
- (spawn
- (lambda ()
- (let ((select-rvs (map (lambda (rv)
- (wrap
- (with-nack
- (lambda (another-rv)
- (spawn (lambda ()
- (sync another-rv)))
- (cdr rv)))
- (lambda (val)
- (send results val))))
- rvs)))
- (let loop ()
- (apply select select-rvs)
- (loop)))))
- (sleep 50)
- (let loop ((channels channels))
- (if (not (null? channels))
- (let ((channel-no (car (car channels)))
- (channel (cdr (car channels))))
- (send channel channel-no)
- (sleep 100)
- (check (receive results) => channel-no)
- (loop (cdr channels)))))))
- (define-test-suite cml-tests
- (rendezvous-channels-tests
- rendezvous-jars-tests
- rendezvous-placeholders-tests
- with-nack-tests
- ))
-
-
|