github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/timer/x_timing_wheels_v2_linux_test.go (about) 1 //go:build linux 2 // +build 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 } 83 84 func TestXTimingWheelsV2_ScheduleFunc_unixClock_1MsInfinite(t *testing.T) { 85 hrtime.ClockInit() 86 observability.InitAppStats(context.Background(), "unix1msInfinite") 87 ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("timeout")) 88 defer cancel() 89 tw := NewXTimingWheelsV2( 90 ctx, 91 WithTimingWheelTimeSource(UnixClock), 92 WithTimingWheelsStats(), 93 withTimingWheelsDebugStatsInit(5), 94 ) 95 96 delays := []time.Duration{ 97 time.Millisecond, 98 } 99 schedFn := func() Scheduler { 100 return NewInfiniteScheduler(delays...) 101 } 102 assert.NotNil(t, schedFn()) 103 loop := 20 104 tasks := make([]Task, loop) 105 for i := range loop { 106 var err error 107 tasks[i], err = tw.ScheduleFunc(schedFn, func(ctx context.Context, md JobMetadata) {}) 108 assert.NoError(t, err) 109 time.Sleep(2 * time.Millisecond) 110 } 111 112 <-ctx.Done() 113 time.Sleep(100 * time.Millisecond) 114 } 115 116 func TestXTimingWheelsV2_ScheduleFunc_unixClock_2MsInfinite(t *testing.T) { 117 hrtime.ClockInit() 118 observability.InitAppStats(context.Background(), "unix2msInfinite") 119 ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("timeout")) 120 defer cancel() 121 tw := NewXTimingWheelsV2( 122 ctx, 123 WithTimingWheelsTickMs(2*time.Millisecond), 124 WithTimingWheelsSlotSize(20), 125 WithTimingWheelTimeSource(UnixClock), 126 withTimingWheelsDebugStatsInit(5), 127 WithTimingWheelsStats(), 128 ) 129 130 delays := []time.Duration{ 131 2 * time.Millisecond, 132 } 133 schedFn := func() Scheduler { 134 return NewInfiniteScheduler(delays...) 135 } 136 assert.NotNil(t, schedFn()) 137 loop := 20 138 tasks := make([]Task, loop) 139 for i := range loop { 140 var err error 141 tasks[i], err = tw.ScheduleFunc(schedFn, func(ctx context.Context, md JobMetadata) {}) 142 assert.NoError(t, err) 143 time.Sleep(2 * time.Millisecond) 144 } 145 146 <-ctx.Done() 147 time.Sleep(100 * time.Millisecond) 148 }