Search in sources :

Example 11 with MockKeyValueStore

use of org.apache.kafka.test.MockKeyValueStore in project kafka by apache.

the class ProcessorStateManagerTest method shouldPreserveStreamsExceptionOnFlushIfStoreThrows.

@Test
public void shouldPreserveStreamsExceptionOnFlushIfStoreThrows() {
    final StreamsException exception = new StreamsException("KABOOM!");
    final ProcessorStateManager stateManager = getStateManager(Task.TaskType.ACTIVE);
    final MockKeyValueStore stateStore = new MockKeyValueStore(persistentStoreName, true) {

        @Override
        public void flush() {
            throw exception;
        }
    };
    stateManager.registerStore(stateStore, stateStore.stateRestoreCallback, null);
    final StreamsException thrown = assertThrows(StreamsException.class, stateManager::flush);
    assertEquals(exception, thrown);
}
Also used : StreamsException(org.apache.kafka.streams.errors.StreamsException) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) Test(org.junit.Test)

Example 12 with MockKeyValueStore

use of org.apache.kafka.test.MockKeyValueStore in project kafka by apache.

the class ProcessorStateManagerTest method shouldThrowIfStateStoreHasLoggingDisabled.

@Test
public void shouldThrowIfStateStoreHasLoggingDisabled() {
    final ProcessorStateManager stateMgr = getStateManager(Task.TaskType.ACTIVE);
    final String storeName = "store-with-logging-disabled";
    final MockKeyValueStore storeWithLoggingDisabled = new MockKeyValueStore(storeName, true);
    stateMgr.registerStore(storeWithLoggingDisabled, null, null);
    assertThrows("Registered state store " + storeName + " does not have a registered changelog partition." + " This may happen if logging is disabled for the state store.", IllegalStateException.class, () -> stateMgr.registeredChangelogPartitionFor(storeName));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) Test(org.junit.Test)

Example 13 with MockKeyValueStore

use of org.apache.kafka.test.MockKeyValueStore in project kafka by apache.

the class ProcessorStateManagerTest method shouldCloseAllStoresEvenIfStoreThrowsException.

@Test
public void shouldCloseAllStoresEvenIfStoreThrowsException() {
    final AtomicBoolean closedStore = new AtomicBoolean(false);
    final MockKeyValueStore stateStore1 = new MockKeyValueStore(persistentStoreName, true) {

        @Override
        public void close() {
            throw new RuntimeException("KABOOM!");
        }
    };
    final MockKeyValueStore stateStore2 = new MockKeyValueStore(persistentStoreTwoName, true) {

        @Override
        public void close() {
            closedStore.set(true);
        }
    };
    final ProcessorStateManager stateManager = getStateManager(Task.TaskType.ACTIVE);
    stateManager.registerStore(stateStore1, stateStore1.stateRestoreCallback, null);
    stateManager.registerStore(stateStore2, stateStore2.stateRestoreCallback, null);
    try {
        stateManager.close();
    } catch (final ProcessorStateException expected) {
    /* ignore */
    }
    Assert.assertTrue(closedStore.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) Test(org.junit.Test)

Example 14 with MockKeyValueStore

use of org.apache.kafka.test.MockKeyValueStore in project kafka by apache.

the class StreamTaskTest method createOptimizedStatefulTask.

private StreamTask createOptimizedStatefulTask(final StreamsConfig config, final Consumer<byte[], byte[]> consumer) {
    final StateStore stateStore = new MockKeyValueStore(storeName, true);
    final ProcessorTopology topology = ProcessorTopologyFactories.with(singletonList(source1), mkMap(mkEntry(topic1, source1)), singletonList(stateStore), Collections.singletonMap(storeName, topic1));
    final InternalProcessorContext context = new ProcessorContextImpl(taskId, config, stateManager, streamsMetrics, null);
    return new StreamTask(taskId, mkSet(partition1), topology, consumer, new TopologyConfig(null, config, new Properties()).getTaskConfig(), streamsMetrics, stateDirectory, cache, time, stateManager, recordCollector, context, logContext);
}
Also used : StateStore(org.apache.kafka.streams.processor.StateStore) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) Utils.mkProperties(org.apache.kafka.common.utils.Utils.mkProperties) Properties(java.util.Properties) TopologyConfig(org.apache.kafka.streams.processor.internals.namedtopology.TopologyConfig)

Example 15 with MockKeyValueStore

use of org.apache.kafka.test.MockKeyValueStore in project kafka by apache.

the class StateManagerUtilTest method testRegisterStateStoreFailToLockStateDirectory.

@Test
public void testRegisterStateStoreFailToLockStateDirectory() {
    expect(topology.stateStores()).andReturn(singletonList(new MockKeyValueStore("store", false)));
    expect(stateManager.taskId()).andReturn(taskId);
    expect(stateDirectory.lock(taskId)).andReturn(false);
    ctrl.checkOrder(true);
    ctrl.replay();
    final LockException thrown = assertThrows(LockException.class, () -> StateManagerUtil.registerStateStores(logger, "logPrefix:", topology, stateManager, stateDirectory, processorContext));
    assertEquals("logPrefix:Failed to lock the state directory for task 0_0", thrown.getMessage());
    ctrl.verify();
}
Also used : LockException(org.apache.kafka.streams.errors.LockException) MockKeyValueStore(org.apache.kafka.test.MockKeyValueStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

MockKeyValueStore (org.apache.kafka.test.MockKeyValueStore)15 Test (org.junit.Test)11 Properties (java.util.Properties)5 ProcessorStateException (org.apache.kafka.streams.errors.ProcessorStateException)4 Utils.mkProperties (org.apache.kafka.common.utils.Utils.mkProperties)3 StreamsException (org.apache.kafka.streams.errors.StreamsException)3 TopologyConfig (org.apache.kafka.streams.processor.internals.namedtopology.TopologyConfig)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 TopicPartition (org.apache.kafka.common.TopicPartition)2 StateStore (org.apache.kafka.streams.processor.StateStore)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 File (java.io.File)1 BigInteger (java.math.BigInteger)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 MockConsumer (org.apache.kafka.clients.consumer.MockConsumer)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 Serdes (org.apache.kafka.common.serialization.Serdes)1 Utils.mkSet (org.apache.kafka.common.utils.Utils.mkSet)1