Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fifo_test: Add 2 tests for closing after reading #42

Merged
merged 1 commit into from
Jan 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions fifo_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

/*
Expand Down Expand Up @@ -355,6 +356,96 @@ func TestFifoCloseError(t *testing.T) {
assert.Equal(t, ErrReadClosed, err)
}

func TestFifoCloseWhileReading(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

r, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
assert.NoError(t, err)

read := make(chan struct{})
readErr := make(chan error)

go func() {
buf := make([]byte, 32)
_, err := r.Read(buf)

if err != nil {
readErr <- err
return
}

close(read)

}()

time.Sleep(500 * time.Millisecond)
r.Close()

select {
case <-read:
t.Fatal("Read should not succeed")
case err := <-readErr:
assert.Equal(t, ErrReadClosed, err)
case <-time.After(500 * time.Millisecond):
t.Fatal("Read should not be blocked")
}
}

func TestFifoCloseWhileReadingAndWriting(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

r, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
assert.NoError(t, err)

w, err := OpenFifo(ctx, filepath.Join(tmpdir, t.Name()), syscall.O_WRONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0)
assert.NoError(t, err)

read := make(chan struct{})
readErr := make(chan error)
wBuffer := []byte("foo")

go func() {
buf := make([]byte, 32)
_, err := r.Read(buf)

if err != nil {
readErr <- err
return
}

close(read)
}()

time.Sleep(500 * time.Millisecond)

// Close the reader and then write in the writer.
// The reader thread should return an error.
r.Close()

// The write should fail, the reader end of the pipe is closed.
_, err = w.Write(wBuffer)
assert.Error(t, err)

select {
case <-read:
t.Fatal("Read should not succeed")
case err := <-readErr:
assert.Error(t, err)
case <-time.After(500 * time.Millisecond):
t.Fatal("Read should not be blocked")
}
}

func TestFifoWrongRdWrError(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "fifos")
assert.NoError(t, err)
Expand Down