Search in sources :

Example 41 with TopicMetadata

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());
    }
}
Also used : TopicNotFoundException(co.cask.cdap.api.messaging.TopicNotFoundException) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Example 42 with TopicMetadata

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);
}
Also used : ArrayList(java.util.ArrayList) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) RawMessage(co.cask.cdap.messaging.data.RawMessage) TopicMetadata(co.cask.cdap.messaging.TopicMetadata) Test(org.junit.Test)

Aggregations

TopicMetadata (co.cask.cdap.messaging.TopicMetadata)42 TopicId (co.cask.cdap.proto.id.TopicId)29 Test (org.junit.Test)23 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)14 NamespaceId (co.cask.cdap.proto.id.NamespaceId)13 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)12 MessageId (co.cask.cdap.messaging.data.MessageId)9 RawMessage (co.cask.cdap.messaging.data.RawMessage)9 TreeMap (java.util.TreeMap)7 TopicAlreadyExistsException (co.cask.cdap.api.messaging.TopicAlreadyExistsException)6 TimeProvider (co.cask.cdap.common.utils.TimeProvider)5 MetadataTable (co.cask.cdap.messaging.store.MetadataTable)4 Transaction (org.apache.tephra.Transaction)4 DBException (org.iq80.leveldb.DBException)4 RollbackDetail (co.cask.cdap.messaging.RollbackDetail)3 MessageTable (co.cask.cdap.messaging.store.MessageTable)3 Path (javax.ws.rs.Path)3 Result (org.apache.hadoop.hbase.client.Result)3 HashSet (java.util.HashSet)2