Skip to content

Commit

Permalink
wasi: fix flaky tests (#2068)
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
  • Loading branch information
evacchi authored Feb 19, 2024
1 parent f80ef11 commit c1c071e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
5 changes: 5 additions & 0 deletions imports/wasi_snapshot_preview1/testdata/zig-cc/wasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ void main_nonblock(char* fpath) {
ssize_t newLen = 0;
while (newLen == 0) {
newLen = read(fd, buf, sizeof(buf));
// If an empty string is read, newLen might be 1,
// causing the loop to terminate.
if (strlen(buf) == 0) {
newLen = 0;
}
if (errno == EAGAIN || newLen == 0) {
printf(".");
nanosleep(&tim , &tim2) ;
Expand Down
Binary file modified imports/wasi_snapshot_preview1/testdata/zig-cc/wasi.wasm
Binary file not shown.
50 changes: 32 additions & 18 deletions internal/sysfs/poll_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ func TestPoll_Windows(t *testing.T) {
t.Run("peekNamedPipe should report the correct state of incoming data in the pipe", func(t *testing.T) {
r, w, err := os.Pipe()
require.NoError(t, err)
defer r.Close()
defer w.Close()
rh := syscall.Handle(r.Fd())
wh := syscall.Handle(w.Fd())

// Ensure the pipe has no data.
n, err := peekNamedPipe(rh)
Expand All @@ -45,8 +46,8 @@ func TestPoll_Windows(t *testing.T) {
// Write to the channel.
msg, err := syscall.ByteSliceFromString("test\n")
require.NoError(t, err)
_, err = syscall.Write(wh, msg)
require.NoError(t, err)
_, err = write(w, msg)
require.EqualErrno(t, 0, err)

// Ensure the pipe has data.
n, err = peekNamedPipe(rh)
Expand Down Expand Up @@ -132,7 +133,10 @@ func TestPoll_Windows(t *testing.T) {
})

t.Run("poll should return immediately when duration is zero (no data)", func(t *testing.T) {
r, _, err := os.Pipe()
r, w, err := os.Pipe()
defer r.Close()
defer w.Close()

require.NoError(t, err)
fds := []pollFd{{fd: r.Fd(), events: _POLLIN}}
n, err := _poll(fds, 0)
Expand All @@ -143,14 +147,15 @@ func TestPoll_Windows(t *testing.T) {
t.Run("poll should return immediately when duration is zero (data)", func(t *testing.T) {
r, w, err := os.Pipe()
require.NoError(t, err)
defer r.Close()
defer w.Close()
fds := []pollFd{{fd: r.Fd(), events: _POLLIN}}
wh := syscall.Handle(w.Fd())

// Write to the channel immediately.
msg, err := syscall.ByteSliceFromString("test\n")
require.NoError(t, err)
_, err = syscall.Write(wh, msg)
require.NoError(t, err)
_, err = write(w, msg)
require.EqualErrno(t, 0, err)

// Verify that the write is reported.
n, err := _poll(fds, 0)
Expand All @@ -159,8 +164,10 @@ func TestPoll_Windows(t *testing.T) {
})

t.Run("poll should wait forever when duration is nil (no writes)", func(t *testing.T) {
r, _, err := os.Pipe()
r, w, err := os.Pipe()
require.NoError(t, err)
defer r.Close()
defer w.Close()

ch := make(chan result, 1)
go pollToChannel(r.Fd(), -1, ch)
Expand All @@ -173,7 +180,8 @@ func TestPoll_Windows(t *testing.T) {
t.Run("poll should wait forever when duration is nil", func(t *testing.T) {
r, w, err := os.Pipe()
require.NoError(t, err)
wh := syscall.Handle(w.Fd())
defer r.Close()
defer w.Close()

ch := make(chan result, 1)
go pollToChannel(r.Fd(), -1, ch)
Expand All @@ -185,8 +193,8 @@ func TestPoll_Windows(t *testing.T) {
// Write a message to the pipe.
msg, err := syscall.ByteSliceFromString("test\n")
require.NoError(t, err)
_, err = syscall.Write(wh, msg)
require.NoError(t, err)
_, err = write(w, msg)
require.EqualErrno(t, 0, err)

// Ensure that the write occurs (panic after an arbitrary timeout).
select {
Expand All @@ -201,7 +209,8 @@ func TestPoll_Windows(t *testing.T) {
t.Run("poll should wait for the given duration", func(t *testing.T) {
r, w, err := os.Pipe()
require.NoError(t, err)
wh := syscall.Handle(w.Fd())
defer r.Close()
defer w.Close()

ch := make(chan result, 1)
go pollToChannel(r.Fd(), 500, ch)
Expand All @@ -213,8 +222,8 @@ func TestPoll_Windows(t *testing.T) {
// Write a message to the pipe.
msg, err := syscall.ByteSliceFromString("test\n")
require.NoError(t, err)
_, err = syscall.Write(wh, msg)
require.NoError(t, err)
_, err = write(w, msg)
require.EqualErrno(t, 0, err)

// Ensure that the write occurs before the timer expires.
select {
Expand All @@ -227,8 +236,10 @@ func TestPoll_Windows(t *testing.T) {
})

t.Run("poll should timeout after the given duration", func(t *testing.T) {
r, _, err := os.Pipe()
r, w, err := os.Pipe()
require.NoError(t, err)
defer r.Close()
defer w.Close()

ch := make(chan result, 1)
go pollToChannel(r.Fd(), 200, ch)
Expand All @@ -242,7 +253,8 @@ func TestPoll_Windows(t *testing.T) {
t.Run("poll should return when a write occurs before the given duration", func(t *testing.T) {
r, w, err := os.Pipe()
require.NoError(t, err)
wh := syscall.Handle(w.Fd())
defer r.Close()
defer w.Close()

ch := make(chan result, 1)
go pollToChannel(r.Fd(), 800, ch)
Expand All @@ -252,8 +264,8 @@ func TestPoll_Windows(t *testing.T) {

msg, err := syscall.ByteSliceFromString("test\n")
require.NoError(t, err)
_, err = syscall.Write(wh, msg)
require.NoError(t, err)
_, err = write(w, msg)
require.EqualErrno(t, 0, err)

res := <-ch
require.Zero(t, res.err)
Expand All @@ -262,7 +274,9 @@ func TestPoll_Windows(t *testing.T) {

t.Run("poll should return when a regular file is given", func(t *testing.T) {
f, err := os.CreateTemp(t.TempDir(), "ex")
require.NoError(t, err)
defer f.Close()

require.NoError(t, err)
fds := []pollFd{{fd: f.Fd(), events: _POLLIN}}
n, errno := _poll(fds, 0)
Expand Down

0 comments on commit c1c071e

Please sign in to comment.