2022-04-30 13:27:50 +03:00
|
|
|
// vtest flaky: true
|
2021-10-05 15:50:33 +03:00
|
|
|
// vtest retry: 3
|
2021-06-15 04:44:31 +03:00
|
|
|
import os
|
|
|
|
import os.notify
|
|
|
|
|
|
|
|
// make a pipe and return the (read, write) file descriptors
|
|
|
|
fn make_pipe() ?(int, int) {
|
|
|
|
$if linux {
|
|
|
|
pipefd := [2]int{}
|
|
|
|
if C.pipe(&pipefd[0]) != 0 {
|
|
|
|
return error('error $C.errno: ' + os.posix_get_error_msg(C.errno))
|
|
|
|
}
|
|
|
|
return pipefd[0], pipefd[1]
|
|
|
|
}
|
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_level_trigger() {
|
2021-06-15 04:44:31 +03:00
|
|
|
// currently only linux is supported
|
|
|
|
$if linux {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut notifier := notify.new()?
|
|
|
|
reader, writer := make_pipe()?
|
2021-06-15 04:44:31 +03:00
|
|
|
defer {
|
|
|
|
os.fd_close(reader)
|
|
|
|
os.fd_close(writer)
|
|
|
|
notifier.close() or {}
|
|
|
|
}
|
2022-05-13 06:56:21 +03:00
|
|
|
notifier.add(reader, .read)?
|
2021-06-15 04:44:31 +03:00
|
|
|
|
|
|
|
os.fd_write(writer, 'foobar')
|
2021-10-11 15:41:31 +03:00
|
|
|
mut n := ¬ifier
|
|
|
|
check_read_event(mut n, reader, 'foo')
|
|
|
|
check_read_event(mut n, reader, 'bar')
|
2021-06-15 04:44:31 +03:00
|
|
|
|
|
|
|
assert notifier.wait(0).len == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_edge_trigger() {
|
2021-06-15 04:44:31 +03:00
|
|
|
// currently only linux is supported
|
|
|
|
$if linux {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut notifier := notify.new()?
|
|
|
|
reader, writer := make_pipe()?
|
2021-06-15 04:44:31 +03:00
|
|
|
defer {
|
|
|
|
os.fd_close(reader)
|
|
|
|
os.fd_close(writer)
|
|
|
|
notifier.close() or {}
|
|
|
|
}
|
2022-05-13 06:56:21 +03:00
|
|
|
notifier.add(reader, .read, .edge_trigger)?
|
2021-06-15 04:44:31 +03:00
|
|
|
|
2021-10-11 15:41:31 +03:00
|
|
|
mut n := ¬ifier
|
|
|
|
|
2021-06-15 04:44:31 +03:00
|
|
|
os.fd_write(writer, 'foobar')
|
2021-10-11 15:41:31 +03:00
|
|
|
check_read_event(mut n, reader, 'foo')
|
2021-06-15 04:44:31 +03:00
|
|
|
|
|
|
|
assert notifier.wait(0).len == 0
|
|
|
|
|
|
|
|
os.fd_write(writer, 'baz')
|
|
|
|
// we do not get an event because there is still data
|
|
|
|
// to be read
|
2021-10-05 16:01:54 +03:00
|
|
|
// assert notifier.wait(0).len == 0
|
|
|
|
// TODO: investigage why the above assert suddenly started failing on the latest Ubuntu kernel update:
|
|
|
|
// 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
|
2021-06-15 04:44:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_one_shot() {
|
2021-06-15 04:44:31 +03:00
|
|
|
$if linux {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut notifier := notify.new()?
|
|
|
|
reader, writer := make_pipe()?
|
2021-06-15 04:44:31 +03:00
|
|
|
defer {
|
|
|
|
os.fd_close(reader)
|
|
|
|
os.fd_close(writer)
|
|
|
|
notifier.close() or {}
|
|
|
|
}
|
2022-05-13 06:56:21 +03:00
|
|
|
notifier.add(reader, .read, .one_shot)?
|
2021-06-15 04:44:31 +03:00
|
|
|
|
2021-10-11 15:41:31 +03:00
|
|
|
mut n := ¬ifier
|
|
|
|
|
2021-06-15 04:44:31 +03:00
|
|
|
os.fd_write(writer, 'foobar')
|
2021-10-11 15:41:31 +03:00
|
|
|
check_read_event(mut n, reader, 'foo')
|
2021-06-15 04:44:31 +03:00
|
|
|
os.fd_write(writer, 'baz')
|
|
|
|
|
|
|
|
assert notifier.wait(0).len == 0
|
|
|
|
|
|
|
|
// rearm
|
2022-05-13 06:56:21 +03:00
|
|
|
notifier.modify(reader, .read)?
|
2021-10-11 15:41:31 +03:00
|
|
|
check_read_event(mut n, reader, 'barbaz')
|
2021-06-15 04:44:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_hangup() {
|
2021-06-15 04:44:31 +03:00
|
|
|
$if linux {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut notifier := notify.new()?
|
|
|
|
reader, writer := make_pipe()?
|
2021-06-15 04:44:31 +03:00
|
|
|
defer {
|
|
|
|
os.fd_close(reader)
|
|
|
|
notifier.close() or {}
|
|
|
|
}
|
2022-05-13 06:56:21 +03:00
|
|
|
notifier.add(reader, .hangup)?
|
2021-06-15 04:44:31 +03:00
|
|
|
|
|
|
|
assert notifier.wait(0).len == 0
|
|
|
|
|
|
|
|
// closing on the writer end of the pipe will
|
|
|
|
// cause a hangup on the reader end
|
|
|
|
os.fd_close(writer)
|
|
|
|
events := notifier.wait(0)
|
|
|
|
assert events.len == 1
|
|
|
|
assert events[0].fd == reader
|
|
|
|
assert events[0].kind.has(.hangup)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_write() {
|
2021-06-15 04:44:31 +03:00
|
|
|
$if linux {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut notifier := notify.new()?
|
|
|
|
reader, writer := make_pipe()?
|
2021-06-15 04:44:31 +03:00
|
|
|
defer {
|
|
|
|
os.fd_close(reader)
|
|
|
|
os.fd_close(writer)
|
|
|
|
notifier.close() or {}
|
|
|
|
}
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
notifier.add(reader, .write)?
|
2021-06-15 04:44:31 +03:00
|
|
|
assert notifier.wait(0).len == 0
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
notifier.add(writer, .write)?
|
2021-06-15 04:44:31 +03:00
|
|
|
events := notifier.wait(0)
|
|
|
|
assert events.len == 1
|
|
|
|
assert events[0].fd == writer
|
|
|
|
assert events[0].kind.has(.write)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:45:43 +03:00
|
|
|
fn test_remove() {
|
2021-06-15 04:44:31 +03:00
|
|
|
$if linux {
|
2022-05-13 06:56:21 +03:00
|
|
|
mut notifier := notify.new()?
|
|
|
|
reader, writer := make_pipe()?
|
2021-06-15 04:44:31 +03:00
|
|
|
defer {
|
|
|
|
os.fd_close(reader)
|
|
|
|
os.fd_close(writer)
|
|
|
|
notifier.close() or {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// level triggered - will keep getting events while
|
|
|
|
// there is data to read
|
2022-05-13 06:56:21 +03:00
|
|
|
notifier.add(reader, .read)?
|
2021-06-15 04:44:31 +03:00
|
|
|
os.fd_write(writer, 'foobar')
|
|
|
|
assert notifier.wait(0).len == 1
|
|
|
|
assert notifier.wait(0).len == 1
|
|
|
|
|
2022-05-13 06:56:21 +03:00
|
|
|
notifier.remove(reader)?
|
2021-06-15 04:44:31 +03:00
|
|
|
assert notifier.wait(0).len == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:41:31 +03:00
|
|
|
fn check_read_event(mut notifier notify.FdNotifier, reader_fd int, expected string) {
|
2021-06-15 04:44:31 +03:00
|
|
|
events := notifier.wait(0)
|
|
|
|
assert events.len == 1
|
|
|
|
assert events[0].fd == reader_fd
|
|
|
|
assert events[0].kind.has(.read)
|
|
|
|
s, _ := os.fd_read(events[0].fd, expected.len)
|
|
|
|
assert s == expected
|
|
|
|
}
|