|
18 | 18 | */ |
19 | 19 | package org.apache.pulsar.client.impl; |
20 | 20 |
|
21 | | -import static org.testng.Assert.assertEquals; |
22 | | -import static org.testng.Assert.assertFalse; |
23 | | -import static org.testng.Assert.assertNotNull; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.Mockito.doAnswer; |
| 23 | +import static org.mockito.Mockito.spy; |
| 24 | +import static org.testng.Assert.*; |
24 | 25 |
|
25 | 26 | import java.util.concurrent.CompletableFuture; |
26 | 27 | import java.util.concurrent.CountDownLatch; |
27 | 28 | import java.util.concurrent.TimeUnit; |
| 29 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 30 | + |
28 | 31 | import lombok.Cleanup; |
29 | 32 | import lombok.extern.slf4j.Slf4j; |
30 | 33 | import org.apache.pulsar.broker.BrokerTestUtil; |
31 | 34 | import org.apache.pulsar.broker.service.ServerCnx; |
32 | | -import org.apache.pulsar.client.api.BatcherBuilder; |
33 | | -import org.apache.pulsar.client.api.Consumer; |
34 | | -import org.apache.pulsar.client.api.Message; |
35 | | -import org.apache.pulsar.client.api.MessageId; |
36 | | -import org.apache.pulsar.client.api.Producer; |
37 | | -import org.apache.pulsar.client.api.ProducerConsumerBase; |
38 | | -import org.apache.pulsar.client.api.SubscriptionType; |
| 35 | +import org.apache.pulsar.client.api.*; |
39 | 36 | import org.apache.pulsar.common.api.proto.CommandCloseProducer; |
40 | 37 | import org.apache.pulsar.common.policies.data.PersistentTopicInternalStats; |
41 | 38 | import org.awaitility.Awaitility; |
@@ -230,4 +227,44 @@ public void testRetentionPolicyByProducingMessages() throws Exception { |
230 | 227 | assertEquals(internalStats.ledgers.size(), 1); |
231 | 228 | }); |
232 | 229 | } |
| 230 | + |
| 231 | + |
| 232 | + @Test |
| 233 | + public void testProducerCompressionMinMsgBodySize() throws PulsarClientException { |
| 234 | + byte[] msg1022 = new byte[1022]; |
| 235 | + byte[] msg1025 = new byte[1025]; |
| 236 | + AtomicBoolean isCompressed = new AtomicBoolean(false); |
| 237 | + final String topicName = BrokerTestUtil.newUniqueName("persistent://my-property/my-ns/tp_"); |
| 238 | + @Cleanup |
| 239 | + ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) pulsarClient.newProducer() |
| 240 | + .topic(topicName) |
| 241 | + .create(); |
| 242 | + producer = spy(producer); |
| 243 | + doAnswer(invocation -> { |
| 244 | + isCompressed.set(false); |
| 245 | + return null; |
| 246 | + }).when(producer).applyCompression(any()); |
| 247 | + |
| 248 | + producer.conf.setCompressMinMsgBodySize(1024); |
| 249 | + producer.conf.setCompressionType(CompressionType.LZ4); |
| 250 | + // disable batch |
| 251 | + producer.conf.setBatchingEnabled(false); |
| 252 | + isCompressed.set(false); |
| 253 | + producer.newMessage().value(msg1022).send(); |
| 254 | + assertFalse(isCompressed.get()); |
| 255 | + |
| 256 | + isCompressed.set(false); |
| 257 | + producer.newMessage().value(msg1025).send(); |
| 258 | + assertTrue(isCompressed.get()); |
| 259 | + |
| 260 | + // enable batch |
| 261 | + producer.conf.setBatchingEnabled(true); |
| 262 | + isCompressed.set(false); |
| 263 | + producer.newMessage().value(msg1022).send(); |
| 264 | + assertFalse(isCompressed.get()); |
| 265 | + |
| 266 | + isCompressed.set(false); |
| 267 | + producer.newMessage().value(msg1025).send(); |
| 268 | + assertTrue(isCompressed.get()); |
| 269 | + } |
233 | 270 | } |
0 commit comments