-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider_global_test.go
192 lines (146 loc) · 5.19 KB
/
provider_global_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
package log
import (
"sync/atomic"
"testing"
"github.com/echocat/slf4g/internal/test/assert"
)
func Test_GetProvider_returnsFacade(t *testing.T) {
defer resetGlobal()
actual := GetProvider()
assert.ToBeOfType(t, providerFacade(nil), actual)
assert.ToBeEqual(t, providerFacade(getProvider), actual)
}
func Test_getProvider_returnsDefaultIfNoOtherIsRegistered(t *testing.T) {
defer resetGlobal()
// Nothing cached
assert.ToBeNil(t, getCurrentProvider())
actual := getProvider()
assert.ToBeSame(t, fallbackProviderV, actual)
// Also now cached
assert.ToBeSame(t, fallbackProviderV, getCurrentProvider())
}
func Test_getProvider_returnsRegistered(t *testing.T) {
defer resetGlobal()
instance := newMockProvider("instance")
RegisterProvider(instance)
// Nothing cached
assert.ToBeNil(t, getCurrentProvider())
actual := getProvider()
assert.ToBeSame(t, instance, actual)
// Also now cached
assert.ToBeSame(t, instance, getCurrentProvider())
}
func Test_getProvider_returnsCachedRegistered(t *testing.T) {
defer resetGlobal()
instance := newMockProvider("instance")
// Nothing cached
assert.ToBeNil(t, getCurrentProvider())
SetProvider(instance)
actual := getProvider()
assert.ToBeSame(t, instance, actual)
// Cached
assert.ToBeSame(t, instance, getCurrentProvider())
}
func Test_getProvider_failsIfMoreThenOneAreRegistered(t *testing.T) {
defer resetGlobal()
RegisterProvider(newMockProvider("instance1"))
RegisterProvider(newMockProvider("instance2"))
// Nothing cached
assert.ToBeNil(t, getCurrentProvider())
assert.Execution(t, func() {
_ = getProvider()
}).WillPanicWith("^There are more than provider registered; Got:.*")
}
func Test_RegisterProvider_registersOne(t *testing.T) {
defer resetGlobal()
instanceA := newMockProvider("instanceA")
instanceB := newMockProvider("instanceB")
assert.ToBeEqual(t, map[string]Provider{}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
actualA := RegisterProvider(instanceA)
assert.ToBeSame(t, instanceA, actualA)
assert.ToBeEqual(t, map[string]Provider{"instanceA": instanceA}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
actualB := RegisterProvider(instanceB)
assert.ToBeSame(t, instanceB, actualB)
assert.ToBeEqual(t, map[string]Provider{"instanceA": instanceA, "instanceB": instanceB}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
}
func Test_RegisterProvider_failsOnNil(t *testing.T) {
defer resetGlobal()
assert.Execution(t, func() {
RegisterProvider(nil)
}).WillPanicWith("^Provided Provider is nil$")
}
func Test_RegisterProvider_failsOnDoubleRegistration(t *testing.T) {
defer resetGlobal()
instance1 := newMockProvider("instance")
instance2 := newMockProvider("instance")
assert.ToBeEqual(t, map[string]Provider{}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
actual := RegisterProvider(instance1)
assert.ToBeSame(t, instance1, actual)
assert.ToBeEqual(t, map[string]Provider{"instance": instance1}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
assert.Execution(t, func() {
RegisterProvider(instance2)
}).WillPanicWith("^Multiple try of registering of Provider with the same name: instance\n\tAlready existing type: ")
}
func Test_RegisterProvider_ignoresDoubleRegistrationOfSameInstance(t *testing.T) {
defer resetGlobal()
instance := newMockProvider("instance")
assert.ToBeEqual(t, map[string]Provider{}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
actual1 := RegisterProvider(instance)
assert.ToBeSame(t, instance, actual1)
assert.ToBeEqual(t, map[string]Provider{"instance": instance}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
actual2 := RegisterProvider(instance)
assert.ToBeSame(t, instance, actual2)
assert.ToBeEqual(t, map[string]Provider{"instance": instance}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
}
func Test_UnregisterProvider(t *testing.T) {
defer resetGlobal()
instanceA := newMockProvider("instanceA")
instanceB := newOtherMockProvider("instanceB")
RegisterProvider(instanceA)
RegisterProvider(instanceB)
SetProvider(instanceA)
actualA := UnregisterProvider("instanceA")
assert.ToBeSame(t, instanceA, actualA)
assert.ToBeEqual(t, map[string]Provider{"instanceB": instanceB}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
actualB := UnregisterProvider("instanceB")
assert.ToBeSame(t, instanceB, actualB)
assert.ToBeEqual(t, map[string]Provider{}, knownProviders)
assert.ToBeNil(t, getCurrentProvider())
}
func Test_GetAllProviders_nonRegistered(t *testing.T) {
defer resetGlobal()
assert.ToBeEqual(t, []Provider{}, GetAllProviders())
}
func Test_GetAllProviders_afterOneRegistered(t *testing.T) {
defer resetGlobal()
instance1 := newMockProvider("instance")
RegisterProvider(instance1)
assert.ToBeEqual(t, []Provider{instance1}, GetAllProviders())
}
func resetGlobal() {
SetProvider(nil)
knownProvidersMutex.Lock()
defer knownProvidersMutex.Unlock()
knownProviders = map[string]Provider{}
}
func getCurrentProvider() Provider {
if v := (*Provider)(atomic.LoadPointer(&providerPointer)); v != nil && *v != nil {
return *v
}
return nil
}
func newOtherMockProvider(name string) Provider {
return &otherMockProvider{newMockProvider(name)}
}
type otherMockProvider struct {
*mockProvider
}