@@ -32,8 +32,9 @@ describe('Pool', () => {
32
32
const r1 = pool . acquire ( key ) ;
33
33
34
34
// Then
35
- expect ( r0 . id ) . toBe ( 0 ) ;
36
- expect ( r1 . id ) . toBe ( 1 ) ;
35
+ expect ( r0 . id ) . toBe ( 0 ) ;
36
+ expect ( r1 . id ) . toBe ( 1 ) ;
37
+ expect ( r0 ) . not . toBe ( r1 ) ;
37
38
} ) ;
38
39
39
40
it ( 'pools if resources are returned' , ( ) => {
@@ -48,8 +49,9 @@ describe('Pool', () => {
48
49
const r1 = pool . acquire ( key ) ;
49
50
50
51
// Then
51
- expect ( r0 . id ) . toBe ( 0 ) ;
52
- expect ( r1 . id ) . toBe ( 0 ) ;
52
+ expect ( r0 . id ) . toBe ( 0 ) ;
53
+ expect ( r1 . id ) . toBe ( 0 ) ;
54
+ expect ( r0 ) . toBe ( r1 ) ;
53
55
} ) ;
54
56
55
57
it ( 'handles multiple keys' , ( ) => {
@@ -67,10 +69,13 @@ describe('Pool', () => {
67
69
const r3 = pool . acquire ( key2 ) ;
68
70
69
71
// Then
70
- expect ( r0 . id ) . toBe ( 0 ) ;
71
- expect ( r1 . id ) . toBe ( 1 ) ;
72
- expect ( r2 . id ) . toBe ( 0 ) ;
73
- expect ( r3 . id ) . toBe ( 2 ) ;
72
+ expect ( r0 . id ) . toBe ( 0 ) ;
73
+ expect ( r1 . id ) . toBe ( 1 ) ;
74
+ expect ( r2 . id ) . toBe ( 0 ) ;
75
+ expect ( r3 . id ) . toBe ( 2 ) ;
76
+
77
+ expect ( r0 ) . toBe ( r2 ) ;
78
+ expect ( r1 ) . not . toBe ( r3 ) ;
74
79
} ) ;
75
80
76
81
it ( 'frees if pool reaches max size' , ( ) => {
@@ -96,8 +101,8 @@ describe('Pool', () => {
96
101
r2 . close ( ) ;
97
102
98
103
// Then
99
- expect ( destroyed . length ) . toBe ( 1 ) ;
100
- expect ( destroyed [ 0 ] . id ) . toBe ( r2 . id ) ;
104
+ expect ( destroyed . length ) . toBe ( 1 ) ;
105
+ expect ( destroyed [ 0 ] . id ) . toBe ( r2 . id ) ;
101
106
} ) ;
102
107
103
108
it ( 'frees if validate returns false' , ( ) => {
@@ -121,9 +126,9 @@ describe('Pool', () => {
121
126
r1 . close ( ) ;
122
127
123
128
// Then
124
- expect ( destroyed . length ) . toBe ( 2 ) ;
125
- expect ( destroyed [ 0 ] . id ) . toBe ( r0 . id ) ;
126
- expect ( destroyed [ 1 ] . id ) . toBe ( r1 . id ) ;
129
+ expect ( destroyed . length ) . toBe ( 2 ) ;
130
+ expect ( destroyed [ 0 ] . id ) . toBe ( r0 . id ) ;
131
+ expect ( destroyed [ 1 ] . id ) . toBe ( r1 . id ) ;
127
132
} ) ;
128
133
129
134
@@ -144,21 +149,111 @@ describe('Pool', () => {
144
149
const r1 = pool . acquire ( key2 ) ;
145
150
r0 . close ( ) ;
146
151
r1 . close ( ) ;
147
- expect ( pool . has ( key1 ) ) . toBe ( true ) ;
148
- expect ( pool . has ( key2 ) ) . toBe ( true ) ;
152
+ expect ( pool . has ( key1 ) ) . toBeTruthy ( ) ;
153
+ expect ( pool . has ( key2 ) ) . toBeTruthy ( ) ;
149
154
pool . purge ( key1 ) ;
150
- expect ( pool . has ( key1 ) ) . toBe ( false ) ;
151
- expect ( pool . has ( key2 ) ) . toBe ( true ) ;
155
+ expect ( pool . has ( key1 ) ) . toBeFalsy ( ) ;
156
+ expect ( pool . has ( key2 ) ) . toBeTruthy ( ) ;
152
157
153
158
const r2 = pool . acquire ( key1 ) ;
154
159
const r3 = pool . acquire ( key2 ) ;
155
160
156
161
// Then
157
- expect ( r0 . id ) . toBe ( 0 ) ;
158
- expect ( r0 . destroyed ) . toBe ( true ) ;
159
- expect ( r1 . id ) . toBe ( 1 ) ;
160
- expect ( r2 . id ) . toBe ( 2 ) ;
161
- expect ( r3 . id ) . toBe ( 1 ) ;
162
+ expect ( r0 . id ) . toBe ( 0 ) ;
163
+ expect ( r0 . destroyed ) . toBeTruthy ( ) ;
164
+ expect ( r1 . id ) . toBe ( 1 ) ;
165
+ expect ( r2 . id ) . toBe ( 2 ) ;
166
+ expect ( r3 . id ) . toBe ( 1 ) ;
167
+ } ) ;
168
+
169
+ it ( 'destroys resource when key was purged' , ( ) => {
170
+ let counter = 0 ;
171
+ const key = 'bolt://localhost:7687' ;
172
+ const pool = new Pool ( ( url , release ) => new Resource ( url , counter ++ , release ) ,
173
+ res => {
174
+ res . destroyed = true ;
175
+ return true ;
176
+ }
177
+ ) ;
178
+
179
+ const r0 = pool . acquire ( key ) ;
180
+ expect ( pool . has ( key ) ) . toBeTruthy ( ) ;
181
+ expect ( r0 . id ) . toEqual ( 0 ) ;
182
+
183
+ pool . purge ( key ) ;
184
+ expect ( pool . has ( key ) ) . toBeFalsy ( ) ;
185
+ expect ( r0 . destroyed ) . toBeFalsy ( ) ;
186
+
187
+ r0 . close ( ) ;
188
+ expect ( pool . has ( key ) ) . toBeFalsy ( ) ;
189
+ expect ( r0 . destroyed ) . toBeTruthy ( ) ;
190
+ } ) ;
191
+
192
+ it ( 'purges all keys' , ( ) => {
193
+ let counter = 0 ;
194
+
195
+ const key1 = 'bolt://localhost:7687' ;
196
+ const key2 = 'bolt://localhost:7688' ;
197
+ const key3 = 'bolt://localhost:7689' ;
198
+
199
+ const pool = new Pool ( ( url , release ) => new Resource ( url , counter ++ , release ) ,
200
+ res => {
201
+ res . destroyed = true ;
202
+ return true ;
203
+ }
204
+ ) ;
205
+
206
+ const acquiredResources = [
207
+ pool . acquire ( key1 ) ,
208
+ pool . acquire ( key2 ) ,
209
+ pool . acquire ( key3 ) ,
210
+ pool . acquire ( key1 ) ,
211
+ pool . acquire ( key2 ) ,
212
+ pool . acquire ( key3 )
213
+ ] ;
214
+ acquiredResources . forEach ( resource => resource . close ( ) ) ;
215
+
216
+ pool . purgeAll ( ) ;
217
+
218
+ acquiredResources . forEach ( resource => expect ( resource . destroyed ) . toBeTruthy ( ) ) ;
219
+ } ) ;
220
+
221
+ it ( 'skips broken connections during acquire' , ( ) => {
222
+ let validated = false ;
223
+ let counter = 0 ;
224
+ const key = 'bolt://localhost:7687' ;
225
+ const pool = new Pool ( ( url , release ) => new Resource ( url , counter ++ , release ) ,
226
+ res => {
227
+ res . destroyed = true ;
228
+ return true ;
229
+ } ,
230
+ ( ) => {
231
+ if ( validated ) {
232
+ return false ;
233
+ }
234
+ validated = true ;
235
+ return true ;
236
+ }
237
+ ) ;
238
+
239
+ const r0 = pool . acquire ( key ) ;
240
+ r0 . close ( ) ;
241
+
242
+ const r1 = pool . acquire ( key ) ;
243
+ expect ( r1 ) . not . toBe ( r0 ) ;
244
+ } ) ;
245
+
246
+ it ( 'reports presence of the key' , ( ) => {
247
+ const existingKey = 'bolt://localhost:7687' ;
248
+ const absentKey = 'bolt://localhost:7688' ;
249
+
250
+ const pool = new Pool ( ( url , release ) => new Resource ( url , 42 , release ) ) ;
251
+
252
+ pool . acquire ( existingKey ) ;
253
+ pool . acquire ( existingKey ) ;
254
+
255
+ expect ( pool . has ( existingKey ) ) . toBeTruthy ( ) ;
256
+ expect ( pool . has ( absentKey ) ) . toBeFalsy ( ) ;
162
257
} ) ;
163
258
} ) ;
164
259
@@ -168,9 +263,10 @@ class Resource {
168
263
this . id = id ;
169
264
this . key = key ;
170
265
this . release = release ;
266
+ this . destroyed = false ;
171
267
}
172
268
173
269
close ( ) {
174
- this . release ( key , self ) ;
270
+ this . release ( this . key , this ) ;
175
271
}
176
272
}
0 commit comments