From 12c8755eab3d20855fdaa5f722db97e01fa3672c Mon Sep 17 00:00:00 2001 From: TremblingV5 Date: Sun, 10 Nov 2024 14:32:32 +0800 Subject: [PATCH 1/2] add some ut 4 gopkgs --- backend/gopkgs/cachex/cache.go | 18 ++++++--- backend/gopkgs/errorx/errorx_test.go | 26 ++++++++++++ backend/gopkgs/go.mod | 7 +++- backend/gopkgs/gofer/errors.go | 6 +++ backend/gopkgs/gofer/gofer_test.go | 59 ++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 backend/gopkgs/errorx/errorx_test.go create mode 100644 backend/gopkgs/gofer/errors.go create mode 100644 backend/gopkgs/gofer/gofer_test.go diff --git a/backend/gopkgs/cachex/cache.go b/backend/gopkgs/cachex/cache.go index be46cde..747b0c4 100644 --- a/backend/gopkgs/cachex/cache.go +++ b/backend/gopkgs/cachex/cache.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "errors" - "fmt" + "github.com/jellydator/ttlcache/v3" "time" "github.com/cloudzenith/DouTok/backend/gopkgs/gofer" @@ -29,6 +29,7 @@ type Cache[T any] struct { localTTL *time.Duration redisTTL *time.Duration client *redis.Client + localCache *ttlcache.Cache[string, []byte] } func NewCache[T any](options ...CacheOption[T]) *Cache[T] { @@ -50,6 +51,10 @@ func NewCache[T any](options ...CacheOption[T]) *Cache[T] { cache.redisTTL = &DefaultTTL } + if cache.useLocal { + cache.localCache = ttlcache.New[string, []byte]() + } + return cache } @@ -79,8 +84,7 @@ func (c *Cache[T]) fetchSet(ctx context.Context, key string, fetch func(ctx cont ttl = c.localTTL } - // TODO: set local cache - fmt.Println("when use local will todo something", ttl) + c.localCache.Set(key, val, *ttl) } return value, nil @@ -88,8 +92,10 @@ func (c *Cache[T]) fetchSet(ctx context.Context, key string, fetch func(ctx cont func (c *Cache[T]) getCache(ctx context.Context, key string) ([]byte, error) { if c.useLocal { - // TODO: 读本地缓存并返回 - fmt.Println("when use local will todo something") + item := c.localCache.Get(key) + if item != nil { + return item.Value(), nil + } } result, err := c.client.Get(ctx, key).Result() @@ -109,7 +115,7 @@ func (c *Cache[T]) Fetch( key string, fetch func(ctx context.Context) (T, error), ) (t T, err error) { - value, err, _ := gofer.SingleFlightDo(key, func() (interface{}, error) { + value, err, _ := gofer.SingleFlightDo(key, func() (any, error) { if c.useFallback { val, err := c.fetchSet(ctx, key, fetch) if err == nil { diff --git a/backend/gopkgs/errorx/errorx_test.go b/backend/gopkgs/errorx/errorx_test.go new file mode 100644 index 0000000..77e61c9 --- /dev/null +++ b/backend/gopkgs/errorx/errorx_test.go @@ -0,0 +1,26 @@ +package errorx + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNew(t *testing.T) { + err := New(1, "some error") + assert.NotNil(t, err) + assert.Equal(t, int32(1), err.Code) + assert.Equal(t, "some error", err.Error()) +} + +func TestNewWithCode(t *testing.T) { + err := NewWithCode(1) + assert.NotNil(t, err) + assert.Equal(t, int32(1), err.Code) + assert.Equal(t, "unknown error", err.Error()) + + RegisterErrors(1, "some error") + err = NewWithCode(1) + assert.NotNil(t, err) + assert.Equal(t, int32(1), err.Code) + assert.Equal(t, "some error", err.Error()) +} diff --git a/backend/gopkgs/go.mod b/backend/gopkgs/go.mod index f245733..05fa19f 100644 --- a/backend/gopkgs/go.mod +++ b/backend/gopkgs/go.mod @@ -13,10 +13,12 @@ require ( github.com/go-kratos/kratos/v2 v2.8.0 github.com/google/uuid v1.6.0 github.com/hashicorp/consul/api v1.29.2 + github.com/jellydator/ttlcache/v3 v3.3.0 github.com/minio/minio-go/v7 v7.0.75 github.com/panjf2000/ants/v2 v2.10.0 github.com/redis/go-redis/v9 v9.6.1 github.com/samber/lo v1.46.0 + github.com/stretchr/testify v1.9.0 go.etcd.io/etcd/client/v3 v3.5.15 go.uber.org/zap v1.27.0 golang.org/x/sync v0.8.0 @@ -34,12 +36,11 @@ require ( github.com/armon/go-metrics v0.4.1 // indirect github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect - github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect - github.com/chenzhuoyu/iasm v0.9.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/emirpasic/gods v1.18.1 // indirect @@ -51,6 +52,7 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/go-playground/assert/v2 v2.2.0 // indirect github.com/go-playground/form/v4 v4.2.0 // indirect + github.com/go-redis/redismock/v9 v9.2.0 // indirect github.com/go-sql-driver/mysql v1.7.0 // indirect github.com/goccy/go-json v0.10.3 // indirect github.com/gogo/protobuf v1.3.2 // indirect @@ -79,6 +81,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rs/xid v1.5.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect github.com/tidwall/gjson v1.17.3 // indirect diff --git a/backend/gopkgs/gofer/errors.go b/backend/gopkgs/gofer/errors.go new file mode 100644 index 0000000..bdae853 --- /dev/null +++ b/backend/gopkgs/gofer/errors.go @@ -0,0 +1,6 @@ +package gofer + +var ( + ErrSubmittedTaskNil = "submitted task can't be nil" + ErrGroupHashBeenStarted = "can't submit task to a error group after Wait()" +) diff --git a/backend/gopkgs/gofer/gofer_test.go b/backend/gopkgs/gofer/gofer_test.go new file mode 100644 index 0000000..e468b92 --- /dev/null +++ b/backend/gopkgs/gofer/gofer_test.go @@ -0,0 +1,59 @@ +package gofer + +import ( + "context" + "github.com/stretchr/testify/assert" + "sync" + "testing" +) + +func TestSetUseGlobalPool(t *testing.T) { + assert.Equal(t, false, useGlobalPool) + + SetUseGlobalPool(true) + assert.Equal(t, true, useGlobalPool) +} + +func submitGo() bool { + var wg sync.WaitGroup + wg.Add(1) + var flag bool + Go(func() { + flag = true + wg.Done() + }) + + wg.Wait() + return flag +} + +func TestGo(t *testing.T) { + assert.Equal(t, true, submitGo()) + + InitGlobalPool() + SetUseGlobalPool(true) + assert.Equal(t, true, submitGo()) +} + +func submitGoWithCtx() bool { + var wg sync.WaitGroup + wg.Add(1) + + var flag bool + ctx := context.Background() + GoWithCtx(ctx, func(ctx context.Context) { + flag = true + wg.Done() + }) + + wg.Wait() + return flag +} + +func TestGoWithCtx(t *testing.T) { + assert.Equal(t, true, submitGoWithCtx()) + + InitGlobalPool() + SetUseGlobalPool(true) + assert.Equal(t, true, submitGoWithCtx()) +} From 065c89cff0f04f22b80ee189e9560b2639f601e5 Mon Sep 17 00:00:00 2001 From: TremblingV5 Date: Sun, 10 Nov 2024 16:40:40 +0800 Subject: [PATCH 2/2] fix ut --- backend/gopkgs/gofer/gofer.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/gopkgs/gofer/gofer.go b/backend/gopkgs/gofer/gofer.go index 63a9e22..2210157 100644 --- a/backend/gopkgs/gofer/gofer.go +++ b/backend/gopkgs/gofer/gofer.go @@ -19,6 +19,10 @@ func SetUseGlobalPool(value bool) { func Go(f func()) { if useGlobalPool { + if pool == nil { + InitGlobalPool() + } + _ = pool.Submit(f) return }