forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue 4803][client] return null if the message value/data is not set…
… by producer (apache#6379) Fixes apache#4803 ### Motivation Allow the typed consumer receive messages with `null` value if the producer sends message without payload. ### Modifications - add a flag in `MessageMetadata` to indicate if the payload is set when the message is created - check and return `null` if the flag is not set when reading data from a message
- Loading branch information
Showing
15 changed files
with
370 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/NullValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.service; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import lombok.Cleanup; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.pulsar.client.api.Consumer; | ||
import org.apache.pulsar.client.api.Message; | ||
import org.apache.pulsar.client.api.Producer; | ||
import org.apache.pulsar.client.api.PulsarClientException; | ||
import org.apache.pulsar.client.api.Schema; | ||
import org.testng.Assert; | ||
import org.testng.annotations.AfterMethod; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Null value message produce and consume test. | ||
*/ | ||
@Slf4j | ||
public class NullValueTest extends BrokerTestBase { | ||
|
||
@BeforeMethod | ||
@Override | ||
protected void setup() throws Exception { | ||
super.baseSetup(); | ||
} | ||
|
||
@AfterMethod | ||
@Override | ||
protected void cleanup() throws Exception { | ||
super.internalCleanup(); | ||
} | ||
|
||
@Test | ||
public void nullValueBytesSchemaTest() throws PulsarClientException { | ||
String topic = "persistent://prop/ns-abc/null-value-bytes-test"; | ||
|
||
@Cleanup | ||
Producer producer = pulsarClient.newProducer() | ||
.topic(topic) | ||
.create(); | ||
|
||
@Cleanup | ||
Consumer consumer = pulsarClient.newConsumer() | ||
.topic(topic) | ||
.subscriptionName("test") | ||
.subscribe(); | ||
|
||
int numMessage = 10; | ||
for (int i = 0; i < numMessage; i++) { | ||
if (i % 2 == 0) { | ||
producer.newMessage().value("not null".getBytes()).send(); | ||
} else { | ||
producer.newMessage().value(null).send(); | ||
} | ||
} | ||
|
||
for (int i = 0; i < numMessage; i++) { | ||
Message message = consumer.receive(); | ||
if (i % 2 == 0) { | ||
Assert.assertNotNull(message.getData()); | ||
Assert.assertNotNull(message.getValue()); | ||
Assert.assertEquals(new String(message.getData()), "not null"); | ||
} else { | ||
Assert.assertNull(message.getData()); | ||
Assert.assertNull(message.getValue()); | ||
} | ||
consumer.acknowledge(message); | ||
} | ||
|
||
for (int i = 0; i < numMessage; i++) { | ||
if (i % 2 == 0) { | ||
producer.newMessage().value("not null".getBytes()).sendAsync(); | ||
} else { | ||
producer.newMessage().value(null).sendAsync(); | ||
} | ||
} | ||
|
||
for (int i = 0; i < numMessage; i++) { | ||
CompletableFuture<Message> completableFuture = consumer.receiveAsync(); | ||
final int index = i; | ||
completableFuture.whenComplete((message, throwable) -> { | ||
Assert.assertNull(throwable); | ||
if (index % 2 == 0) { | ||
Assert.assertNotNull(message.getData()); | ||
Assert.assertNotNull(message.getValue()); | ||
Assert.assertEquals(new String(message.getData()), "not null"); | ||
} else { | ||
Assert.assertNull(message.getData()); | ||
Assert.assertNull(message.getValue()); | ||
} | ||
try { | ||
consumer.acknowledge(message); | ||
} catch (PulsarClientException e) { | ||
Assert.assertNull(e); | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void nullValueBooleanSchemaTest() throws PulsarClientException { | ||
String topic = "persistent://prop/ns-abc/null-value-bool-test"; | ||
|
||
@Cleanup | ||
Producer<Boolean> producer = pulsarClient.newProducer(Schema.BOOL) | ||
.topic(topic) | ||
.create(); | ||
|
||
@Cleanup | ||
Consumer<Boolean> consumer = pulsarClient.newConsumer(Schema.BOOL) | ||
.topic(topic) | ||
.subscriptionName("test") | ||
.subscribe(); | ||
|
||
int numMessage = 10; | ||
for (int i = 0; i < numMessage; i++) { | ||
producer.newMessage().value(null).sendAsync(); | ||
} | ||
|
||
for (int i = 0; i < numMessage; i++) { | ||
Message<Boolean> message = consumer.receive(); | ||
Assert.assertNull(message.getValue()); | ||
Assert.assertNull(message.getData()); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.