Skip to content

Commit

Permalink
test: add test for KeyCodec and compress (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Jan 3, 2025
1 parent 95f2bb5 commit 82b3db5
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.netease.nim.camellia.redis.proxy.test;

import com.netease.nim.camellia.redis.proxy.upstream.embedded.storage.compress.NoneCompressor;
import com.netease.nim.camellia.redis.proxy.upstream.embedded.storage.compress.ZstdCompressor;
import org.junit.Assert;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

/**
* Created by caojiajun on 2025/1/3
*/
public class CompressorTest {

@Test
public void testZstd1() {
ZstdCompressor compressor = new ZstdCompressor();
String data = "A".repeat(2000);
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
System.out.println(bytes.length);
byte[] compressed = compressor.compress(bytes, 0, bytes.length);
System.out.println(compressed.length);
byte[] decompress = compressor.decompress(compressed, 0, compressed.length, bytes.length);
Assert.assertEquals(new String(decompress, StandardCharsets.UTF_8), data);
}

@Test
public void testZstd2() {
ZstdCompressor compressor = new ZstdCompressor();
String data = "A".repeat(2000);
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
System.out.println(bytes.length);
byte[] compressed = compressor.compress(bytes, 10, bytes.length - 10);
System.out.println(compressed.length);
byte[] decompress = compressor.decompress(compressed, 0, compressed.length, bytes.length - 10);
Assert.assertEquals(new String(decompress, StandardCharsets.UTF_8), "A".repeat(1990));
}

@Test
public void testNone() {
NoneCompressor compressor = new NoneCompressor();
String data = "A".repeat(2000);
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
System.out.println(bytes.length);
byte[] compressed = compressor.compress(bytes, 0, bytes.length);
System.out.println(compressed.length);
byte[] decompress = compressor.decompress(compressed, 0, compressed.length, bytes.length);
Assert.assertEquals(new String(compressed, StandardCharsets.UTF_8), data);
Assert.assertEquals(new String(decompress, StandardCharsets.UTF_8), data);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.netease.nim.camellia.redis.proxy.test;

import com.netease.nim.camellia.redis.proxy.upstream.embedded.storage.codec.KeyCodec;
import com.netease.nim.camellia.redis.proxy.upstream.embedded.storage.enums.DataType;
import com.netease.nim.camellia.redis.proxy.upstream.embedded.storage.key.KeyInfo;
import com.netease.nim.camellia.redis.proxy.upstream.embedded.storage.value.ValueLocation;
import com.netease.nim.camellia.tools.utils.BytesKey;
import org.junit.Assert;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static com.netease.nim.camellia.redis.proxy.upstream.embedded.storage.constants.EmbeddedStorageConstants._4k;

/**
* Created by caojiajun on 2025/1/3
*/
public class KeyCodecTest {

@Test
public void test() {
Map<BytesKey, KeyInfo> map = new HashMap<>();
KeyInfo keyInfo1 = keyInfo("k1", 0, null, "v1");
KeyInfo keyInfo2 = keyInfo("k2", 10000, null, "v2");
KeyInfo keyInfo3 = keyInfo("k3", 20000, null, null);
KeyInfo keyInfo4 = keyInfo("k4", 0, new ValueLocation(1, 1000), null);
KeyInfo keyInfo5 = keyInfo("k5", 30000, new ValueLocation(2, 2000), "v5");
KeyInfo keyInfo6 = keyInfo("k6", 40000, new ValueLocation(3, 3000), null);

map.put(new BytesKey(keyInfo1.getKey()), keyInfo1);
map.put(new BytesKey(keyInfo2.getKey()), keyInfo2);
map.put(new BytesKey(keyInfo3.getKey()), keyInfo3);
map.put(new BytesKey(keyInfo4.getKey()), keyInfo4);
map.put(new BytesKey(keyInfo5.getKey()), keyInfo5);
map.put(new BytesKey(keyInfo6.getKey()), keyInfo6);

byte[] bytes = KeyCodec.encodeBucket(map);
Assert.assertNotNull(bytes);
Assert.assertEquals(bytes.length, _4k);

Map<BytesKey, KeyInfo> decodeMap = KeyCodec.decodeBucket(bytes);
Assert.assertEquals(decodeMap.size(), map.size());

{
KeyInfo keyInfo = decodeMap.get(new BytesKey("k1".getBytes(StandardCharsets.UTF_8)));
Assert.assertEquals(new String(keyInfo.getExtra(), StandardCharsets.UTF_8), "v1");
}
{
KeyInfo keyInfo = decodeMap.get(new BytesKey("k2".getBytes(StandardCharsets.UTF_8)));
Assert.assertEquals(new String(keyInfo.getExtra(), StandardCharsets.UTF_8), "v2");
}
{
KeyInfo keyInfo = decodeMap.get(new BytesKey("k3".getBytes(StandardCharsets.UTF_8)));
Assert.assertNull(keyInfo.getExtra());
}
{
KeyInfo keyInfo = decodeMap.get(new BytesKey("k4".getBytes(StandardCharsets.UTF_8)));
Assert.assertNull(keyInfo.getExtra());
}
{
KeyInfo keyInfo = decodeMap.get(new BytesKey("k5".getBytes(StandardCharsets.UTF_8)));
Assert.assertEquals(new String(keyInfo.getExtra(), StandardCharsets.UTF_8), "v5");
}
{
KeyInfo keyInfo = decodeMap.get(new BytesKey("k6".getBytes(StandardCharsets.UTF_8)));
Assert.assertNull(keyInfo.getExtra());
}
}

private KeyInfo keyInfo(String key, long expireTime, ValueLocation location, String extra) {
KeyInfo keyInfo = new KeyInfo();
keyInfo.setDataType(DataType.string);
keyInfo.setKey(key.getBytes(StandardCharsets.UTF_8));
keyInfo.setExpireTime(expireTime);
keyInfo.setValueLocation(location);
if (extra != null) {
keyInfo.setExtra(extra.getBytes(StandardCharsets.UTF_8));
}
return keyInfo;
}
}

0 comments on commit 82b3db5

Please sign in to comment.