use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class MetadataTableTest method testBasic.
@Test
public void testBasic() throws Exception {
try (MetadataTable table = createMetadataTable()) {
Assert.assertTrue(table.listTopics().isEmpty());
Assert.assertTrue(table.listTopics(NamespaceId.DEFAULT).isEmpty());
try {
table.getMetadata(NamespaceId.DEFAULT.topic("t1"));
Assert.fail("Expected exception on topic that doesn't exist");
} catch (TopicNotFoundException e) {
// expected
}
// Create topic default:t1
table.createTopic(new TopicMetadata(NamespaceId.DEFAULT.topic("t1"), "ttl", 10));
;
Assert.assertEquals(1, table.listTopics(NamespaceId.DEFAULT).size());
// Create topic default:t2
TopicMetadata topicMetadata = new TopicMetadata(NamespaceId.DEFAULT.topic("t2"), "ttl", 20);
table.createTopic(topicMetadata);
Assert.assertEquals(topicMetadata.getTopicId(), table.getMetadata(NamespaceId.DEFAULT.topic("t2")).getTopicId());
Assert.assertEquals(topicMetadata.getTTL(), table.getMetadata(NamespaceId.DEFAULT.topic("t2")).getTTL());
Assert.assertEquals(1, table.getMetadata(NamespaceId.DEFAULT.topic("t2")).getGeneration());
// Create topic system:t3
table.createTopic(new TopicMetadata(NamespaceId.SYSTEM.topic("t3"), "ttl", 30));
// List default namespace, should get 2
Assert.assertEquals(2, table.listTopics(NamespaceId.DEFAULT).size());
// List all topics, should get 3
Assert.assertEquals(3, table.listTopics().size());
// Delete t1
table.deleteTopic(NamespaceId.DEFAULT.topic("t1"));
Assert.assertEquals(1, table.listTopics(NamespaceId.DEFAULT).size());
Assert.assertEquals(2, table.listTopics().size());
// Delete t2
table.deleteTopic(NamespaceId.DEFAULT.topic("t2"));
Assert.assertTrue(table.listTopics(NamespaceId.DEFAULT).isEmpty());
Assert.assertEquals(1, table.listTopics(NamespaceId.SYSTEM).size());
// Delete t3
table.deleteTopic(NamespaceId.SYSTEM.topic("t3"));
Assert.assertTrue(table.listTopics(NamespaceId.DEFAULT).isEmpty());
Assert.assertTrue(table.listTopics(NamespaceId.SYSTEM).isEmpty());
Assert.assertTrue(table.listTopics().isEmpty());
}
}
use of co.cask.cdap.messaging.TopicMetadata in project cdap by caskdata.
the class MessagingHttpServiceTest method testChunkConsume.
@Test
public void testChunkConsume() throws Exception {
// This test is to verify the message fetching body producer works correctly
TopicId topicId = new NamespaceId("ns1").topic("testChunkConsume");
client.createTopic(new TopicMetadata(topicId));
// Publish 10 messages, each payload is half the size of the chunk size
int payloadSize = cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_CONSUME_CHUNK_SIZE) / 2;
for (int i = 0; i < 10; i++) {
String payload = Strings.repeat(Integer.toString(i), payloadSize);
client.publish(StoreRequestBuilder.of(topicId).addPayloads(payload).build());
}
// Fetch messages. All of them should be fetched correctly
List<RawMessage> messages = new ArrayList<>();
try (CloseableIterator<RawMessage> iterator = client.prepareFetch(topicId).fetch()) {
Iterators.addAll(messages, iterator);
}
Assert.assertEquals(10, messages.size());
for (int i = 0; i < 10; i++) {
RawMessage message = messages.get(i);
Assert.assertEquals(payloadSize, message.getPayload().length);
String payload = Strings.repeat(Integer.toString(i), payloadSize);
Assert.assertEquals(payload, Bytes.toString(message.getPayload()));
}
client.deleteTopic(topicId);
}
Aggregations