-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_mock.go
283 lines (235 loc) · 8.47 KB
/
cache_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package artifacts
// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT.
import (
"sync"
mm_atomic "sync/atomic"
mm_time "time"
"github.com/gojuno/minimock"
"github.com/insolar/insolar/insolar"
)
// CacheMock implements cache
type CacheMock struct {
t minimock.Tester
funcget func(ref insolar.Reference, getter func() (val interface{}, err error)) (val interface{}, err error)
inspectFuncget func(ref insolar.Reference, getter func() (val interface{}, err error))
aftergetCounter uint64
beforegetCounter uint64
getMock mCacheMockget
}
// NewCacheMock returns a mock for cache
func NewCacheMock(t minimock.Tester) *CacheMock {
m := &CacheMock{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.getMock = mCacheMockget{mock: m}
m.getMock.callArgs = []*CacheMockgetParams{}
return m
}
type mCacheMockget struct {
mock *CacheMock
defaultExpectation *CacheMockgetExpectation
expectations []*CacheMockgetExpectation
callArgs []*CacheMockgetParams
mutex sync.RWMutex
}
// CacheMockgetExpectation specifies expectation struct of the cache.get
type CacheMockgetExpectation struct {
mock *CacheMock
params *CacheMockgetParams
results *CacheMockgetResults
Counter uint64
}
// CacheMockgetParams contains parameters of the cache.get
type CacheMockgetParams struct {
ref insolar.Reference
getter func() (val interface{}, err error)
}
// CacheMockgetResults contains results of the cache.get
type CacheMockgetResults struct {
val interface{}
err error
}
// Expect sets up expected params for cache.get
func (mmget *mCacheMockget) Expect(ref insolar.Reference, getter func() (val interface{}, err error)) *mCacheMockget {
if mmget.mock.funcget != nil {
mmget.mock.t.Fatalf("CacheMock.get mock is already set by Set")
}
if mmget.defaultExpectation == nil {
mmget.defaultExpectation = &CacheMockgetExpectation{}
}
mmget.defaultExpectation.params = &CacheMockgetParams{ref, getter}
for _, e := range mmget.expectations {
if minimock.Equal(e.params, mmget.defaultExpectation.params) {
mmget.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmget.defaultExpectation.params)
}
}
return mmget
}
// Inspect accepts an inspector function that has same arguments as the cache.get
func (mmget *mCacheMockget) Inspect(f func(ref insolar.Reference, getter func() (val interface{}, err error))) *mCacheMockget {
if mmget.mock.inspectFuncget != nil {
mmget.mock.t.Fatalf("Inspect function is already set for CacheMock.get")
}
mmget.mock.inspectFuncget = f
return mmget
}
// Return sets up results that will be returned by cache.get
func (mmget *mCacheMockget) Return(val interface{}, err error) *CacheMock {
if mmget.mock.funcget != nil {
mmget.mock.t.Fatalf("CacheMock.get mock is already set by Set")
}
if mmget.defaultExpectation == nil {
mmget.defaultExpectation = &CacheMockgetExpectation{mock: mmget.mock}
}
mmget.defaultExpectation.results = &CacheMockgetResults{val, err}
return mmget.mock
}
//Set uses given function f to mock the cache.get method
func (mmget *mCacheMockget) Set(f func(ref insolar.Reference, getter func() (val interface{}, err error)) (val interface{}, err error)) *CacheMock {
if mmget.defaultExpectation != nil {
mmget.mock.t.Fatalf("Default expectation is already set for the cache.get method")
}
if len(mmget.expectations) > 0 {
mmget.mock.t.Fatalf("Some expectations are already set for the cache.get method")
}
mmget.mock.funcget = f
return mmget.mock
}
// When sets expectation for the cache.get which will trigger the result defined by the following
// Then helper
func (mmget *mCacheMockget) When(ref insolar.Reference, getter func() (val interface{}, err error)) *CacheMockgetExpectation {
if mmget.mock.funcget != nil {
mmget.mock.t.Fatalf("CacheMock.get mock is already set by Set")
}
expectation := &CacheMockgetExpectation{
mock: mmget.mock,
params: &CacheMockgetParams{ref, getter},
}
mmget.expectations = append(mmget.expectations, expectation)
return expectation
}
// Then sets up cache.get return parameters for the expectation previously defined by the When method
func (e *CacheMockgetExpectation) Then(val interface{}, err error) *CacheMock {
e.results = &CacheMockgetResults{val, err}
return e.mock
}
// get implements cache
func (mmget *CacheMock) get(ref insolar.Reference, getter func() (val interface{}, err error)) (val interface{}, err error) {
mm_atomic.AddUint64(&mmget.beforegetCounter, 1)
defer mm_atomic.AddUint64(&mmget.aftergetCounter, 1)
if mmget.inspectFuncget != nil {
mmget.inspectFuncget(ref, getter)
}
params := &CacheMockgetParams{ref, getter}
// Record call args
mmget.getMock.mutex.Lock()
mmget.getMock.callArgs = append(mmget.getMock.callArgs, params)
mmget.getMock.mutex.Unlock()
for _, e := range mmget.getMock.expectations {
if minimock.Equal(e.params, params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.val, e.results.err
}
}
if mmget.getMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmget.getMock.defaultExpectation.Counter, 1)
want := mmget.getMock.defaultExpectation.params
got := CacheMockgetParams{ref, getter}
if want != nil && !minimock.Equal(*want, got) {
mmget.t.Errorf("CacheMock.get got unexpected parameters, want: %#v, got: %#v%s\n", *want, got, minimock.Diff(*want, got))
}
results := mmget.getMock.defaultExpectation.results
if results == nil {
mmget.t.Fatal("No results are set for the CacheMock.get")
}
return (*results).val, (*results).err
}
if mmget.funcget != nil {
return mmget.funcget(ref, getter)
}
mmget.t.Fatalf("Unexpected call to CacheMock.get. %v %v", ref, getter)
return
}
// getAfterCounter returns a count of finished CacheMock.get invocations
func (mmget *CacheMock) getAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmget.aftergetCounter)
}
// getBeforeCounter returns a count of CacheMock.get invocations
func (mmget *CacheMock) getBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmget.beforegetCounter)
}
// Calls returns a list of arguments used in each call to CacheMock.get.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmget *mCacheMockget) Calls() []*CacheMockgetParams {
mmget.mutex.RLock()
argCopy := make([]*CacheMockgetParams, len(mmget.callArgs))
copy(argCopy, mmget.callArgs)
mmget.mutex.RUnlock()
return argCopy
}
// MinimockgetDone returns true if the count of the get invocations corresponds
// the number of defined expectations
func (m *CacheMock) MinimockgetDone() bool {
for _, e := range m.getMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
// if default expectation was set then invocations count should be greater than zero
if m.getMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.aftergetCounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcget != nil && mm_atomic.LoadUint64(&m.aftergetCounter) < 1 {
return false
}
return true
}
// MinimockgetInspect logs each unmet expectation
func (m *CacheMock) MinimockgetInspect() {
for _, e := range m.getMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to CacheMock.get with params: %#v", *e.params)
}
}
// if default expectation was set then invocations count should be greater than zero
if m.getMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.aftergetCounter) < 1 {
if m.getMock.defaultExpectation.params == nil {
m.t.Error("Expected call to CacheMock.get")
} else {
m.t.Errorf("Expected call to CacheMock.get with params: %#v", *m.getMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcget != nil && mm_atomic.LoadUint64(&m.aftergetCounter) < 1 {
m.t.Error("Expected call to CacheMock.get")
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *CacheMock) MinimockFinish() {
if !m.minimockDone() {
m.MinimockgetInspect()
m.t.FailNow()
}
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *CacheMock) MinimockWait(timeout mm_time.Duration) {
timeoutCh := mm_time.After(timeout)
for {
if m.minimockDone() {
return
}
select {
case <-timeoutCh:
m.MinimockFinish()
return
case <-mm_time.After(10 * mm_time.Millisecond):
}
}
}
func (m *CacheMock) minimockDone() bool {
done := true
return done &&
m.MinimockgetDone()
}