Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 6561] [pulsar-client] Fix NPE while call getLastMessageId #6562

Merged
merged 1 commit into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class BatchMessageAcker {

private BatchMessageAcker() {
this.bitSet = null;
this.bitSet = new BitSet();
this.batchSize = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
*/
package org.apache.pulsar.client.impl;

import java.util.BitSet;

class BatchMessageAckerDisabled extends BatchMessageAcker {

static final BatchMessageAckerDisabled INSTANCE = new BatchMessageAckerDisabled();

private BatchMessageAckerDisabled() {
super(null, 0);
super(new BitSet(), 0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.apache.pulsar.common.util.ObjectMapperFactory;
import org.testng.annotations.Test;

public class BatchMessageIdImplTest {
Expand Down Expand Up @@ -68,4 +72,30 @@ public void equalsTest() {
assertEquals(msgId, batchMsgId4);
}

@Test
public void deserializationTest() {
// initialize BitSet with null
BatchMessageAcker ackerDisabled = new BatchMessageAcker(null, 0);
BatchMessageIdImpl batchMsgId = new BatchMessageIdImpl(0, 0, 0, 0, ackerDisabled);

ObjectWriter writer = ObjectMapperFactory.create().writerWithDefaultPrettyPrinter();

try {
writer.writeValueAsString(batchMsgId);
fail("Shouldn't be deserialized");
} catch (JsonProcessingException e) {
// expected
assertTrue(e.getCause() instanceof NullPointerException);
}

// use the default BatchMessageAckerDisabled
BatchMessageIdImpl batchMsgIdToDeserialize = new BatchMessageIdImpl(0, 0, 0, 0);

try {
writer.writeValueAsString(batchMsgIdToDeserialize);
} catch (JsonProcessingException e) {
fail("Should be successful");
}
}

}