| 1 |
;; Tests for control.* modules |
| 2 |
|
| 3 |
(use gauche.test) |
| 4 |
(use gauche.vport) |
| 5 |
(use gauche.threads) |
| 6 |
(use scheme.list) |
| 7 |
(use srfi.19) |
| 8 |
(use data.queue) |
| 9 |
|
| 10 |
(test-start "control") |
| 11 |
|
| 12 |
;;-------------------------------------------------------------------- |
| 13 |
;; control.job |
| 14 |
;; |
| 15 |
(test-section "control.job") |
| 16 |
(use control.job) |
| 17 |
(test-module 'control.job) |
| 18 |
|
| 19 |
(let ((j #f) |
| 20 |
(z #f)) |
| 21 |
(test* "make-job" #t |
| 22 |
(begin (set! j (make-job (^[] #f))) |
| 23 |
(job? j))) |
| 24 |
|
| 25 |
(dolist [when `((:acknowledge ,job-acknowledge-time) |
| 26 |
(:start ,job-start-time) |
| 27 |
(:finish ,job-finish-time))] |
| 28 |
(test* (format "job-touch! ~a (pre)" (car when)) #f |
| 29 |
((cadr when) j)) |
| 30 |
(test* (format "job-touch! ~a" (car when)) #t |
| 31 |
(let1 time (job-touch! j (car when)) |
| 32 |
(time=? time ((cadr when) j))))) |
| 33 |
|
| 34 |
(set! j (make-job (^[] (set! z #t) 'ok))) |
| 35 |
(test* "job-run! precondition" '(#f #f) (list (job-status j) z)) |
| 36 |
(test* "job-run! postcondition" '(done ok #t) |
| 37 |
(begin (job-run! j) |
| 38 |
(list (job-status j) (job-result j) z))) |
| 39 |
|
| 40 |
(set! j (make-job (^[] (raise 'gosh) 'ok))) |
| 41 |
(test* "job-run! error condition" '(error gosh) |
| 42 |
(begin (job-run! j) |
| 43 |
(list (job-status j) (job-result j)))) |
| 44 |
|
| 45 |
(set! j (make-job (^[] #f) :cancellable #t)) |
| 46 |
(test* "job-run! killed" '(killed test) |
| 47 |
(begin (job-mark-killed! j 'test) |
| 48 |
(list (job-status j) (job-result j)))) |
| 49 |
) |
| 50 |
|
| 51 |
(cond-expand |
| 52 |
[gauche.sys.pthreads |
| 53 |
(test* "job-wait, job-kill" '(killed foo) |
| 54 |
(let* ([gate (make-mtqueue :max-length 0)] |
| 55 |
[job (make-job (^[] (enqueue/wait! gate #t) (sys-sleep 10)) |
| 56 |
:waitable #t)] |
| 57 |
[t1 (thread-start! (make-thread (^[] (job-run! job))))] |
| 58 |
[t2 (thread-start! (make-thread (^[] (job-wait job))))]) |
| 59 |
(dequeue/wait! gate) |
| 60 |
(job-mark-killed! job 'foo) |
| 61 |
(list (thread-join! t2) (job-result job)))) |
| 62 |
|
| 63 |
(test* "job-wait and error" '(error "bang") |
| 64 |
(let* ([job (make-job (^[] (error "bang")) :waitable #t)] |
| 65 |
[t1 (thread-start! (make-thread (^[] (job-wait job))))]) |
| 66 |
(job-run! job) |
| 67 |
(list (thread-join! t1) (and (<error> (job-result job)) |
| 68 |
(~ (job-result job)'message))))) |
| 69 |
] |
| 70 |
[else]) |
| 71 |
|
| 72 |
;;-------------------------------------------------------------------- |
| 73 |
;; control.thread-pool |
| 74 |
;; |
| 75 |
|
| 76 |
(test-section "control.thread-pool") |
| 77 |
(use control.thread-pool) |
| 78 |
(test-module 'control.thread-pool) |
| 79 |
|
| 80 |
(let ([pool (make-thread-pool 5)] |
| 81 |
[rvec (make-vector 10 #f)]) |
| 82 |
(test* "pool" '(5 #t 5 #f) |
| 83 |
(list (length (~ pool'pool)) |
| 84 |
(every thread? (~ pool'pool)) |
| 85 |
(~ pool'size) |
| 86 |
(~ pool'max-backlog))) |
| 87 |
|
| 88 |
(test* "doit" '#(0 1 2 3 4 5 6 7 8 9) |
| 89 |
(begin (dotimes [k 10] |
| 90 |
(add-job! pool (^[] |
| 91 |
(sys-nanosleep 1e7) |
| 92 |
(vector-set! rvec k k)))) |
| 93 |
(and (wait-all pool #f 1e7) rvec))) |
| 94 |
|
| 95 |
(test* "error results" '(ng ng ng ng ng) |
| 96 |
(begin (dotimes [k 5] |
| 97 |
(add-job! pool (^[] |
| 98 |
(sys-nanosleep 1e7) |
| 99 |
(raise 'ng) |
| 100 |
(vector-set! rvec k k)) |
| 101 |
#t)) |
| 102 |
(and (wait-all pool #f 1e7) |
| 103 |
(map (cut job-result <>) |
| 104 |
(queue->list (~ pool'result-queue)))))) |
| 105 |
) |
| 106 |
|
| 107 |
;; Testing max backlog and timeout |
| 108 |
(let ([pool (make-thread-pool 1 :max-backlog 1)] |
| 109 |
[gate #f]) |
| 110 |
(define (work) (do [] [gate] (sys-nanosleep #e1e8))) |
| 111 |
|
| 112 |
(add-job! pool work) |
| 113 |
|
| 114 |
(test* "add-job! backlog" #t |
| 115 |
(job? (add-job! pool work))) |
| 116 |
|
| 117 |
(test* "add-job! timeout" #f |
| 118 |
(add-job! pool work #f 0.1)) |
| 119 |
|
| 120 |
(set! gate #t) |
| 121 |
(test* "add-job! backlog" #t |
| 122 |
(job? (add-job! pool work #t))) |
| 123 |
|
| 124 |
;; synchronize |
| 125 |
(dequeue/wait! (thread-pool-results pool)) |
| 126 |
|
| 127 |
(set! gate #f) |
| 128 |
(test* "wait-all timeout" #f |
| 129 |
(begin (add-job! pool work) |
| 130 |
(wait-all pool 0.1 #e1e7))) |
| 131 |
|
| 132 |
;; fill the queue. |
| 133 |
(add-job! pool work #t) |
| 134 |
|
| 135 |
(test* "shutdown - raising <thread-pool-shutting-down>" |
| 136 |
(test-error <thread-pool-shut-down>) |
| 137 |
;; subtle arrangement: The timing of thread execution isn't |
| 138 |
;; guaranteed, but we hope to run the following events in |
| 139 |
;; sequence: |
| 140 |
;; - main: add-job! - this will block because queue is full |
| 141 |
;; - t1: enqueue/wait! triggers q1. |
| 142 |
;; - t1: terminate-all! - this causes the pending add-job! to |
| 143 |
;; raise an exception. this call blocks. |
| 144 |
;; - main: add-job! raises an exception, and triggers q2. |
| 145 |
;; - t2: triggered by q1, this sets gate to #t, causing the |
| 146 |
;; suspended jobs to resume. |
| 147 |
;; - t1: terminate-all! returns once all the suspended jobs |
| 148 |
;; are finished. making sure q2 is already triggered, |
| 149 |
;; we set gate to 'finished for the later check. |
| 150 |
(let* ([q1 (make-mtqueue :max-length 0)] |
| 151 |
[q2 (make-mtqueue :max-length 0)] |
| 152 |
[t1 (make-thread (^[] |
| 153 |
(enqueue/wait! q1 #t) |
| 154 |
(terminate-all! pool |
| 155 |
:cancel-queued-jobs #t) |
| 156 |
(dequeue/wait! q2) |
| 157 |
(set! gate 'finished)))] |
| 158 |
[t2 (make-thread (^[] |
| 159 |
(dequeue/wait! q1) |
| 160 |
(sys-nanosleep #e2e8) |
| 161 |
(set! gate #t)))]) |
| 162 |
(thread-start! t1) |
| 163 |
(thread-start! t2) |
| 164 |
(unwind-protect (add-job! pool work) |
| 165 |
(enqueue/wait! q2 #t)))) |
| 166 |
|
| 167 |
(test* "shutdown - killing a job in the queue" |
| 168 |
'killed |
| 169 |
(job-status (dequeue/wait! (thread-pool-results pool)))) |
| 170 |
(test* "shutdown check" 'finished |
| 171 |
(let retry ([n 0]) |
| 172 |
(cond [(= n 10) #f] |
| 173 |
[(symbol? gate) gate] |
| 174 |
[else (sys-nanosleep #e1e8) (retry (+ n 1))]))) |
| 175 |
) |
| 176 |
|
| 177 |
;; now, test forcible termination |
| 178 |
(let ([pool (make-thread-pool 1)] |
| 179 |
[gate #f]) |
| 180 |
(define (work) (do [] [gate] (sys-nanosleep #e1e8))) |
| 181 |
(test* "forced shutdown" 'killed |
| 182 |
(let1 xjob (add-job! pool work) |
| 183 |
(terminate-all! pool :force-timeout 0.05) |
| 184 |
(job-status xjob)))) |
| 185 |
|
| 186 |
;; This SEGVs on 0.9.3.3 (test code by @cryks) |
| 187 |
(test* "thread pool termination" 'terminated |
| 188 |
(let ([t (thread-start! (make-thread (cut undefined)))] |
| 189 |
[pool (make-thread-pool 10)]) |
| 190 |
(terminate-all! pool) |
| 191 |
(thread-terminate! t) |
| 192 |
(thread-state t))) |
| 193 |
|
| 194 |
;;-------------------------------------------------------------------- |
| 195 |
;; control.cseq |
| 196 |
;; |
| 197 |
|
| 198 |
(test-section "control.cseq") |
| 199 |
(use control.cseq) |
| 200 |
(test-module 'control.cseq) |
| 201 |
|
| 202 |
;; This doesn't always work because of the timing. |
| 203 |
'(let ((k 0)) |
| 204 |
(define (gen) (if (>= k 10) (eof-object) (begin0 k (inc! k)))) |
| 205 |
(define seq (generator->cseq gen)) |
| 206 |
(test* "cseq (gen)" '(0 10) |
| 207 |
(list (car seq) k))) |
| 208 |
|
| 209 |
(let ((k 0)) |
| 210 |
(define (gen) (if (>= k 10) (eof-object) (begin0 k (inc! k)))) |
| 211 |
(define seq (generator->cseq gen)) |
| 212 |
(test* "cseq (gen)" '(0 1 2 3 4 5 6 7 8 9) seq)) |
| 213 |
|
| 214 |
(let ((k 0)) |
| 215 |
(define (gen) (if (= k 5) (error "oops") (begin0 k (inc! k)))) |
| 216 |
(define seq (generator->cseq gen)) |
| 217 |
(test* "cseq (gen)" '(0 1 2 3) (take seq 4)) |
| 218 |
(test* "cseq (gen)" (test-error <error> "oops") (take seq 5))) |
| 219 |
|
| 220 |
(let () |
| 221 |
(define (coro yield) |
| 222 |
(let loop ((k 0)) (when (< k 10) (yield k) (loop (+ k 1))))) |
| 223 |
(define seq (coroutine->cseq coro)) |
| 224 |
(test* "cseq (coroutine)" '(0 1 2 3 4 5 6 7 8 9) seq)) |
| 225 |
|
| 226 |
;;-------------------------------------------------------------------- |
| 227 |
;; control.future |
| 228 |
;; |
| 229 |
|
| 230 |
(test-section "control.future") |
| 231 |
(use control.future) |
| 232 |
(test-module 'control.future) |
| 233 |
|
| 234 |
(test* "simple future" '(#t 3) |
| 235 |
(let1 f (future 3) |
| 236 |
(list (future? f) |
| 237 |
(future-get f)))) |
| 238 |
(test* "multiple values" '(1 2 3) |
| 239 |
(values->list (future-get (future (values 1 2 3))))) |
| 240 |
(let1 f (future (error "oops")) |
| 241 |
(test* "future error handling (delayed)" #t |
| 242 |
(future? f)) |
| 243 |
(test* "future error handling (propagated)" (test-error <error> "oops") |
| 244 |
(future-get f))) |
| 245 |
|
| 246 |
;;-------------------------------------------------------------------- |
| 247 |
;; control.pmap |
| 248 |
;; |
| 249 |
(test-section "control.pmap") |
| 250 |
(use control.pmap) |
| 251 |
(test-module 'control.pmap) |
| 252 |
|
| 253 |
(test* "pmap (default)" |
| 254 |
(map (cut * <> 2) (iota 100)) |
| 255 |
(pmap (cut * <> 2) (iota 100))) |
| 256 |
(test* "pmap (thread-pool)" |
| 257 |
(map (cut * <> 2) (iota 100)) |
| 258 |
(pmap (cut * <> 2) (iota 100) :mapper (make-pool-mapper))) |
| 259 |
(test* "pmap (thread-pool reuse)" |
| 260 |
(append (map (cut * <> 2) (iota 100)) |
| 261 |
(map (cut * <> 3) (iota 100))) |
| 262 |
(let* ([pool (make-thread-pool (sys-available-processors))] |
| 263 |
[mapper (make-pool-mapper pool)]) |
| 264 |
(unwind-protect |
| 265 |
(append (pmap (cut * <> 2) (iota 100) :mapper mapper) |
| 266 |
(pmap (cut * <> 3) (iota 100) :mapper mapper)) |
| 267 |
(terminate-all! pool)))) |
| 268 |
(test* "pmap (fully concurrent)" |
| 269 |
(map (cut * <> 2) (iota 25)) |
| 270 |
(pmap (cut * <> 2) (iota 25) :mapper (make-fully-concurrent-mapper))) |
| 271 |
(let ([flag #t]) |
| 272 |
(test* "pmap (fully concurrent, timeout)" |
| 273 |
(and flag '(timeout timeout timeout timeout timeout)) |
| 274 |
(pmap (^n (begin (sys-sleep 60) (set! flag #f) n)) |
| 275 |
(iota 5) |
| 276 |
:mapper (make-fully-concurrent-mapper 0.2 'timeout)))) |
| 277 |
|
| 278 |
(test* "pfind (single)" |
| 279 |
(find (cut = <> 7) (iota 10)) |
| 280 |
(pfind (cut = <> 7) (iota 10) :mapper (sequential-mapper))) |
| 281 |
|
| 282 |
(test* "pany (signle)" |
| 283 |
(any (^x (and (= x 7) 42)) (iota 10)) |
| 284 |
(pany (^x (and (= x 7) 42)) (iota 10) :mapper (sequential-mapper))) |
| 285 |
|
| 286 |
(test* "pfind (static)" |
| 287 |
(find (cut = <> 1) (iota 20)) |
| 288 |
(pfind (^x (and (sys-sleep (quotient x 2)) |
| 289 |
(= x 1))) |
| 290 |
(iota 20) |
| 291 |
:mapper (make-static-mapper))) |
| 292 |
(test* "pany (static)" |
| 293 |
(any (^x (and (= x 1) 42)) (iota 10)) |
| 294 |
(pany (^x (and (sys-sleep (quotient x 2)) |
| 295 |
(= x 1) |
| 296 |
42)) |
| 297 |
(iota 20) |
| 298 |
:mapper (make-static-mapper))) |
| 299 |
(test* "pfind (pool)" |
| 300 |
(find (cut = <> 1) (iota 20)) |
| 301 |
(pfind (^x (and (sys-sleep (quotient x 2)) |
| 302 |
(= x 1))) |
| 303 |
(iota 20) |
| 304 |
:mapper (make-pool-mapper))) |
| 305 |
(test* "pany (pool)" |
| 306 |
(any (^x (and (= x 1) 42)) (iota 10)) |
| 307 |
(pany (^x (and (sys-sleep (quotient x 2)) |
| 308 |
(= x 1) |
| 309 |
42)) |
| 310 |
(iota 20) |
| 311 |
:mapper (make-pool-mapper))) |
| 312 |
(test* "pfind (fully-concurrent)" |
| 313 |
(find (cut = <> 1) (iota 20)) |
| 314 |
(pfind (^x (and (sys-sleep (quotient x 2)) |
| 315 |
(= x 1))) |
| 316 |
(iota 20) |
| 317 |
:mapper (make-fully-concurrent-mapper))) |
| 318 |
(test* "pany (fully-concurrent)" |
| 319 |
(any (^x (and (= x 1) 42)) (iota 10)) |
| 320 |
(pany (^x (and (sys-sleep (quotient x 2)) |
| 321 |
(= x 1) |
| 322 |
42)) |
| 323 |
(iota 20) |
| 324 |
:mapper (make-fully-concurrent-mapper))) |
| 325 |
|
| 326 |
;;-------------------------------------------------------------------- |
| 327 |
;; control.scheduler |
| 328 |
;; |
| 329 |
(test-section "control.scheduler") |
| 330 |
(use control.scheduler) |
| 331 |
(test-module 'control.scheduler) |
| 332 |
|
| 333 |
(let () |
| 334 |
(define a '()) |
| 335 |
(define b #f) |
| 336 |
(define e #f) |
| 337 |
(define sched (make <scheduler> :error-handler (^e (set! e e)))) |
| 338 |
|
| 339 |
(let ((id1 #f) (id2 #f)) |
| 340 |
(test* "scheduler-schedule!" #t |
| 341 |
(begin |
| 342 |
(set! id1 (scheduler-schedule! sched |
| 343 |
(constantly #f) |
| 344 |
(make-time time-duration 0 10000))) |
| 345 |
(integer? id1))) |
| 346 |
(test* "scheduler-exists?" '(#t #f) |
| 347 |
(list (scheduler-exists? sched id1) |
| 348 |
(scheduler-exists? sched (+ id1 1)))) |
| 349 |
(test* "scheduler-schedule!" '(#t #t) |
| 350 |
(begin |
| 351 |
(set! id2 (scheduler-schedule! sched |
| 352 |
(constantly #f) |
| 353 |
(make-time time-duration 0 10000))) |
| 354 |
(list (scheduler-exists? sched id1) |
| 355 |
(scheduler-exists? sched id2)))) |
| 356 |
(test* "scheduler-remove!" '(#f #t) |
| 357 |
(begin (scheduler-remove! sched id1) |
| 358 |
(list (scheduler-exists? sched id1) |
| 359 |
(scheduler-exists? sched id2)))) |
| 360 |
(test* "scheduler-remove!" '(#f #f) |
| 361 |
(begin (scheduler-remove! sched id2) |
| 362 |
(list (scheduler-exists? sched id2) |
| 363 |
(scheduler-exists? sched id2)))) |
| 364 |
) |
| 365 |
|
| 366 |
(let () |
| 367 |
(test* "running?" #t |
| 368 |
(scheduler-running? sched)) |
| 369 |
(test* "scheduler-terminate!" '(a) |
| 370 |
(begin |
| 371 |
(scheduler-schedule! sched (^[] (push! a 'a)) 0 1) |
| 372 |
(scheduler-terminate! sched) |
| 373 |
a)) |
| 374 |
(test* "don't accept new task" (test-error <error> #/queue is closed/) |
| 375 |
(scheduler-schedule! sched (^[] (set! b 'b)) 0)) |
| 376 |
(test* "running?" #f |
| 377 |
(scheduler-running? sched)) |
| 378 |
(test* "see if tasks are really stopped" '((a) #f) |
| 379 |
(list a b))) |
| 380 |
) |
| 381 |
|
| 382 |
(let () |
| 383 |
;; test cancellation |
| 384 |
(define sched (make <scheduler>)) |
| 385 |
(scheduler-schedule! sched (^[] (error "oops")) 0) |
| 386 |
(sys-nanosleep 1000) |
| 387 |
(test* "scheduler-terminate! on error" (test-error <error> #/oops/) |
| 388 |
(scheduler-terminate! sched)) |
| 389 |
) |
| 390 |
|
| 391 |
;; SRFI-120 is a wrapper of control.scheduler |
| 392 |
(use srfi.120) |
| 393 |
(test-module 'srfi.120) |
| 394 |
|
| 395 |
(define-module srfi-120-test |
| 396 |
(use gauche.test) |
| 397 |
(use srfi.18) |
| 398 |
(use srfi.19) |
| 399 |
(use srfi.64) |
| 400 |
(use srfi.120) |
| 401 |
;; we don't want to use cond-expand gauche clause in the srfi-120 test; |
| 402 |
;; rather we want to let it use SRFI-64. |
| 403 |
(define-syntax cond-expand (syntax-rules () ([_ . x] '()))) |
| 404 |
(test-include-r7 "include/srfi-120-tests" (exclude (timer)))) |
| 405 |
|
| 406 |
;;-------------------------------------------------------------------- |
| 407 |
;; control.port |
| 408 |
;; |
| 409 |
(test-section "control.plumbing") |
| 410 |
(use control.plumbing) |
| 411 |
(test-module 'control.plumbing) |
| 412 |
|
| 413 |
;; pipe, 1-in 1-out |
| 414 |
(let () |
| 415 |
(define r '()) |
| 416 |
|
| 417 |
;; >[inlet]===[outlet]> |
| 418 |
(define plumbing (make-plumbing)) |
| 419 |
(define inlet (open-inlet-output-port plumbing)) |
| 420 |
(define outlet (open-outlet-input-port plumbing)) |
| 421 |
|
| 422 |
(define t |
| 423 |
(thread-start! (make-thread (^[] (let loop () |
| 424 |
(let1 c (read-char outlet) |
| 425 |
(if (eof-object? c) |
| 426 |
'done |
| 427 |
(begin (push! r c) |
| 428 |
(loop))))))))) |
| 429 |
|
| 430 |
(define (gather-result expected-length) |
| 431 |
(if (= (length r) expected-length) |
| 432 |
(list->string (reverse r)) |
| 433 |
(begin (sys-nanosleep #e1e6) (gather-result expected-length)))) |
| 434 |
|
| 435 |
(test* "simple pipe, accessors" `((,inlet) (,outlet)) |
| 436 |
(list (plumbing-inlet-ports plumbing) |
| 437 |
(plumbing-outlet-ports plumbing))) |
| 438 |
(test* "simple pipe, port to plumbing" `(,plumbing ,plumbing) |
| 439 |
(list (port-plumbing inlet) |
| 440 |
(port-plumbing outlet))) |
| 441 |
|
| 442 |
(display "abc" inlet) |
| 443 |
(test* "simple pipe, not flushed" '() r) |
| 444 |
(flush inlet) |
| 445 |
(test* "simple pipe, flushed" "abc" (gather-result 3)) |
| 446 |
|
| 447 |
(display "de" inlet) |
| 448 |
(flush inlet) |
| 449 |
(test* "simple pipe, flushed" "abcde" (gather-result 5)) |
| 450 |
|
| 451 |
(close-output-port inlet) |
| 452 |
(test* "closing inlet causes EOF on outlet" 'done |
| 453 |
(thread-join! t)) |
| 454 |
) |
| 455 |
|
| 456 |
;; closing outlet automatically |
| 457 |
(let () |
| 458 |
;; >[inlet0]==\ /=> {outlet0} |
| 459 |
;; +=+ |
| 460 |
;; >[inlet1]==/ \=> {outlet1} |
| 461 |
|
| 462 |
(define plumbing (make-plumbing)) |
| 463 |
(define inlet0 (open-inlet-output-port plumbing 'inlet0)) |
| 464 |
(define inlet1 (open-inlet-output-port plumbing 'inlet1)) |
| 465 |
(define outlet0 (open-output-string)) |
| 466 |
(define outlet1 (open-output-string)) |
| 467 |
(add-outlet-output-port! plumbing outlet0 'outlet0) |
| 468 |
(add-outlet-output-port! plumbing outlet1 'outlet1 :close-on-eof #t) |
| 469 |
|
| 470 |
(test* "multi inlets, accessors" (list inlet0 inlet1) |
| 471 |
(plumbing-inlet-ports plumbing) |
| 472 |
(cut lset= eq? <> <>)) |
| 473 |
(test* "multi outlets, accessors" (list outlet0 outlet1) |
| 474 |
(plumbing-outlet-ports plumbing) |
| 475 |
(cut lset= eq? <> <>)) |
| 476 |
(test* "multi inlets/outlets, plumbing access" `(,plumbing ,plumbing #f #f) |
| 477 |
(list (port-plumbing inlet0) |
| 478 |
(port-plumbing inlet1) |
| 479 |
(port-plumbing outlet0) |
| 480 |
(port-plumbing outlet1))) |
| 481 |
(test* "access by name" (list inlet0 inlet1 outlet0 outlet1) |
| 482 |
(list (plumbing-get-port plumbing 'inlet0) |
| 483 |
(plumbing-get-port plumbing 'inlet1) |
| 484 |
(plumbing-get-port plumbing 'outlet0) |
| 485 |
(plumbing-get-port plumbing 'outlet1))) |
| 486 |
|
| 487 |
(test* "closing outlet ports (not yet)" '(#f #f) |
| 488 |
(begin |
| 489 |
(display "a" inlet0) |
| 490 |
(close-output-port inlet0) |
| 491 |
(list (port-closed? outlet0) |
| 492 |
(port-closed? outlet1)))) |
| 493 |
(test* "closing outlet ports" '(#f #t) |
| 494 |
(begin |
| 495 |
(display "b" inlet1) |
| 496 |
(close-output-port inlet1) |
| 497 |
(list (port-closed? outlet0) |
| 498 |
(port-closed? outlet1)))) |
| 499 |
|
| 500 |
(test* "outlet content" '("ab" "ab") |
| 501 |
(list (get-output-string outlet0) |
| 502 |
(get-output-string outlet1))) |
| 503 |
|
| 504 |
) |
| 505 |
|
| 506 |
;; asynchronous output outlet |
| 507 |
(let () |
| 508 |
;; >[inlet] ==> {outlet} |
| 509 |
(define outlet (open-output-string)) |
| 510 |
(define plumbing (make-plumbing)) |
| 511 |
(define inlet (open-inlet-output-port plumbing)) |
| 512 |
|
| 513 |
(test* "async outlet output" "a" |
| 514 |
(begin |
| 515 |
(add-outlet-output-port! plumbing outlet #f |
| 516 |
:asynchronous #t |
| 517 |
:close-on-eof #t) |
| 518 |
(display "a" inlet) |
| 519 |
(flush inlet) |
| 520 |
(sys-nanosleep #e5e6) |
| 521 |
(get-output-string outlet))) |
| 522 |
(test* "async outlet output" "abc" |
| 523 |
(begin |
| 524 |
(display "bc" inlet) |
| 525 |
(close-output-port inlet) |
| 526 |
(sys-nanosleep #e5e6) |
| 527 |
(get-output-string outlet))) |
| 528 |
(test* "async outlet output" #t |
| 529 |
(port-closed? outlet)) |
| 530 |
) |
| 531 |
|
| 532 |
;; broadcast output port |
| 533 |
(let () |
| 534 |
;; /=> {outlet0] |
| 535 |
;; >[inlet] ==+==> {outlet1} |
| 536 |
;; \=> {outlet2} |
| 537 |
(define outlet0 (open-output-string)) |
| 538 |
(define outlet1 (open-output-string)) |
| 539 |
(define outlet2 (open-output-string)) |
| 540 |
(define inlet |
| 541 |
(open-broadcast-output-port outlet0 outlet1 outlet2)) |
| 542 |
|
| 543 |
(test* "broadcast output port" '("a" "a" "a") |
| 544 |
(begin |
| 545 |
(display "a" inlet) |
| 546 |
(close-output-port inlet) |
| 547 |
(sys-nanosleep #e5e6) |
| 548 |
(list (get-output-string outlet0) |
| 549 |
(get-output-string outlet1) |
| 550 |
(get-output-string outlet2)))) |
| 551 |
) |
| 552 |
|
| 553 |
;; pipe |
| 554 |
(let () |
| 555 |
;; >[in0]==\ /==[out0]> |
| 556 |
;; +==+ |
| 557 |
;; >[in1]==/ \==[out1]> |
| 558 |
(define-values (ins outs) (make-pipe :num-inlets 2 :num-outlets 2)) |
| 559 |
(define-values (in0 in1) (apply values ins)) |
| 560 |
(define-values (out0 out1) (apply values outs)) |
| 561 |
(test* "pipe" '("abcdef" "ghij") |
| 562 |
(begin |
| 563 |
(display "abcde" in0) |
| 564 |
(flush in0) |
| 565 |
(display "f\nghij\n" in1) |
| 566 |
(flush in1) |
| 567 |
(let* ([a (read-line out0)] |
| 568 |
[b (read-line out0)]) |
| 569 |
(list a b)))) |
| 570 |
(test* "pipe, flush on close" "klmno" |
| 571 |
(begin |
| 572 |
(display "klmno\n" in0) |
| 573 |
(close-output-port in0) |
| 574 |
(read-line out0))) |
| 575 |
(test* "pipe, closing propagation (not yet)" #f |
| 576 |
(port-closed? out0)) |
| 577 |
(test* "pipe, closing propagation" (eof-object) |
| 578 |
(begin |
| 579 |
(close-output-port in1) |
| 580 |
(read-line out0))) |
| 581 |
(test* "pipe, closing" "abcdef\nghij\nklmno\n" |
| 582 |
(port->string out1)) |
| 583 |
) |
| 584 |
|
| 585 |
;; pump |
| 586 |
(let () |
| 587 |
;; {in} >====> {out} |
| 588 |
(define mtq (make-mtqueue)) |
| 589 |
(define in (let ([eof? #f]) |
| 590 |
(make <buffered-input-port> |
| 591 |
:fill (^[buf] |
| 592 |
(if eof? |
| 593 |
(eof-object) |
| 594 |
(let1 ch (dequeue/wait! mtq) |
| 595 |
(if (eof-object? ch) |
| 596 |
(begin (set! eof? #t) ch) |
| 597 |
(begin (set! (~ buf 0) (char->integer ch)) 1))))) |
| 598 |
:ready (^[] (or eof? (not (queue-empty? mtq))))))) |
| 599 |
(define out (open-output-string)) |
| 600 |
(define pump (make-pump `(,in) `(,out))) |
| 601 |
|
| 602 |
(set! (port-buffering in) :none) |
| 603 |
(test* "pump" "" |
| 604 |
(get-output-string out)) |
| 605 |
(test* "pump" "a" |
| 606 |
(begin (enqueue/wait! mtq #\a) |
| 607 |
(sys-nanosleep #e5e7) |
| 608 |
(get-output-string out))) |
| 609 |
(test* "pump" "ab" |
| 610 |
(begin (enqueue/wait! mtq #\b) |
| 611 |
(sys-nanosleep #e5e6) |
| 612 |
(get-output-string out))) |
| 613 |
(test* "pump" "ab" |
| 614 |
(begin (enqueue/wait! mtq (eof-object)) |
| 615 |
(sys-nanosleep #e5e6) |
| 616 |
(get-output-string out))) |
| 617 |
) |
| 618 |
|
| 619 |
|
| 620 |
;; tapping |
| 621 |
(let () |
| 622 |
;; pipe tapping |
| 623 |
;; {feed} >===[intermediate]>>==+==> {outlet} |
| 624 |
;; \ |
| 625 |
;; =[tap]> |
| 626 |
(define-values (feeds intermediates) (make-pipe)) |
| 627 |
(define outlet (open-output-string)) |
| 628 |
(define tap (open-tapping-input-port (car intermediates) |
| 629 |
outlet :close-on-eof #t)) |
| 630 |
(test* "pipe & tap (tap)" "abcde" |
| 631 |
(begin |
| 632 |
(display "abcde\n" (car feeds)) |
| 633 |
(flush (car feeds)) |
| 634 |
(read-line tap))) |
| 635 |
(test* "pipe & tap (outlet)" "abcde\nfghij\n" |
| 636 |
(begin |
| 637 |
(display "fghij\n" (car feeds)) |
| 638 |
(close-output-port (car feeds)) |
| 639 |
(while (not (port-closed? outlet)) |
| 640 |
(sys-nanosleep #e5e7)) |
| 641 |
(get-output-string outlet))) |
| 642 |
(test* "pipe & tap (outlet, close)" #t |
| 643 |
(port-closed? outlet)) |
| 644 |
(test* "pipe & tap (tap)" "fghij" |
| 645 |
(read-line tap)) |
| 646 |
(test* "pipe & tap (eof)" (eof-object) |
| 647 |
(read-line tap)) |
| 648 |
) |
| 649 |
|
| 650 |
;; plumbing |
| 651 |
(let () |
| 652 |
(define-values (feeds inlets) (make-pipe)) |
| 653 |
(define inlet0 (car inlets)) |
| 654 |
(define outlet0 (open-output-string)) |
| 655 |
(define pl (plumbing `(< ,inlet0 inlet0) |
| 656 |
`(< inlet1) |
| 657 |
`(> ,outlet0 (:coe :async)) |
| 658 |
`(> outlet1))) |
| 659 |
(define inlet1 (plumbing-get-port pl 'inlet1)) |
| 660 |
(define outlet1 (plumbing-get-port pl 'outlet1)) |
| 661 |
|
| 662 |
(test* "plumbing - created ports" '(#t #t) |
| 663 |
(list (output-port? inlet1) |
| 664 |
(input-port? outlet1))) |
| 665 |
|
| 666 |
(test* "plumbing - feed inlet0" "abc" |
| 667 |
(begin |
| 668 |
(display "abc" (car feeds)) |
| 669 |
(flush (car feeds)) |
| 670 |
(sys-nanosleep #e5e7) |
| 671 |
(get-output-string outlet0))) |
| 672 |
|
| 673 |
(test* "plumbing - feed inlet1" (test-one-of "abcdefxyz" "abcxyzdef") |
| 674 |
(begin |
| 675 |
(display "def" (car feeds)) |
| 676 |
(close-port (car feeds)) |
| 677 |
(display "xyz" inlet1) |
| 678 |
(flush inlet1) |
| 679 |
(sys-nanosleep #e5e7) |
| 680 |
(get-output-string outlet0))) |
| 681 |
|
| 682 |
(test* "plumbing - reading outlet1" (test-one-of "abcdefxyz" "abcxyzdef") |
| 683 |
(begin |
| 684 |
(close-port inlet1) |
| 685 |
(sys-nanosleep #e5e7) |
| 686 |
(port->string outlet1))) |
| 687 |
|
| 688 |
(test* "plumbing - coe" #t |
| 689 |
(port-closed? outlet0)) |
| 690 |
) |
| 691 |
|
| 692 |
(test-end) |