Search in sources :

Example 1 with TopicMetadata

use of org.apache.pulsar.client.api.TopicMetadata in project incubator-pulsar by apache.

the class FunctionResultRouterTest method testChoosePartitionWithKeySequenceId.

@Test
public void testChoosePartitionWithKeySequenceId() {
    String key1 = "key1";
    String key2 = "key2";
    Message msg1 = mock(Message.class);
    when(msg1.hasKey()).thenReturn(true);
    when(msg1.getKey()).thenReturn(key1);
    // make sure sequence id is different from hashcode, so the test can be tested correctly.
    when(msg1.getSequenceId()).thenReturn((long) ((key1.hashCode() % 100) + 1));
    Message msg2 = mock(Message.class);
    when(msg2.hasKey()).thenReturn(true);
    when(msg2.getKey()).thenReturn(key2);
    when(msg1.getSequenceId()).thenReturn((long) ((key2.hashCode() % 100) + 1));
    FunctionResultRouter router = new FunctionResultRouter(0);
    TopicMetadata metadata = mock(TopicMetadata.class);
    when(metadata.numPartitions()).thenReturn(100);
    assertEquals(hash.makeHash(key1) % 100, router.choosePartition(msg1, metadata));
    assertEquals(hash.makeHash(key2) % 100, router.choosePartition(msg2, metadata));
}
Also used : Message(org.apache.pulsar.client.api.Message) TopicMetadata(org.apache.pulsar.client.api.TopicMetadata) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with TopicMetadata

use of org.apache.pulsar.client.api.TopicMetadata in project incubator-pulsar by apache.

the class FunctionResultRouterTest method testChoosePartitionWithKeyWithoutSequenceId.

@Test
public void testChoosePartitionWithKeyWithoutSequenceId() {
    String key1 = "key1";
    String key2 = "key2";
    Message msg1 = mock(Message.class);
    when(msg1.hasKey()).thenReturn(true);
    when(msg1.getKey()).thenReturn(key1);
    when(msg1.getSequenceId()).thenReturn(-1L);
    Message msg2 = mock(Message.class);
    when(msg2.hasKey()).thenReturn(true);
    when(msg2.getKey()).thenReturn(key2);
    when(msg1.getSequenceId()).thenReturn(-1L);
    FunctionResultRouter router = new FunctionResultRouter(0);
    TopicMetadata metadata = mock(TopicMetadata.class);
    when(metadata.numPartitions()).thenReturn(100);
    assertEquals(hash.makeHash(key1) % 100, router.choosePartition(msg1, metadata));
    assertEquals(hash.makeHash(key2) % 100, router.choosePartition(msg2, metadata));
}
Also used : Message(org.apache.pulsar.client.api.Message) TopicMetadata(org.apache.pulsar.client.api.TopicMetadata) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with TopicMetadata

use of org.apache.pulsar.client.api.TopicMetadata in project incubator-pulsar by apache.

the class FunctionResultRouterTest method testChoosePartitionWithoutKeySequenceId.

@Test
public void testChoosePartitionWithoutKeySequenceId() {
    TopicMetadata topicMetadata = mock(TopicMetadata.class);
    when(topicMetadata.numPartitions()).thenReturn(5);
    FunctionResultRouter router = new FunctionResultRouter(0);
    for (int i = 0; i < 10; i++) {
        Message msg = mock(Message.class);
        when(msg.hasKey()).thenReturn(false);
        when(msg.getKey()).thenReturn(null);
        when(msg.getSequenceId()).thenReturn((long) (2 * i));
        assertEquals((2 * i) % 5, router.choosePartition(msg, topicMetadata));
    }
}
Also used : Message(org.apache.pulsar.client.api.Message) TopicMetadata(org.apache.pulsar.client.api.TopicMetadata) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with TopicMetadata

use of org.apache.pulsar.client.api.TopicMetadata in project incubator-pulsar by apache.

the class FunctionResultRouterTest method testChoosePartitionWithoutKeyWithoutSequenceId.

@Test
public void testChoosePartitionWithoutKeyWithoutSequenceId() {
    Message msg = mock(Message.class);
    when(msg.hasKey()).thenReturn(false);
    when(msg.getKey()).thenReturn(null);
    when(msg.getSequenceId()).thenReturn(-1L);
    TopicMetadata topicMetadata = mock(TopicMetadata.class);
    when(topicMetadata.numPartitions()).thenReturn(5);
    PowerMockito.mockStatic(System.class);
    FunctionResultRouter router = new FunctionResultRouter(0);
    for (int i = 0; i < 10; i++) {
        PowerMockito.when(System.currentTimeMillis()).thenReturn(123450L + i);
        assertEquals(i % 5, router.choosePartition(msg, topicMetadata));
    }
}
Also used : Message(org.apache.pulsar.client.api.Message) TopicMetadata(org.apache.pulsar.client.api.TopicMetadata) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Message (org.apache.pulsar.client.api.Message)4 TopicMetadata (org.apache.pulsar.client.api.TopicMetadata)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Test (org.testng.annotations.Test)4