Read does not return when FD is closed for UIO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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