#lang racket/base (require rackunit racket/async-channel racket/serialize port-channel uni-channel) (serializable-struct msg (id payload) #:transparent) (test-case "single async channel" (define ch (make-uni-async-channel #:name 'single)) (check-true (uni-channel? ch)) (check-equal? (uni-channel-kind ch) 'async) (uni-channel-put ch 'hello) (check-equal? (uni-channel-get ch) 'hello) (check-false (uni-channel-try-get ch)) (sync (uni-channel-put-evt ch 'via-evt)) (check-equal? (sync ch) 'via-evt) (uni-channel-close ch) (check-true (uni-channel-closed? ch)) (check-exn exn:fail? (lambda () (uni-channel-put ch 'after-close)))) (test-case "async channel pair" (define-values (a b) (make-uni-async-channel-pair)) (uni-channel-send a '(from a)) (uni-channel-send b '(from b)) (check-equal? (uni-channel-recv b) '(from a)) (check-equal? (uni-channel-recv a) '(from b))) (test-case "place channel pair" (define-values (a b) (make-uni-place-channel-pair)) (uni-channel-send a '(place a)) (check-equal? (uni-channel-recv b) '(place a)) (uni-channel-send b '(place b)) (check-equal? (sync a) '(place b))) (test-case "port channel over pipe" (define-values (in out) (make-pipe)) (define ch (make-uni-port-channel #:input in #:output out #:source 'pipe-test #:name 'pipe)) (check-equal? (uni-channel-kind ch) 'port) (check-equal? (uni-channel-direction ch) 'bidirectional) (uni-channel-put ch '(hello 1 2 3)) (uni-channel-put ch (msg 7 '(a b c))) (check-equal? (uni-channel-get ch) '(hello 1 2 3)) (check-equal? (uni-channel-get ch) (msg 7 '(a b c))) (uni-channel-close ch) (check-true (uni-channel-closed? ch)) (check-true (eof-object? (uni-channel-get ch)))) (test-case "wrap existing port-channel endpoints" (define-values (in out) (make-pipe)) (define reader (make-port-channel in #:source 'reader)) (define writer (make-port-channel out #:source 'writer)) (define in-ch (make-uni-channel reader #:name 'reader)) (define out-ch (make-uni-channel writer #:name 'writer)) (check-equal? (uni-channel-direction in-ch) 'input) (check-equal? (uni-channel-direction out-ch) 'output) (uni-channel-put out-ch 'wrapped) (check-equal? (uni-channel-get in-ch) 'wrapped) (uni-channel-close out-ch) (check-true (eof-object? (uni-channel-get in-ch)))) (module+ main (displayln "uni-channel tests ok"))