Search in sources :

Example 21 with MockProcessorContext

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

the class RocksDBKeyValueStoreSupplierTest method shouldNotBeLoggingEnabledStoreWhenLoggingNotEnabled.

@Test
public void shouldNotBeLoggingEnabledStoreWhenLoggingNotEnabled() throws Exception {
    store = createStore(false, false);
    final List<ProducerRecord> logged = new ArrayList<>();
    final NoOpRecordCollector collector = new NoOpRecordCollector() {

        @Override
        public <K, V> void send(final String topic, K key, V value, Integer partition, Long timestamp, Serializer<K> keySerializer, Serializer<V> valueSerializer) {
            logged.add(new ProducerRecord<K, V>(topic, partition, timestamp, key, value));
        }
    };
    final MockProcessorContext context = new MockProcessorContext(TestUtils.tempDirectory(), Serdes.String(), Serdes.String(), collector, cache);
    context.setTime(1);
    store.init(context, store);
    store.put("a", "b");
    assertTrue(logged.isEmpty());
}
Also used : NoOpRecordCollector(org.apache.kafka.test.NoOpRecordCollector) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ArrayList(java.util.ArrayList) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Serializer(org.apache.kafka.common.serialization.Serializer) Test(org.junit.Test)

Example 22 with MockProcessorContext

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

the class RocksDBKeyValueStoreTest method shouldPerformAllQueriesWithCachingDisabled.

@Test
public void shouldPerformAllQueriesWithCachingDisabled() throws Exception {
    final KeyValueStoreTestDriver<Integer, String> driver = KeyValueStoreTestDriver.create(Integer.class, String.class);
    final MockProcessorContext context = (MockProcessorContext) driver.context();
    final KeyValueStore<Integer, String> store = createStore(context, Integer.class, String.class, false, false);
    context.setTime(1L);
    store.put(1, "hi");
    store.put(2, "goodbye");
    final KeyValueIterator<Integer, String> range = store.all();
    assertEquals("hi", range.next().value);
    assertEquals("goodbye", range.next().value);
    assertFalse(range.hasNext());
}
Also used : MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Test(org.junit.Test)

Example 23 with MockProcessorContext

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

the class RocksDBKeyValueStoreTest method shouldCloseOpenIteratorsWhenStoreClosedAndThrowInvalidStateStoreOnHasNextAndNext.

@Test
public void shouldCloseOpenIteratorsWhenStoreClosedAndThrowInvalidStateStoreOnHasNextAndNext() throws Exception {
    final KeyValueStoreTestDriver<Integer, String> driver = KeyValueStoreTestDriver.create(Integer.class, String.class);
    final MockProcessorContext context = (MockProcessorContext) driver.context();
    context.setTime(1L);
    final KeyValueStore<Integer, String> store = createStore(context, Integer.class, String.class, false, false);
    store.put(1, "hi");
    store.put(2, "goodbye");
    final KeyValueIterator<Integer, String> iteratorOne = store.range(1, 5);
    final KeyValueIterator<Integer, String> iteratorTwo = store.range(1, 4);
    assertTrue(iteratorOne.hasNext());
    assertTrue(iteratorTwo.hasNext());
    store.close();
    try {
        iteratorOne.hasNext();
        fail("should have thrown InvalidStateStoreException on closed store");
    } catch (InvalidStateStoreException e) {
    // ok
    }
    try {
        iteratorOne.next();
        fail("should have thrown InvalidStateStoreException on closed store");
    } catch (InvalidStateStoreException e) {
    // ok
    }
    try {
        iteratorTwo.hasNext();
        fail("should have thrown InvalidStateStoreException on closed store");
    } catch (InvalidStateStoreException e) {
    // ok
    }
    try {
        iteratorTwo.next();
        fail("should have thrown InvalidStateStoreException on closed store");
    } catch (InvalidStateStoreException e) {
    // ok
    }
}
Also used : InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Test(org.junit.Test)

Example 24 with MockProcessorContext

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

the class MeteredSegmentedBytesStoreTest method setUp.

@SuppressWarnings("unchecked")
@Before
public void setUp() throws Exception {
    final Metrics metrics = new Metrics();
    final StreamsMetrics streamsMetrics = new StreamsMetrics() {

        @Override
        public Map<MetricName, ? extends Metric> metrics() {
            return Collections.unmodifiableMap(metrics.metrics());
        }

        @Override
        public Sensor addLatencyAndThroughputSensor(String scopeName, String entityName, String operationName, Sensor.RecordingLevel recordLevel, String... tags) {
            return metrics.sensor(operationName);
        }

        @Override
        public void recordLatency(final Sensor sensor, final long startNs, final long endNs) {
            latencyRecorded.add(sensor.name());
        }

        @Override
        public Sensor addThroughputSensor(String scopeName, String entityName, String operationName, Sensor.RecordingLevel recordLevel, String... tags) {
            return metrics.sensor(operationName);
        }

        @Override
        public void recordThroughput(Sensor sensor, long value) {
            throughputRecorded.add(sensor.name());
        }

        @Override
        public void removeSensor(Sensor sensor) {
            metrics.removeSensor(sensor.name());
        }

        @Override
        public Sensor addSensor(String name, Sensor.RecordingLevel recordLevel) {
            return metrics.sensor(name);
        }

        @Override
        public Sensor addSensor(String name, Sensor.RecordingLevel recordLevel, Sensor... parents) {
            return metrics.sensor(name);
        }
    };
    final MockProcessorContext context = new MockProcessorContext(TestUtils.tempDirectory(), Serdes.String(), Serdes.Long(), new NoOpRecordCollector(), new ThreadCache("testCache", 0, streamsMetrics)) {

        @Override
        public StreamsMetrics metrics() {
            return streamsMetrics;
        }
    };
    store.init(context, store);
}
Also used : MetricName(org.apache.kafka.common.MetricName) Metrics(org.apache.kafka.common.metrics.Metrics) StreamsMetrics(org.apache.kafka.streams.StreamsMetrics) NoOpRecordCollector(org.apache.kafka.test.NoOpRecordCollector) StreamsMetrics(org.apache.kafka.streams.StreamsMetrics) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Sensor(org.apache.kafka.common.metrics.Sensor) Before(org.junit.Before)

Example 25 with MockProcessorContext

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

the class RocksDBSessionStoreTest method before.

@Before
public void before() {
    final RocksDBSegmentedBytesStore bytesStore = new RocksDBSegmentedBytesStore("session-store", 10000L, 3, new SessionKeySchema());
    sessionStore = new RocksDBSessionStore<>(bytesStore, Serdes.String(), Serdes.Long());
    final MockProcessorContext context = new MockProcessorContext(TestUtils.tempDirectory(), Serdes.String(), Serdes.Long(), new NoOpRecordCollector(), new ThreadCache("testCache", 0, new MockStreamsMetrics(new Metrics())));
    sessionStore.init(context, sessionStore);
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) NoOpRecordCollector(org.apache.kafka.test.NoOpRecordCollector) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Before(org.junit.Before)

Aggregations

MockProcessorContext (org.apache.kafka.test.MockProcessorContext)29 NoOpRecordCollector (org.apache.kafka.test.NoOpRecordCollector)17 Test (org.junit.Test)15 Metrics (org.apache.kafka.common.metrics.Metrics)13 Serializer (org.apache.kafka.common.serialization.Serializer)13 Before (org.junit.Before)13 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)11 ArrayList (java.util.ArrayList)6 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)6 StateSerdes (org.apache.kafka.streams.state.StateSerdes)5 MockProducer (org.apache.kafka.clients.producer.MockProducer)4 StreamsException (org.apache.kafka.streams.errors.StreamsException)4 Bytes (org.apache.kafka.common.utils.Bytes)3 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)2 File (java.io.File)1 HashMap (java.util.HashMap)1 MetricName (org.apache.kafka.common.MetricName)1 TopicPartition (org.apache.kafka.common.TopicPartition)1