Search in sources :

Example 1 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method shouldThrowIllegalArgumentExceptionIfStoreNameIsSameAsCheckpointFileName.

@Test
public void shouldThrowIllegalArgumentExceptionIfStoreNameIsSameAsCheckpointFileName() throws IOException {
    final ProcessorStateManager stateManager = new ProcessorStateManager(taskId, noPartitions, false, stateDirectory, Collections.<String, String>emptyMap(), changelogReader, false, logContext);
    try {
        stateManager.register(new MockStateStore(ProcessorStateManager.CHECKPOINT_FILE_NAME, true), null);
        fail("should have thrown illegal argument exception when store name same as checkpoint file");
    } catch (final IllegalArgumentException e) {
    // pass
    }
}
Also used : MockStateStore(org.apache.kafka.test.MockStateStore) Test(org.junit.Test)

Example 2 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method shouldFlushAllStoresEvenIfStoreThrowsExcepiton.

@Test
public void shouldFlushAllStoresEvenIfStoreThrowsExcepiton() throws IOException {
    final ProcessorStateManager stateManager = new ProcessorStateManager(taskId, Collections.singleton(changelogTopicPartition), false, stateDirectory, Collections.singletonMap(storeName, changelogTopic), changelogReader, false, logContext);
    final AtomicBoolean flushedStore = new AtomicBoolean(false);
    final MockStateStore stateStore1 = new MockStateStore(storeName, true) {

        @Override
        public void flush() {
            throw new RuntimeException("KABOOM!");
        }
    };
    final MockStateStore stateStore2 = new MockStateStore(storeName + "2", true) {

        @Override
        public void flush() {
            flushedStore.set(true);
        }
    };
    stateManager.register(stateStore1, stateStore1.stateRestoreCallback);
    stateManager.register(stateStore2, stateStore2.stateRestoreCallback);
    try {
        stateManager.flush();
    } catch (final ProcessorStateException expected) {
    /* ignode */
    }
    Assert.assertTrue(flushedStore.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockStateStore(org.apache.kafka.test.MockStateStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 3 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method shouldThrowProcessorStateExceptionOnFlushIfStoreThrowsAnException.

@Test
public void shouldThrowProcessorStateExceptionOnFlushIfStoreThrowsAnException() throws IOException {
    final ProcessorStateManager stateManager = new ProcessorStateManager(taskId, Collections.singleton(changelogTopicPartition), false, stateDirectory, Collections.singletonMap(storeName, changelogTopic), changelogReader, false, logContext);
    final MockStateStore stateStore = new MockStateStore(storeName, true) {

        @Override
        public void flush() {
            throw new RuntimeException("KABOOM!");
        }
    };
    stateManager.register(stateStore, stateStore.stateRestoreCallback);
    try {
        stateManager.flush();
        fail("Should throw ProcessorStateException if store flush throws exception");
    } catch (final ProcessorStateException e) {
    // pass
    }
}
Also used : MockStateStore(org.apache.kafka.test.MockStateStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 4 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method shouldNotChangeOffsetsIfAckedOffsetsIsNull.

@Test
public void shouldNotChangeOffsetsIfAckedOffsetsIsNull() throws IOException {
    final Map<TopicPartition, Long> offsets = Collections.singletonMap(persistentStorePartition, 99L);
    checkpoint.write(offsets);
    final MockStateStore persistentStore = new MockStateStore(persistentStoreName, true);
    final ProcessorStateManager stateMgr = new ProcessorStateManager(taskId, noPartitions, false, stateDirectory, Collections.<String, String>emptyMap(), changelogReader, false, logContext);
    stateMgr.register(persistentStore, persistentStore.stateRestoreCallback);
    stateMgr.close(null);
    final Map<TopicPartition, Long> read = checkpoint.read();
    assertThat(read, equalTo(offsets));
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) MockStateStore(org.apache.kafka.test.MockStateStore) Test(org.junit.Test)

Example 5 with MockStateStore

use of org.apache.kafka.test.MockStateStore in project apache-kafka-on-k8s by banzaicloud.

the class ProcessorStateManagerTest method shouldRestoreStoreWithSinglePutRestoreSpecification.

@Test
public void shouldRestoreStoreWithSinglePutRestoreSpecification() throws Exception {
    final TaskId taskId = new TaskId(0, 2);
    final Integer intKey = 1;
    final MockStateStore persistentStore = getPersistentStore();
    final ProcessorStateManager stateMgr = getStandByStateManager(taskId);
    try {
        stateMgr.register(persistentStore, persistentStore.stateRestoreCallback);
        stateMgr.updateStandbyStates(persistentStorePartition, Collections.singletonList(consumerRecord));
        assertThat(persistentStore.keys.size(), is(1));
        assertTrue(persistentStore.keys.contains(intKey));
    } finally {
        stateMgr.close(Collections.<TopicPartition, Long>emptyMap());
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) MockStateStore(org.apache.kafka.test.MockStateStore) Test(org.junit.Test)

Aggregations

MockStateStore (org.apache.kafka.test.MockStateStore)15 Test (org.junit.Test)15 TopicPartition (org.apache.kafka.common.TopicPartition)6 TaskId (org.apache.kafka.streams.processor.TaskId)6 ProcessorStateException (org.apache.kafka.streams.errors.ProcessorStateException)4 File (java.io.File)2 HashMap (java.util.HashMap)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 OffsetCheckpoint (org.apache.kafka.streams.state.internals.OffsetCheckpoint)2 Properties (java.util.Properties)1 StreamsConfig (org.apache.kafka.streams.StreamsConfig)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 InternalStreamsBuilderTest (org.apache.kafka.streams.kstream.internals.InternalStreamsBuilderTest)1 StateRestoreCallback (org.apache.kafka.streams.processor.StateRestoreCallback)1 StateStore (org.apache.kafka.streams.processor.StateStore)1 MockBatchingStateRestoreListener (org.apache.kafka.test.MockBatchingStateRestoreListener)1 NoOpProcessorContext (org.apache.kafka.test.NoOpProcessorContext)1 ProcessorTopologyTestDriver (org.apache.kafka.test.ProcessorTopologyTestDriver)1