@@ -11,13 +11,13 @@ import (
11
11
)
12
12
13
13
type mockObjectFactory struct {
14
- createDelay time.Duration
15
- disposeDeplay time.Duration
16
- err error
17
- totalCreated int
18
- totalDisplosed int
19
- idGenerator int
20
- lock sync.Mutex
14
+ createDelay time.Duration
15
+ disposeDeplay time.Duration
16
+ err error
17
+ totalCreated int
18
+ totalDisposed int
19
+ idGenerator int
20
+ lock sync.Mutex
21
21
}
22
22
23
23
type mockNetworkResource struct {
@@ -55,24 +55,36 @@ func (f *mockObjectFactory) Dispose(types.NetworkResource) error {
55
55
time .Sleep (f .disposeDeplay )
56
56
f .lock .Lock ()
57
57
defer f .lock .Unlock ()
58
- f .totalDisplosed ++
58
+ f .totalDisposed ++
59
59
return f .err
60
60
}
61
61
62
+ func (f * mockObjectFactory ) getTotalDisposed () int {
63
+ f .lock .Lock ()
64
+ defer f .lock .Unlock ()
65
+ return f .totalDisposed
66
+ }
67
+
68
+ func (f * mockObjectFactory ) getTotalCreated () int {
69
+ f .lock .Lock ()
70
+ defer f .lock .Unlock ()
71
+ return f .totalCreated
72
+ }
73
+
62
74
func TestInitializerWithoutAutoCreate (t * testing.T ) {
63
75
factory := & mockObjectFactory {}
64
76
createPool (factory , 3 , 0 )
65
77
time .Sleep (time .Second )
66
- assert .Equal (t , 0 , factory .totalCreated )
67
- assert .Equal (t , 0 , factory .totalDisplosed )
78
+ assert .Equal (t , 0 , factory .getTotalCreated () )
79
+ assert .Equal (t , 0 , factory .getTotalDisposed () )
68
80
}
69
81
70
82
func TestInitializerWithAutoCreate (t * testing.T ) {
71
83
factory := & mockObjectFactory {}
72
84
createPool (factory , 0 , 0 )
73
85
time .Sleep (time .Second )
74
- assert .Equal (t , 3 , factory .totalCreated )
75
- assert .Equal (t , 0 , factory .totalDisplosed )
86
+ assert .Equal (t , 3 , factory .getTotalCreated () )
87
+ assert .Equal (t , 0 , factory .getTotalDisposed () )
76
88
}
77
89
78
90
func createPool (factory ObjectFactory , initIdle , initInuse int ) ObjectPool {
@@ -105,39 +117,39 @@ func TestInitializerExceedMaxIdle(t *testing.T) {
105
117
factory := & mockObjectFactory {}
106
118
createPool (factory , 6 , 0 )
107
119
time .Sleep (1 * time .Second )
108
- assert .Equal (t , 0 , factory .totalCreated )
109
- assert .Equal (t , 1 , factory .totalDisplosed )
120
+ assert .Equal (t , 0 , factory .getTotalCreated () )
121
+ assert .Equal (t , 1 , factory .getTotalDisposed () )
110
122
}
111
123
112
124
func TestInitializerExceedCapacity (t * testing.T ) {
113
125
factory := & mockObjectFactory {}
114
126
createPool (factory , 1 , 10 )
115
127
time .Sleep (time .Second )
116
- assert .Equal (t , 0 , factory .totalCreated )
117
- assert .Equal (t , 1 , factory .totalDisplosed )
128
+ assert .Equal (t , 0 , factory .getTotalCreated () )
129
+ assert .Equal (t , 1 , factory .getTotalDisposed () )
118
130
}
119
131
120
132
func TestAcquireIdle (t * testing.T ) {
121
133
factory := & mockObjectFactory {}
122
134
pool := createPool (factory , 3 , 0 )
123
135
_ , err := pool .Acquire (context .Background (), "" )
124
136
assert .Nil (t , err )
125
- assert .Equal (t , 0 , factory .totalCreated )
137
+ assert .Equal (t , 0 , factory .getTotalCreated () )
126
138
}
127
139
func TestAcquireNonExists (t * testing.T ) {
128
140
factory := & mockObjectFactory {}
129
141
pool := createPool (factory , 3 , 0 )
130
142
_ , err := pool .Acquire (context .Background (), "1000" )
131
143
assert .Nil (t , err )
132
- assert .Equal (t , 0 , factory .totalCreated )
144
+ assert .Equal (t , 0 , factory .getTotalCreated () )
133
145
}
134
146
135
147
func TestAcquireExists (t * testing.T ) {
136
148
factory := & mockObjectFactory {}
137
149
pool := createPool (factory , 3 , 0 )
138
150
res , err := pool .Acquire (context .Background (), "2" )
139
151
assert .Nil (t , err )
140
- assert .Equal (t , 0 , factory .totalCreated )
152
+ assert .Equal (t , 0 , factory .getTotalCreated () )
141
153
assert .Equal (t , "2" , res .GetResourceID ())
142
154
}
143
155
@@ -176,7 +188,7 @@ func TestConcurrencyAcquireMoreThanCapacity(t *testing.T) {
176
188
}()
177
189
}
178
190
wg .Wait ()
179
- assert .Equal (t , 7 , factory .totalCreated )
191
+ assert .Equal (t , 7 , factory .getTotalCreated () )
180
192
}
181
193
182
194
func TestRelease (t * testing.T ) {
@@ -190,19 +202,19 @@ func TestRelease(t *testing.T) {
190
202
n4 , _ := pool .Acquire (context .Background (), "" )
191
203
n5 , _ := pool .Acquire (context .Background (), "" )
192
204
n6 , _ := pool .Acquire (context .Background (), "" )
193
- assert .Equal (t , 3 , factory .totalCreated )
205
+ assert .Equal (t , 3 , factory .getTotalCreated () )
194
206
pool .Release (n1 .GetResourceID ())
195
207
pool .Release (n2 .GetResourceID ())
196
208
pool .Release (n3 .GetResourceID ())
197
209
time .Sleep (1 * time .Second )
198
- assert .Equal (t , 0 , factory .totalDisplosed )
210
+ assert .Equal (t , 0 , factory .getTotalDisposed () )
199
211
pool .Release (n4 .GetResourceID ())
200
212
pool .Release (n5 .GetResourceID ())
201
213
time .Sleep (1 * time .Second )
202
- assert .Equal (t , 0 , factory .totalDisplosed )
214
+ assert .Equal (t , 0 , factory .getTotalDisposed () )
203
215
pool .Release (n6 .GetResourceID ())
204
216
time .Sleep (1 * time .Second )
205
- assert .Equal (t , 1 , factory .totalDisplosed )
217
+ assert .Equal (t , 1 , factory .getTotalDisposed () )
206
218
}
207
219
208
220
func TestReleaseInvalid (t * testing.T ) {
0 commit comments