|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.common.protocol; |
| 20 | + |
| 21 | +import static org.mockito.Mockito.doAnswer; |
| 22 | +import static org.testng.Assert.assertEquals; |
| 23 | +import static org.testng.Assert.assertNotNull; |
| 24 | +import static org.testng.Assert.assertTrue; |
| 25 | +import static org.mockito.Mockito.spy; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 31 | +import lombok.extern.slf4j.Slf4j; |
| 32 | +import org.apache.pulsar.broker.BrokerTestUtil; |
| 33 | +import org.apache.pulsar.client.api.Message; |
| 34 | +import org.apache.pulsar.client.api.MessageId; |
| 35 | +import org.apache.pulsar.client.api.Producer; |
| 36 | +import org.apache.pulsar.client.api.ProducerConsumerBase; |
| 37 | + |
| 38 | +import org.apache.pulsar.client.api.Schema; |
| 39 | +import org.apache.pulsar.client.impl.ConsumerImpl; |
| 40 | +import org.apache.pulsar.common.api.proto.BaseCommand; |
| 41 | +import org.awaitility.Awaitility; |
| 42 | +import org.testng.annotations.AfterClass; |
| 43 | +import org.testng.annotations.BeforeClass; |
| 44 | +import org.testng.annotations.DataProvider; |
| 45 | +import org.testng.annotations.Test; |
| 46 | + |
| 47 | +@Slf4j |
| 48 | +@Test |
| 49 | +public class ProducerBatchSendTest extends ProducerConsumerBase { |
| 50 | + |
| 51 | + @BeforeClass(alwaysRun = true) |
| 52 | + @Override |
| 53 | + protected void setup() throws Exception { |
| 54 | + super.internalSetup(); |
| 55 | + super.producerBaseSetup(); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterClass(alwaysRun = true) |
| 59 | + @Override |
| 60 | + protected void cleanup() throws Exception { |
| 61 | + super.internalCleanup(); |
| 62 | + } |
| 63 | + |
| 64 | + @DataProvider |
| 65 | + public Object[][] flushSend() { |
| 66 | + return new Object[][] { |
| 67 | + {Collections.emptyList()}, |
| 68 | + {Arrays.asList(1)}, |
| 69 | + {Arrays.asList(2)}, |
| 70 | + {Arrays.asList(3)}, |
| 71 | + {Arrays.asList(1, 2)}, |
| 72 | + {Arrays.asList(2, 3)}, |
| 73 | + {Arrays.asList(1, 2, 3)}, |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + @Test(timeOut = 30_000, dataProvider = "flushSend") |
| 78 | + public void testNoEnoughMemSend(List<Integer> flushSend) throws Exception { |
| 79 | + final String topic = BrokerTestUtil.newUniqueName("persistent://public/default/tp"); |
| 80 | + final String subscription = "s1"; |
| 81 | + admin.topics().createNonPartitionedTopic(topic); |
| 82 | + admin.topics().createSubscription(topic, subscription, MessageId.earliest); |
| 83 | + Producer<String> producer = pulsarClient.newProducer(Schema.STRING).topic(topic).enableBatching(true) |
| 84 | + .batchingMaxMessages(Integer.MAX_VALUE).batchingMaxPublishDelay(1, TimeUnit.HOURS).create(); |
| 85 | + |
| 86 | + /** |
| 87 | + * The method {@link org.apache.pulsar.client.impl.BatchMessageContainerImpl#createOpSendMsg} may fail due to |
| 88 | + * many errors, such like allocate more memory failed when calling |
| 89 | + * {@link Commands#serializeCommandSendWithSize}. We mock an error here. |
| 90 | + */ |
| 91 | + AtomicBoolean failure = new AtomicBoolean(true); |
| 92 | + BaseCommand threadLocalBaseCommand = Commands.LOCAL_BASE_COMMAND.get(); |
| 93 | + BaseCommand spyBaseCommand = spy(threadLocalBaseCommand); |
| 94 | + doAnswer(invocation -> { |
| 95 | + if (failure.get()) { |
| 96 | + throw new RuntimeException("mocked exception"); |
| 97 | + } else { |
| 98 | + return invocation.callRealMethod(); |
| 99 | + } |
| 100 | + }).when(spyBaseCommand).setSend(); |
| 101 | + Commands.LOCAL_BASE_COMMAND.set(spyBaseCommand); |
| 102 | + |
| 103 | + // Failed sending 3 times. |
| 104 | + producer.sendAsync("1"); |
| 105 | + if (flushSend.contains(1)) { |
| 106 | + producer.flushAsync(); |
| 107 | + } |
| 108 | + producer.sendAsync("2"); |
| 109 | + if (flushSend.contains(2)) { |
| 110 | + producer.flushAsync(); |
| 111 | + } |
| 112 | + producer.sendAsync("3"); |
| 113 | + if (flushSend.contains(3)) { |
| 114 | + producer.flushAsync(); |
| 115 | + } |
| 116 | + // Publishing is finished eventually. |
| 117 | + failure.set(false); |
| 118 | + producer.flush(); |
| 119 | + Awaitility.await().untilAsserted(() -> { |
| 120 | + assertTrue(admin.topics().getStats(topic).getSubscriptions().get(subscription).getMsgBacklog() > 0); |
| 121 | + }); |
| 122 | + |
| 123 | + // Verify: all messages can be consumed. |
| 124 | + ConsumerImpl<String> consumer = (ConsumerImpl<String>) pulsarClient.newConsumer(Schema.STRING).topic(topic) |
| 125 | + .subscriptionName(subscription).subscribe(); |
| 126 | + Message<String> msg1 = consumer.receive(2, TimeUnit.SECONDS); |
| 127 | + assertNotNull(msg1); |
| 128 | + assertEquals(msg1.getValue(), "1"); |
| 129 | + Message<String> msg2 = consumer.receive(2, TimeUnit.SECONDS); |
| 130 | + assertNotNull(msg2); |
| 131 | + assertEquals(msg2.getValue(), "2"); |
| 132 | + Message<String> msg3 = consumer.receive(2, TimeUnit.SECONDS); |
| 133 | + assertNotNull(msg3); |
| 134 | + assertEquals(msg3.getValue(), "3"); |
| 135 | + |
| 136 | + // cleanup. |
| 137 | + consumer.close(); |
| 138 | + producer.close(); |
| 139 | + admin.topics().delete(topic, false); |
| 140 | + } |
| 141 | +} |
0 commit comments