@@ -47,9 +47,17 @@ func create(t *testing.T, ptty pty.PTY, name string) *PTY {
47
47
logDone := make (chan struct {})
48
48
logr , logw := io .Pipe ()
49
49
t .Cleanup (func () {
50
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
51
+ defer cancel ()
52
+
50
53
_ = logw .Close ()
51
54
_ = logr .Close ()
52
- <- logDone // Guard against logging after test.
55
+ time .Sleep (5 * time .Second )
56
+ select {
57
+ case <- ctx .Done ():
58
+ fatalf (t , name , "cleanup" , "log pipe did not close in time" )
59
+ case <- logDone : // Guard against logging after test.
60
+ }
53
61
})
54
62
55
63
// Write to log and output buffer.
@@ -62,9 +70,16 @@ func create(t *testing.T, ptty pty.PTY, name string) *PTY {
62
70
_ = out .closeErr (err )
63
71
}()
64
72
t .Cleanup (func () {
73
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
74
+ defer cancel ()
75
+
65
76
_ = out .Close ()
66
77
_ = ptty .Close ()
67
- <- copyDone
78
+ select {
79
+ case <- ctx .Done ():
80
+ fatalf (t , name , "cleanup" , "copy did not close in time" )
81
+ case <- copyDone :
82
+ }
68
83
})
69
84
70
85
tpty := & PTY {
@@ -164,20 +179,30 @@ func (p *PTY) WriteLine(str string) {
164
179
165
180
func (p * PTY ) logf (format string , args ... interface {}) {
166
181
p .t .Helper ()
182
+ logf (p .t , p .name , format , args ... )
183
+ }
184
+
185
+ func (p * PTY ) fatalf (reason string , format string , args ... interface {}) {
186
+ p .t .Helper ()
187
+ fatalf (p .t , p .name , reason , format , args ... )
188
+ }
189
+
190
+ func logf (t * testing.T , name , format string , args ... interface {}) {
191
+ t .Helper ()
167
192
168
193
// Match regular logger timestamp format, we seem to be logging in
169
194
// UTC in other places as well, so match here.
170
- p . t .Logf ("%s: %s: %s" , time .Now ().UTC ().Format ("2006-01-02 15:04:05.000" ), p . name , fmt .Sprintf (format , args ... ))
195
+ t .Logf ("%s: %s: %s" , time .Now ().UTC ().Format ("2006-01-02 15:04:05.000" ), name , fmt .Sprintf (format , args ... ))
171
196
}
172
197
173
- func ( p * PTY ) fatalf ( reason string , format string , args ... interface {}) {
174
- p . t .Helper ()
198
+ func fatalf ( t * testing. T , name , reason , format string , args ... interface {}) {
199
+ t .Helper ()
175
200
176
201
// Ensure the message is part of the normal log stream before
177
202
// failing the test.
178
- p . logf ("%s: %s" , reason , fmt .Sprintf (format , args ... ))
203
+ logf (t , name , "%s: %s" , reason , fmt .Sprintf (format , args ... ))
179
204
180
- require .FailNowf (p . t , reason , format , args ... )
205
+ require .FailNowf (t , reason , format , args ... )
181
206
}
182
207
183
208
// stdbuf is like a buffered stdout, it buffers writes until read.
0 commit comments