Skip to content

Commit

Permalink
Merge add use cache example
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby authored Jun 12, 2022
2 parents e71b6ee + 782e5ea commit 8b26ce5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/simple/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// counter fixture - increment globalCounter every non cached call
// and return new globalCounter value
func counter(e fixenv.Env) int {
return fixenv.Cache(e, "", nil, func() (res int, err error) {
return fixenv.Cache(e, nil, nil, func() (res int, err error) {
globalCounter++
return globalCounter, nil
})
Expand Down
29 changes: 29 additions & 0 deletions examples/simple/use_cache_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build go1.18
// +build go1.18

package simple

import (
"math/rand"
"testing"

"github.com/rekby/fixenv"
"github.com/stretchr/testify/require"
)

// namedRandom return random number for new name args
// but return same value for all calls with same names
func namedRandom(e fixenv.Env, name string) int {
return fixenv.Cache(e, name, nil, func() (res int, err error) {
return rand.Int(), nil
})
}

func TestNamedRandom(t *testing.T) {
e := fixenv.NewEnv(t)
first := namedRandom(e, "first")
second := namedRandom(e, "second")
require.NotEqual(t, first, second)
require.Equal(t, first, namedRandom(e, "first"))
require.Equal(t, second, namedRandom(e, "second"))
}

0 comments on commit 8b26ce5

Please sign in to comment.