From: PatrikBuhr Date: Thu, 24 Mar 2022 09:34:22 +0000 (+0100) Subject: Fetch of authorization token X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=c185c0fd344f75ca380204af3080a921fc214f91;p=nonrtric.git Fetch of authorization token Minor change in unittest. Signed-off-by: PatrikBuhr Issue-ID: NONRTRIC-735 Change-Id: Ia961f4caa9a17852309af92f84226b8d7aefd40a --- 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 {