Search in sources :

Example 31 with MockStreamsMetrics

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

the class ThreadCacheTest method shouldThrowIfNoPeekNextKey.

@Test(expected = NoSuchElementException.class)
public void shouldThrowIfNoPeekNextKey() throws Exception {
    final ThreadCache cache = new ThreadCache("testCache", 10000L, new MockStreamsMetrics(new Metrics()));
    final ThreadCache.MemoryLRUCacheBytesIterator iterator = cache.range("", Bytes.wrap(new byte[] { 0 }), Bytes.wrap(new byte[] { 1 }));
    iterator.peekNextKey();
}
Also used : 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 32 with MockStreamsMetrics

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

the class ThreadCacheTest method evict.

@Test
public void evict() throws IOException {
    final List<KeyValue<String, String>> received = new ArrayList<>();
    List<KeyValue<String, String>> expected = Collections.singletonList(new KeyValue<>("K1", "V1"));
    List<KeyValue<String, String>> toInsert = Arrays.asList(new KeyValue<>("K1", "V1"), new KeyValue<>("K2", "V2"), new KeyValue<>("K3", "V3"), new KeyValue<>("K4", "V4"), new KeyValue<>("K5", "V5"));
    final KeyValue<String, String> kv = toInsert.get(0);
    final String namespace = "kafka";
    ThreadCache cache = new ThreadCache("testCache", memoryCacheEntrySize(kv.key.getBytes(), kv.value.getBytes(), ""), new MockStreamsMetrics(new Metrics()));
    cache.addDirtyEntryFlushListener(namespace, new ThreadCache.DirtyEntryFlushListener() {

        @Override
        public void apply(final List<ThreadCache.DirtyEntry> dirty) {
            for (ThreadCache.DirtyEntry dirtyEntry : dirty) {
                received.add(new KeyValue<>(dirtyEntry.key().toString(), new String(dirtyEntry.newValue())));
            }
        }
    });
    for (KeyValue<String, String> kvToInsert : toInsert) {
        final Bytes key = Bytes.wrap(kvToInsert.key.getBytes());
        final byte[] value = kvToInsert.value.getBytes();
        cache.put(namespace, key, new LRUCacheEntry(value, true, 1, 1, 1, ""));
    }
    for (int i = 0; i < expected.size(); i++) {
        KeyValue<String, String> expectedRecord = expected.get(i);
        KeyValue<String, String> actualRecord = received.get(i);
        assertEquals(expectedRecord, actualRecord);
    }
    assertEquals(cache.evicts(), 4);
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) ArrayList(java.util.ArrayList) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) Test(org.junit.Test)

Example 33 with MockStreamsMetrics

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

the class ThreadCacheTest method shouldCleanupNamedCacheOnClose.

@Test
public void shouldCleanupNamedCacheOnClose() throws Exception {
    final ThreadCache cache = new ThreadCache("testCache", 100000, new MockStreamsMetrics(new Metrics()));
    cache.put("one", Bytes.wrap(new byte[] { 1 }), cleanEntry(new byte[] { 1 }));
    cache.put("two", Bytes.wrap(new byte[] { 1 }), cleanEntry(new byte[] { 1 }));
    assertEquals(cache.size(), 2);
    cache.close("two");
    assertEquals(cache.size(), 1);
    assertNull(cache.get("two", Bytes.wrap(new byte[] { 1 })));
}
Also used : 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 34 with MockStreamsMetrics

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

the class ThreadCacheTest method basicPutGet.

@Test
public void basicPutGet() throws IOException {
    List<KeyValue<String, String>> toInsert = Arrays.asList(new KeyValue<>("K1", "V1"), new KeyValue<>("K2", "V2"), new KeyValue<>("K3", "V3"), new KeyValue<>("K4", "V4"), new KeyValue<>("K5", "V5"));
    final KeyValue<String, String> kv = toInsert.get(0);
    final String name = "name";
    ThreadCache cache = new ThreadCache("testCache", toInsert.size() * memoryCacheEntrySize(kv.key.getBytes(), kv.value.getBytes(), ""), new MockStreamsMetrics(new Metrics()));
    for (KeyValue<String, String> kvToInsert : toInsert) {
        Bytes key = Bytes.wrap(kvToInsert.key.getBytes());
        byte[] value = kvToInsert.value.getBytes();
        cache.put(name, key, new LRUCacheEntry(value, true, 1L, 1L, 1, ""));
    }
    for (KeyValue<String, String> kvToInsert : toInsert) {
        Bytes key = Bytes.wrap(kvToInsert.key.getBytes());
        LRUCacheEntry entry = cache.get(name, key);
        assertEquals(entry.isDirty(), true);
        assertEquals(new String(entry.value), kvToInsert.value);
    }
    assertEquals(cache.gets(), 5);
    assertEquals(cache.puts(), 5);
    assertEquals(cache.evicts(), 0);
    assertEquals(cache.flushes(), 0);
}
Also used : Bytes(org.apache.kafka.common.utils.Bytes) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) KeyValue(org.apache.kafka.streams.KeyValue) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Test(org.junit.Test)

Example 35 with MockStreamsMetrics

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

the class ThreadCacheTest method shouldPutIfAbsent.

@Test
public void shouldPutIfAbsent() throws Exception {
    final ThreadCache cache = new ThreadCache("testCache", 100000, new MockStreamsMetrics(new Metrics()));
    final Bytes key = Bytes.wrap(new byte[] { 10 });
    final byte[] value = { 30 };
    assertNull(cache.putIfAbsent("n", key, dirtyEntry(value)));
    assertArrayEquals(value, cache.putIfAbsent("n", key, dirtyEntry(new byte[] { 8 })).value);
    assertArrayEquals(value, cache.get("n", key).value);
}
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)

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