| 1 | //go:build !windows && !plan9 |
| 2 | |
| 3 | package fileutil |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "syscall" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestOpenFileBeneathDoesNotBlockOnFIFO(t *testing.T) { |
| 14 | root := t.TempDir() |
| 15 | path := filepath.Join(root, "context.fifo") |
| 16 | if err := syscall.Mkfifo(path, 0o600); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | done := make(chan error, 1) |
| 20 | go func() { |
| 21 | file, err := OpenFileBeneath(root, "context.fifo") |
| 22 | if file != nil { |
| 23 | _ = file.Close() |
| 24 | } |
| 25 | done <- err |
| 26 | }() |
| 27 | select { |
| 28 | case err := <-done: |
| 29 | if err != nil { |
| 30 | t.Fatalf("OpenFileBeneath() error = %v", err) |
| 31 | } |
| 32 | case <-time.After(2 * time.Second): |
| 33 | writer, _ := os.OpenFile(path, os.O_WRONLY|syscall.O_NONBLOCK, 0) |
| 34 | if writer != nil { |
| 35 | _ = writer.Close() |
| 36 | } |
| 37 | t.Fatal("OpenFileBeneath blocked while opening a FIFO") |
| 38 | } |
| 39 | } |
| 40 |