github.com/searKing/golang/go@v1.2.117/time/wait_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package time_test 6 7 import ( 8 "context" 9 "testing" 10 "time" 11 12 time_ "github.com/searKing/golang/go/time" 13 ) 14 15 func TestUntil(t *testing.T) { 16 ctx, cancel := context.WithCancel(context.Background()) 17 cancel() 18 time_.Until(ctx, func(ctx context.Context) { 19 t.Fatal("should not have been invoked") 20 }, 0) 21 22 ctx, cancel = context.WithCancel(context.Background()) 23 called := make(chan struct{}) 24 go func() { 25 time_.Until(ctx, func(ctx context.Context) { 26 called <- struct{}{} 27 }, 0) 28 close(called) 29 }() 30 <-called 31 cancel() 32 <-called 33 } 34 35 func TestNonSlidingUntil(t *testing.T) { 36 ctx, cancel := context.WithCancel(context.TODO()) 37 cancel() 38 time_.NonSlidingUntil(ctx, func(context.Context) { 39 t.Fatal("should not have been invoked") 40 }, 0) 41 42 ctx, cancel = context.WithCancel(context.TODO()) 43 called := make(chan struct{}) 44 go func() { 45 time_.NonSlidingUntil(ctx, func(context.Context) { 46 called <- struct{}{} 47 }, 0) 48 close(called) 49 }() 50 <-called 51 cancel() 52 <-called 53 } 54 55 func TestUntilReturnsImmediately(t *testing.T) { 56 ctx, cancel := context.WithCancel(context.TODO()) 57 58 now := time.Now() 59 time_.Until(ctx, func(ctx context.Context) { 60 cancel() 61 }, 30*time.Second) 62 if now.Add(25 * time.Second).Before(time.Now()) { 63 t.Errorf("Until did not return immediately when the stop chan was closed inside the func") 64 } 65 } 66 67 func TestJitterUntil(t *testing.T) { 68 ctx, cancel := context.WithCancel(context.TODO()) 69 cancel() 70 time_.JitterUntil(ctx, func(context.Context) { 71 t.Fatal("should not have been invoked") 72 }, true, 73 time_.WithExponentialBackOffOptionRandomizationFactor(0.5), 74 time_.WithExponentialBackOffOptionMultiplier(1), 75 time_.WithExponentialBackOffOptionInitialInterval(0)) 76 77 ctx, cancel = context.WithCancel(context.TODO()) 78 called := make(chan struct{}) 79 go func() { 80 time_.JitterUntil(ctx, func(context.Context) { 81 called <- struct{}{} 82 }, true, 83 time_.WithExponentialBackOffOptionRandomizationFactor(0.5), 84 time_.WithExponentialBackOffOptionMultiplier(1), 85 time_.WithExponentialBackOffOptionInitialInterval(0)) 86 close(called) 87 }() 88 <-called 89 cancel() 90 <-called 91 } 92 93 func TestJitterUntilReturnsImmediately(t *testing.T) { 94 ctx, cancel := context.WithCancel(context.TODO()) 95 96 now := time.Now() 97 time_.JitterUntil(ctx, func(ctx context.Context) { 98 cancel() 99 }, true, 100 time_.WithExponentialBackOffOptionRandomizationFactor(0.5), 101 time_.WithExponentialBackOffOptionMultiplier(1), 102 time_.WithExponentialBackOffOptionInitialInterval(30*time.Second)) 103 if now.Add(25 * time.Second).Before(time.Now()) { 104 t.Errorf("JitterUntil did not return immediately when the stop chan was closed inside the func") 105 } 106 }