-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemofy_test.go
37 lines (28 loc) · 864 Bytes
/
memofy_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
package memofy
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestBasic(t *testing.T) {
// some expensive function
expensiveSumFunction := func(a, b int) int {
time.Sleep(2 * time.Second)
return a + b
}
cache := NewMemofier(90*time.Second, 10*time.Minute)
// first call should return cached = false,
// because it is not yet cached
result, cached, err := cache.Memofy(expensiveSumFunction, 5, 7)
results := result.([]interface{})
assert.Equal(t, cached, false, "==")
assert.Equal(t, results[0].(int), 5+7, "==")
assert.Nil(t, err)
// second call should return cached = true,
// because it was computer before
result, cached, err = cache.Memofy(expensiveSumFunction, 5, 7)
results = result.([]interface{})
assert.Equal(t, cached, true, "==")
assert.Equal(t, results[0].(int), 5+7, "==")
assert.Nil(t, err)
}