github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/timer/x_timing_wheels_v2_others_test.go (about) 1 //go:build !windows && !linux 2 // +build !windows,!linux 3 4 package timer 5 6 import ( 7 "context" 8 "errors" 9 "testing" 10 "time" 11 12 "github.com/stretchr/testify/assert" 13 14 "github.com/benz9527/xboot/lib/hrtime" 15 "github.com/benz9527/xboot/observability" 16 ) 17 18 func TestXTimingWheelsV2_ScheduleFunc_goNativeClock_1MsInfinite(t *testing.T) { 19 hrtime.ClockInit() 20 observability.InitAppStats(context.Background(), "goNative1msInfinite") 21 ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("timeout")) 22 defer cancel() 23 tw := NewXTimingWheelsV2( 24 ctx, 25 withTimingWheelsDebugStatsInit(5), 26 WithTimingWheelsStats(), 27 WithTimingWheelTimeSource(GoNativeClock), 28 ) 29 30 delays := []time.Duration{ 31 time.Millisecond, 32 } 33 schedFn := func() Scheduler { 34 return NewInfiniteScheduler(delays...) 35 } 36 assert.NotNil(t, schedFn()) 37 loop := 20 38 tasks := make([]Task, loop) 39 for i := range loop { 40 var err error 41 tasks[i], err = tw.ScheduleFunc(schedFn, func(ctx context.Context, md JobMetadata) {}) 42 assert.NoError(t, err) 43 time.Sleep(2 * time.Millisecond) 44 } 45 46 <-ctx.Done() 47 time.Sleep(100 * time.Millisecond) 48 } 49 50 func TestXTimingWheelsV2_ScheduleFunc_goNativeClock_2MsInfinite(t *testing.T) { 51 hrtime.ClockInit() 52 observability.InitAppStats(context.Background(), "goNative2msInfinite") 53 ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("timeout")) 54 defer cancel() 55 tw := NewXTimingWheelsV2( 56 ctx, 57 WithTimingWheelsTickMs(2*time.Millisecond), 58 WithTimingWheelsSlotSize(20), 59 WithTimingWheelTimeSource(GoNativeClock), 60 withTimingWheelsDebugStatsInit(5), 61 WithTimingWheelsStats(), 62 ) 63 64 delays := []time.Duration{ 65 2 * time.Millisecond, 66 } 67 schedFn := func() Scheduler { 68 return NewInfiniteScheduler(delays...) 69 } 70 assert.NotNil(t, schedFn()) 71 loop := 20 72 tasks := make([]Task, loop) 73 for i := range loop { 74 var err error 75 tasks[i], err = tw.ScheduleFunc(schedFn, func(ctx context.Context, md JobMetadata) {}) 76 assert.NoError(t, err) 77 time.Sleep(2 * time.Millisecond) 78 } 79 80 <-ctx.Done() 81 time.Sleep(100 * time.Millisecond) 82 }