Browse Source

测试:改进核心功能中的缓存处理

- 在 cache_test.go 中添加缓存过期的测试
- 在 core.go 中,如果缓存中的数据为空,则添加 panic

Signed-off-by: u <u@a.com>
fast
u 2 years ago
parent
commit
75f0c54506
  1. 31
      cache_test.go
  2. 3
      core.go

31
cache_test.go

@ -0,0 +1,31 @@
package time_arrow
import (
"testing"
"time"
)
func Test_cache_IsExpired(t *testing.T) {
cachet := cache{}
if cachet.IsExpired() {
cachet.SetData("hello", time.Second*5)
}
time.Sleep(time.Second * 1)
data := cachet.GetData()
if cachet.IsExpired() {
t.Error("cache should not be expired 1")
}
if data != "hello" {
t.Error("cache should not be expired 2")
}
time.Sleep(time.Second * 5)
data = cachet.GetData()
if !cachet.IsExpired() {
t.Error("cache should be expired 1 ")
}
if data != "" {
t.Error("cache should be expired 2")
}
}

3
core.go

@ -392,6 +392,9 @@ func GetHolidaysDataWithCache() ([]HolidaysItem, error) {
data := holidayCache.GetData()
var result []HolidaysItem
err := json.Unmarshal([]byte(data), &result)
if len(data) == 0 {
panic("GetHolidaysDataWithCache json unmarshal error")
}
return result, err
}

Loading…
Cancel
Save