1
2
3
4
5 package work
6
7 import (
8 "errors"
9 "io"
10 "os"
11 "path/filepath"
12 "runtime"
13 "testing"
14 "time"
15 )
16
17 func TestExportPipeNotify(t *testing.T) {
18 if !earlyExportSupported {
19 t.Skip("early export not supported on this platform")
20 }
21
22 objdir := t.TempDir() + string(filepath.Separator)
23 ep, err := newExportPipe(objdir)
24 if err != nil {
25 t.Fatalf("newExportPipe: %v", err)
26 }
27
28 writeDone := make(chan error, 1)
29 go func() {
30 time.Sleep(10 * time.Millisecond)
31 f, err := os.OpenFile(ep.name(), os.O_WRONLY, 0)
32 if err != nil {
33 writeDone <- err
34 return
35 }
36 _, werr := f.Write([]byte{'\n'})
37 cerr := f.Close()
38 writeDone <- errors.Join(werr, cerr)
39 }()
40
41 if err := ep.wait(); err != nil {
42 t.Fatalf("wait: %v", err)
43 }
44 if err := <-writeDone; err != nil {
45 t.Fatalf("writer: %v", err)
46 }
47
48 ep.close()
49 if runtime.GOOS != "windows" {
50 if _, err := os.Stat(ep.name()); !errors.Is(err, os.ErrNotExist) {
51 t.Errorf("export pipe %s still exists after close: %v", ep.name(), err)
52 }
53 }
54 }
55
56 func TestExportPipeCloseUnblocksWait(t *testing.T) {
57 if !earlyExportSupported {
58 t.Skip("early export not supported on this platform")
59 }
60
61 objdir := t.TempDir() + string(filepath.Separator)
62 ep, err := newExportPipe(objdir)
63 if err != nil {
64 t.Fatalf("newExportPipe: %v", err)
65 }
66
67 waitDone := make(chan error, 1)
68 go func() {
69 waitDone <- ep.wait()
70 }()
71
72 time.Sleep(10 * time.Millisecond)
73 ep.close()
74
75 select {
76 case err := <-waitDone:
77 if !errors.Is(err, io.EOF) {
78 t.Fatalf("wait after close = %v, want io.EOF", err)
79 }
80 case <-time.After(5 * time.Second):
81 t.Fatal("wait did not unblock after close")
82 }
83 }
84
85 func TestExportPipeCloseBeforeWait(t *testing.T) {
86 if !earlyExportSupported {
87 t.Skip("early export not supported on this platform")
88 }
89
90 objdir := t.TempDir() + string(filepath.Separator)
91 ep, err := newExportPipe(objdir)
92 if err != nil {
93 t.Fatalf("newExportPipe: %v", err)
94 }
95
96 ep.close()
97 if err := ep.wait(); !errors.Is(err, io.EOF) {
98 t.Fatalf("wait after close = %v, want io.EOF", err)
99 }
100 }
101
102 func TestExportPipeCloseWithoutWait(t *testing.T) {
103 if !earlyExportSupported {
104 t.Skip("early export not supported on this platform")
105 }
106
107 objdir := t.TempDir() + string(filepath.Separator)
108 ep, err := newExportPipe(objdir)
109 if err != nil {
110 t.Fatalf("newExportPipe: %v", err)
111 }
112
113 ep.close()
114 if runtime.GOOS != "windows" {
115 if _, err := os.Stat(ep.name()); !errors.Is(err, os.ErrNotExist) {
116 t.Errorf("export pipe %s still exists after close: %v", ep.name(), err)
117 }
118 }
119 }
120
View as plain text