From 75f0c54506272d0f688d8b03e7c2db1ab434af89 Mon Sep 17 00:00:00 2001 From: u Date: Fri, 21 Apr 2023 11:33:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=EF=BC=9A=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=E6=A0=B8=E5=BF=83=E5=8A=9F=E8=83=BD=E4=B8=AD=E7=9A=84=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 cache_test.go 中添加缓存过期的测试 - 在 core.go 中,如果缓存中的数据为空,则添加 panic Signed-off-by: u --- cache_test.go | 31 +++++++++++++++++++++++++++++++ core.go | 3 +++ 2 files changed, 34 insertions(+) create mode 100644 cache_test.go diff --git a/cache_test.go b/cache_test.go new file mode 100644 index 0000000..1dd2252 --- /dev/null +++ b/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") + } +} diff --git a/core.go b/core.go index 2374be7..22687aa 100644 --- a/core.go +++ b/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 }