forked from go-macaron/inject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject_test.go
285 lines (227 loc) · 7.31 KB
/
inject_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
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
// Copyright 2013 Jeremy Saenz
// Copyright 2015 The Macaron Authors
//
// 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 inject_test
import (
"fmt"
"reflect"
"testing"
. "github.com/smartystreets/goconvey/convey"
"go.wandrs.dev/inject"
)
type SpecialString interface {
}
type TestStruct struct {
Dep1 string `inject:"t" json:"-"`
Dep2 SpecialString `inject`
Dep3 string
}
type Greeter struct {
Name string
}
func (g *Greeter) String() string {
return "Hello, My name is" + g.Name
}
type myFastInvoker func(string)
func (f myFastInvoker) Invoke([]interface{}) ([]reflect.Value, error) {
return nil, nil
}
func Test_Injector_Invoke(t *testing.T) {
Convey("Invokes function", t, func() {
injector := inject.New()
So(injector, ShouldNotBeNil)
dep := "some dependency"
injector.Map(dep)
dep2 := "another dep"
injector.MapTo(dep2, (*SpecialString)(nil))
dep3 := make(chan *SpecialString)
dep4 := make(chan *SpecialString)
typRecv := reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(dep3).Elem())
typSend := reflect.ChanOf(reflect.SendDir, reflect.TypeOf(dep4).Elem())
injector.Set(typRecv, reflect.ValueOf(dep3))
injector.Set(typSend, reflect.ValueOf(dep4))
_, err := injector.Invoke(func(d1 string, d2 SpecialString, d3 <-chan *SpecialString, d4 chan<- *SpecialString) {
So(d1, ShouldEqual, dep)
So(d2, ShouldEqual, dep2)
So(reflect.TypeOf(d3).Elem(), ShouldEqual, reflect.TypeOf(dep3).Elem())
So(reflect.TypeOf(d4).Elem(), ShouldEqual, reflect.TypeOf(dep4).Elem())
So(reflect.TypeOf(d3).ChanDir(), ShouldEqual, reflect.RecvDir)
So(reflect.TypeOf(d4).ChanDir(), ShouldEqual, reflect.SendDir)
})
So(err, ShouldBeNil)
_, err = injector.Invoke(myFastInvoker(func(string) {}))
So(err, ShouldBeNil)
})
Convey("Invokes function with return value", t, func() {
injector := inject.New()
So(injector, ShouldNotBeNil)
dep := "some dependency"
injector.Map(dep)
dep2 := "another dep"
injector.MapTo(dep2, (*SpecialString)(nil))
result, err := injector.Invoke(func(d1 string, d2 SpecialString) string {
So(d1, ShouldEqual, dep)
So(d2, ShouldEqual, dep2)
return "Hello world"
})
So(result[0].String(), ShouldEqual, "Hello world")
So(err, ShouldBeNil)
})
}
func Test_Injector_Apply(t *testing.T) {
Convey("Apply a type", t, func() {
injector := inject.New()
So(injector, ShouldNotBeNil)
injector.Map("a dep").MapTo("another dep", (*SpecialString)(nil))
s := TestStruct{}
So(injector.Apply(&s), ShouldBeNil)
So(s.Dep1, ShouldEqual, "a dep")
So(s.Dep2, ShouldEqual, "another dep")
})
}
func Test_Injector_InterfaceOf(t *testing.T) {
Convey("Check interface of a type", t, func() {
iType := inject.InterfaceOf((*SpecialString)(nil))
So(iType.Kind(), ShouldEqual, reflect.Interface)
iType = inject.InterfaceOf((**SpecialString)(nil))
So(iType.Kind(), ShouldEqual, reflect.Interface)
defer func() {
So(recover(), ShouldNotBeNil)
}()
iType = inject.InterfaceOf((*testing.T)(nil))
})
}
func Test_Injector_Set(t *testing.T) {
Convey("Set and get type", t, func() {
injector := inject.New()
So(injector, ShouldNotBeNil)
typ := reflect.TypeOf("string")
typSend := reflect.ChanOf(reflect.SendDir, typ)
typRecv := reflect.ChanOf(reflect.RecvDir, typ)
// instantiating unidirectional channels is not possible using reflect
// http://golang.org/src/pkg/reflect/value.go?s=60463:60504#L2064
chanRecv := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, typ), 0)
chanSend := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, typ), 0)
injector.Set(typSend, chanSend)
injector.Set(typRecv, chanRecv)
So(injector.GetVal(typSend).IsValid(), ShouldBeTrue)
So(injector.GetVal(typRecv).IsValid(), ShouldBeTrue)
So(injector.GetVal(chanSend.Type()).IsValid(), ShouldBeFalse)
})
}
func Test_Injector_GetVal(t *testing.T) {
Convey("Map and get type", t, func() {
injector := inject.New()
So(injector, ShouldNotBeNil)
injector.Map("some dependency")
So(injector.GetVal(reflect.TypeOf("string")).IsValid(), ShouldBeTrue)
So(injector.GetVal(reflect.TypeOf(11)).IsValid(), ShouldBeFalse)
})
}
func Test_Injector_SetParent(t *testing.T) {
Convey("Set parent of injector", t, func() {
injector := inject.New()
So(injector, ShouldNotBeNil)
injector.MapTo("another dep", (*SpecialString)(nil))
injector2 := inject.New()
So(injector, ShouldNotBeNil)
injector2.SetParent(injector)
So(injector2.GetVal(inject.InterfaceOf((*SpecialString)(nil))).IsValid(), ShouldBeTrue)
})
}
func Test_Injector_Implementors(t *testing.T) {
Convey("Check implementors", t, func() {
injector := inject.New()
So(injector, ShouldNotBeNil)
g := &Greeter{"Jeremy"}
injector.Map(g)
So(injector.GetVal(inject.InterfaceOf((*fmt.Stringer)(nil))).IsValid(), ShouldBeTrue)
})
}
func Test_FastInvoker(t *testing.T) {
Convey("Check fast invoker", t, func() {
So(inject.IsFastInvoker(myFastInvoker(nil)), ShouldBeTrue)
})
}
//----------Benchmark InjectorInvoke-------------
func f1InjectorInvoke(d1 string, d2 SpecialString) string {
return "f1"
}
func f2InjectorInvoke(d1 string, d2 SpecialString) string {
return "f2"
}
func f1SimpleInjectorInvoke() {
}
func f2SimpleInjectorInvoke() {
}
// f2InjectorInvokeHandler f2 Invoke Handler
type f2InjectorInvokeHandler func(d1 string, d2 SpecialString) string
func (f2 f2InjectorInvokeHandler) Invoke(p []interface{}) ([]reflect.Value, error) {
ret := f2(p[0].(string), p[1].(SpecialString))
return []reflect.Value{reflect.ValueOf(ret)}, nil
}
type f2SimpleInjectorInvokeHandler func()
func (f2 f2SimpleInjectorInvokeHandler) Invoke(p []interface{}) ([]reflect.Value, error) {
f2()
return nil, nil
}
func BenchmarkInjectorInvokeNative(b *testing.B) {
b.StopTimer()
dep := "some dependency"
dep2 := "another dep"
var d2 SpecialString
d2 = dep2
b.StartTimer()
for i := 0; i < b.N; i++ {
f1InjectorInvoke(dep, d2)
}
}
func BenchmarkInjectorInvokeOriginal(b *testing.B) {
benchmarkInjectorInvoke(b, false, false)
}
func BenchmarkInjectorInvokeFast(b *testing.B) {
benchmarkInjectorInvoke(b, true, false)
}
func BenchmarkInjectorInvokeOriginalSimple(b *testing.B) {
benchmarkInjectorInvoke(b, false, true)
}
func BenchmarkInjectorInvokeFastSimple(b *testing.B) {
benchmarkInjectorInvoke(b, true, true)
}
func benchmarkInjectorInvoke(b *testing.B, isFast, isSimple bool) {
b.StopTimer()
injector := inject.New()
dep := "some dependency"
injector.Map(dep)
dep2 := "another dep"
injector.MapTo(dep2, (*SpecialString)(nil))
var f1, f2 interface{}
if isSimple { //func()
f1 = f1SimpleInjectorInvoke
f2 = f2SimpleInjectorInvokeHandler(f2SimpleInjectorInvoke)
} else { //func(p1, p2) ret
f1 = f1InjectorInvoke
f2 = f2InjectorInvokeHandler(f2InjectorInvoke)
}
injector.Invoke(f1)
injector.Invoke(f2)
b.StartTimer()
for i := 0; i < b.N; i++ {
if isFast {
injector.Invoke(f2)
} else {
injector.Invoke(f1)
}
}
}