github.com/igoogolx/clash@v1.19.8/common/picker/picker_test.go (about) 1 package picker 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func sleepAndSend(ctx context.Context, delay int, input any) func() (any, error) { 12 return func() (any, error) { 13 timer := time.NewTimer(time.Millisecond * time.Duration(delay)) 14 select { 15 case <-timer.C: 16 return input, nil 17 case <-ctx.Done(): 18 return nil, ctx.Err() 19 } 20 } 21 } 22 23 func TestPicker_Basic(t *testing.T) { 24 picker, ctx := WithContext(context.Background()) 25 picker.Go(sleepAndSend(ctx, 30, 2)) 26 picker.Go(sleepAndSend(ctx, 20, 1)) 27 28 number := picker.Wait() 29 assert.NotNil(t, number) 30 assert.Equal(t, number.(int), 1) 31 } 32 33 func TestPicker_Timeout(t *testing.T) { 34 picker, ctx := WithTimeout(context.Background(), time.Millisecond*5) 35 picker.Go(sleepAndSend(ctx, 20, 1)) 36 37 number := picker.Wait() 38 assert.Nil(t, number) 39 assert.NotNil(t, picker.Error()) 40 }