Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 13:59
What would you like to do?
Read does not return when FD is closed for UIO
package foo
import (
func TestReadClosed(t *testing.T) {
f, err := os.OpenFile(
"/dev/uio0", // Replace this with a regular file, and this test will work just fine
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])
t.Fatal("Got unexpected error:", err)
done <- 1
// Need to give the goroutine enough time to start the read.
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