fstest: retry cleaning the integration test directory if necessary

This commit is contained in:
Nick Craig-Wood 2018-04-09 19:26:27 +01:00
parent d5b2ec32f1
commit 4b5ff33125

View File

@ -107,12 +107,20 @@ func newRun() *Run {
return r return r
} }
// dirsToRemove sorts by string length // run f(), retrying it until it returns with no error or the limit
type dirsToRemove []string // expires and it calls t.Fatalf
func retry(t *testing.T, what string, f func() error) {
func (d dirsToRemove) Len() int { return len(d) } var err error
func (d dirsToRemove) Swap(i, j int) { d[i], d[j] = d[j], d[i] } for try := 1; try <= *ListRetries; try++ {
func (d dirsToRemove) Less(i, j int) bool { return len(d[i]) > len(d[j]) } err = f()
if err == nil {
return
}
t.Logf("%s failed - try %d/%d: %v", what, try, *ListRetries, err)
time.Sleep(time.Second)
}
t.Logf("%s failed: %v", what, err)
}
// NewRun initialise the remote and local for testing and returns a // NewRun initialise the remote and local for testing and returns a
// run object. Call this from the tests. // run object. Call this from the tests.
@ -130,7 +138,7 @@ func NewRun(t *testing.T) *Run {
r = new(Run) r = new(Run)
*r = *oneRun *r = *oneRun
r.cleanRemote = func() { r.cleanRemote = func() {
var toDelete dirsToRemove var toDelete []string
err := walk.Walk(r.Fremote, "", true, -1, func(dirPath string, entries fs.DirEntries, err error) error { err := walk.Walk(r.Fremote, "", true, -1, func(dirPath string, entries fs.DirEntries, err error) error {
if err != nil { if err != nil {
if err == fs.ErrorDirNotFound { if err == fs.ErrorDirNotFound {
@ -141,10 +149,7 @@ func NewRun(t *testing.T) *Run {
for _, entry := range entries { for _, entry := range entries {
switch x := entry.(type) { switch x := entry.(type) {
case fs.Object: case fs.Object:
err = x.Remove() retry(t, fmt.Sprintf("removing file %q", x.Remote()), x.Remove)
if err != nil {
t.Errorf("Error removing file %q: %v", x.Remote(), err)
}
case fs.Directory: case fs.Directory:
toDelete = append(toDelete, x.Remote()) toDelete = append(toDelete, x.Remote())
} }
@ -155,12 +160,12 @@ func NewRun(t *testing.T) *Run {
return return
} }
require.NoError(t, err) require.NoError(t, err)
sort.Sort(toDelete) sort.Strings(toDelete)
for _, dir := range toDelete { for i := len(toDelete) - 1; i >= 0; i-- {
err := r.Fremote.Rmdir(dir) dir := toDelete[i]
if err != nil { retry(t, fmt.Sprintf("removing dir %q", dir), func() error {
t.Errorf("Error removing dir %q: %v", dir, err) return r.Fremote.Rmdir(dir)
} })
} }
// Check remote is empty // Check remote is empty
CheckListingWithPrecision(t, r.Fremote, []Item{}, []string{}, r.Fremote.Precision()) CheckListingWithPrecision(t, r.Fremote, []Item{}, []string{}, r.Fremote.Precision())