|
| 1 | +package io.netifi.proteus; |
| 2 | + |
| 3 | +import io.netifi.proteus.frames.DestinationSetupFlyweight; |
| 4 | +import io.netty.buffer.*; |
| 5 | +import io.rsocket.Payload; |
| 6 | +import java.nio.charset.Charset; |
| 7 | +import java.util.Collection; |
| 8 | +import java.util.function.Function; |
| 9 | +import org.junit.*; |
| 10 | + |
| 11 | +public class ProteusBrokerServiceTest { |
| 12 | + |
| 13 | + @Test |
| 14 | + public void setupPayloadLeakTest() { |
| 15 | + ByteBuf token = Unpooled.wrappedBuffer("token".getBytes(Charset.defaultCharset())); |
| 16 | + PooledByteBufAllocator alloc = nonCachingAllocator(); |
| 17 | + |
| 18 | + for (int i = 0; i < 100000; i++) { |
| 19 | + Payload payload = |
| 20 | + DefaultProteusBrokerService.getSetupPayload(alloc, "foo", "bar", 123L, token); |
| 21 | + } |
| 22 | + Assert.assertEquals(0, directBuffersCount(alloc)); |
| 23 | + Assert.assertEquals(0, heapBuffersCount(alloc)); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void setupDecodeTest() { |
| 28 | + String expectedToken = "token"; |
| 29 | + String expectedDest = "foo"; |
| 30 | + String expectedGroup = "bar"; |
| 31 | + long expectedKey = 123L; |
| 32 | + |
| 33 | + ByteBuf token = Unpooled.wrappedBuffer(expectedToken.getBytes(Charset.defaultCharset())); |
| 34 | + |
| 35 | + Payload payload = |
| 36 | + DefaultProteusBrokerService.getSetupPayload( |
| 37 | + ByteBufAllocator.DEFAULT, expectedDest, expectedGroup, expectedKey, token); |
| 38 | + ByteBuf metadata = payload.sliceMetadata(); |
| 39 | + String actualDest = DestinationSetupFlyweight.destination(metadata); |
| 40 | + String actualGroup = DestinationSetupFlyweight.group(metadata); |
| 41 | + long actualAccessKey = DestinationSetupFlyweight.accessKey(metadata); |
| 42 | + String actualAccessToken = |
| 43 | + DestinationSetupFlyweight.accessToken(metadata).toString(Charset.defaultCharset()); |
| 44 | + |
| 45 | + Assert.assertEquals(expectedToken, actualAccessToken); |
| 46 | + Assert.assertEquals(expectedDest, actualDest); |
| 47 | + Assert.assertEquals(expectedGroup, actualGroup); |
| 48 | + Assert.assertEquals(expectedKey, actualAccessKey); |
| 49 | + } |
| 50 | + |
| 51 | + private static PooledByteBufAllocator nonCachingAllocator() { |
| 52 | + return new PooledByteBufAllocator(true, 1, 1, 8192, 11, 0, 0, 0); |
| 53 | + } |
| 54 | + |
| 55 | + private static long directBuffersCount(PooledByteBufAllocator alloc) { |
| 56 | + return count(alloc, PooledByteBufAllocator::directArenas); |
| 57 | + } |
| 58 | + |
| 59 | + private static long heapBuffersCount(PooledByteBufAllocator alloc) { |
| 60 | + return count(alloc, PooledByteBufAllocator::heapArenas); |
| 61 | + } |
| 62 | + |
| 63 | + private static long count( |
| 64 | + PooledByteBufAllocator alloc, |
| 65 | + Function<PooledByteBufAllocator, Collection<PoolArenaMetric>> f) { |
| 66 | + return f.apply(alloc).stream().mapToLong(PoolArenaMetric::numActiveAllocations).sum(); |
| 67 | + } |
| 68 | +} |
0 commit comments