github.com/wfusion/gofusion@v1.1.14/test/common/utils/cases/time_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/wfusion/gofusion/common/utils"
    11  	"github.com/wfusion/gofusion/log"
    12  
    13  	testUtl "github.com/wfusion/gofusion/test/common/utils"
    14  )
    15  
    16  func TestTime(t *testing.T) {
    17  	t.Parallel()
    18  	testingSuite := &Time{Test: new(testUtl.Test)}
    19  	suite.Run(t, testingSuite)
    20  }
    21  
    22  type Time struct {
    23  	*testUtl.Test
    24  }
    25  
    26  func (t *Time) BeforeTest(suiteName, testName string) {
    27  	t.Catch(func() {
    28  		log.Info(context.Background(), "right before %s %s", suiteName, testName)
    29  	})
    30  }
    31  
    32  func (t *Time) AfterTest(suiteName, testName string) {
    33  	t.Catch(func() {
    34  		log.Info(context.Background(), "right after %s %s", suiteName, testName)
    35  	})
    36  }
    37  
    38  func (t *Time) TestNextJitterIntervalFunc() {
    39  	t.Catch(func() {
    40  		ctx := context.Background()
    41  		t.Run("not symmetric", func() {
    42  			base := time.Second
    43  			ratio := 0.2
    44  			next := utils.NextJitterIntervalFunc(base, 20*base, ratio, 2, false)
    45  			for i := 0; i < 10; i++ {
    46  				interval := next()
    47  				log.Info(ctx, "next not symmetric jitter interval: %s", interval)
    48  				t.Greater(interval, base)
    49  				base *= 2
    50  				if base > 20*time.Second {
    51  					base = 20 * time.Second
    52  				}
    53  			}
    54  		})
    55  		t.Run("symmetric", func() {
    56  			base := time.Second
    57  			ratio := 0.2
    58  			next := utils.NextJitterIntervalFunc(base, 20*base, ratio, 2, true)
    59  			for i := 0; i < 10; i++ {
    60  				interval := next()
    61  				log.Info(ctx, "next symmetric jitter interval: %s", interval)
    62  				t.Greater(interval, base-time.Duration(float64(base)*(ratio/2)))
    63  				base *= 2
    64  				if base > 20*time.Second {
    65  					base = 20 * time.Second
    66  				}
    67  			}
    68  		})
    69  	})
    70  }