Search in sources :

Example 16 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class ThreadCacheTest method shouldPeekAndIterateOverRange.

@Test
public void shouldPeekAndIterateOverRange() throws Exception {
    final ThreadCache cache = new ThreadCache("testCache", 10000L, new MockStreamsMetrics(new Metrics()));
    final byte[][] bytes = { { 0 }, { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 } };
    final String namespace = "streams";
    for (final byte[] aByte : bytes) {
        cache.put(namespace, Bytes.wrap(aByte), dirtyEntry(aByte));
    }
    final ThreadCache.MemoryLRUCacheBytesIterator iterator = cache.range(namespace, Bytes.wrap(new byte[] { 1 }), Bytes.wrap(new byte[] { 4 }));
    int bytesIndex = 1;
    while (iterator.hasNext()) {
        Bytes peekedKey = iterator.peekNextKey();
        final KeyValue<Bytes, LRUCacheEntry> next = iterator.next();
        assertArrayEquals(bytes[bytesIndex], peekedKey.get());
        assertArrayEquals(bytes[bytesIndex], next.key.get());
        bytesIndex++;
    }
    assertEquals(5, bytesIndex);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 17 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class ThreadCacheTest method shouldPeekNextKey.

@Test
public void shouldPeekNextKey() throws Exception {
    final ThreadCache cache = new ThreadCache("testCache", 10000L, new MockStreamsMetrics(new Metrics()));
    final Bytes theByte = Bytes.wrap(new byte[] { 0 });
    final String namespace = "streams";
    cache.put(namespace, theByte, dirtyEntry(theByte.get()));
    final ThreadCache.MemoryLRUCacheBytesIterator iterator = cache.range(namespace, theByte, Bytes.wrap(new byte[] { 1 }));
    assertEquals(theByte, iterator.peekNextKey());
    assertEquals(theByte, iterator.peekNextKey());
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 18 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class ThreadCacheTest method shouldNotFlushAfterDelete.

@Test
public void shouldNotFlushAfterDelete() throws Exception {
    final Bytes key = Bytes.wrap(new byte[] { 0 });
    final ThreadCache cache = new ThreadCache("testCache", 10000L, new MockStreamsMetrics(new Metrics()));
    final List<ThreadCache.DirtyEntry> received = new ArrayList<>();
    final String namespace = "namespace";
    cache.addDirtyEntryFlushListener(namespace, new ThreadCache.DirtyEntryFlushListener() {

        @Override
        public void apply(final List<ThreadCache.DirtyEntry> dirty) {
            received.addAll(dirty);
        }
    });
    cache.put(namespace, key, dirtyEntry(key.get()));
    assertEquals(key.get(), cache.delete(namespace, key).value);
    // flushing should have no further effect
    cache.flush(namespace);
    assertEquals(0, received.size());
    assertEquals(cache.flushes(), 1);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) ArrayList(java.util.ArrayList) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 19 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class ThreadCacheTest method shouldEvictAfterPutIfAbsent.

@Test
public void shouldEvictAfterPutIfAbsent() throws Exception {
    final List<ThreadCache.DirtyEntry> received = new ArrayList<>();
    final String namespace = "namespace";
    final ThreadCache cache = new ThreadCache("testCache", 1, new MockStreamsMetrics(new Metrics()));
    cache.addDirtyEntryFlushListener(namespace, new ThreadCache.DirtyEntryFlushListener() {

        @Override
        public void apply(final List<ThreadCache.DirtyEntry> dirty) {
            received.addAll(dirty);
        }
    });
    cache.putIfAbsent(namespace, Bytes.wrap(new byte[] { 0 }), dirtyEntry(new byte[] { 5 }));
    cache.putIfAbsent(namespace, Bytes.wrap(new byte[] { 1 }), dirtyEntry(new byte[] { 6 }));
    cache.putIfAbsent(namespace, Bytes.wrap(new byte[] { 1 }), dirtyEntry(new byte[] { 6 }));
    assertEquals(cache.evicts(), 3);
    assertEquals(received.size(), 3);
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) ArrayList(java.util.ArrayList) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 20 with MockStreamsMetrics

use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project kafka by apache.

the class KStreamSessionWindowAggregateProcessorTest method initializeStore.

@SuppressWarnings("unchecked")
@Before
public void initializeStore() {
    final File stateDir = TestUtils.tempDirectory();
    context = new MockProcessorContext(stateDir, Serdes.String(), Serdes.String(), new NoOpRecordCollector(), new ThreadCache("testCache", 100000, new MockStreamsMetrics(new Metrics()))) {

        @Override
        public <K, V> void forward(final K key, final V value) {
            results.add(KeyValue.pair(key, value));
        }
    };
    initStore(true);
    processor.init(context);
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) NoOpRecordCollector(org.apache.kafka.test.NoOpRecordCollector) ThreadCache(org.apache.kafka.streams.state.internals.ThreadCache) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) File(java.io.File) MockProcessorContext(org.apache.kafka.test.MockProcessorContext) Before(org.junit.Before)

Aggregations

Metrics (org.apache.kafka.common.metrics.Metrics)40 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)40 Test (org.junit.Test)26 Bytes (org.apache.kafka.common.utils.Bytes)14 Before (org.junit.Before)13 MockProcessorContext (org.apache.kafka.test.MockProcessorContext)11 NoOpRecordCollector (org.apache.kafka.test.NoOpRecordCollector)8 ArrayList (java.util.ArrayList)7 Serializer (org.apache.kafka.common.serialization.Serializer)3 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)3 KeyValue (org.apache.kafka.streams.KeyValue)2 File (java.io.File)1 ThreadCache (org.apache.kafka.streams.state.internals.ThreadCache)1