@@ -5,59 +5,51 @@ import os.notify
55
66// make a pipe and return the (read, write) file descriptors
77fn 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 ]
8+ pipefd := [2 ]int {}
9+ if C.pipe (& pipefd[0 ]) != 0 {
10+ return error ('error ${C .errno }: ' + os.posix_get_error_msg (C.errno))
1411 }
15- return - 1 , - 1
12+ return pipefd[ 0 ], pipefd[ 1 ]
1613}
1714
1815fn test_level_trigger () {
19- // currently only linux and macos are supported
20- $if linux || macos {
21- mut notifier := notify.new ()!
22- reader , writer := make_pipe ()!
23- defer {
24- os.fd_close (reader)
25- os.fd_close (writer)
26- notifier.close () or {}
27- }
28- notifier.add (reader, .read)!
16+ mut notifier := notify.new ()!
17+ reader , writer := make_pipe ()!
18+ defer {
19+ os.fd_close (reader)
20+ os.fd_close (writer)
21+ notifier.close () or {}
22+ }
23+ notifier.add (reader, .read)!
2924
30- os.fd_write (writer, 'foobar' )
31- mut n := & notifier
32- check_read_event (mut n, reader, 'foo' )
33- check_read_event (mut n, reader, 'bar' )
25+ os.fd_write (writer, 'foobar' )
26+ mut n := & notifier
27+ check_read_event (mut n, reader, 'foo' )
28+ check_read_event (mut n, reader, 'bar' )
3429
35- assert notifier.wait (0 ).len == 0
36- }
30+ assert notifier.wait (0 ).len == 0
3731}
3832
3933fn test_edge_trigger () {
40- // currently only linux and macos are supported
41- $if linux || macos {
42- mut notifier := notify.new ()!
43- reader , writer := make_pipe ()!
44- defer {
45- os.fd_close (reader)
46- os.fd_close (writer)
47- notifier.close () or {}
48- }
49- notifier.add (reader, .read, .edge_trigger)!
34+ mut notifier := notify.new ()!
35+ reader , writer := make_pipe ()!
36+ defer {
37+ os.fd_close (reader)
38+ os.fd_close (writer)
39+ notifier.close () or {}
40+ }
41+ notifier.add (reader, .read, .edge_trigger)!
5042
51- mut n := & notifier
43+ mut n := & notifier
5244
53- os.fd_write (writer, 'foobar' )
54- check_read_event (mut n, reader, 'foo' )
45+ os.fd_write (writer, 'foobar' )
46+ check_read_event (mut n, reader, 'foo' )
5547
56- $if linux {
57- assert notifier.wait (0 ).len == 0
58- }
59- $if macos {
60- /*
48+ $if linux {
49+ assert notifier.wait (0 ).len == 0
50+ }
51+ $if macos {
52+ /*
6153 In the kqueue of macos, EV_CLEAR flag represents a clear event,
6254 which is mainly used for pipeline and socket class events. When this flag is set,
6355 kqueue will trigger the corresponding event when the data is readable or writable,
@@ -72,44 +64,40 @@ fn test_edge_trigger() {
7264 the next kqueue event notification may still be triggered
7365 */
7466
75- // notifier.wait(0).len == 1 or 0
76- }
77-
78- os.fd_write (writer, 'baz' )
79- // we do not get an event because there is still data
80- // to be read
81- // assert notifier.wait(0).len == 0
82- // TODO: investigage why the above assert suddenly started failing on the latest Ubuntu kernel update:
83- // 5.11.0-37-generic #41~20.04.2-Ubuntu SMP Fri Sep 24 09:06:38 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
67+ // notifier.wait(0).len == 1 or 0
8468 }
69+
70+ os.fd_write (writer, 'baz' )
71+ // we do not get an event because there is still data
72+ // to be read
73+ // assert notifier.wait(0).len == 0
74+ // TODO: investigage why the above assert suddenly started failing on the latest Ubuntu kernel update:
75+ // 5.11.0-37-generic #41~20.04.2-Ubuntu SMP Fri Sep 24 09:06:38 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
8576}
8677
8778fn test_one_shot () {
88- $if linux || macos {
89- mut notifier := notify.new ()!
90- reader , writer := make_pipe ()!
91- defer {
92- os.fd_close (reader)
93- os.fd_close (writer)
94- notifier.close () or {}
95- }
96- notifier.add (reader, .read, .one_shot)!
79+ mut notifier := notify.new ()!
80+ reader , writer := make_pipe ()!
81+ defer {
82+ os.fd_close (reader)
83+ os.fd_close (writer)
84+ notifier.close () or {}
85+ }
86+ notifier.add (reader, .read, .one_shot)!
9787
98- mut n := & notifier
88+ mut n := & notifier
9989
100- os.fd_write (writer, 'foobar' )
101- check_read_event (mut n, reader, 'foo' )
102- os.fd_write (writer, 'baz' )
90+ os.fd_write (writer, 'foobar' )
91+ check_read_event (mut n, reader, 'foo' )
92+ os.fd_write (writer, 'baz' )
10393
104- assert notifier.wait (0 ).len == 0
94+ assert notifier.wait (0 ).len == 0
10595
106- // rearm
107- notifier.modify (reader, .read)!
108- check_read_event (mut n, reader, 'barbaz' )
109- }
96+ // rearm
97+ notifier.modify (reader, .read)!
98+ check_read_event (mut n, reader, 'barbaz' )
11099}
111100
112- // Kqueue does not support 'hangup' event type.
113101fn test_hangup () {
114102 $if linux {
115103 mut notifier := notify.new ()!
@@ -133,46 +121,42 @@ fn test_hangup() {
133121}
134122
135123fn test_write () {
136- $if linux || macos {
137- mut notifier := notify.new ()!
138- reader , writer := make_pipe ()!
139- defer {
140- os.fd_close (reader)
141- os.fd_close (writer)
142- notifier.close () or {}
143- }
124+ mut notifier := notify.new ()!
125+ reader , writer := make_pipe ()!
126+ defer {
127+ os.fd_close (reader)
128+ os.fd_close (writer)
129+ notifier.close () or {}
130+ }
144131
145- notifier.add (reader, .write)!
146- assert notifier.wait (0 ).len == 0
132+ notifier.add (reader, .write)!
133+ assert notifier.wait (0 ).len == 0
147134
148- notifier.add (writer, .write)!
149- events := notifier.wait (0 )
150- assert events.len == 1
151- assert events[0 ].fd == writer
152- assert events[0 ].kind.has (.write)
153- }
135+ notifier.add (writer, .write)!
136+ events := notifier.wait (0 )
137+ assert events.len == 1
138+ assert events[0 ].fd == writer
139+ assert events[0 ].kind.has (.write)
154140}
155141
156142fn test_remove () {
157- $if linux || macos {
158- mut notifier := notify.new ()!
159- reader , writer := make_pipe ()!
160- defer {
161- os.fd_close (reader)
162- os.fd_close (writer)
163- notifier.close () or {}
164- }
143+ mut notifier := notify.new ()!
144+ reader , writer := make_pipe ()!
145+ defer {
146+ os.fd_close (reader)
147+ os.fd_close (writer)
148+ notifier.close () or {}
149+ }
165150
166- // level triggered - will keep getting events while
167- // there is data to read
168- notifier.add (reader, .read)!
169- os.fd_write (writer, 'foobar' )
170- assert notifier.wait (0 ).len == 1
171- assert notifier.wait (0 ).len == 1
151+ // level triggered - will keep getting events while
152+ // there is data to read
153+ notifier.add (reader, .read)!
154+ os.fd_write (writer, 'foobar' )
155+ assert notifier.wait (0 ).len == 1
156+ assert notifier.wait (0 ).len == 1
172157
173- notifier.remove (reader)!
174- assert notifier.wait (0 ).len == 0
175- }
158+ notifier.remove (reader)!
159+ assert notifier.wait (0 ).len == 0
176160}
177161
178162fn check_read_event (mut notifier notify.FdNotifier, reader_fd int , expected string ) {
0 commit comments