This repository has been archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
177 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package verify | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Eventually executes the test function until it returns an empty FailureMessage | ||
// or timeout elapses. | ||
func Eventually(timeout, interval time.Duration, fn func() FailureMessage) FailureMessage { | ||
timer := time.NewTimer(timeout) | ||
defer timer.Stop() | ||
ticker := time.NewTicker(interval) | ||
defer ticker.Stop() | ||
return EventuallyChan(timer.C, ticker.C, fn) | ||
} | ||
|
||
// EventuallyChan executes the test function until it returns an empty FailureMessage or timeout elapses. | ||
func EventuallyChan[TTimerPayload, TTickPayload any](timeout <-chan (TTimerPayload), ticker <-chan (TTickPayload), fn func() FailureMessage) FailureMessage { | ||
var err string | ||
failMsg := func(cause string) FailureMessage { | ||
return FailureMessage("function never passed, last failure message:\n" + err) | ||
} | ||
|
||
for { | ||
select { | ||
case <-timeout: | ||
return failMsg("timeout") | ||
default: | ||
} | ||
|
||
err = string(fn()) | ||
|
||
select { | ||
case <-timeout: | ||
return failMsg("timeout") | ||
default: | ||
} | ||
|
||
if err == "" { | ||
return "" | ||
} | ||
|
||
select { | ||
case <-timeout: | ||
return failMsg("timeout") | ||
case <-ticker: | ||
} | ||
} | ||
} |
Oops, something went wrong.