Skip to content

Commit 08ff7de

Browse files
authored
core: fixed flakey reorg test (#3794)
Fixed flakey `TestHandleChainReorgEvent`. category: fixbuild ticket: none
1 parent 7a2971e commit 08ff7de

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

core/scheduler/scheduler_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"context"
77
"flag"
88
"os"
9+
"runtime"
910
"sort"
1011
"sync"
12+
"sync/atomic"
1113
"testing"
1214
"time"
1315

@@ -411,8 +413,6 @@ func TestNoActive(t *testing.T) {
411413
}
412414

413415
func TestHandleChainReorgEvent(t *testing.T) {
414-
t.Skip("Known flakey test, to be fixed later")
415-
416416
var (
417417
t0 time.Time
418418
valSet = beaconmock.ValidatorSetA
@@ -453,11 +453,15 @@ func TestHandleChainReorgEvent(t *testing.T) {
453453
}()
454454

455455
for slot := range schedSlotCh {
456+
clock.Pause()
457+
456458
switch slot.Slot {
457459
case 1: // epoch 0
458460
_, err := sched.GetDutyDefinition(t.Context(), core.NewAttesterDuty(1))
459461
require.NoError(t, err)
460462
case 5: // epoch 1
463+
_, err := sched.GetDutyDefinition(t.Context(), core.NewAttesterDuty(5))
464+
require.NoError(t, err)
461465
sched.HandleChainReorgEvent(t.Context(), 0)
462466
_, err = sched.GetDutyDefinition(t.Context(), core.NewAttesterDuty(5))
463467
require.ErrorContains(t, err, "epoch not resolved yet")
@@ -466,6 +470,8 @@ func TestHandleChainReorgEvent(t *testing.T) {
466470
require.NoError(t, err)
467471
sched.Stop()
468472
}
473+
474+
clock.Resume()
469475
}
470476

471477
require.NoError(t, <-doneCh)
@@ -515,6 +521,7 @@ type testClock struct {
515521
nowMutex sync.Mutex
516522
now time.Time
517523
callbacks map[time.Time]func()
524+
paused atomic.Bool
518525
}
519526

520527
// CallbackAfter sets a callback function that is called once
@@ -534,6 +541,10 @@ func (c *testClock) After(d time.Duration) <-chan time.Time {
534541
}
535542

536543
func (c *testClock) Sleep(d time.Duration) {
544+
for c.paused.Load() {
545+
runtime.Gosched()
546+
}
547+
537548
c.nowMutex.Lock()
538549
defer c.nowMutex.Unlock()
539550

@@ -566,6 +577,14 @@ func (c *testClock) Since(t time.Time) time.Duration {
566577
return since
567578
}
568579

580+
func (c *testClock) Pause() {
581+
c.paused.Store(true)
582+
}
583+
584+
func (c *testClock) Resume() {
585+
c.paused.Store(false)
586+
}
587+
569588
func (c *testClock) NewTicker(time.Duration) clockwork.Ticker {
570589
panic("not supported")
571590
}

0 commit comments

Comments
 (0)