-
Notifications
You must be signed in to change notification settings - Fork 1
/
default_fallback_test.go
44 lines (37 loc) · 1.12 KB
/
default_fallback_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
package mgcache
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestInvalidate(t *testing.T) {
var loadFunc LoadFunc = func(key string) (interface{}, error) {
return fmt.Sprintf("value_of_key[%s]", key), nil
}
defaultFallbackStorage := NewDefaultFallbackStorage(loadFunc, WithTimeToLive(1 * time.Second))
if err := defaultFallbackStorage.Invalidate("a"); err != nil {
t.Error("test failed")
}
}
func TestGet_Success(t *testing.T) {
var defaultFallbackStorage IFallbackStorage
var loadFunc LoadFunc = func(key string) (interface{}, error) {
return "value", nil
}
defaultFallbackStorage = NewDefaultFallbackStorage(loadFunc)
bytes, err := defaultFallbackStorage.GetBytes("")
assert.Nil(t, err)
assert.NotNil(t, bytes)
}
func TestGet_Failed(t *testing.T) {
var defaultFallbackStorage IFallbackStorage
customErr := errors.New("custom error")
var loadFunc LoadFunc = func(key string) (interface{}, error) {
return nil, customErr
}
defaultFallbackStorage = NewDefaultFallbackStorage(loadFunc)
_, err := defaultFallbackStorage.GetBytes("")
assert.Equal(t, customErr, err)
}