gitee.com/quant1x/gox@v1.21.2/coroutine/rolling_once_test.go (about) 1 package coroutine 2 3 import ( 4 "fmt" 5 "gitee.com/quant1x/gox/concurrent" 6 "gitee.com/quant1x/gox/timestamp" 7 "strconv" 8 "testing" 9 "time" 10 ) 11 12 var ( 13 rollingOnce RollingOnce 14 cache1 = concurrent.NewHashMap[string, int]() 15 ) 16 17 func lazyCacheInit() { 18 for i := 0; i < 5; i++ { 19 k := strconv.Itoa(i) 20 //cache[k] = i 21 cache1.Set(k, i) 22 fmt.Println("reset", k, "=>", i) 23 } 24 } 25 26 func cacheGetInt(key string) (int, bool) { 27 rollingOnce.Do(lazyCacheInit) 28 v, ok := cache1.Get(key) 29 return v, ok 30 } 31 32 func cacheSetInt(key string, value int) { 33 rollingOnce.Do(lazyCacheInit) 34 cache1.Set(key, value) 35 } 36 37 func TestRollingOnce(t *testing.T) { 38 var o1 RollingOnce 39 o1.Do(func() { 40 41 }) 42 o1.Close() 43 rwCount := 1000 44 producer := func() { 45 for i := 0; i < rwCount; i++ { 46 k := strconv.Itoa(i % 5) 47 cacheSetInt(k, i) 48 fmt.Println(k, "=>", i) 49 time.Sleep(time.Millisecond * 10) 50 } 51 } 52 reader := func() { 53 for i := 0; i < rwCount; i++ { 54 k := strconv.Itoa(i % 5) 55 v, ok := cacheGetInt(k) 56 fmt.Println(v, "<=", i, ":", ok) 57 _ = v 58 _ = ok 59 time.Sleep(time.Millisecond * 10) 60 } 61 } 62 63 go producer() 64 go reader() 65 count := 60 66 for i := 0; i < count; i++ { 67 //once.Reset() 68 fmt.Println("--------------------") 69 time.Sleep(time.Second) 70 } 71 } 72 73 func Test_defaultTimeWindow(t *testing.T) { 74 observer := getCurrentObserver(offsetWindow) 75 fmt.Println(observer) 76 a, b, c := nextTimeWindow(observer, rollingWindow) 77 fmt.Println(a, b, c) 78 nt := timestamp.Time(a) 79 fmt.Println(nt.Format(time.DateTime)) 80 }