Skip to content

Instantly share code, notes, and snippets.

@mikelikespie
Last active Aug 29, 2015
Embed
What would you like to do?
Read does not return when FD is closed for UIO
package foo
import (
"io"
"os"
"testing"
"time"
)
func TestReadClosed(t *testing.T) {
f, err := os.OpenFile(
"/dev/uio0", // Replace this with a regular file, and this test will work just fine
os.O_SYNC|os.O_RDWR,
0600,
)
if err != nil {
t.Fatal("Error opening", err)
}
done := make(chan int)
go func() {
buff := make([]byte, 4)
n, err := f.Read(buff)
switch err {
case io.EOF:
// expected
case nil:
t.Fatalf("Expected eof. Instead got no error, read %d bytes with the contents: %+v", n, buff[:n])
default:
t.Fatal("Got unexpected error:", err)
}
done <- 1
}()
// Need to give the goroutine enough time to start the read.
time.Sleep(time.Millisecond)
if err = f.Close(); err != nil {
t.Fatal("Got unexpected error:", err)
}
timeout := time.After(time.Millisecond * 100)
select {
case <-timeout:
t.Fatal("Timeout waiting for goroutine to end")
case <-done:
// Expected
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment