2020const Pulsar = require ( '../index' ) ;
2121const httpUtils = require ( './http_utils' ) ;
2222
23+ function getPartition ( msgId ) {
24+ // The message id string is in the format of "entryId,ledgerId,partition,batchIndex"
25+ return Number ( msgId . toString ( ) . split ( ',' ) [ 2 ] ) ;
26+ }
27+
2328( ( ) => {
2429 describe ( 'Producer' , ( ) => {
2530 let client ;
@@ -187,7 +192,7 @@ const httpUtils = require('./http_utils');
187192 console . log ( `All messages have been sent. IDs: ${ allMsgIds . join ( ', ' ) } ` ) ;
188193 for ( let i = 0 ; i < allMsgIds . length ; i += 1 ) {
189194 // The message id string is in the format of "entryId,ledgerId,partition,batchIndex"
190- const partition = Number ( allMsgIds [ i ] . toString ( ) . split ( ',' ) [ 2 ] ) ;
195+ const partition = getPartition ( allMsgIds [ i ] ) ;
191196 expect ( i % numPartitions ) . toBe ( partition ) ;
192197 }
193198 } catch ( error ) {
@@ -210,6 +215,29 @@ const httpUtils = require('./http_utils');
210215 producer . send ( { data : Buffer . from ( 'test' ) } ) ,
211216 ) . rejects . toThrow ( 'Failed to send message: UnknownError' ) ;
212217 } , 30000 ) ;
218+ test ( 'Not CustomPartition' , async ( ) => {
219+ const topic = `test-not-custom-part-${ Date . now ( ) } ` ;
220+ const numPartitions = 2 ;
221+ const response = await httpUtils . createPartitionedTopic ( topic , numPartitions ) ;
222+ expect ( response . statusCode ) . toBe ( 204 ) ;
223+
224+ let index = 0 ;
225+ const producer = await client . createProducer ( {
226+ topic,
227+ messageRouter : ( _ , topicMetadata ) => {
228+ const result = index % topicMetadata . numPartitions ;
229+ index += 1 ;
230+ return result ;
231+ } ,
232+ messageRoutingMode : 'UseSinglePartition' ,
233+ } ) ;
234+ const partitions = new Set ( ) ;
235+ for ( let i = 0 ; i < 10 ; i += 1 ) {
236+ const msgId = await producer . send ( { data : Buffer . from ( 'msg' ) } ) ;
237+ partitions . add ( getPartition ( msgId ) ) ;
238+ }
239+ expect ( partitions . size ) . toBe ( 1 ) ;
240+ } , 30000 ) ;
213241 } ) ;
214242 } ) ;
215243} ) ( ) ;
0 commit comments