X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=auth-token-fetch%2Fmain_test.go;fp=auth-token-fetch%2Fmain_test.go;h=cceff070e7e2d8e8c6240085d3fa7abf543efbdd;hb=c185c0fd344f75ca380204af3080a921fc214f91;hp=b0b6862d6f4ce6089df22f4d8825f267d23cb895;hpb=a2042d0be66816154768f896817bc690b731f017;p=nonrtric.git diff --git a/auth-token-fetch/main_test.go b/auth-token-fetch/main_test.go index b0b6862d..cceff070 100644 --- a/auth-token-fetch/main_test.go +++ b/auth-token-fetch/main_test.go @@ -28,7 +28,6 @@ import ( "io/ioutil" "net/http" "os" - "sync" "testing" "time" @@ -82,7 +81,7 @@ func TestFetchAndStoreToken(t *testing.T) { go periodicRefreshIwtToken(clientMock, context) - waitForFile(configuration.AuthTokenOutputFileName, 30, t) + await(func() bool { return fileExists(configuration.AuthTokenOutputFileName) }, t) tokenFileContent, err := ioutil.ReadFile(configuration.AuthTokenOutputFileName) check(err) @@ -92,16 +91,24 @@ func TestFetchAndStoreToken(t *testing.T) { context.Running = false } -func waitForFile(fileName string, maxTimeSeconds int, t *testing.T) bool { - for i := 1; i < maxTimeSeconds; i++ { - if _, err := os.Stat(fileName); err == nil { - return true +func fileExists(fileName string) bool { + if _, err := os.Stat(fileName); err == nil { + return true + } + log.Debug("Waiting for file: " + fileName) + return false +} + +func await(predicate func() bool, t *testing.T) { + MAX_TIME_SECONDS := 30 + for i := 1; i < MAX_TIME_SECONDS; i++ { + if predicate() { + return } time.Sleep(time.Second) } - t.Error("File not created: " + fileName) + t.Error("Predicate not fulfilled") t.Fail() - return false } func TestStart(t *testing.T) { @@ -145,22 +152,6 @@ func NewTestClient(fn RoundTripFunc) *http.Client { } } -// waitTimeout waits for the waitgroup for the specified max timeout. -// Returns true if waiting timed out. -func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool { - c := make(chan struct{}) - go func() { - defer close(c) - wg.Wait() - }() - select { - case <-c: - return false // completed normally - case <-time.After(timeout): - return true // timed out - } -} - func getBodyAsString(req *http.Request, t *testing.T) string { buf := new(bytes.Buffer) if _, err := buf.ReadFrom(req.Body); err != nil {