From 21d0d9d748f61fe71dcccb577a75dcecb41449c2 Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 3 Jul 2023 23:59:03 +0200 Subject: [PATCH 1/2] improve unit test for caching - Configure `setting.CacheService.TTL` which will force the code to go trough the caching mechanism. - Remove the TODO and uncomment the test code. (cherry picked from commit a201f2f1896c1a086f3e37f86b1306d44c07f2e4) Refs: https://codeberg.org/forgejo/forgejo/pulls/974 --- modules/cache/cache_test.go | 76 ++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/modules/cache/cache_test.go b/modules/cache/cache_test.go index 1c8f7d55ba8c2..0429966ce931f 100644 --- a/modules/cache/cache_test.go +++ b/modules/cache/cache_test.go @@ -18,6 +18,7 @@ func createTestCache() { Adapter: "memory", TTL: time.Minute, }) + setting.CacheService.TTL = 30 } func TestNewContext(t *testing.T) { @@ -54,12 +55,12 @@ func TestGetString(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "", data) - // data, err = GetString("key", func() (string, error) { - // return "some data", nil - // }) - // assert.NoError(t, err) - // assert.Equal(t, "", data) - // Remove("key") + data, err = GetString("key", func() (string, error) { + return "some data", nil + }) + assert.NoError(t, err) + assert.Equal(t, "", data) + Remove("key") data, err = GetString("key", func() (string, error) { return "some data", nil @@ -67,13 +68,12 @@ func TestGetString(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "some data", data) - // data, err = GetString("key", func() (string, error) { - // return "", fmt.Errorf("some error") - // }) - // assert.NoError(t, err) - // assert.Equal(t, "some data", data) - - // TODO: uncommented code works in IDE but not with go test + data, err = GetString("key", func() (string, error) { + return "", fmt.Errorf("some error") + }) + assert.NoError(t, err) + assert.Equal(t, "some data", data) + Remove("key") } func TestGetInt(t *testing.T) { @@ -91,12 +91,12 @@ func TestGetInt(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 0, data) - // data, err = GetInt("key", func() (int, error) { - // return 100, nil - // }) - // assert.NoError(t, err) - // assert.Equal(t, 0, data) - // Remove("key") + data, err = GetInt("key", func() (int, error) { + return 100, nil + }) + assert.NoError(t, err) + assert.Equal(t, 0, data) + Remove("key") data, err = GetInt("key", func() (int, error) { return 100, nil @@ -104,13 +104,12 @@ func TestGetInt(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 100, data) - // data, err = GetInt("key", func() (int, error) { - // return 0, fmt.Errorf("some error") - // }) - // assert.NoError(t, err) - // assert.Equal(t, 100, data) - - // TODO: uncommented code works in IDE but not with go test + data, err = GetInt("key", func() (int, error) { + return 0, fmt.Errorf("some error") + }) + assert.NoError(t, err) + assert.Equal(t, 100, data) + Remove("key") } func TestGetInt64(t *testing.T) { @@ -128,12 +127,12 @@ func TestGetInt64(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, 0, data) - // data, err = GetInt64("key", func() (int64, error) { - // return 100, nil - // }) - // assert.NoError(t, err) - // assert.EqualValues(t, 0, data) - // Remove("key") + data, err = GetInt64("key", func() (int64, error) { + return 100, nil + }) + assert.NoError(t, err) + assert.EqualValues(t, 0, data) + Remove("key") data, err = GetInt64("key", func() (int64, error) { return 100, nil @@ -141,11 +140,10 @@ func TestGetInt64(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, 100, data) - // data, err = GetInt64("key", func() (int, error) { - // return 0, fmt.Errorf("some error") - // }) - // assert.NoError(t, err) - // assert.EqualValues(t, 100, data) - - // TODO: uncommented code works in IDE but not with go test + data, err = GetInt64("key", func() (int64, error) { + return 0, fmt.Errorf("some error") + }) + assert.NoError(t, err) + assert.EqualValues(t, 100, data) + Remove("key") } From d00239177f45f8d4307ddc88f831647510d046ea Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Thu, 27 Jul 2023 15:02:28 +0200 Subject: [PATCH 2/2] setting.CacheService.TTL = 24 * time.Hour --- modules/cache/cache_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/cache/cache_test.go b/modules/cache/cache_test.go index 0429966ce931f..cf464af392d53 100644 --- a/modules/cache/cache_test.go +++ b/modules/cache/cache_test.go @@ -18,7 +18,7 @@ func createTestCache() { Adapter: "memory", TTL: time.Minute, }) - setting.CacheService.TTL = 30 + setting.CacheService.TTL = 24 * time.Hour } func TestNewContext(t *testing.T) {