-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock.go
163 lines (144 loc) · 4.5 KB
/
mock.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
155
156
157
158
159
160
161
162
163
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xcache/blob/main/LICENSE.
package xcache
import (
"context"
"sync/atomic"
"time"
)
// Mock is a mock to be used in UT.
type Mock struct {
saveCallsCnt uint32
saveCallback func(context.Context, string, []byte, time.Duration) error
loadCallsCnt uint32
loadCallback func(context.Context, string) ([]byte, error)
ttlCallsCnt uint32
ttlCallback func(context.Context, string) (time.Duration, error)
statsCallsCnt uint32
statsCallback func(context.Context) (Stats, error)
}
// Save mock logic...
func (mock *Mock) Save(
ctx context.Context,
key string,
value []byte,
expire time.Duration,
) error {
atomic.AddUint32(&mock.saveCallsCnt, 1)
if mock.saveCallback != nil {
return mock.saveCallback(ctx, key, value, expire)
}
return nil
}
// Load mock logic...
func (mock *Mock) Load(ctx context.Context, key string) ([]byte, error) {
atomic.AddUint32(&mock.loadCallsCnt, 1)
if mock.loadCallback != nil {
return mock.loadCallback(ctx, key)
}
return nil, ErrNotFound
}
// TTL mock logic...
func (mock *Mock) TTL(ctx context.Context, key string) (time.Duration, error) {
atomic.AddUint32(&mock.ttlCallsCnt, 1)
if mock.ttlCallback != nil {
return mock.ttlCallback(ctx, key)
}
return -1, nil
}
// Stats mock logic...
func (mock *Mock) Stats(ctx context.Context) (Stats, error) {
atomic.AddUint32(&mock.statsCallsCnt, 1)
if mock.statsCallback != nil {
return mock.statsCallback(ctx)
}
return Stats{}, nil
}
// SetSaveCallback sets the given callback to be executed inside Save() method.
// You can inject yourself to make assertions upon passed parameter(s) this way
// and/or control the returned value.
//
// Usage example:
//
// mock.SetSaveCallback(func(ctx context.Context, k string, v []byte, exp time.Duration) {
// if k != "expected-key" {
// t.Error("expected ...")
// }
// if !reflect.DeepEqual(v, []byte("expected value")) {
// t.Error("expected ...")
// }
// if exp != 10 * time.Minute {
// t.Error("expected ...")
// }
//
// return nil
// })
func (mock *Mock) SetSaveCallback(callback func(context.Context, string, []byte, time.Duration) error) {
mock.saveCallback = callback
}
// SetLoadCallback sets the given callback to be executed inside Load() method.
// You can inject yourself to make assertions upon passed parameter(s) this way
// and/or control the returned value.
//
// Usage example:
//
// mock.SetLoadCallback(func(ctx context.Context, k string) ([]byte, error) {
// if k != "expected-key" {
// t.Error("expected ...")
// }
//
// return []byte("expected value"), nil
// })
func (mock *Mock) SetLoadCallback(callback func(context.Context, string) ([]byte, error)) {
mock.loadCallback = callback
}
// SetTTLCallback sets the given callback to be executed inside TTL() method.
// You can inject yourself to make assertions upon passed parameter(s) this way
// and/or control the returned value.
//
// Usage example:
//
// mock.SetTTLCallback(func(ctx context.Context, k string) (time.Duration, error) {
// if k != "expected-key" {
// t.Error("expected ...")
// }
//
// return 123 * time.Second, nil
// })
func (mock *Mock) SetTTLCallback(callback func(context.Context, string) (time.Duration, error)) {
mock.ttlCallback = callback
}
// SetStatsCallback sets the given callback to be executed inside Stats() method.
// You can inject yourself to make assertions upon passed parameter(s) this way
// and/or control the returned value.
//
// Usage example:
//
// mock.SetStatsCallback(func(ctx context.Context) (xcache.Stats, error) {
// if ctx != context.Background() {
// t.Error("expected ...")
// }
//
// return xcache.Stats{Memory: 1024}, nil
// })
func (mock *Mock) SetStatsCallback(callback func(context.Context) (Stats, error)) {
mock.statsCallback = callback
}
// SaveCallsCount returns the no. of times Save() method was called.
func (mock *Mock) SaveCallsCount() int {
return int(atomic.LoadUint32(&mock.saveCallsCnt))
}
// LoadCallsCount returns the no. of times Load() method was called.
func (mock *Mock) LoadCallsCount() int {
return int(atomic.LoadUint32(&mock.loadCallsCnt))
}
// TTLCallsCount returns the no. of times TTL() method was called.
func (mock *Mock) TTLCallsCount() int {
return int(atomic.LoadUint32(&mock.ttlCallsCnt))
}
// StatsCallsCount returns the no. of times Stats() method was called.
func (mock *Mock) StatsCallsCount() int {
return int(atomic.LoadUint32(&mock.statsCallsCnt))
}