diff options
author | Fabricio Voznika <fvoznika@google.com> | 2018-07-25 17:36:52 -0700 |
---|---|---|
committer | Shentubot <shentubot@google.com> | 2018-07-25 17:37:53 -0700 |
commit | e5adf42f66a3090f6124bceb5487238bf7526302 (patch) | |
tree | b6cc931ea9cf1d565dd7c79f7b16ae97deeb7df0 /runsc/container/container_test.go | |
parent | 7cd9405b9cc112ebe352af0e5f13b7b57628001b (diff) |
Replace sleeps with waits in tests - part I
PiperOrigin-RevId: 206084473
Change-Id: I44e1b64b9cdd2964357799dca27cc0cbc19ce07d
Diffstat (limited to 'runsc/container/container_test.go')
-rw-r--r-- | runsc/container/container_test.go | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/runsc/container/container_test.go b/runsc/container/container_test.go index 34febe038..50f038450 100644 --- a/runsc/container/container_test.go +++ b/runsc/container/container_test.go @@ -122,18 +122,24 @@ func createWriteableOutputFile(path string) (*os.File, error) { return outputFile, nil } -func readOutputNum(f *os.File, first bool) (int, error) { - var num int - time.Sleep(1 * time.Second) - - // Check that f exists and contains counting data. - fileInfo, err := f.Stat() - if err != nil { - return 0, fmt.Errorf("error creating output file: %v", err) +func waitForFile(f *os.File) error { + op := func() error { + fi, err := f.Stat() + if err != nil { + return err + } + if fi.Size() == 0 { + return fmt.Errorf("file %q is empty", f.Name()) + } + return nil } + return testutil.Poll(op, 5*time.Second) +} - if fileInfo.Size() == 0 { - return 0, fmt.Errorf("failed to write to file, file still appears empty") +func readOutputNum(f *os.File, first bool) (int, error) { + // Wait until file has contents. + if err := waitForFile(f); err != nil { + return 0, err } // Read the first number in the new file @@ -147,6 +153,7 @@ func readOutputNum(f *os.File, first bool) (int, error) { nums := strings.Split(string(b), "\n") + var num int if first { num, err = strconv.Atoi(nums[0]) } else { @@ -579,7 +586,10 @@ func TestCheckpointRestore(t *testing.T) { } defer file.Close() - time.Sleep(1 * time.Second) + // Wait until application has ran. + if err := waitForFile(outputFile); err != nil { + t.Fatalf("Failed to wait for output file: %v", err) + } // Checkpoint running container; save state into new file. if err := cont.Checkpoint(file); err != nil { @@ -727,7 +737,7 @@ func TestPauseResume(t *testing.T) { t.Errorf("container status got %v, want %v", got, want) } - time.Sleep(10 * time.Second) + time.Sleep(6 * time.Second) // Verify that the two processes still exist. Sleep 5 is paused so // it should still be in the process list after 10 seconds. |