-
Notifications
You must be signed in to change notification settings - Fork 24
/
mock.go
445 lines (383 loc) · 12.4 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mockey
import (
"reflect"
"sync"
"sync/atomic"
"github.com/bytedance/mockey/internal/monkey"
"github.com/bytedance/mockey/internal/tool"
)
type FilterGoroutineType int64
const (
Disable FilterGoroutineType = 0
Include FilterGoroutineType = 1
Exclude FilterGoroutineType = 2
)
type Mocker struct {
target reflect.Value // mock target value
hook reflect.Value // mock hook
proxy interface{} // proxy function to origin
times int64
mockTimes int64
patch *monkey.Patch
lock sync.Mutex
isPatched bool
builder *MockBuilder
outerCaller tool.CallerInfo
}
type MockBuilder struct {
target interface{} // mock target
proxyCaller interface{} // origin function caller hook
conditions []*mockCondition // mock conditions
filterGoroutine FilterGoroutineType
gId int64
unsafe bool
generic bool
}
// Mock mocks target function
//
// If target is a generic method or method of generic types, you need add a genericOpt, like this:
//
// func f[int, float64](x int, y T1) T2
// Mock(f[int, float64], OptGeneric)
func Mock(target interface{}, opt ...optionFn) *MockBuilder {
tool.AssertFunc(target)
option := resolveOpt(opt...)
builder := &MockBuilder{
target: target,
unsafe: option.unsafe,
generic: option.generic,
}
builder.resetCondition()
return builder
}
// MockUnsafe has the full ability of the Mock function and removes some security restrictions. This is an alternative
// when the Mock function fails. It may cause some unknown problems, so we recommend using Mock under normal conditions.
func MockUnsafe(target interface{}) *MockBuilder {
return Mock(target, OptUnsafe)
}
func (builder *MockBuilder) hookType() reflect.Type {
targetType := reflect.TypeOf(builder.target)
if builder.generic {
targetIn := []reflect.Type{genericInfoType}
for i := 0; i < targetType.NumIn(); i++ {
targetIn = append(targetIn, targetType.In(i))
}
targetOut := []reflect.Type{}
for i := 0; i < targetType.NumOut(); i++ {
targetOut = append(targetOut, targetType.Out(i))
}
return reflect.FuncOf(targetIn, targetOut, targetType.IsVariadic())
}
return targetType
}
func (builder *MockBuilder) resetCondition() *MockBuilder {
builder.conditions = []*mockCondition{builder.newCondition()} // at least 1 condition is needed
return builder
}
// Origin add an origin hook which can be used to call un-mocked origin function
//
// For example:
//
// origin := Fun // only need the same type
// mock := func(p string) string {
// return origin(p + "mocked")
// }
// mock2 := Mock(Fun).To(mock).Origin(&origin).Build()
//
// Origin only works when call origin hook directly, target will still be mocked in recursive call
func (builder *MockBuilder) Origin(funcPtr interface{}) *MockBuilder {
tool.Assert(builder.proxyCaller == nil, "re-set builder origin")
return builder.origin(funcPtr)
}
func (builder *MockBuilder) origin(funcPtr interface{}) *MockBuilder {
tool.AssertPtr(funcPtr)
builder.proxyCaller = funcPtr
return builder
}
func (builder *MockBuilder) lastCondition() *mockCondition {
cond := builder.conditions[len(builder.conditions)-1]
if cond.Complete() {
cond = builder.newCondition()
builder.conditions = append(builder.conditions, cond)
}
return cond
}
func (builder *MockBuilder) newCondition() *mockCondition {
return &mockCondition{builder: builder}
}
// When declares the condition hook that's called to determine whether the mock should be executed.
//
// The condition hook function must have the same parameters as the target function.
//
// The following example would execute the mock when input int is negative
//
// func Fun(input int) string {
// return strconv.Itoa(input)
// }
// Mock(Fun).When(func(input int) bool { return input < 0 }).Return("0").Build()
//
// Note that if the target function is a struct method, you may optionally include
// the receiver as the first argument of the condition hook function. For example,
//
// type Foo struct {
// Age int
// }
// func (f *Foo) GetAge(younger int) string {
// return strconv.Itoa(f.Age - younger)
// }
// Mock((*Foo).GetAge).When(func(f *Foo, younger int) bool { return younger < 0 }).Return("0").Build()
func (builder *MockBuilder) When(when interface{}) *MockBuilder {
builder.lastCondition().SetWhen(when)
return builder
}
// To declares the hook function that's called to replace the target function.
//
// The hook function must have the same signature as the target function.
//
// The following example would make Fun always return true
//
// func Fun(input string) bool {
// return input == "fun"
// }
//
// Mock(Fun).To(func(_ string) bool {return true}).Build()
//
// Note that if the target function is a struct method, you may optionally include
// the receiver as the first argument of the hook function. For example,
//
// type Foo struct {
// Name string
// }
// func (f *Foo) Bar(other string) bool {
// return other == f.Name
// }
// Mock((*Foo).Bar).To(func(f *Foo, other string) bool {return true}).Build()
func (builder *MockBuilder) To(hook interface{}) *MockBuilder {
builder.lastCondition().SetTo(hook)
return builder
}
func (builder *MockBuilder) Return(results ...interface{}) *MockBuilder {
builder.lastCondition().SetReturn(results...)
return builder
}
func (builder *MockBuilder) IncludeCurrentGoRoutine() *MockBuilder {
return builder.FilterGoRoutine(Include, tool.GetGoroutineID())
}
func (builder *MockBuilder) ExcludeCurrentGoRoutine() *MockBuilder {
return builder.FilterGoRoutine(Exclude, tool.GetGoroutineID())
}
func (builder *MockBuilder) FilterGoRoutine(filter FilterGoroutineType, gId int64) *MockBuilder {
builder.filterGoroutine = filter
builder.gId = gId
return builder
}
func (builder *MockBuilder) Build() *Mocker {
mocker := Mocker{target: reflect.ValueOf(builder.target), builder: builder}
mocker.buildHook()
mocker.Patch()
return &mocker
}
func (mocker *Mocker) missReceiver(target reflect.Type, hook interface{}) bool {
hType := reflect.TypeOf(hook)
tool.Assert(hType.Kind() == reflect.Func, "Param(%v) a is not a func", hType.Kind())
tool.Assert(target.IsVariadic() == hType.IsVariadic(), "target:%v, hook:%v args not match", target, hook)
// has receiver
if tool.CheckFuncArgs(target, hType, 0, 0) {
return false
}
if tool.CheckFuncArgs(target, hType, 1, 0) {
return true
}
tool.Assert(false, "target:%v, hook:%v args not match", target, hook)
return false
}
func (mocker *Mocker) buildHook() {
proxySetter := mocker.buildProxy()
originExec := func(args []reflect.Value) []reflect.Value {
return tool.ReflectCall(reflect.ValueOf(mocker.proxy).Elem(), args)
}
match := []func(args []reflect.Value) bool{}
exec := []func(args []reflect.Value) []reflect.Value{}
for i := range mocker.builder.conditions {
condition := mocker.builder.conditions[i]
if condition.when == nil {
// when condition is not set, just go into hook exec
match = append(match, func(args []reflect.Value) bool { return true })
} else {
match = append(match, func(args []reflect.Value) bool {
return tool.ReflectCall(reflect.ValueOf(condition.when), args)[0].Bool()
})
}
if condition.hook == nil {
// hook condition is not set, just go into original exec
exec = append(exec, originExec)
} else {
exec = append(exec, func(args []reflect.Value) []reflect.Value {
mocker.mock()
return tool.ReflectCall(reflect.ValueOf(condition.hook), args)
})
}
}
mockerHook := reflect.MakeFunc(mocker.builder.hookType(), func(args []reflect.Value) []reflect.Value {
proxySetter(args) // 设置origin调用proxy
mocker.access()
switch mocker.builder.filterGoroutine {
case Disable:
break
case Include:
if tool.GetGoroutineID() != mocker.builder.gId {
return originExec(args)
}
case Exclude:
if tool.GetGoroutineID() == mocker.builder.gId {
return originExec(args)
}
}
for i, matchFn := range match {
execFn := exec[i]
if matchFn(args) {
return execFn(args)
}
}
return originExec(args)
})
mocker.hook = mockerHook
}
// buildProx create a proxyCaller which could call origin directly
func (mocker *Mocker) buildProxy() func(args []reflect.Value) {
proxy := reflect.New(mocker.builder.hookType())
proxyCallerSetter := func(args []reflect.Value) {}
if mocker.builder.proxyCaller != nil {
pVal := reflect.ValueOf(mocker.builder.proxyCaller)
tool.Assert(pVal.Kind() == reflect.Ptr && pVal.Elem().Kind() == reflect.Func, "origin receiver must be a function pointer")
pElem := pVal.Elem()
shift := 0
if mocker.builder.generic {
shift += 1
}
if mocker.missReceiver(mocker.target.Type(), pElem.Interface()) {
shift += 1
}
proxyCallerSetter = func(args []reflect.Value) {
pElem.Set(reflect.MakeFunc(pElem.Type(), func(innerArgs []reflect.Value) (results []reflect.Value) {
return tool.ReflectCall(proxy.Elem(), append(args[0:shift], innerArgs...))
}))
}
}
mocker.proxy = proxy.Interface()
return proxyCallerSetter
}
func (mocker *Mocker) Patch() *Mocker {
mocker.lock.Lock()
defer mocker.lock.Unlock()
if mocker.isPatched {
return mocker
}
mocker.patch = monkey.PatchValue(mocker.target, mocker.hook, reflect.ValueOf(mocker.proxy), mocker.builder.unsafe, mocker.builder.generic)
mocker.isPatched = true
addToGlobal(mocker)
mocker.outerCaller = tool.OuterCaller()
return mocker
}
func (mocker *Mocker) UnPatch() *Mocker {
mocker.lock.Lock()
defer mocker.lock.Unlock()
if !mocker.isPatched {
return mocker
}
mocker.patch.Unpatch()
mocker.isPatched = false
removeFromGlobal(mocker)
atomic.StoreInt64(&mocker.times, 0)
atomic.StoreInt64(&mocker.mockTimes, 0)
return mocker
}
func (mocker *Mocker) Release() *MockBuilder {
mocker.UnPatch()
mocker.builder.resetCondition()
return mocker.builder
}
func (mocker *Mocker) ExcludeCurrentGoRoutine() *Mocker {
return mocker.rePatch(func() {
mocker.builder.ExcludeCurrentGoRoutine()
})
}
func (mocker *Mocker) FilterGoRoutine(filter FilterGoroutineType, gId int64) *Mocker {
return mocker.rePatch(func() {
mocker.builder.FilterGoRoutine(filter, gId)
})
}
func (mocker *Mocker) IncludeCurrentGoRoutine() *Mocker {
return mocker.rePatch(func() {
mocker.builder.IncludeCurrentGoRoutine()
})
}
func (mocker *Mocker) When(when interface{}) *Mocker {
tool.Assert(len(mocker.builder.conditions) == 1, "only one-condition mocker could reset when (You can call Release first, then rebuild mocker)")
return mocker.rePatch(func() {
mocker.builder.conditions[0].SetWhenForce(when)
})
}
func (mocker *Mocker) To(to interface{}) *Mocker {
tool.Assert(len(mocker.builder.conditions) == 1, "only one-condition mocker could reset to (You can call Release first, then rebuild mocker)")
return mocker.rePatch(func() {
mocker.builder.conditions[0].SetToForce(to)
})
}
func (mocker *Mocker) Return(results ...interface{}) *Mocker {
tool.Assert(len(mocker.builder.conditions) == 1, "only one-condition mocker could reset return (You can call Release first, then rebuild mocker)")
return mocker.rePatch(func() {
mocker.builder.conditions[0].SetReturnForce(results...)
})
}
func (mocker *Mocker) Origin(funcPtr interface{}) *Mocker {
return mocker.rePatch(func() {
mocker.builder.origin(funcPtr)
})
}
func (mocker *Mocker) rePatch(do func()) *Mocker {
mocker.UnPatch()
do()
mocker.buildHook()
mocker.Patch()
return mocker
}
func (mocker *Mocker) access() {
atomic.AddInt64(&mocker.times, 1)
}
func (mocker *Mocker) mock() {
atomic.AddInt64(&mocker.mockTimes, 1)
}
func (mocker *Mocker) Times() int {
return int(atomic.LoadInt64(&mocker.times))
}
func (mocker *Mocker) MockTimes() int {
return int(atomic.LoadInt64(&mocker.mockTimes))
}
func (mocker *Mocker) key() uintptr {
return mocker.target.Pointer()
}
func (mocker *Mocker) name() string {
return mocker.target.String()
}
func (mocker *Mocker) unPatch() {
mocker.UnPatch()
}
func (mocker *Mocker) caller() tool.CallerInfo {
return mocker.outerCaller
}