-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for KeyCodec and compress (#364)
- Loading branch information
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...is-proxy-core/src/test/java/com/netease/nim/camellia/redis/proxy/test/CompressorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...edis-proxy-core/src/test/java/com/netease/nim/camellia/redis/proxy/test/KeyCodecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |