lib/model: Introduce waitForState test utility (#5585)

* lib/model: Introduce waitForState test utility

* folder id as param to waitForState
This commit is contained in:
Simon Frei 2019-03-09 11:36:55 +01:00 committed by Audrius Butkevicius
parent b1564e53e4
commit 27ff20faa3

View File

@ -1768,6 +1768,23 @@ func TestIgnores(t *testing.T) {
changeIgnores(t, m, []string{}) changeIgnores(t, m, []string{})
} }
func waitForState(t *testing.T, m *model, folder, status string) {
t.Helper()
timeout := time.Now().Add(2 * time.Second)
var err error
for !time.Now().After(timeout) {
_, _, err = m.State(folder)
if err == nil && status == "" {
return
}
if err != nil && err.Error() == status {
return
}
time.Sleep(10 * time.Millisecond)
}
t.Fatalf("Timed out waiting for status: %s, current status: %v", status, err)
}
func TestROScanRecovery(t *testing.T) { func TestROScanRecovery(t *testing.T) {
testOs := &fatalOs{t} testOs := &fatalOs{t}
@ -1802,57 +1819,24 @@ func TestROScanRecovery(t *testing.T) {
m.ServeBackground() m.ServeBackground()
defer m.Stop() defer m.Stop()
waitFor := func(status string) error { waitForState(t, m, "default", "folder path missing")
timeout := time.Now().Add(2 * time.Second)
for {
_, _, err := m.State("default")
if err == nil && status == "" {
return nil
}
if err != nil && err.Error() == status {
return nil
}
if time.Now().After(timeout) {
return fmt.Errorf("Timed out waiting for status: %s, current status: %v", status, err)
}
time.Sleep(10 * time.Millisecond)
}
}
if err := waitFor("folder path missing"); err != nil {
t.Error(err)
return
}
testOs.Mkdir(fcfg.Path, 0700) testOs.Mkdir(fcfg.Path, 0700)
if err := waitFor("folder marker missing"); err != nil { waitForState(t, m, "default", "folder marker missing")
t.Error(err)
return
}
fd := testOs.Create(filepath.Join(fcfg.Path, config.DefaultMarkerName)) fd := testOs.Create(filepath.Join(fcfg.Path, config.DefaultMarkerName))
fd.Close() fd.Close()
if err := waitFor(""); err != nil { waitForState(t, m, "default", "")
t.Error(err)
return
}
testOs.Remove(filepath.Join(fcfg.Path, config.DefaultMarkerName)) testOs.Remove(filepath.Join(fcfg.Path, config.DefaultMarkerName))
if err := waitFor("folder marker missing"); err != nil { waitForState(t, m, "default", "folder marker missing")
t.Error(err)
return
}
testOs.Remove(fcfg.Path) testOs.Remove(fcfg.Path)
if err := waitFor("folder path missing"); err != nil { waitForState(t, m, "default", "folder path missing")
t.Error(err)
return
}
} }
func TestRWScanRecovery(t *testing.T) { func TestRWScanRecovery(t *testing.T) {
@ -1889,54 +1873,24 @@ func TestRWScanRecovery(t *testing.T) {
m.ServeBackground() m.ServeBackground()
defer m.Stop() defer m.Stop()
waitFor := func(status string) error { waitForState(t, m, "default", "folder path missing")
timeout := time.Now().Add(2 * time.Second)
for {
_, _, err := m.State("default")
if err == nil && status == "" {
return nil
}
if err != nil && err.Error() == status {
return nil
}
if time.Now().After(timeout) {
return fmt.Errorf("Timed out waiting for status: %s, current status: %v", status, err)
}
time.Sleep(10 * time.Millisecond)
}
}
if err := waitFor("folder path missing"); err != nil {
t.Error(err)
return
}
testOs.Mkdir(fcfg.Path, 0700) testOs.Mkdir(fcfg.Path, 0700)
if err := waitFor("folder marker missing"); err != nil { waitForState(t, m, "default", "folder marker missing")
t.Error(err)
return
}
fd := testOs.Create(filepath.Join(fcfg.Path, config.DefaultMarkerName)) fd := testOs.Create(filepath.Join(fcfg.Path, config.DefaultMarkerName))
fd.Close() fd.Close()
if err := waitFor(""); err != nil { waitForState(t, m, "default", "")
t.Fatal(err)
}
testOs.Remove(filepath.Join(fcfg.Path, config.DefaultMarkerName)) testOs.Remove(filepath.Join(fcfg.Path, config.DefaultMarkerName))
if err := waitFor("folder marker missing"); err != nil { waitForState(t, m, "default", "folder marker missing")
t.Fatal(err)
}
testOs.Remove(fcfg.Path) testOs.Remove(fcfg.Path)
if err := waitFor("folder path missing"); err != nil { waitForState(t, m, "default", "folder path missing")
t.Fatal(err)
}
} }
func TestGlobalDirectoryTree(t *testing.T) { func TestGlobalDirectoryTree(t *testing.T) {
@ -3058,35 +3012,13 @@ func TestCustomMarkerName(t *testing.T) {
m.ServeBackground() m.ServeBackground()
defer m.Stop() defer m.Stop()
waitFor := func(status string) error { waitForState(t, m, "default", "folder path missing")
timeout := time.Now().Add(2 * time.Second)
for {
_, _, err := m.State("default")
if err == nil && status == "" {
return nil
}
if err != nil && err.Error() == status {
return nil
}
if time.Now().After(timeout) {
return fmt.Errorf("Timed out waiting for status: %s, current status: %v", status, err)
}
time.Sleep(10 * time.Millisecond)
}
}
if err := waitFor("folder path missing"); err != nil {
t.Fatal(err)
}
testOs.Mkdir(fcfg.Path, 0700) testOs.Mkdir(fcfg.Path, 0700)
fd := testOs.Create(filepath.Join(fcfg.Path, "myfile")) fd := testOs.Create(filepath.Join(fcfg.Path, "myfile"))
fd.Close() fd.Close()
if err := waitFor(""); err != nil { waitForState(t, m, "default", "")
t.Fatal(err)
}
} }
func TestRemoveDirWithContent(t *testing.T) { func TestRemoveDirWithContent(t *testing.T) {