-
-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from GaruGaru/master
Multiple wait strategy
- Loading branch information
Showing
2 changed files
with
124 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package wait | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// Implement interface | ||
var _ Strategy = (*MultiStrategy)(nil) | ||
|
||
type MultiStrategy struct { | ||
// all Strategies should have a startupTimeout to avoid waiting infinitely | ||
startupTimeout time.Duration | ||
|
||
// additional properties | ||
Strategies []Strategy | ||
} | ||
|
||
func (ms *MultiStrategy) WithStartupTimeout(startupTimeout time.Duration) *MultiStrategy { | ||
ms.startupTimeout = startupTimeout | ||
return ms | ||
} | ||
|
||
func ForAll(strategies ...Strategy) *MultiStrategy { | ||
return &MultiStrategy{ | ||
startupTimeout: defaultStartupTimeout(), | ||
Strategies: strategies, | ||
} | ||
} | ||
|
||
func (ms *MultiStrategy) WaitUntilReady(ctx context.Context, target StrategyTarget) (err error) { | ||
ctx, cancelContext := context.WithTimeout(ctx, ms.startupTimeout) | ||
defer cancelContext() | ||
|
||
if len(ms.Strategies) == 0 { | ||
return fmt.Errorf("no wait strategy supplied") | ||
} | ||
|
||
for _, strategy := range ms.Strategies { | ||
err := strategy.WaitUntilReady(ctx, target) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |