-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wait aggregator to start until data is ready
- Loading branch information
1 parent
39ae053
commit 1cf353b
Showing
4 changed files
with
95 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
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,41 @@ | ||
package condition | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
errorsentinel "bisonai.com/miko/node/pkg/error" | ||
) | ||
|
||
// can be blocking infinitely if condition is not met, use with caution | ||
func WaitForCondition(ctx context.Context, condition func() bool) { | ||
for { | ||
if condition() { | ||
return | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
default: | ||
time.Sleep(500 * time.Millisecond) | ||
} | ||
} | ||
} | ||
|
||
func WaitForConditionWithTimeout(ctx context.Context, timeout time.Duration, condition func() bool) error { | ||
for { | ||
if condition() { | ||
return nil | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-time.After(timeout): | ||
return errorsentinel.ErrConditionTimedOut | ||
default: | ||
time.Sleep(500 * time.Millisecond) | ||
} | ||
} | ||
} |
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,41 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"bisonai.com/miko/node/pkg/utils/condition" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWaitForCondition_Success(t *testing.T) { | ||
conditionMet := false | ||
|
||
testCond := func() bool { | ||
return conditionMet | ||
} | ||
|
||
go func() { | ||
time.Sleep(100 * time.Millisecond) | ||
conditionMet = true | ||
}() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) | ||
defer cancel() | ||
|
||
condition.WaitForCondition(ctx, testCond) | ||
|
||
assert.True(t, conditionMet) | ||
} | ||
|
||
func TestWaitForCondition_Timeout(t *testing.T) { | ||
testCond := func() bool { | ||
return false | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) | ||
defer cancel() | ||
|
||
condition.WaitForCondition(ctx, testCond) | ||
} |