Skip to content

Commit

Permalink
Revert "add NoCache option"
Browse files Browse the repository at this point in the history
This reverts commit 644db7b.
  • Loading branch information
rekby committed Jun 12, 2022
1 parent 9945dfd commit f9de235
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 50 deletions.
5 changes: 0 additions & 5 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fixenv

import (
"errors"
"strconv"
"sync"
)

Expand All @@ -14,10 +13,6 @@ type cache struct {

type cacheKey string

func (key *cacheKey) AppendNocacheCounter(counter int) {
*key = cacheKey(string(*key) + "-nocache-" + strconv.Itoa(counter))
}

type cacheVal struct {
res interface{}
err error
Expand Down
12 changes: 2 additions & 10 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ type EnvT struct {
t T
c *cache

m sync.Locker
scopes map[string]*scopeInfo
counter int
m sync.Locker
scopes map[string]*scopeInfo
}

// NewEnv create EnvT from test
Expand Down Expand Up @@ -102,19 +101,12 @@ func (e *EnvT) cache(params interface{}, opt *FixtureOptions, f FixtureCallbackF
if opt == nil {
opt = globalEmptyFixtureOptions
}

key, err := makeCacheKey(e.t.Name(), params, opt, false)
if err != nil {
e.t.Fatalf("failed to create cache key: %v", err)
// return not reacheble after Fatalf
return nil
}

if opt.NoCache {
e.counter++
key.AppendNocacheCounter(e.counter)
}

wrappedF := e.fixtureCallWrapper(key, f, opt)
res, err := e.c.GetOrSet(key, wrappedF)
if err != nil {
Expand Down
30 changes: 1 addition & 29 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fixenv

import (
"errors"
"regexp"
"runtime"
"sync"
"testing"
Expand Down Expand Up @@ -148,29 +147,6 @@ func Test_Env_Cache(t *testing.T) {
at.Equal(1, cntF())
})

t.Run("NoCache", func(t *testing.T) {
tMock := &testMock{name: t.Name()}
env := newTestEnv(tMock)
calledTimes := 0

fixture := func(e Env) int {
return e.Cache(nil, &FixtureOptions{NoCache: true}, func() (res interface{}, err error) {
calledTimes++
return calledTimes, nil
}).(int)
}

require.Equal(t, 0, calledTimes)

require.Equal(t, 1, fixture(env))
require.Equal(t, 1, calledTimes)

// double, no cache
require.Equal(t, 2, fixture(env))
require.Equal(t, 2, calledTimes)

})

t.Run("subtest_and_test_scope", func(t *testing.T) {
at := assert.New(t)
e := NewEnv(t)
Expand Down Expand Up @@ -679,14 +655,10 @@ func Test_ScopeName(t *testing.T) {
},
}

noCacheRe := regexp.MustCompile(`-nocache-.*`)

for _, c := range table {
t.Run(c.name, func(t *testing.T) {
at := assert.New(t)
name := makeScopeName(c.testName, c.scope)
noCacheRe.ReplaceAllString(name, "-nocache-XXX")
at.Equal(c.result, name)
at.Equal(c.result, makeScopeName(c.testName, c.scope))
})
}
})
Expand Down
8 changes: 2 additions & 6 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ const (

// ScopePackage mean fixture function with same parameters called once per package
// for use the scope with TearDown function developer must initialize global handler and cleaner at TestMain.
ScopePackage
ScopePackage CacheScope = iota

// ScopeTestAndSubtests mean fixture cached for top level test and subtests
ScopeTestAndSubtests
ScopeTestAndSubtests CacheScope = iota
)

// FixtureCallbackFunc - function, which result can cached
Expand Down Expand Up @@ -72,10 +72,6 @@ type FixtureOptions struct {
// Scope for cache result
Scope CacheScope

// NoCache mean result will not cached
// in the case fiexenv used for lifetime manages, or simple to common code style
NoCache bool

// cleanupFunc if not nil - called for cleanup fixture results
// internal implementation details
cleanupFunc FixtureCleanupFunc
Expand Down

0 comments on commit f9de235

Please sign in to comment.