Commit 007e1a2d authored by OptimismBot's avatar OptimismBot Committed by GitHub

Merge pull request #7457 from ethereum-optimism/aj/fast-wait

op-e2e: Modify wait to check before waiting
parents 0b492be0 78e681b6
...@@ -58,22 +58,27 @@ func TestMonitorGames(t *testing.T) { ...@@ -58,22 +58,27 @@ func TestMonitorGames(t *testing.T) {
go func() { go func() {
headerNotSent := true headerNotSent := true
waitErr := wait.For(context.Background(), 100*time.Millisecond, func() (bool, error) { for {
if len(sched.scheduled) >= 1 { if len(sched.scheduled) >= 1 {
return true, nil break
} }
if mockHeadSource.sub == nil { if mockHeadSource.sub == nil {
return false, nil continue
} }
if headerNotSent { if headerNotSent {
mockHeadSource.sub.headers <- &ethtypes.Header{ select {
case mockHeadSource.sub.headers <- &ethtypes.Header{
Number: big.NewInt(1), Number: big.NewInt(1),
}:
headerNotSent = false
case <-ctx.Done():
break
default:
} }
headerNotSent = false
} }
return false, nil // Just to avoid a tight loop
}) time.Sleep(100 * time.Millisecond)
require.NoError(t, waitErr) }
mockHeadSource.err = fmt.Errorf("eth subscribe test error") mockHeadSource.err = fmt.Errorf("eth subscribe test error")
cancel() cancel()
}() }()
...@@ -94,27 +99,29 @@ func TestMonitorGames(t *testing.T) { ...@@ -94,27 +99,29 @@ func TestMonitorGames(t *testing.T) {
defer cancel() defer cancel()
go func() { go func() {
headerNotSent := true
waitErr := wait.For(context.Background(), 100*time.Millisecond, func() (bool, error) { waitErr := wait.For(context.Background(), 100*time.Millisecond, func() (bool, error) {
return mockHeadSource.sub != nil, nil return mockHeadSource.sub != nil, nil
}) })
require.NoError(t, waitErr) require.NoError(t, waitErr)
mockHeadSource.sub.errChan <- fmt.Errorf("test error") mockHeadSource.sub.errChan <- fmt.Errorf("test error")
waitErr = wait.For(context.Background(), 100*time.Millisecond, func() (bool, error) { for {
if len(sched.scheduled) >= 1 { if len(sched.scheduled) >= 1 {
return true, nil break
} }
if mockHeadSource.sub == nil { if mockHeadSource.sub == nil {
return false, nil continue
} }
if headerNotSent { select {
mockHeadSource.sub.headers <- &ethtypes.Header{ case mockHeadSource.sub.headers <- &ethtypes.Header{
Number: big.NewInt(1), Number: big.NewInt(1),
} }:
headerNotSent = false case <-ctx.Done():
break
default:
} }
return false, nil // Just to avoid a tight loop
}) time.Sleep(100 * time.Millisecond)
}
require.NoError(t, waitErr) require.NoError(t, waitErr)
mockHeadSource.err = fmt.Errorf("eth subscribe test error") mockHeadSource.err = fmt.Errorf("eth subscribe test error")
cancel() cancel()
...@@ -122,7 +129,7 @@ func TestMonitorGames(t *testing.T) { ...@@ -122,7 +129,7 @@ func TestMonitorGames(t *testing.T) {
err := monitor.MonitorGames(ctx) err := monitor.MonitorGames(ctx)
require.NoError(t, err) require.NoError(t, err)
require.Len(t, sched.scheduled, 1) require.NotEmpty(t, sched.scheduled) // We might get more than one update scheduled.
require.Equal(t, []common.Address{addr1, addr2}, sched.scheduled[0]) require.Equal(t, []common.Address{addr1, addr2}, sched.scheduled[0])
}) })
} }
......
...@@ -197,7 +197,7 @@ func (h *Helper) WaitForGameDataDeletion(ctx context.Context, games ...GameAddr) ...@@ -197,7 +197,7 @@ func (h *Helper) WaitForGameDataDeletion(ctx context.Context, games ...GameAddr)
if err != nil { if err != nil {
return false, fmt.Errorf("failed to check dir %v is deleted: %w", dir, err) return false, fmt.Errorf("failed to check dir %v is deleted: %w", dir, err)
} }
h.t.Errorf("Game data directory %v not yet deleted", dir) h.t.Logf("Game data directory %v not yet deleted", dir)
return false, nil return false, nil
} }
return true, nil return true, nil
......
...@@ -111,17 +111,20 @@ func For(ctx context.Context, rate time.Duration, cb func() (bool, error)) error ...@@ -111,17 +111,20 @@ func For(ctx context.Context, rate time.Duration, cb func() (bool, error)) error
defer tick.Stop() defer tick.Stop()
for { for {
// Perform the first check before any waiting.
done, err := cb()
if err != nil {
return err
}
if done {
return nil
}
select { select {
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return ctx.Err()
case <-tick.C: case <-tick.C:
done, err := cb() // Allow loop to continue for next retry
if err != nil {
return err
}
if done {
return nil
}
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment