Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
x1ah committed Jan 12, 2021
1 parent 9efa49f commit 1a270f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions hacache/hacache.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ func (hc *HaCache) worker() {
event := <-hc.events
switch e := event.(type) {
case *EventCacheExpired:
data, err := hc.FnRun(true, e.Args)
data, err := hc.FnRun(true, e.Args...)
if err != nil {
continue
}
if err := hc.Set(hc.GenCacheKey(e.Args), data); err != nil {
if err := hc.Set(hc.GenCacheKey(e.Args...), data); err != nil {
continue
}
case *EventCacheInvalid:
Expand Down
12 changes: 7 additions & 5 deletions hacache/hacache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestHaCache_Cache_expiration(t *testing.T) {
t.Error(e)
}

time.Sleep(time.Second)
time.Sleep(2 * time.Second)

var v *ExpValue
if result, err := hc.Do("aa"); err == nil {
Expand All @@ -192,16 +192,18 @@ func TestHaCache_Cache_expiration(t *testing.T) {
}

// 触发了缓存更新任务,这里拿到的会是最新的
time.Sleep(100 * time.Millisecond)
// 完全过期,强制更新
time.Sleep(5 * time.Second)
res, _ := hc.Do("aa")
if time.Now().Unix()-res.(*ExpValue).CreateTS > 1 {
t.Fatal("background worker error")
now := time.Now().Unix()
if now-res.(*ExpValue).CreateTS > 1 {
t.Fatal("background worker error: ", now, res.(*ExpValue).CreateTS)
}

hc.opt.MaxAcceptableExpiration = time.Second
hc.opt.Expiration = time.Second
// 这里缓存过期时间太长,缓存无效,触发同步更新
time.Sleep(2 * time.Second)
time.Sleep(3 * time.Second)

res, _ = hc.Do("aa")
if time.Now().Unix()-res.(*ExpValue).CreateTS > 1 {
Expand Down

0 comments on commit 1a270f4

Please sign in to comment.