"io/ioutil"
"net/http"
"os"
- "sync"
"testing"
"time"
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)
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) {
}
}
-// 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 {