use of org.apache.kafka.common.metrics.Metrics in project kafka by apache.
the class ThreadCacheTest method shouldFlushDirtyEntriesForNamespace.
@Test
public void shouldFlushDirtyEntriesForNamespace() throws Exception {
final ThreadCache cache = new ThreadCache("testCache", 100000, new MockStreamsMetrics(new Metrics()));
final List<byte[]> received = new ArrayList<>();
cache.addDirtyEntryFlushListener("1", new ThreadCache.DirtyEntryFlushListener() {
@Override
public void apply(final List<ThreadCache.DirtyEntry> dirty) {
for (ThreadCache.DirtyEntry dirtyEntry : dirty) {
received.add(dirtyEntry.key().get());
}
}
});
final List<byte[]> expected = Arrays.asList(new byte[] { 0 }, new byte[] { 1 }, new byte[] { 2 });
for (byte[] bytes : expected) {
cache.put("1", Bytes.wrap(bytes), dirtyEntry(bytes));
}
cache.put("2", Bytes.wrap(new byte[] { 4 }), dirtyEntry(new byte[] { 4 }));
cache.flush("1");
assertEquals(expected, received);
}
use of org.apache.kafka.common.metrics.Metrics in project kafka by apache.
the class ThreadCacheTest method shouldNotForwardCleanEntryOnEviction.
@Test
public void shouldNotForwardCleanEntryOnEviction() throws Exception {
final ThreadCache cache = new ThreadCache("testCache", 0, new MockStreamsMetrics(new Metrics()));
final List<ThreadCache.DirtyEntry> received = new ArrayList<>();
cache.addDirtyEntryFlushListener("name", new ThreadCache.DirtyEntryFlushListener() {
@Override
public void apply(final List<ThreadCache.DirtyEntry> dirty) {
received.addAll(dirty);
}
});
cache.put("name", Bytes.wrap(new byte[] { 1 }), cleanEntry(new byte[] { 0 }));
assertEquals(0, received.size());
}
use of org.apache.kafka.common.metrics.Metrics in project kafka by apache.
the class ThreadCacheTest method shouldNotBlowUpOnNonExistentKeyWhenDeleting.
@Test
public void shouldNotBlowUpOnNonExistentKeyWhenDeleting() throws Exception {
final Bytes key = Bytes.wrap(new byte[] { 0 });
final ThreadCache cache = new ThreadCache("testCache", 10000L, new MockStreamsMetrics(new Metrics()));
cache.put("name", key, dirtyEntry(key.get()));
assertNull(cache.delete("name", Bytes.wrap(new byte[] { 1 })));
}
use of org.apache.kafka.common.metrics.Metrics 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);
}
use of org.apache.kafka.common.metrics.Metrics 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());
}
Aggregations