use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project apache-kafka-on-k8s by banzaicloud.
the class ThreadCacheTest method shouldPutAll.
@Test
public void shouldPutAll() {
final ThreadCache cache = new ThreadCache(logContext, 100000, new MockStreamsMetrics(new Metrics()));
cache.putAll(namespace, Arrays.asList(KeyValue.pair(Bytes.wrap(new byte[] { 0 }), dirtyEntry(new byte[] { 5 })), KeyValue.pair(Bytes.wrap(new byte[] { 1 }), dirtyEntry(new byte[] { 6 }))));
assertArrayEquals(new byte[] { 5 }, cache.get(namespace, Bytes.wrap(new byte[] { 0 })).value);
assertArrayEquals(new byte[] { 6 }, cache.get(namespace, Bytes.wrap(new byte[] { 1 })).value);
}
use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project apache-kafka-on-k8s by banzaicloud.
the class ThreadCacheTest method shouldNotFlushAfterDelete.
@Test
public void shouldNotFlushAfterDelete() {
final Bytes key = Bytes.wrap(new byte[] { 0 });
final ThreadCache cache = new ThreadCache(logContext, 10000L, new MockStreamsMetrics(new Metrics()));
final List<ThreadCache.DirtyEntry> received = new ArrayList<>();
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);
}
use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project apache-kafka-on-k8s by banzaicloud.
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);
ThreadCache cache = new ThreadCache(logContext, 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);
}
use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project apache-kafka-on-k8s by banzaicloud.
the class ThreadCacheTest method shouldPutIfAbsent.
@Test
public void shouldPutIfAbsent() {
final ThreadCache cache = new ThreadCache(logContext, 100000, new MockStreamsMetrics(new Metrics()));
final Bytes key = Bytes.wrap(new byte[] { 10 });
final byte[] value = { 30 };
assertNull(cache.putIfAbsent(namespace, key, dirtyEntry(value)));
assertArrayEquals(value, cache.putIfAbsent(namespace, key, dirtyEntry(new byte[] { 8 })).value);
assertArrayEquals(value, cache.get(namespace, key).value);
}
use of org.apache.kafka.streams.processor.internals.MockStreamsMetrics in project apache-kafka-on-k8s by banzaicloud.
the class ThreadCacheTest method shouldCleanupNamedCacheOnClose.
@Test
public void shouldCleanupNamedCacheOnClose() {
final ThreadCache cache = new ThreadCache(logContext, 100000, new MockStreamsMetrics(new Metrics()));
cache.put(namespace1, Bytes.wrap(new byte[] { 1 }), cleanEntry(new byte[] { 1 }));
cache.put(namespace2, Bytes.wrap(new byte[] { 1 }), cleanEntry(new byte[] { 1 }));
assertEquals(cache.size(), 2);
cache.close(namespace2);
assertEquals(cache.size(), 1);
assertNull(cache.get(namespace2, Bytes.wrap(new byte[] { 1 })));
}
Aggregations