Skip to content

Commit 8b26ce5

Browse files
authored
Merge add use cache example
2 parents e71b6ee + 782e5ea commit 8b26ce5

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

examples/simple/simple_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// counter fixture - increment globalCounter every non cached call
1313
// and return new globalCounter value
1414
func counter(e fixenv.Env) int {
15-
return fixenv.Cache(e, "", nil, func() (res int, err error) {
15+
return fixenv.Cache(e, nil, nil, func() (res int, err error) {
1616
globalCounter++
1717
return globalCounter, nil
1818
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
package simple
5+
6+
import (
7+
"math/rand"
8+
"testing"
9+
10+
"github.com/rekby/fixenv"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
// namedRandom return random number for new name args
15+
// but return same value for all calls with same names
16+
func namedRandom(e fixenv.Env, name string) int {
17+
return fixenv.Cache(e, name, nil, func() (res int, err error) {
18+
return rand.Int(), nil
19+
})
20+
}
21+
22+
func TestNamedRandom(t *testing.T) {
23+
e := fixenv.NewEnv(t)
24+
first := namedRandom(e, "first")
25+
second := namedRandom(e, "second")
26+
require.NotEqual(t, first, second)
27+
require.Equal(t, first, namedRandom(e, "first"))
28+
require.Equal(t, second, namedRandom(e, "second"))
29+
}

0 commit comments

Comments
 (0)