diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 79d6389..44fa215 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -23,5 +23,3 @@ jobs: fetch-depth: '0' - name: golangci-lint uses: golangci/golangci-lint-action@v6 - with: - version: v1.55 diff --git a/.golangci.yml b/.golangci.yml index 3b49afb..6e31c30 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -3,23 +3,11 @@ run: concurrency: 4 timeout: 5m tests: true - # vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ - skip-dirs-use-default: true modules-download-mode: readonly allow-parallel-runners: false - go: '1.19' # output configuration options output: - # Format: colored-line-number|line-number|json|tab|checkstyle|code-climate|junit-xml|github-actions - # - # Multiple can be specified by separating them by comma, output can be provided - # for each of them by separating format name and path by colon symbol. - # Output path can be either `stdout`, `stderr` or path to the file to write to. - # Example: "checkstyle:report.json,colored-line-number" - # - # Default: colored-line-number - format: colored-line-number print-issued-lines: true print-linter-name: true uniq-by-line: true @@ -44,9 +32,9 @@ linters: - gocognit - goconst - gocritic - - goerr113 + - err113 - gofmt - - gomnd + - mnd - gomoddirectives - gosec - gosimple @@ -94,7 +82,7 @@ issues: - wrapcheck linters-settings: - gomnd: + mnd: ignored-functions: - context.WithTimeout nolintlint: diff --git a/factory_test.go b/factory_test.go index f26975d..7094d09 100644 --- a/factory_test.go +++ b/factory_test.go @@ -60,10 +60,12 @@ func TestHTTPFrontendFactory_BuildBackend(t *testing.T) { t.Run("memory", func(t *testing.T) { t.Parallel() + testConfig := httpcache.BackendConfig{ BackendType: "memory", Memory: &httpcache.MemoryBackendConfig{Size: 10}, } + backend, err := factory.BuildBackend(testConfig, "test") assert.NoError(t, err) assert.IsType(t, &httpcache.MemoryBackend{}, backend) @@ -71,15 +73,18 @@ func TestHTTPFrontendFactory_BuildBackend(t *testing.T) { t.Run("inmemory error", func(t *testing.T) { t.Parallel() + testConfig := httpcache.BackendConfig{ BackendType: "memory", } + _, err := factory.BuildBackend(testConfig, "test") assert.Error(t, err) }) t.Run("redis", func(t *testing.T) { t.Parallel() + testConfig := httpcache.BackendConfig{ BackendType: "redis", Redis: &httpcache.RedisBackendConfig{ @@ -88,6 +93,7 @@ func TestHTTPFrontendFactory_BuildBackend(t *testing.T) { Port: "8080", }, } + backend, err := factory.BuildBackend(testConfig, "test") assert.NoError(t, err) assert.IsType(t, &httpcache.RedisBackend{}, backend) diff --git a/frontend.go b/frontend.go index 5823ce2..3efa20d 100644 --- a/frontend.go +++ b/frontend.go @@ -12,6 +12,7 @@ import ( ) var ErrInvalidEntry = errors.New("cache returned invalid entry type") +var ErrNoCacheBackend = errors.New("no backend defined") type ( // Frontend caches and delivers HTTP responses @@ -40,7 +41,7 @@ func (f *Frontend) SetBackend(b Backend) *Frontend { func (f *Frontend) Purge(ctx context.Context, key string) error { if f.backend == nil { - return errors.New("no backend defined") + return ErrNoCacheBackend } _, span := trace.StartSpan(ctx, "flamingo/httpcache/purge") @@ -60,7 +61,7 @@ func (f *Frontend) Purge(ctx context.Context, key string) error { // The result of loader will be returned and cached func (f *Frontend) Get(ctx context.Context, key string, loader HTTPLoader) (Entry, error) { if f.backend == nil { - return Entry{}, errors.New("no backend defined") + return Entry{}, ErrNoCacheBackend } ctx, span := trace.StartSpan(ctx, "flamingo/httpcache/get") @@ -98,7 +99,7 @@ func (f *Frontend) Get(ctx context.Context, key string, loader HTTPLoader) (Entr return f.load(ctx, key, loader) } -func (f *Frontend) load(ctx context.Context, key string, loader HTTPLoader) (Entry, error) { +func (f *Frontend) load(ctx context.Context, key string, loader HTTPLoader) (Entry, error) { //nolint:contextcheck // this is fine oldSpan := trace.FromContext(ctx) newContext := trace.NewContext(context.Background(), oldSpan) diff --git a/frontend_test.go b/frontend_test.go index c9a1246..ef8366c 100644 --- a/frontend_test.go +++ b/frontend_test.go @@ -118,6 +118,7 @@ func TestFrontend_Get(t *testing.T) { test := test t.Run(test.name, func(t *testing.T) { t.Parallel() + wait := make(chan struct{}, 1) loaderCalled := false loader := func(ctx context.Context) (httpcache.Entry, error) { @@ -127,9 +128,11 @@ func TestFrontend_Get(t *testing.T) { } backend := new(mocks.Backend) + if test.cacheGetter != nil { backend.EXPECT().Get(testKey).Return(test.cacheGetter()) } + if test.wantSet != nil { backend.EXPECT().Set(testKey, *test.wantSet).Run(func(key string, entry httpcache.Entry) { wait <- struct{}{} @@ -150,8 +153,10 @@ func TestFrontend_Get(t *testing.T) { if (err != nil) != test.wantErr { t.Errorf("Get() error = %v, wantErr %v", err, test.wantErr) + return } + assert.Equal(t, test.want, got) }) } diff --git a/twoLevelBackend.go b/twoLevelBackend.go index 1984d7e..2cf4f37 100644 --- a/twoLevelBackend.go +++ b/twoLevelBackend.go @@ -95,7 +95,7 @@ func (mb *TwoLevelBackend) Set(key string, entry Entry) error { mb.logger.WithField("category", "TwoLevelBackend").Error(fmt.Sprintf("Failed to set key %v with error %v", key, err)) } - if errorCount >= 2 { //nolint:gomnd // there are two backends no need to introduce const for that + if errorCount >= 2 { //nolint:mnd // there are two backends no need to introduce const for that return ErrAllBackendsFailed }