File tree 2 files changed +48
-2
lines changed 2 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -42,17 +42,31 @@ class Pool {
42
42
acquire ( key ) {
43
43
let resource ;
44
44
let pool = this . _pools [ key ] || [ ] ;
45
- while ( pool . length ) {
45
+ while ( pool . length ) {
46
46
resource = pool . pop ( ) ;
47
47
48
- if ( this . _validate ( resource ) ) {
48
+ if ( this . _validate ( resource ) ) {
49
49
return resource ;
50
50
}
51
51
}
52
52
53
53
return this . _create ( this . _release ) ;
54
54
}
55
55
56
+ purge ( key ) {
57
+ let resource ;
58
+ let pool = this . _pools [ key ] || [ ] ;
59
+ while ( pool . length ) {
60
+ resource = pool . pop ( ) ;
61
+ this . _destroy ( resource )
62
+ }
63
+ delete this . _pools [ key ]
64
+ }
65
+
66
+ has ( key ) {
67
+ return ( key in this . _pools ) ;
68
+ }
69
+
56
70
_release ( key , resource ) {
57
71
let pool = this . _pools [ key ] ;
58
72
if ( ! pool ) {
Original file line number Diff line number Diff line change @@ -120,6 +120,38 @@ describe('Pool', function() {
120
120
expect ( destroyed [ 0 ] . id ) . toBe ( r0 . id ) ;
121
121
expect ( destroyed [ 1 ] . id ) . toBe ( r1 . id ) ;
122
122
} ) ;
123
+
124
+
125
+ it ( 'purges keys' , function ( ) {
126
+ // Given a pool that allocates
127
+ var counter = 0 ;
128
+ var key1 = "bolt://localhost:7687" ;
129
+ var key2 = "bolt://localhost:7688" ;
130
+ var pool = new Pool ( function ( release ) { return new Resource ( counter ++ , release ) } ,
131
+ function ( res ) { res . destroyed = true ; return true }
132
+ ) ;
133
+
134
+ // When
135
+ var r0 = pool . acquire ( key1 ) ;
136
+ var r1 = pool . acquire ( key2 ) ;
137
+ r0 . close ( key1 ) ;
138
+ r1 . close ( key2 ) ;
139
+ expect ( pool . has ( key1 ) ) . toBe ( true ) ;
140
+ expect ( pool . has ( key2 ) ) . toBe ( true ) ;
141
+ pool . purge ( key1 ) ;
142
+ expect ( pool . has ( key1 ) ) . toBe ( false ) ;
143
+ expect ( pool . has ( key2 ) ) . toBe ( true ) ;
144
+
145
+ var r2 = pool . acquire ( key1 ) ;
146
+ var r3 = pool . acquire ( key2 ) ;
147
+
148
+ // Then
149
+ expect ( r0 . id ) . toBe ( 0 ) ;
150
+ expect ( r0 . destroyed ) . toBe ( true ) ;
151
+ expect ( r1 . id ) . toBe ( 1 ) ;
152
+ expect ( r2 . id ) . toBe ( 2 ) ;
153
+ expect ( r3 . id ) . toBe ( 1 ) ;
154
+ } ) ;
123
155
} ) ;
124
156
125
157
function Resource ( id , release ) {
You can’t perform that action at this time.
0 commit comments