@@ -102,12 +102,81 @@ class MyCryptoKeyReader extends Pulsar.CryptoKeyReader {
102102
103103 const msg = await consumer . receive ( ) ;
104104 expect ( msg . getData ( ) . toString ( ) ) . toBe ( msgContent ) ;
105+ const encCtx = msg . getEncryptionContext ( ) ;
106+ expect ( encCtx ) . not . toBeNull ( ) ;
107+ expect ( encCtx . isDecryptionFailed ) . toBe ( false ) ;
108+ expect ( encCtx . keys ) . toBeDefined ( ) ;
109+ expect ( encCtx . keys . length ) . toBeGreaterThan ( 0 ) ;
110+ expect ( encCtx . keys [ 0 ] . value ) . toBeInstanceOf ( Buffer ) ;
111+ expect ( encCtx . param ) . toBeInstanceOf ( Buffer ) ;
112+ expect ( encCtx . algorithm ) . toBe ( '' ) ;
113+ expect ( encCtx . compressionType ) . toBe ( 'None' ) ;
114+ expect ( encCtx . uncompressedMessageSize ) . toBe ( 0 ) ;
115+ expect ( encCtx . batchSize ) . toBe ( 1 ) ;
105116
106117 await consumer . acknowledge ( msg ) ;
107118 await producer . close ( ) ;
108119 await consumer . close ( ) ;
109120 } ) ;
110121
122+ test ( 'End-to-End Encryption with Batching and Compression' , async ( ) => {
123+ const topic = `persistent://public/default/test-encryption-batch-compress-${ Date . now ( ) } ` ;
124+
125+ const cryptoKeyReader = new MyCryptoKeyReader (
126+ { 'my-key' : publicKeyPath } ,
127+ { 'my-key' : privateKeyPath } ,
128+ ) ;
129+
130+ const producer = await client . createProducer ( {
131+ topic,
132+ encryptionKeys : [ 'my-key' ] ,
133+ cryptoKeyReader,
134+ cryptoFailureAction : 'FAIL' ,
135+ batchingEnabled : true ,
136+ batchingMaxMessages : 10 ,
137+ batchingMaxPublishDelayMs : 100 ,
138+ compressionType : 'Zlib' ,
139+ } ) ;
140+
141+ const consumer = await client . subscribe ( {
142+ topic,
143+ subscription : 'sub-encryption-batch-compress' ,
144+ cryptoKeyReader,
145+ cryptoFailureAction : 'CONSUME' ,
146+ subscriptionInitialPosition : 'Earliest' ,
147+ } ) ;
148+
149+ const numMessages = 10 ;
150+ const sendPromises = [ ] ;
151+ for ( let i = 0 ; i < numMessages ; i += 1 ) {
152+ sendPromises . push ( producer . send ( {
153+ data : Buffer . from ( `message-${ i } ` ) ,
154+ } ) ) ;
155+ }
156+ await Promise . all ( sendPromises ) ;
157+
158+ for ( let i = 0 ; i < numMessages ; i += 1 ) {
159+ const msg = await consumer . receive ( ) ;
160+ expect ( msg . getData ( ) . toString ( ) ) . toBe ( `message-${ i } ` ) ;
161+ const encCtx = msg . getEncryptionContext ( ) ;
162+ expect ( encCtx ) . not . toBeNull ( ) ;
163+ expect ( encCtx . isDecryptionFailed ) . toBe ( false ) ;
164+ expect ( encCtx . keys ) . toBeDefined ( ) ;
165+ expect ( encCtx . keys . length ) . toBeGreaterThan ( 0 ) ;
166+ expect ( encCtx . keys [ 0 ] . value ) . toBeInstanceOf ( Buffer ) ;
167+ expect ( encCtx . param ) . toBeInstanceOf ( Buffer ) ;
168+ expect ( encCtx . algorithm ) . toBe ( '' ) ;
169+ expect ( encCtx . compressionType ) . toBe ( 'Zlib' ) ;
170+ expect ( encCtx . uncompressedMessageSize ) . toBeGreaterThan ( 0 ) ;
171+ expect ( encCtx . batchSize ) . toBe ( numMessages ) ;
172+
173+ await consumer . acknowledge ( msg ) ;
174+ }
175+
176+ await producer . close ( ) ;
177+ await consumer . close ( ) ;
178+ } ) ;
179+
111180 test ( 'Decryption Failure' , async ( ) => {
112181 const topic = `persistent://public/default/test-decryption-failure-${ Date . now ( ) } ` ;
113182
0 commit comments