]> gitweb.factorcode.org Git - factor.git/blob - basis/io/pipes/pipes-tests.factor
more test IN: cleanup.
[factor.git] / basis / io / pipes / pipes-tests.factor
1 USING: accessors calendar concurrency.count-downs continuations
2 destructors fry io io.encodings io.encodings.binary
3 io.encodings.utf8 io.pipes io.streams.duplex io.streams.string
4 io.timeouts kernel math namespaces threads tools.test ;
5
6 { "Hello" } [
7     utf8 <pipe> [
8         "Hello" print flush
9         readln
10     ] with-stream
11 ] unit-test
12
13 ! Test run-pipeline
14 { { } } [ { } run-pipeline ] unit-test
15 { { f } } [ { [ f ] } run-pipeline ] unit-test
16 { { "Hello" } } [
17     "Hello" [
18         { [ input-stream [ utf8 <decoder> ] change readln ] } run-pipeline
19     ] with-string-reader
20 ] unit-test
21
22 { { f "Hello" } } [
23     {
24         [ output-stream [ utf8 <encoder> ] change "Hello" print flush f ]
25         [ input-stream [ utf8 <decoder> ] change readln ]
26     } run-pipeline
27 ] unit-test
28
29 ! Test timeout
30 [
31     utf8 <pipe> [
32         1 seconds over set-timeout
33         stream-readln
34     ] with-disposal
35 ] must-fail
36
37 ! Test writing to a half-open pipe
38 { } [
39     1000 [
40         utf8 <pipe> [
41             [ in>> dispose ]
42             [ out>> "hi" over stream-write dispose ]
43             bi
44         ] curry ignore-errors
45     ] times
46 ] unit-test
47
48 ! Test non-blocking operation
49 { } [
50     [
51         2 <count-down> "count-down" set
52
53         utf8 <pipe> &dispose
54         utf8 <pipe> &dispose
55         [
56             [
57                 '[
58                     _ stream-read1 drop
59                     "count-down" get count-down
60                 ] in-thread
61             ] bi@
62
63             ! Give the threads enough time to start blocking on
64             ! read
65             1 seconds sleep
66         ]
67         ! At this point, two threads are blocking on read
68         [ [ "Hi" over stream-write stream-flush ] bi@ ]
69         ! At this point, both threads should wake up
70         2bi
71
72         "count-down" get await
73     ] with-destructors
74 ] unit-test
75
76 ! 0 read should not block
77 { f } [
78     [
79         binary <pipe> &dispose
80         in>>
81         [ 0 read ] with-input-stream
82     ] with-destructors
83 ] unit-test