@@ -1330,4 +1330,118 @@ const Pulsar = require('../index');
13301330 await client . close ( ) ;
13311331 } ) ;
13321332 } ) ;
1333+ describe ( 'KeyBasedBatchingTest' , ( ) => {
1334+ let client ;
1335+ let producer ;
1336+ let consumer ;
1337+ let topicName ;
1338+
1339+ beforeAll ( async ( ) => {
1340+ client = new Pulsar . Client ( {
1341+ serviceUrl : 'pulsar://localhost:6650' ,
1342+ } ) ;
1343+ } ) ;
1344+
1345+ afterAll ( async ( ) => {
1346+ await client . close ( ) ;
1347+ } ) ;
1348+
1349+ beforeEach ( async ( ) => {
1350+ topicName = `KeyBasedBatchingTest-${ Date . now ( ) } ` ;
1351+ } ) ;
1352+
1353+ afterEach ( async ( ) => {
1354+ if ( producer ) await producer . close ( ) ;
1355+ if ( consumer ) await consumer . close ( ) ;
1356+ } ) ;
1357+
1358+ const initProducer = async ( maxMessages ) => {
1359+ producer = await client . createProducer ( {
1360+ topic : topicName ,
1361+ batchingEnabled : true ,
1362+ batchingMaxMessages : maxMessages ,
1363+ batchingType : 'KeyBasedBatching' ,
1364+ batchingMaxPublishDelayMs : 3600 * 1000 ,
1365+ } ) ;
1366+ } ;
1367+
1368+ const initConsumer = async ( ) => {
1369+ consumer = await client . subscribe ( {
1370+ topic : topicName ,
1371+ subscription : 'SubscriptionName' ,
1372+ subscriptionType : 'Exclusive' ,
1373+ } ) ;
1374+ } ;
1375+
1376+ const receiveAndAck = async ( ) => {
1377+ const msg = await consumer . receive ( ) ;
1378+ await consumer . acknowledge ( msg ) ;
1379+ return msg ;
1380+ } ;
1381+
1382+ test ( 'testSequenceId' , async ( ) => {
1383+ await initProducer ( 6 ) ;
1384+ await initConsumer ( ) ;
1385+
1386+ // 0. Send 6 messages, use different keys and order
1387+ producer . send ( { data : Buffer . from ( '0' ) , partitionKey : 'A' } ) ;
1388+ producer . send ( { data : Buffer . from ( '1' ) , partitionKey : 'B' } ) ;
1389+ producer . send ( { data : Buffer . from ( '2' ) , partitionKey : 'C' } ) ;
1390+ producer . send ( { data : Buffer . from ( '3' ) , partitionKey : 'B' } ) ;
1391+ producer . send ( { data : Buffer . from ( '4' ) , partitionKey : 'C' } ) ;
1392+ producer . send ( { data : Buffer . from ( '5' ) , partitionKey : 'A' } ) ;
1393+ await producer . flush ( ) ;
1394+
1395+ // 1. Receive all messages
1396+ const received = [ ] ;
1397+ for ( let i = 0 ; i < 6 ; i += 1 ) {
1398+ const msg = await receiveAndAck ( ) ;
1399+ received . push ( {
1400+ key : msg . getPartitionKey ( ) . toString ( ) ,
1401+ value : msg . getData ( ) . toString ( ) ,
1402+ } ) ;
1403+ }
1404+
1405+ // 2. Verify message order (based on key dictionary order)
1406+ const expected = [
1407+ { key : 'B' , value : '1' } ,
1408+ { key : 'B' , value : '3' } ,
1409+ { key : 'C' , value : '2' } ,
1410+ { key : 'C' , value : '4' } ,
1411+ { key : 'A' , value : '0' } ,
1412+ { key : 'A' , value : '5' } ,
1413+ ] ;
1414+
1415+ expect ( received ) . toEqual ( expected ) ;
1416+ } ) ;
1417+
1418+ test ( 'testOrderingKeyPriority' , async ( ) => {
1419+ await initProducer ( 3 ) ;
1420+ await initConsumer ( ) ;
1421+
1422+ // 1. Send 3 messages to verify orderingKey takes precedence over partitionKey
1423+ producer . send ( {
1424+ data : Buffer . from ( '0' ) ,
1425+ orderingKey : 'A' ,
1426+ partitionKey : 'B' ,
1427+ } ) ;
1428+ producer . send ( { data : Buffer . from ( '2' ) , orderingKey : 'B' } ) ;
1429+ producer . send ( { data : Buffer . from ( '1' ) , orderingKey : 'A' } ) ;
1430+ await producer . flush ( ) ;
1431+
1432+ // 2. Receive messages and verify their order and keys
1433+ const msg1 = await receiveAndAck ( ) ;
1434+ expect ( msg1 . getData ( ) . toString ( ) ) . toBe ( '2' ) ;
1435+ expect ( msg1 . getOrderingKey ( ) . toString ( ) ) . toBe ( 'B' ) ;
1436+
1437+ const msg2 = await receiveAndAck ( ) ;
1438+ expect ( msg2 . getData ( ) . toString ( ) ) . toBe ( '0' ) ;
1439+ expect ( msg2 . getOrderingKey ( ) ) . toBe ( 'A' ) ;
1440+ expect ( msg2 . getPartitionKey ( ) ) . toBe ( 'B' ) ;
1441+
1442+ const msg3 = await receiveAndAck ( ) ;
1443+ expect ( msg3 . getData ( ) . toString ( ) ) . toBe ( '1' ) ;
1444+ expect ( msg3 . getOrderingKey ( ) . toString ( ) ) . toBe ( 'A' ) ;
1445+ } ) ;
1446+ } ) ;
13331447} ) ( ) ;
0 commit comments