-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmemoize_test.go
154 lines (124 loc) · 3.87 KB
/
memoize_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package memoize
import (
"errors"
"testing"
"time"
. "github.com/smartystreets/assertions"
"github.com/smartystreets/gunit"
)
func TestSuite(t *testing.T) {
gunit.Run(new(F), t)
}
type F struct {
*gunit.Fixture
}
// TestBasic adopts the code from readme.md into a simple test case
func (t *F) TestBasic() {
expensiveCalls := 0
// Function tracks how many times its been called
expensive := func() (any, error) {
expensiveCalls++
return expensiveCalls, nil
}
cache := NewMemoizer(90*time.Second, 10*time.Minute)
// First call SHOULD NOT be cached
result, err, cached := cache.Memoize("key1", expensive)
t.So(err, ShouldBeNil)
t.So(result.(int), ShouldEqual, 1)
t.So(cached, ShouldBeFalse)
// Second call on same key SHOULD be cached
result, err, cached = cache.Memoize("key1", expensive)
t.So(err, ShouldBeNil)
t.So(result.(int), ShouldEqual, 1)
t.So(cached, ShouldBeTrue)
// First call on a new key SHOULD NOT be cached
result, err, cached = cache.Memoize("key2", expensive)
t.So(err, ShouldBeNil)
t.So(result.(int), ShouldEqual, 2)
t.So(cached, ShouldBeFalse)
}
// TestFailure checks that failed function values are not cached
func (t *F) TestFailure() {
calls := 0
// This function will fail IFF it has not been called before.
twoForTheMoney := func() (any, error) {
calls++
if calls == 1 {
return calls, errors.New("Try again")
} else {
return calls, nil
}
}
cache := NewMemoizer(90*time.Second, 10*time.Minute)
// First call should fail, and not be cached
result, err, cached := cache.Memoize("key1", twoForTheMoney)
t.So(err, ShouldNotBeNil)
t.So(result.(int), ShouldEqual, 1)
t.So(cached, ShouldBeFalse)
// Second call should succeed, and not be cached
result, err, cached = cache.Memoize("key1", twoForTheMoney)
t.So(err, ShouldBeNil)
t.So(result.(int), ShouldEqual, 2)
t.So(cached, ShouldBeFalse)
// Third call should succeed, and be cached
result, err, cached = cache.Memoize("key1", twoForTheMoney)
t.So(err, ShouldBeNil)
t.So(result.(int), ShouldEqual, 2)
t.So(cached, ShouldBeTrue)
}
// TestBasicGenerics adopts the code from readme.md into a simple test case
// but using generics.
func (t *F) TestBasicGenerics() {
expensiveCalls := 0
// Function tracks how many times its been called
expensive := func() (int, error) {
expensiveCalls++
return expensiveCalls, nil
}
cache := NewMemoizer(90*time.Second, 10*time.Minute)
// First call SHOULD NOT be cached
result, err, cached := Call(cache, "key1", expensive)
t.So(err, ShouldBeNil)
t.So(result, ShouldEqual, 1)
t.So(cached, ShouldBeFalse)
// Second call on same key SHOULD be cached
result, err, cached = Call(cache, "key1", expensive)
t.So(err, ShouldBeNil)
t.So(result, ShouldEqual, 1)
t.So(cached, ShouldBeTrue)
// First call on a new key SHOULD NOT be cached
result, err, cached = Call(cache, "key2", expensive)
t.So(err, ShouldBeNil)
t.So(result, ShouldEqual, 2)
t.So(cached, ShouldBeFalse)
}
// TestFailureGenerics checks that failed function values are not cached
// when using generics.
func (t *F) TestFailureGenerics() {
calls := 0
// This function will fail IFF it has not been called before.
twoForTheMoney := func() (int, error) {
calls++
if calls == 1 {
return calls, errors.New("Try again")
} else {
return calls, nil
}
}
cache := NewMemoizer(90*time.Second, 10*time.Minute)
// First call should fail, and not be cached
result, err, cached := Call(cache, "key1", twoForTheMoney)
t.So(err, ShouldNotBeNil)
t.So(result, ShouldEqual, 1)
t.So(cached, ShouldBeFalse)
// Second call should succeed, and not be cached
result, err, cached = Call(cache, "key1", twoForTheMoney)
t.So(err, ShouldBeNil)
t.So(result, ShouldEqual, 2)
t.So(cached, ShouldBeFalse)
// Third call should succeed, and be cached
result, err, cached = Call(cache, "key1", twoForTheMoney)
t.So(err, ShouldBeNil)
t.So(result, ShouldEqual, 2)
t.So(cached, ShouldBeTrue)
}