Ai
2 Star 2 Fork 9

王布衣/gox

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
rolling_once_test.go 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
package coroutine
import (
"fmt"
"gitee.com/quant1x/gox/concurrent"
"gitee.com/quant1x/gox/timestamp"
"strconv"
"testing"
"time"
)
var (
rollingOnce RollingOnce
cache1 = concurrent.NewHashMap[string, int]()
)
func lazyCacheInit() {
for i := 0; i < 5; i++ {
k := strconv.Itoa(i)
//cache[k] = i
cache1.Set(k, i)
fmt.Println("reset", k, "=>", i)
}
}
func cacheGetInt(key string) (int, bool) {
rollingOnce.Do(lazyCacheInit)
v, ok := cache1.Get(key)
return v, ok
}
func cacheSetInt(key string, value int) {
rollingOnce.Do(lazyCacheInit)
cache1.Set(key, value)
}
func TestRollingOnce(t *testing.T) {
var o1 RollingOnce
o1.Do(func() {
})
o1.Close()
rwCount := 1000
producer := func() {
for i := 0; i < rwCount; i++ {
k := strconv.Itoa(i % 5)
cacheSetInt(k, i)
fmt.Println(k, "=>", i)
time.Sleep(time.Millisecond * 10)
}
}
reader := func() {
for i := 0; i < rwCount; i++ {
k := strconv.Itoa(i % 5)
v, ok := cacheGetInt(k)
fmt.Println(v, "<=", i, ":", ok)
_ = v
_ = ok
time.Sleep(time.Millisecond * 10)
}
}
go producer()
go reader()
count := 60
for i := 0; i < count; i++ {
//once.Reset()
fmt.Println("--------------------")
time.Sleep(time.Second)
}
}
func Test_defaultTimeWindow(t *testing.T) {
observer := getCurrentObserver(offsetWindow)
fmt.Println(observer)
a, b, c := nextTimeWindow(observer, rollingWindow)
fmt.Println(a, b, c)
nt := timestamp.Time(a)
fmt.Println(nt.Format(time.DateTime))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/quant1x/gox.git
git@gitee.com:quant1x/gox.git
quant1x
gox
gox
v1.22.12

搜索帮助