Commit 249b0531 authored by Igor Drozdov's avatar Igor Drozdov

Use require.Eventually instead of manual polling in tests

It refactors the test and also uses smaller tick intervals
to speed up the tests a bit
parent 4ab4b93d
......@@ -284,12 +284,9 @@ func TestUploadHandlerMultipartUploadSizeLimit(t *testing.T) {
contentBuffer, contentType := createTestMultipartForm(t, make([]byte, uploadSize))
response := testUploadArtifacts(t, contentType, ts.URL+Path, &contentBuffer)
require.Equal(t, http.StatusRequestEntityTooLarge, response.Code)
// Poll because AbortMultipartUpload is async
for i := 0; os.IsMultipartUpload(test.ObjectPath) && i < 100; i++ {
time.Sleep(10 * time.Millisecond)
}
require.False(t, os.IsMultipartUpload(test.ObjectPath), "MultipartUpload should not be in progress anymore")
require.Eventually(t, func() bool {
return !os.IsMultipartUpload(test.ObjectPath)
}, time.Second, time.Millisecond, "MultipartUpload should not be in progress anymore")
require.Empty(t, os.GetObjectMD5(test.ObjectPath), "upload should have failed, so the object should not exists")
}
......
......@@ -28,29 +28,15 @@ func testDeadline() time.Time {
func requireFileGetsRemovedAsync(t *testing.T, filePath string) {
var err error
// Poll because the file removal is async
for i := 0; i < 100; i++ {
require.Eventually(t, func() bool {
_, err = os.Stat(filePath)
if err != nil {
break
}
time.Sleep(100 * time.Millisecond)
}
return err != nil
}, 10*time.Second, 10*time.Millisecond)
require.True(t, os.IsNotExist(err), "File hasn't been deleted during cleanup")
}
func requireObjectStoreDeletedAsync(t *testing.T, expectedDeletes int, osStub *test.ObjectstoreStub) {
// Poll because the object removal is async
for i := 0; i < 100; i++ {
if osStub.DeletesCnt() == expectedDeletes {
break
}
time.Sleep(10 * time.Millisecond)
}
require.Equal(t, expectedDeletes, osStub.DeletesCnt(), "Object not deleted")
require.Eventually(t, func() bool { return osStub.DeletesCnt() == expectedDeletes }, time.Second, time.Millisecond, "Object not deleted")
}
func TestSaveFileWrongSize(t *testing.T) {
......
......@@ -56,13 +56,7 @@ func testObjectUploadNoErrors(t *testing.T, startObjectStore osFactory, useDelet
if useDeleteURL {
expectedDeleteCnt = 1
}
// Poll because the object removal is async
for i := 0; i < 100; i++ {
if osStub.DeletesCnt() == expectedDeleteCnt {
break
}
time.Sleep(10 * time.Millisecond)
}
require.Eventually(t, func() bool { return osStub.DeletesCnt() == expectedDeleteCnt }, time.Second, time.Millisecond)
if useDeleteURL {
require.Equal(t, 1, osStub.DeletesCnt(), "Object hasn't been deleted")
......
......@@ -180,11 +180,7 @@ func TestShutdown(t *testing.T) {
}()
go func() {
for countWatchers(runnerKey) == 0 {
time.Sleep(time.Millisecond)
}
require.Equal(t, 1, countWatchers(runnerKey))
require.Eventually(t, func() bool { return countWatchers(runnerKey) == 1 }, 10*time.Second, time.Millisecond)
Shutdown()
wg.Done()
......@@ -192,11 +188,7 @@ func TestShutdown(t *testing.T) {
wg.Wait()
for countWatchers(runnerKey) == 1 {
time.Sleep(time.Millisecond)
}
require.Equal(t, 0, countWatchers(runnerKey))
require.Eventually(t, func() bool { return countWatchers(runnerKey) == 0 }, 10*time.Second, time.Millisecond)
// Adding a key after the shutdown should result in an immediate response
var val WatchKeyStatus
......
......@@ -581,15 +581,9 @@ func newProxy(url string) *proxy.Proxy {
func waitUntilDeleted(t *testing.T, path string) {
var err error
// Poll because the file removal is async
for i := 0; i < 100; i++ {
require.Eventually(t, func() bool {
_, err = os.Stat(path)
if err != nil {
break
}
time.Sleep(100 * time.Millisecond)
}
return err != nil
}, 10*time.Second, 10*time.Millisecond)
require.True(t, os.IsNotExist(err), "expected the file to be deleted")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment