Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit ac28add

Browse files
authored
Merge pull request #41314 from thaJeztah/19.03_backport_fix_racey_logger_test
[19.03 backport] test-fixes for flaky test: TestCheckCapacityAndRotate Upstream-commit: bd33bbf0497b2327516dc799a5e541b720822a4c Component: engine
2 parents 38ac4c1 + a0edb6a commit ac28add

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

components/engine/daemon/logger/loggerutils/logfile_test.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88
"os"
9+
"path/filepath"
910
"strings"
1011
"testing"
1112
"time"
@@ -14,6 +15,7 @@ import (
1415
"github.com/docker/docker/pkg/pubsub"
1516
"github.com/docker/docker/pkg/tailfile"
1617
"gotest.tools/assert"
18+
"gotest.tools/poll"
1719
)
1820

1921
func TestTailFiles(t *testing.T) {
@@ -225,21 +227,15 @@ func TestCheckCapacityAndRotate(t *testing.T) {
225227
defer l.Close()
226228

227229
assert.NilError(t, l.WriteLogEntry(&logger.Message{Line: []byte("hello world!")}))
228-
229-
dStringer := dirStringer{dir}
230-
231230
_, err = os.Stat(f.Name() + ".1")
232-
assert.Assert(t, os.IsNotExist(err), dStringer)
231+
assert.Assert(t, os.IsNotExist(err), dirStringer{dir})
233232

234233
assert.NilError(t, l.WriteLogEntry(&logger.Message{Line: []byte("hello world!")}))
235-
_, err = os.Stat(f.Name() + ".1")
236-
assert.NilError(t, err, dStringer)
234+
poll.WaitOn(t, checkFileExists(f.Name()+".1.gz"), poll.WithDelay(time.Millisecond), poll.WithTimeout(30*time.Second))
237235

238236
assert.NilError(t, l.WriteLogEntry(&logger.Message{Line: []byte("hello world!")}))
239-
_, err = os.Stat(f.Name() + ".1")
240-
assert.NilError(t, err, dStringer)
241-
_, err = os.Stat(f.Name() + ".2.gz")
242-
assert.NilError(t, err, dStringer)
237+
poll.WaitOn(t, checkFileExists(f.Name()+".1.gz"), poll.WithDelay(time.Millisecond), poll.WithTimeout(30*time.Second))
238+
poll.WaitOn(t, checkFileExists(f.Name()+".2.gz"), poll.WithDelay(time.Millisecond), poll.WithTimeout(30*time.Second))
243239

244240
// Now let's simulate a failed rotation where the file was able to be closed but something else happened elsewhere
245241
// down the line.
@@ -265,3 +261,18 @@ func (d dirStringer) String() string {
265261
}
266262
return s.String()
267263
}
264+
265+
func checkFileExists(name string) poll.Check {
266+
return func(t poll.LogT) poll.Result {
267+
_, err := os.Stat(name)
268+
switch {
269+
case err == nil:
270+
return poll.Success()
271+
case os.IsNotExist(err):
272+
return poll.Continue("waiting for %s to exist", name)
273+
default:
274+
t.Logf("%s", dirStringer{filepath.Dir(name)})
275+
return poll.Error(err)
276+
}
277+
}
278+
}

0 commit comments

Comments
 (0)