Search in sources :

Example 1 with SwapFirstLastByteInterceptor

use of org.apache.samza.system.eventhub.producer.SwapFirstLastByteInterceptor in project samza by apache.

the class TestEventHubSystemConsumer method testSinglePartitionConsumptionInterceptor.

@Test
public void testSinglePartitionConsumptionInterceptor() throws Exception {
    String systemName = "eventhubs";
    String streamName = "testStream";
    // needs to be less than BLOCKING_QUEUE_SIZE
    int numEvents = 10;
    int partitionId = 0;
    Interceptor interceptor = new SwapFirstLastByteInterceptor();
    TestMetricsRegistry testMetrics = new TestMetricsRegistry();
    Map<SystemStreamPartition, List<EventData>> eventData = new HashMap<>();
    SystemStreamPartition ssp = new SystemStreamPartition(systemName, streamName, new Partition(partitionId));
    Map<String, Interceptor> interceptors = new HashMap<>();
    interceptors.put(streamName, interceptor);
    // create EventData
    List<EventData> singlePartitionEventData = MockEventData.generateEventData(numEvents);
    eventData.put(ssp, singlePartitionEventData);
    // Set configs
    Map<String, String> configMap = new HashMap<>();
    configMap.put(String.format(EventHubConfig.CONFIG_STREAM_LIST, systemName), streamName);
    configMap.put(String.format(EventHubConfig.CONFIG_STREAM_NAMESPACE, streamName), EVENTHUB_NAMESPACE);
    configMap.put(String.format(EventHubConfig.CONFIG_STREAM_SAS_KEY_NAME, streamName), EVENTHUB_KEY_NAME);
    configMap.put(String.format(EventHubConfig.CONFIG_STREAM_SAS_TOKEN, streamName), EVENTHUB_KEY);
    configMap.put(String.format(EventHubConfig.CONFIG_STREAM_ENTITYPATH, streamName), MOCK_ENTITY_1);
    MapConfig config = new MapConfig(configMap);
    MockEventHubClientManagerFactory eventHubClientWrapperFactory = new MockEventHubClientManagerFactory(eventData);
    EventHubSystemConsumer consumer = new EventHubSystemConsumer(new EventHubConfig(config), systemName, eventHubClientWrapperFactory, interceptors, testMetrics);
    consumer.register(ssp, EventHubSystemConsumer.END_OF_STREAM);
    consumer.start();
    // Mock received data from EventHub
    eventHubClientWrapperFactory.sendToHandlers(consumer.streamPartitionHandlers);
    List<IncomingMessageEnvelope> result = consumer.poll(Collections.singleton(ssp), 1000).get(ssp);
    verifyEvents(result, singlePartitionEventData, interceptor);
    Assert.assertEquals(testMetrics.getCounters(streamName).size(), 3);
    Assert.assertEquals(testMetrics.getGauges(streamName).size(), 2);
    Map<String, Counter> counters = testMetrics.getCounters(streamName).stream().collect(Collectors.toMap(Counter::getName, Function.identity()));
    Assert.assertEquals(counters.get(EventHubSystemConsumer.EVENT_READ_RATE).getCount(), numEvents);
    Assert.assertEquals(counters.get(EventHubSystemConsumer.READ_ERRORS).getCount(), 0);
}
Also used : Partition(org.apache.samza.Partition) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) SwapFirstLastByteInterceptor(org.apache.samza.system.eventhub.producer.SwapFirstLastByteInterceptor) Counter(org.apache.samza.metrics.Counter) MapConfig(org.apache.samza.config.MapConfig) PassThroughInterceptor(org.apache.samza.system.eventhub.admin.PassThroughInterceptor) SwapFirstLastByteInterceptor(org.apache.samza.system.eventhub.producer.SwapFirstLastByteInterceptor) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Partition (org.apache.samza.Partition)1 MapConfig (org.apache.samza.config.MapConfig)1 Counter (org.apache.samza.metrics.Counter)1 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)1 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)1 PassThroughInterceptor (org.apache.samza.system.eventhub.admin.PassThroughInterceptor)1 SwapFirstLastByteInterceptor (org.apache.samza.system.eventhub.producer.SwapFirstLastByteInterceptor)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1