github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/timer/x_timing_wheels_v1_windows_test.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package timer
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"runtime"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"go.uber.org/automaxprocs/maxprocs"
    15  
    16  	"github.com/benz9527/xboot/lib/hrtime"
    17  	"github.com/benz9527/xboot/observability"
    18  )
    19  
    20  func TestXTimingWheels_ScheduleFunc_windowsClock_1MsInfinite(t *testing.T) {
    21  	_, _ = maxprocs.Set(maxprocs.Min(4), maxprocs.Logger(t.Logf))
    22  	hrtime.ClockInit()
    23  	observability.InitAppStats(context.Background(), "window1msInfinite")
    24  	defer func() {
    25  		_ = hrtime.ResetTimeResolutionFrom1ms()
    26  	}()
    27  	ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("timeout"))
    28  	defer cancel()
    29  	tw := NewXTimingWheels(
    30  		ctx,
    31  		WithTimingWheelTimeSource(WindowsClock),
    32  		WithTimingWheelsStats(),
    33  		withTimingWheelsDebugStatsInit(5),
    34  	)
    35  
    36  	delays := []time.Duration{
    37  		time.Millisecond,
    38  	}
    39  	schedFn := func() Scheduler {
    40  		return NewInfiniteScheduler(delays...)
    41  	}
    42  	assert.NotNil(t, schedFn())
    43  	loop := 20
    44  	tasks := make([]Task, loop)
    45  	for i := range loop {
    46  		var err error
    47  		tasks[i], err = tw.ScheduleFunc(schedFn, func(ctx context.Context, md JobMetadata) {})
    48  		assert.NoError(t, err)
    49  		time.Sleep(2 * time.Millisecond)
    50  	}
    51  
    52  	<-ctx.Done()
    53  	time.Sleep(100 * time.Millisecond)
    54  }
    55  
    56  func TestXTimingWheels_ScheduleFunc_windowsClock_1MsInfinite_4Procs(t *testing.T) {
    57  	runtime.GOMAXPROCS(4)
    58  	hrtime.ClockInit()
    59  	observability.InitAppStats(context.Background(), "window1msInfinite_4procs")
    60  	defer func() {
    61  		_ = hrtime.ResetTimeResolutionFrom1ms()
    62  	}()
    63  	ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("timeout"))
    64  	defer cancel()
    65  	tw := NewXTimingWheels(
    66  		ctx,
    67  		WithTimingWheelTimeSource(WindowsClock),
    68  		WithTimingWheelsStats(),
    69  		withTimingWheelsDebugStatsInit(5),
    70  	)
    71  
    72  	delays := []time.Duration{
    73  		time.Millisecond,
    74  	}
    75  	schedFn := func() Scheduler {
    76  		return NewInfiniteScheduler(delays...)
    77  	}
    78  	assert.NotNil(t, schedFn())
    79  	loop := 20
    80  	tasks := make([]Task, loop)
    81  	for i := range loop {
    82  		var err error
    83  		tasks[i], err = tw.ScheduleFunc(schedFn, func(ctx context.Context, md JobMetadata) {})
    84  		assert.NoError(t, err)
    85  		time.Sleep(2 * time.Millisecond)
    86  	}
    87  
    88  	<-ctx.Done()
    89  	time.Sleep(100 * time.Millisecond)
    90  }
    91  
    92  func TestXTimingWheels_ScheduleFunc_windowsClock_2MsInfinite(t *testing.T) {
    93  	_, _ = maxprocs.Set(maxprocs.Min(4), maxprocs.Logger(t.Logf))
    94  	hrtime.ClockInit()
    95  	observability.InitAppStats(context.Background(), "window2msInfinite")
    96  	ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("timeout"))
    97  	defer cancel()
    98  	tw := NewXTimingWheels(
    99  		ctx,
   100  		WithTimingWheelsTickMs(2*time.Millisecond),
   101  		WithTimingWheelsSlotSize(20),
   102  		WithTimingWheelTimeSource(WindowsClock),
   103  		withTimingWheelsDebugStatsInit(5),
   104  		WithTimingWheelsStats(),
   105  	)
   106  
   107  	delays := []time.Duration{
   108  		2 * time.Millisecond,
   109  	}
   110  	schedFn := func() Scheduler {
   111  		return NewInfiniteScheduler(delays...)
   112  	}
   113  	assert.NotNil(t, schedFn())
   114  	loop := 20
   115  	tasks := make([]Task, loop)
   116  	for i := range loop {
   117  		var err error
   118  		tasks[i], err = tw.ScheduleFunc(schedFn, func(ctx context.Context, md JobMetadata) {})
   119  		assert.NoError(t, err)
   120  		time.Sleep(2 * time.Millisecond)
   121  	}
   122  
   123  	<-ctx.Done()
   124  	time.Sleep(100 * time.Millisecond)
   125  }