File tree 2 files changed +54
-14
lines changed
test/esm/integration/pool-cluster
2 files changed +54
-14
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,18 @@ const getMonotonicMilliseconds = function () {
37
37
return Math . floor ( ms ) ;
38
38
} ;
39
39
40
+ const patternRegExp = function ( pattern ) {
41
+ if ( pattern instanceof RegExp ) {
42
+ return pattern ;
43
+ }
44
+
45
+ const source = pattern
46
+ . replace ( / ( [ . + ? ^ = ! : $ { } ( ) | [ \] / \\ ] ) / g, '\\$1' )
47
+ . replace ( / \* / g, '.*' ) ;
48
+
49
+ return new RegExp ( `^${ source } $` ) ;
50
+ } ;
51
+
40
52
class PoolNamespace {
41
53
constructor ( cluster , pattern , selector ) {
42
54
this . _cluster = cluster ;
@@ -256,20 +268,12 @@ class PoolCluster extends EventEmitter {
256
268
let currentTime = 0 ;
257
269
let foundNodeIds = this . _findCaches [ pattern ] ;
258
270
259
- if ( typeof this . _findCaches [ pattern ] === 'undefined' ) {
260
- if ( pattern === '*' ) {
261
- // all
262
- foundNodeIds = this . _serviceableNodeIds ;
263
- } else if ( this . _serviceableNodeIds . indexOf ( pattern ) !== - 1 ) {
264
- // one
265
- foundNodeIds = [ pattern ] ;
266
- } else {
267
- // wild matching
268
- const keyword = pattern . substring ( pattern . length - 1 , 0 ) ;
269
- foundNodeIds = this . _serviceableNodeIds . filter ( ( id ) =>
270
- id . startsWith ( keyword )
271
- ) ;
272
- }
271
+ if ( foundNodeIds === undefined ) {
272
+ const expression = patternRegExp ( pattern ) ;
273
+
274
+ foundNodeIds = this . _serviceableNodeIds . filter ( ( id ) =>
275
+ id . match ( expression )
276
+ ) ;
273
277
}
274
278
275
279
this . _findCaches [ pattern ] = foundNodeIds ;
Original file line number Diff line number Diff line change @@ -110,4 +110,40 @@ const { createPoolCluster } = require('../../../../promise.js');
110
110
111
111
poolCluster . end ( ) ;
112
112
} ) ;
113
+
114
+ await test ( async ( ) => {
115
+ const poolCluster = createPoolCluster ( ) ;
116
+ poolCluster . add ( 'SLAVE' , common . config ) ;
117
+
118
+ try {
119
+ await poolCluster . getConnection ( 'SLAVE1' ) ;
120
+ assert . fail ( 'An error was expected' ) ;
121
+ } catch ( error ) {
122
+ assert . equal (
123
+ error . code ,
124
+ 'POOL_NOEXIST' ,
125
+ 'should throw when PoolNamespace does not exist'
126
+ ) ;
127
+ } finally {
128
+ poolCluster . end ( ) ;
129
+ }
130
+ } ) ;
131
+
132
+ await test ( async ( ) => {
133
+ const poolCluster = createPoolCluster ( ) ;
134
+ poolCluster . add ( 'SLAVE1' , common . config ) ;
135
+
136
+ try {
137
+ const connection = await poolCluster . getConnection ( / S L A V E [ 1 2 ] / ) ;
138
+ assert . equal (
139
+ connection . connection . _clusterId ,
140
+ 'SLAVE1' ,
141
+ 'should match regex pattern'
142
+ ) ;
143
+ } catch ( error ) {
144
+ assert . fail ( 'should not throw' ) ;
145
+ } finally {
146
+ poolCluster . end ( ) ;
147
+ }
148
+ } ) ;
113
149
} ) ( ) ;
You can’t perform that action at this time.
0 commit comments