Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add WaitForWithContext #480

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: WaitFor on first condition
duration must be non-zero if first conditions is true
  • Loading branch information
ccoVeille committed Jul 2, 2024
commit 071a746f6331fe338a895f47b98173e9445292f8
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -3068,9 +3068,9 @@ laterTrue := func(i int) bool {
return i > 5
}

iterations, duration, ok := lo.WaitFor(alwaysTrue, 10*time.Millisecond, time.Millisecond)
iterations, duration, ok := lo.WaitFor(alwaysTrue, 10*time.Millisecond, 2 * time.Millisecond)
// 1
// 0ms
// 1ms
// true

iterations, duration, ok := lo.WaitFor(alwaysFalse, 10*time.Millisecond, time.Millisecond)
6 changes: 1 addition & 5 deletions concurrency.go
Original file line number Diff line number Diff line change
@@ -99,10 +99,6 @@ func Async6[A, B, C, D, E, F any](f func() (A, B, C, D, E, F)) <-chan Tuple6[A,

// WaitFor runs periodically until a condition is validated.
func WaitFor(condition func(i int) bool, maxDuration time.Duration, tick time.Duration) (int, time.Duration, bool) {
ccoVeille marked this conversation as resolved.
Show resolved Hide resolved
if condition(0) {
return 1, 0, true
}

start := time.Now()

timer := time.NewTimer(maxDuration)
@@ -113,7 +109,7 @@ func WaitFor(condition func(i int) bool, maxDuration time.Duration, tick time.Du
ticker.Stop()
}()

i := 1
i := 0

for {
select {
6 changes: 3 additions & 3 deletions concurrency_test.go
Original file line number Diff line number Diff line change
@@ -265,7 +265,7 @@ func TestWaitFor(t *testing.T) {
is := assert.New(t)

iter, duration, ok := WaitFor(alwaysFalse, shortTimeout, 10*time.Millisecond)
is.Equal(1, iter, "unexpected iteration count")
is.Equal(0, iter, "unexpected iteration count")
is.InEpsilon(10*time.Millisecond, duration, float64(500*time.Microsecond))
is.False(ok)
})
@@ -278,7 +278,7 @@ func TestWaitFor(t *testing.T) {

shortTimeout := 4 * time.Millisecond
iter, duration, ok := WaitFor(alwaysFalse, shortTimeout, 10*time.Millisecond)
is.Equal(1, iter, "unexpected iteration count")
is.Equal(0, iter, "unexpected iteration count")
is.InEpsilon(10*time.Millisecond, duration, float64(500*time.Microsecond))
is.False(ok)
})
@@ -291,7 +291,7 @@ func TestWaitFor(t *testing.T) {

iter, duration, ok := WaitFor(alwaysTrue, 10*time.Millisecond, time.Millisecond)
is.Equal(1, iter, "unexpected iteration count")
is.Zero(duration)
is.InEpsilon(time.Millisecond, duration, float64(5*time.Microsecond))
is.True(ok)
})
}