...
1
2
3
18
19 package fifo
20
21 import (
22 "context"
23 "os"
24 "path/filepath"
25 "syscall"
26 "testing"
27 "time"
28
29 "github.com/stretchr/testify/assert"
30 )
31
32 func TestFifoCloseAfterRm(t *testing.T) {
33 tmpdir, err := os.MkdirTemp("", "fifos")
34 assert.NoError(t, err)
35 defer os.RemoveAll(tmpdir)
36
37
38
39 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
40 defer cancel()
41 f, err := OpenFifo(ctx, filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
42 assert.NoError(t, err)
43
44 time.Sleep(time.Second)
45
46 err = os.RemoveAll(filepath.Join(tmpdir, "f0"))
47 assert.NoError(t, err)
48
49 cerr := make(chan error)
50
51 go func() {
52 b := make([]byte, 32)
53 _, err := f.Read(b)
54 cerr <- err
55 }()
56
57 select {
58 case err := <-cerr:
59 t.Fatalf("read should have blocked, but got %v", err)
60 case <-time.After(500 * time.Millisecond):
61 }
62
63 err = f.Close()
64 assert.NoError(t, err)
65
66 select {
67 case err := <-cerr:
68 assert.EqualError(t, err, "reading from a closed fifo")
69 case <-time.After(500 * time.Millisecond):
70 t.Fatal("read should have been unblocked")
71 }
72
73 cancel()
74 ctx, cancel = context.WithCancel(context.Background())
75 cerr = make(chan error)
76 go func() {
77 _, err = OpenFifo(ctx, filepath.Join(tmpdir, "f1"), syscall.O_WRONLY|syscall.O_CREAT, 0600)
78 cerr <- err
79 }()
80
81 select {
82 case err := <-cerr:
83 t.Fatalf("open should have blocked, but got %v", err)
84 case <-time.After(500 * time.Millisecond):
85 }
86
87 err = os.RemoveAll(filepath.Join(tmpdir, "f1"))
88 cancel()
89
90 select {
91 case err := <-cerr:
92 assert.Error(t, err)
93 case <-time.After(500 * time.Millisecond):
94 t.Fatal("open should have been unblocked")
95 }
96 }
97
View as plain text