github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatime/once_in_test.go (about)

     1  package ekatime_test
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/qioalice/ekago/v3/ekatime"
    10  )
    11  
    12  func TestFoo(t *testing.T) {
    13  	rand.Seed(time.Now().UnixNano())
    14  
    15  	//goland:noinspection GoSnakeCaseUsage
    16  	const WAIT_MS_MAX = 500
    17  
    18  	//goland:noinspection GoSnakeCaseUsage
    19  	const (
    20  		INV_NOW_1 = true
    21  		INV_NOW_2 = true
    22  		INV_NOW_3 = true
    23  		INV_NOW_4 = false
    24  		INV_NOW_5 = false
    25  		INV_NOW_6 = true
    26  		INV_NOW_7 = false
    27  		INV_NOW_8 = false
    28  	)
    29  
    30  	onceIn := &ekatime.OnceInMinute
    31  
    32  	onceIn.Call(INV_NOW_1, func(ts ekatime.Timestamp) {
    33  		st := time.Duration(rand.Intn(WAIT_MS_MAX)) * time.Millisecond
    34  		time.Sleep(st)
    35  		fmt.Println("Call 1", ts.String(), "sleep time", st)
    36  	})
    37  	onceIn.Call(INV_NOW_2, func(ts ekatime.Timestamp) {
    38  		st := time.Duration(rand.Intn(WAIT_MS_MAX)) * time.Millisecond
    39  		time.Sleep(st)
    40  		fmt.Println("Call 2", ts.String(), "sleep time", st)
    41  	})
    42  	onceIn.Call(INV_NOW_3, func(ts ekatime.Timestamp) {
    43  		st := time.Duration(rand.Intn(WAIT_MS_MAX)) * time.Millisecond
    44  		time.Sleep(st)
    45  		fmt.Println("Call 3", ts.String(), "sleep time", st)
    46  	})
    47  	onceIn.Call(INV_NOW_4, func(ts ekatime.Timestamp) {
    48  		st := time.Duration(rand.Intn(WAIT_MS_MAX)) * time.Millisecond
    49  		time.Sleep(st)
    50  		fmt.Println("Call 4", ts.String(), "sleep time", st)
    51  	})
    52  	onceIn.Call(INV_NOW_5, func(ts ekatime.Timestamp) {
    53  		st := time.Duration(rand.Intn(WAIT_MS_MAX)) * time.Millisecond
    54  		time.Sleep(st)
    55  		fmt.Println("Call 5", ts.String(), "sleep time", st)
    56  	})
    57  	onceIn.Call(INV_NOW_6, func(ts ekatime.Timestamp) {
    58  		st := time.Duration(rand.Intn(WAIT_MS_MAX)) * time.Millisecond
    59  		time.Sleep(st)
    60  		fmt.Println("Call 6", ts.String(), "sleep time", st)
    61  	})
    62  	onceIn.Call(INV_NOW_7, func(ts ekatime.Timestamp) {
    63  		st := time.Duration(rand.Intn(WAIT_MS_MAX)) * time.Millisecond
    64  		time.Sleep(st)
    65  		fmt.Println("Call 7", ts.String(), "sleep time", st)
    66  	})
    67  	onceIn.Call(INV_NOW_8, func(ts ekatime.Timestamp) {
    68  		st := time.Duration(rand.Intn(WAIT_MS_MAX)) * time.Millisecond
    69  		time.Sleep(st)
    70  		fmt.Println("Call FINAL", ts.String(), "sleep time", st)
    71  	})
    72  
    73  	select {}
    74  }