@@ -6,6 +6,7 @@ package poll_test
6
6
7
7
import (
8
8
"internal/poll"
9
+ "internal/syscall/unix"
9
10
"runtime"
10
11
"syscall"
11
12
"testing"
@@ -16,8 +17,8 @@ import (
16
17
func checkPipes (fds []int ) bool {
17
18
for _ , fd := range fds {
18
19
// Check if each pipe fd has been closed.
19
- err := syscall .FcntlFlock ( uintptr (fd ), syscall .F_GETFD , nil )
20
- if err == nil {
20
+ _ , _ , errno := syscall .Syscall ( unix . FcntlSyscall , uintptr (fd ), syscall .F_GETPIPE_SZ , 0 )
21
+ if errno == 0 {
21
22
return false
22
23
}
23
24
}
@@ -37,28 +38,37 @@ func TestSplicePipePool(t *testing.T) {
37
38
if err != nil {
38
39
t .Skip ("failed to create pipe, skip this test" )
39
40
}
40
- prfd , pwfd := poll .GetPipeFds (p )
41
- fds = append (fds , prfd , pwfd )
41
+ _ , pwfd := poll .GetPipeFds (p )
42
+ fds = append (fds , pwfd )
42
43
ps = append (ps , p )
43
44
}
44
45
for _ , p = range ps {
45
46
poll .PutPipe (p )
46
47
}
47
48
ps = nil
48
49
49
- var ok bool
50
- // Trigger garbage collection to free the pipes in sync.Pool and check whether or not
51
- // those pipe buffers have been closed as we expected.
52
- for i := 0 ; i < 5 ; i ++ {
50
+ // Exploit the timeout of "go test" as a timer for the subsequent verification.
51
+ timeout := 5 * time .Minute
52
+ if deadline , ok := t .Deadline (); ok {
53
+ timeout = deadline .Sub (time .Now ())
54
+ timeout -= timeout / 10 // Leave 10% headroom for cleanup.
55
+ }
56
+ expiredTime := time .NewTimer (timeout )
57
+ defer expiredTime .Stop ()
58
+
59
+ // Trigger garbage collection repeatedly, waiting for all pipes in sync.Pool
60
+ // to either be deallocated and closed, or to time out.
61
+ for {
53
62
runtime .GC ()
54
- time .Sleep (time . Duration ( i * 100 + 10 ) * time .Millisecond )
55
- if ok = checkPipes (fds ); ok {
63
+ time .Sleep (10 * time .Millisecond )
64
+ if checkPipes (fds ) {
56
65
break
57
66
}
58
- }
59
-
60
- if ! ok {
61
- t .Fatal ("at least one pipe is still open" )
67
+ select {
68
+ case <- expiredTime .C :
69
+ t .Fatal ("at least one pipe is still open" )
70
+ default :
71
+ }
62
72
}
63
73
}
64
74
0 commit comments