Skip to content

Instantly share code, notes, and snippets.

@mikelikespie
Last active August 29, 2015 13:59
Show Gist options
  • Save mikelikespie/10945232 to your computer and use it in GitHub Desktop.
Save mikelikespie/10945232 to your computer and use it in GitHub Desktop.
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