Skip to content

Commit

Permalink
Merge pull request #31 from treid314/add-util-test
Browse files Browse the repository at this point in the history
Add util/test package
  • Loading branch information
Tyler Reid authored Sep 7, 2021
2 parents 29a5cc8 + 301d015 commit 62a1798
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/poll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package test

import (
"reflect"
"testing"
"time"
)

// Poll repeatedly calls a function until the function returns the correct response or until poll timeout.
func Poll(t testing.TB, d time.Duration, want interface{}, have func() interface{}) {
t.Helper()
deadline := time.Now().Add(d)
for {
if time.Now().After(deadline) {
break
}
if reflect.DeepEqual(want, have()) {
return
}
time.Sleep(d / 100)
}
h := have()
if !reflect.DeepEqual(want, h) {
t.Fatalf("expected %v, got %v", want, h)
}
}

0 comments on commit 62a1798

Please sign in to comment.