use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by apache.
the class TopicsTest method testLookUpWithException.
@Test
public void testLookUpWithException() throws Exception {
String topicName = "persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName;
admin.topics().createNonPartitionedTopic(topicName);
NamespaceService nameSpaceService = mock(NamespaceService.class);
CompletableFuture future = new CompletableFuture();
future.completeExceptionally(new BrokerServiceException("Fake Exception"));
CompletableFuture existFuture = new CompletableFuture();
existFuture.complete(true);
doReturn(future).when(nameSpaceService).getBrokerServiceUrlAsync(any(), any());
doReturn(existFuture).when(nameSpaceService).checkTopicExists(any());
doReturn(nameSpaceService).when(pulsar).getNamespaceService();
AsyncResponse asyncResponse = mock(AsyncResponse.class);
ProducerMessages producerMessages = new ProducerMessages();
producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(Schema.INT64.getSchemaInfo()));
String message = "[]";
producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
}));
topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName, false, producerMessages);
ArgumentCaptor<RestException> responseCaptor = ArgumentCaptor.forClass(RestException.class);
verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
Assert.assertEquals(responseCaptor.getValue().getMessage(), "Can't find owner of given topic.");
}
use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by apache.
the class TopicsTest method testProduceFailed.
@Test
public void testProduceFailed() throws Exception {
admin.topics().createNonPartitionedTopic("persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName);
pulsar.getBrokerService().getTopic("persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName, false).thenAccept(topic -> {
try {
PersistentTopic mockPersistentTopic = spy((PersistentTopic) topic.get());
AtomicInteger count = new AtomicInteger();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Topic.PublishContext publishContext = invocationOnMock.getArgument(1);
if (count.getAndIncrement() < 2) {
publishContext.completed(null, -1, -1);
} else {
publishContext.completed(new BrokerServiceException.TopicFencedException("Fake exception"), -1, -1);
}
return null;
}
}).when(mockPersistentTopic).publishMessage(any(), any());
BrokerService mockBrokerService = spy(pulsar.getBrokerService());
doReturn(CompletableFuture.completedFuture(Optional.of(mockPersistentTopic))).when(mockBrokerService).getTopic(anyString(), anyBoolean());
doReturn(mockBrokerService).when(pulsar).getBrokerService();
AsyncResponse asyncResponse = mock(AsyncResponse.class);
Schema<String> schema = StringSchema.utf8();
ProducerMessages producerMessages = new ProducerMessages();
producerMessages.setKeySchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
String message = "[" + "{\"key\":\"my-key\",\"payload\":\"RestProducer:1\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:2\",\"eventTime\":1603045262772,\"sequenceId\":2}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:3\",\"eventTime\":1603045262772,\"sequenceId\":3}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:4\",\"eventTime\":1603045262772,\"sequenceId\":4}]";
producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
}));
// Previous request should trigger namespace bundle loading, retry produce.
topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName, false, producerMessages);
ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
Object responseEntity = responseCaptor.getValue().getEntity();
Assert.assertTrue(responseEntity instanceof ProducerAcks);
ProducerAcks response = (ProducerAcks) responseEntity;
Assert.assertEquals(response.getMessagePublishResults().size(), 4);
int errorResponse = 0;
for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
int errorCode = response.getMessagePublishResults().get(index).getErrorCode();
if (0 == errorCode) {
Assert.assertEquals(Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2]), -1);
Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
} else {
errorResponse++;
Assert.assertEquals(errorCode, 2);
Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorMsg(), "org.apache.pulsar.broker.service.BrokerServiceException$" + "TopicFencedException: Fake exception");
}
}
// Add entry start to fail after 2nd operation, we published 4 msg so expecting 2 error response.
Assert.assertTrue(errorResponse == 2);
} catch (Throwable e) {
Assert.fail(e.getMessage());
}
}).get();
}
use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by yahoo.
the class TopicsTest method testProduceWithJsonSchema.
@Test
public void testProduceWithJsonSchema() throws Exception {
String topicName = "persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName;
admin.topics().createNonPartitionedTopic(topicName);
AsyncResponse asyncResponse = mock(AsyncResponse.class);
GenericSchema jsonSchema = GenericJsonSchema.of(JSONSchema.of(SchemaDefinition.builder().withPojo(PC.class).build()).getSchemaInfo());
PC pc = new PC("dell", "alienware", 2021, GPU.AMD, new Seller("WA", "main street", 98004));
PC anotherPc = new PC("asus", "rog", 2020, GPU.NVIDIA, new Seller("CA", "back street", 90232));
Consumer consumer = pulsarClient.newConsumer(jsonSchema).topic(topicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).subscribe();
ProducerMessages producerMessages = new ProducerMessages();
producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(jsonSchema.getSchemaInfo()));
String message = "[" + "{\"key\":\"my-key\",\"payload\":\"" + ObjectMapperFactory.getThreadLocal().writeValueAsString(pc).replace("\"", "\\\"") + "\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"" + ObjectMapperFactory.getThreadLocal().writeValueAsString(anotherPc).replace("\"", "\\\"") + "\",\"eventTime\":1603045262772,\"sequenceId\":2}]";
producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
}));
topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName, false, producerMessages);
ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
Object responseEntity = responseCaptor.getValue().getEntity();
Assert.assertTrue(responseEntity instanceof ProducerAcks);
ProducerAcks response = (ProducerAcks) responseEntity;
Assert.assertEquals(response.getMessagePublishResults().size(), 2);
Assert.assertEquals(response.getSchemaVersion(), 0);
for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
Assert.assertEquals(Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2]), -1);
Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorCode(), 0);
Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
}
List<PC> expected = Arrays.asList(pc, anotherPc);
Message<String> msg = null;
// Assert all messages published by REST producer can be received by consumer in expected order.
for (int i = 0; i < 2; i++) {
msg = consumer.receive(2, TimeUnit.SECONDS);
PC msgPc = ObjectMapperFactory.getThreadLocal().treeToValue(((GenericJsonRecord) jsonSchema.decode(msg.getData())).getJsonNode(), PC.class);
Assert.assertEquals(msgPc.brand, expected.get(i).brand);
Assert.assertEquals(msgPc.model, expected.get(i).model);
Assert.assertEquals(msgPc.year, expected.get(i).year);
Assert.assertEquals(msgPc.gpu, expected.get(i).gpu);
Assert.assertEquals(msgPc.seller.state, expected.get(i).seller.state);
Assert.assertEquals(msgPc.seller.street, expected.get(i).seller.street);
Assert.assertEquals(msgPc.seller.zipCode, expected.get(i).seller.zipCode);
Assert.assertEquals("my-key", msg.getKey());
}
}
use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by yahoo.
the class TopicsTest method testProduceToPartitionedTopic.
@Test
public void testProduceToPartitionedTopic() throws Exception {
admin.topics().createPartitionedTopic("persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName + "-p", 5);
AsyncResponse asyncResponse = mock(AsyncResponse.class);
Schema<String> schema = StringSchema.utf8();
ProducerMessages producerMessages = new ProducerMessages();
producerMessages.setKeySchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
String message = "[" + "{\"key\":\"my-key\",\"payload\":\"RestProducer:1\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:2\",\"eventTime\":1603045262772,\"sequenceId\":2}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:3\",\"eventTime\":1603045262772,\"sequenceId\":3}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:4\",\"eventTime\":1603045262772,\"sequenceId\":4}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:5\",\"eventTime\":1603045262772,\"sequenceId\":5}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:6\",\"eventTime\":1603045262772,\"sequenceId\":6}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:7\",\"eventTime\":1603045262772,\"sequenceId\":7}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:8\",\"eventTime\":1603045262772,\"sequenceId\":8}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:9\",\"eventTime\":1603045262772,\"sequenceId\":9}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:10\",\"eventTime\":1603045262772,\"sequenceId\":10}]";
producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
}));
topics.produceOnPersistentTopic(asyncResponse, testTenant, testNamespace, testTopicName + "-p", false, producerMessages);
ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
Object responseEntity = responseCaptor.getValue().getEntity();
Assert.assertTrue(responseEntity instanceof ProducerAcks);
ProducerAcks response = (ProducerAcks) responseEntity;
Assert.assertEquals(response.getMessagePublishResults().size(), 10);
Assert.assertEquals(response.getSchemaVersion(), 0);
int[] messagePerPartition = new int[5];
for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
messagePerPartition[Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2])]++;
Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorCode(), 0);
Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
}
for (int index = 0; index < messagePerPartition.length; index++) {
// We publish to each partition in round robin mode so each partition should get at most 2 message.
Assert.assertTrue(messagePerPartition[index] <= 2);
}
}
use of org.apache.pulsar.websocket.data.ProducerMessages in project pulsar by yahoo.
the class TopicsTest method testProduceToPartitionedTopicSpecificPartition.
@Test
public void testProduceToPartitionedTopicSpecificPartition() throws Exception {
admin.topics().createPartitionedTopic("persistent://" + testTenant + "/" + testNamespace + "/" + testTopicName, 5);
AsyncResponse asyncResponse = mock(AsyncResponse.class);
Schema<String> schema = StringSchema.utf8();
ProducerMessages producerMessages = new ProducerMessages();
producerMessages.setKeySchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
producerMessages.setValueSchema(ObjectMapperFactory.getThreadLocal().writeValueAsString(schema.getSchemaInfo()));
String message = "[" + "{\"key\":\"my-key\",\"payload\":\"RestProducer:1\",\"eventTime\":1603045262772,\"sequenceId\":1}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:2\",\"eventTime\":1603045262772,\"sequenceId\":2}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:3\",\"eventTime\":1603045262772,\"sequenceId\":3}," + "{\"key\":\"my-key\",\"payload\":\"RestProducer:4\",\"eventTime\":1603045262772,\"sequenceId\":4}]";
producerMessages.setMessages(ObjectMapperFactory.getThreadLocal().readValue(message, new TypeReference<List<ProducerMessage>>() {
}));
topics.produceOnPersistentTopicPartition(asyncResponse, testTenant, testNamespace, testTopicName, 2, false, producerMessages);
ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
verify(asyncResponse, timeout(5000).times(1)).resume(responseCaptor.capture());
Assert.assertEquals(responseCaptor.getValue().getStatus(), Response.Status.OK.getStatusCode());
Object responseEntity = responseCaptor.getValue().getEntity();
Assert.assertTrue(responseEntity instanceof ProducerAcks);
ProducerAcks response = (ProducerAcks) responseEntity;
Assert.assertEquals(response.getMessagePublishResults().size(), 4);
Assert.assertEquals(response.getSchemaVersion(), 0);
for (int index = 0; index < response.getMessagePublishResults().size(); index++) {
Assert.assertEquals(Integer.parseInt(response.getMessagePublishResults().get(index).getMessageId().split(":")[2]), 2);
Assert.assertEquals(response.getMessagePublishResults().get(index).getErrorCode(), 0);
Assert.assertTrue(response.getMessagePublishResults().get(index).getMessageId().length() > 0);
}
}
Aggregations