github.com/ethereum-optimism/optimism@v1.7.2/op-node/p2p/store/gc_test.go (about) 1 package store 2 3 import ( 4 "context" 5 "sync" 6 "testing" 7 "time" 8 9 "github.com/ethereum-optimism/optimism/op-service/clock" 10 "github.com/ethereum-optimism/optimism/op-service/testlog" 11 "github.com/ethereum/go-ethereum/log" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestScheduleGcPeriodically(t *testing.T) { 16 var bgTasks sync.WaitGroup 17 ctx, cancel := context.WithCancel(context.Background()) 18 defer func() { 19 cancel() 20 // Wait for the gc background process to complete after cancelling the context 21 bgTasks.Wait() 22 }() 23 logger := testlog.Logger(t, log.LevelInfo) 24 clock := clock.NewDeterministicClock(time.UnixMilli(5000)) 25 26 called := make(chan struct{}, 10) 27 action := func() error { 28 called <- struct{}{} 29 return nil 30 } 31 waitForGc := func(failMsg string) { 32 timeout, cancel := context.WithTimeout(ctx, 10*time.Second) 33 defer cancel() 34 select { 35 case <-timeout.Done(): 36 t.Fatal(failMsg) 37 case <-called: 38 require.Len(t, called, 0, "should only run once after gc period") 39 } 40 } 41 startGc(ctx, logger, clock, &bgTasks, action) 42 timeout, tCancel := context.WithTimeout(ctx, 10*time.Second) 43 defer tCancel() 44 require.True(t, clock.WaitForNewPendingTask(timeout), "did not schedule pending GC") 45 46 require.Len(t, called, 0, "should not run immediately") 47 48 clock.AdvanceTime(gcPeriod) 49 waitForGc("should run gc after first time period") 50 51 clock.AdvanceTime(gcPeriod) 52 waitForGc("should run gc again after second time period") 53 }