forked from i-love-flamingo/dingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scope_test.go
150 lines (121 loc) · 3.39 KB
/
scope_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
package dingo
import (
"fmt"
"reflect"
"runtime"
"sync"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func testScope(t *testing.T, scope Scope) {
var requestedUnscoped int64
test := reflect.TypeOf(new(string))
test2 := reflect.TypeOf(new(int))
unscoped := func(t reflect.Type, annotation string, optional bool) (reflect.Value, error) {
atomic.AddInt64(&requestedUnscoped, 1)
runtime.Gosched()
if optional {
return reflect.Value{}, nil
}
return reflect.New(t).Elem(), nil
}
runs := 1000 // change to 10? 100? 1000? to trigger a bug? todo investigate
wg := new(sync.WaitGroup)
wg.Add(runs)
for i := 0; i < runs; i++ {
go func() {
t1, err := scope.ResolveType(test, "", unscoped)
assert.NoError(t, err)
t12, err := scope.ResolveType(test2, "", unscoped)
assert.NoError(t, err)
t2, err := scope.ResolveType(test, "", unscoped)
assert.NoError(t, err)
t22, err := scope.ResolveType(test2, "", unscoped)
assert.NoError(t, err)
assert.Same(t, t1.Interface(), t2.Interface())
assert.Same(t, t12.Interface(), t22.Interface())
wg.Done()
}()
}
wg.Wait()
// should be 2, one for each type
assert.Equal(t, int64(2), requestedUnscoped)
}
func TestSingleton_ResolveType(t *testing.T) {
// reset instance
Singleton = NewSingletonScope()
testScope(t, Singleton)
}
func TestChildSingleton_ResolveType(t *testing.T) {
// reset instance
ChildSingleton = NewChildSingletonScope()
testScope(t, ChildSingleton)
}
type (
singletonA struct {
B *singletonB `inject:""`
}
singletonB struct {
C *singletonC `inject:""`
}
singletonC string
)
func TestScopeWithSubDependencies(t *testing.T) {
sc := singletonC("singleton C")
scp := &sc
for i := 0; i < 10; i++ {
t.Run(fmt.Sprintf("Run %d", i), func(t *testing.T) {
injector, err := NewInjector()
assert.NoError(t, err)
injector.Bind(new(singletonA)).In(Singleton)
injector.Bind(new(singletonB)).In(Singleton)
injector.Bind(new(singletonC)).In(Singleton).ToInstance(scp)
runs := 100
wg := new(sync.WaitGroup)
wg.Add(runs)
for i := 0; i < runs; i++ {
go func() {
i, err := injector.GetInstance(new(singletonA))
assert.NoError(t, err)
a := i.(*singletonA)
assert.Same(t, a.B.C, scp)
wg.Done()
}()
}
wg.Wait()
})
}
}
type inheritedScopeIface interface{}
type inheritedScopeStruct struct{}
type inheritedScopeInjected struct {
i inheritedScopeIface
s *inheritedScopeStruct
}
func (s *inheritedScopeInjected) Inject(ss *inheritedScopeStruct, si inheritedScopeIface) {
s.s = ss
s.i = si
}
func TestInheritedScope(t *testing.T) {
injector, err := NewInjector()
assert.NoError(t, err)
injector.Bind(new(inheritedScopeStruct)).In(ChildSingleton)
injector.Bind(new(inheritedScopeIface)).To(new(inheritedScopeStruct))
injector, err = injector.Child()
assert.NoError(t, err)
i, err := injector.GetInstance(new(inheritedScopeInjected))
assert.NoError(t, err)
firstS := i.(*inheritedScopeInjected)
i, err = injector.GetInstance(new(inheritedScopeInjected))
assert.NoError(t, err)
secondS := i.(*inheritedScopeInjected)
assert.Same(t, firstS.s, secondS.s)
i, err = injector.GetInstance(new(inheritedScopeInjected))
assert.NoError(t, err)
firstI := i.(*inheritedScopeInjected)
i, err = injector.GetInstance(new(inheritedScopeInjected))
assert.NoError(t, err)
secondI := i.(*inheritedScopeInjected)
assert.Same(t, firstI.i, secondI.i)
}