-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
55 lines (48 loc) · 1.12 KB
/
examples_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
//go:build integration
// +build integration
package cache
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"testing"
)
func TestInApp(t *testing.T) {
initConfig()
ctx := context.Background()
key := "test-key"
inApp := NewInApp()
res, err := Cache(ctx, key, inApp, fn, "AbcD")
//need to type cast back to the return type of the function (from interface{})
s := res.(string)
fmt.Printf("s:%s, err:%v", s, err)
Delete(ctx, key, inApp)
}
func TestInRedis(t *testing.T) {
initConfig()
ctx := context.Background()
key := "test-key"
inRedis := NewInRedis("127.0.0.1", "6379")
res, err := Cache(ctx, key, inRedis, fn2, "AbcD")
//need to type cast back to the return type of the function (from interface{})
s := res.(string)
fmt.Printf("s:%s, err:%v", s, err)
Delete(ctx, key, inRedis)
}
func fn(s string) (string, error) {
return strings.ToLower(s), nil
}
func fn2(s string) (string, error) {
return strings.ToUpper(s), nil
}
func initConfig() {
cfg := make(map[string]Config)
b, err := ioutil.ReadFile("config.json")
if err != nil {
panic("config not found")
}
json.Unmarshal(b, &cfg)
Init(cfg)
}