33import os
44import os.notify
55
6- // make a pipe and return the (read, write) file descriptors
7- fn make_pipe () ! (int , int ) {
8- $if linux || macos {
9- pipefd := [2 ]int {}
10- if C.pipe (& pipefd[0 ]) != 0 {
11- return error ('error ${C .errno }: ' + os.posix_get_error_msg (C.errno))
12- }
13- return pipefd[0 ], pipefd[1 ]
14- }
15- return - 1 , - 1
16- }
17-
186fn test_level_trigger () {
197 // currently only linux and macos are supported
208 $if linux || macos {
219 mut notifier := notify.new ()!
22- reader , writer := make_pipe ()!
10+ mut pipe := os. pipe ()!
2311 defer {
24- os.fd_close (reader)
25- os.fd_close (writer)
12+ pipe.close ()
2613 notifier.close () or {}
2714 }
28- notifier.add (reader , .read)!
15+ notifier.add (pipe.read_fd , .read)!
2916
30- os. fd_write (writer, 'foobar' )
17+ pipe. write_string ( 'foobar' )!
3118 mut n := & notifier
32- check_read_event (mut n, reader , 'foo' )
33- check_read_event (mut n, reader , 'bar' )
19+ check_read_event (mut n, pipe.read_fd , 'foo' )
20+ check_read_event (mut n, pipe.read_fd , 'bar' )
3421
3522 assert notifier.wait (0 ).len == 0
3623 }
@@ -40,18 +27,17 @@ fn test_edge_trigger() {
4027 // currently only linux and macos are supported
4128 $if linux || macos {
4229 mut notifier := notify.new ()!
43- reader , writer := make_pipe ()!
30+ mut pipe := os. pipe ()!
4431 defer {
45- os.fd_close (reader)
46- os.fd_close (writer)
32+ pipe.close ()
4733 notifier.close () or {}
4834 }
49- notifier.add (reader , .read, .edge_trigger)!
35+ notifier.add (pipe.read_fd , .read, .edge_trigger)!
5036
5137 mut n := & notifier
5238
53- os. fd_write (writer, 'foobar' )
54- check_read_event (mut n, reader , 'foo' )
39+ pipe. write_string ( 'foobar' )!
40+ check_read_event (mut n, pipe.read_fd , 'foo' )
5541
5642 $if linux {
5743 assert notifier.wait (0 ).len == 0
@@ -75,7 +61,8 @@ fn test_edge_trigger() {
7561 // notifier.wait(0).len == 1 or 0
7662 }
7763
78- os.fd_write (writer, 'baz' )
64+ pipe.write_string ('baz' )!
65+ check_read_event (mut n, pipe.read_fd, 'barbaz' )
7966 // we do not get an event because there is still data
8067 // to be read
8168 // assert notifier.wait(0).len == 0
@@ -87,90 +74,87 @@ fn test_edge_trigger() {
8774fn test_one_shot () {
8875 $if linux || macos {
8976 mut notifier := notify.new ()!
90- reader , writer := make_pipe ()!
77+ mut pipe := os. pipe ()!
9178 defer {
92- os.fd_close (reader)
93- os.fd_close (writer)
79+ pipe.close ()
9480 notifier.close () or {}
9581 }
96- notifier.add (reader , .read, .one_shot)!
82+ notifier.add (pipe.read_fd , .read, .one_shot)!
9783
9884 mut n := & notifier
9985
100- os. fd_write (writer, 'foobar' )
101- check_read_event (mut n, reader , 'foo' )
102- os. fd_write (writer, 'baz' )
86+ pipe. write_string ( 'foobar' )!
87+ check_read_event (mut n, pipe.read_fd , 'foo' )
88+ pipe. write_string ( 'baz' )!
10389
10490 assert notifier.wait (0 ).len == 0
10591
10692 // rearm
107- notifier.modify (reader , .read)!
108- check_read_event (mut n, reader , 'barbaz' )
93+ notifier.modify (pipe.read_fd , .read)!
94+ check_read_event (mut n, pipe.read_fd , 'barbaz' )
10995 }
11096}
11197
11298// Kqueue does not support 'hangup' event type.
11399fn test_hangup () {
114100 $if linux {
115101 mut notifier := notify.new ()!
116- reader , writer := make_pipe ()!
102+ mut pipe := os. pipe ()!
117103 defer {
118- os.fd_close (reader )
104+ os.fd_close (pipe.read_fd )
119105 notifier.close () or {}
120106 }
121- notifier.add (reader , .hangup)!
107+ notifier.add (pipe.read_fd , .hangup)!
122108
123109 assert notifier.wait (0 ).len == 0
124110
125111 // closing on the writer end of the pipe will
126112 // cause a hangup on the reader end
127- os.fd_close (writer )
113+ os.fd_close (pipe.write_fd )
128114 events := notifier.wait (0 )
129115 assert events.len == 1
130- assert events[0 ].fd == reader
116+ assert events[0 ].fd == pipe.read_fd
131117 assert events[0 ].kind.has (.hangup)
132118 }
133119}
134120
135121fn test_write () {
136122 $if linux || macos {
137123 mut notifier := notify.new ()!
138- reader , writer := make_pipe ()!
124+ mut pipe := os. pipe ()!
139125 defer {
140- os.fd_close (reader)
141- os.fd_close (writer)
126+ pipe.close ()
142127 notifier.close () or {}
143128 }
144129
145- notifier.add (reader , .write)!
130+ notifier.add (pipe.read_fd , .write)!
146131 assert notifier.wait (0 ).len == 0
147132
148- notifier.add (writer , .write)!
133+ notifier.add (pipe.write_fd , .write)!
149134 events := notifier.wait (0 )
150135 assert events.len == 1
151- assert events[0 ].fd == writer
136+ assert events[0 ].fd == pipe.write_fd
152137 assert events[0 ].kind.has (.write)
153138 }
154139}
155140
156141fn test_remove () {
157142 $if linux || macos {
158143 mut notifier := notify.new ()!
159- reader , writer := make_pipe ()!
144+ mut pipe := os. pipe ()!
160145 defer {
161- os.fd_close (reader)
162- os.fd_close (writer)
146+ pipe.close ()
163147 notifier.close () or {}
164148 }
165149
166150 // level triggered - will keep getting events while
167151 // there is data to read
168- notifier.add (reader , .read)!
169- os. fd_write (writer, 'foobar' )
152+ notifier.add (pipe.read_fd , .read)!
153+ pipe. write_string ( 'foobar' )!
170154 assert notifier.wait (0 ).len == 1
171155 assert notifier.wait (0 ).len == 1
172156
173- notifier.remove (reader )!
157+ notifier.remove (pipe.read_fd )!
174158 assert notifier.wait (0 ).len == 0
175159 }
176160}
0 commit comments