CavalliumDBEngine/src/test/java/it/cavallium/dbengine/tests/TestLLDictionaryLeaks.java

180 lines
5.6 KiB
Java
Raw Normal View History

2023-02-22 22:31:36 +01:00
package it.cavallium.dbengine.tests;
2021-08-28 22:42:51 +02:00
2023-02-22 22:31:36 +01:00
import static it.cavallium.dbengine.tests.DbTestUtils.ensureNoLeaks;
import static it.cavallium.dbengine.tests.DbTestUtils.runVoid;
2021-08-28 22:42:51 +02:00
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
2023-02-22 22:31:36 +01:00
import it.cavallium.dbengine.tests.DbTestUtils.TempDb;
2023-03-06 12:19:08 +01:00
import it.cavallium.buffer.Buf;
2021-08-28 22:42:51 +02:00
import it.cavallium.dbengine.database.LLDictionary;
import it.cavallium.dbengine.database.LLDictionaryResultType;
import it.cavallium.dbengine.database.LLKeyValueDatabase;
import it.cavallium.dbengine.database.UpdateMode;
import it.cavallium.dbengine.database.UpdateReturnMode;
2023-02-22 22:31:36 +01:00
import java.io.IOException;
2021-08-28 22:42:51 +02:00
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;
2021-11-08 16:33:41 +01:00
import org.jetbrains.annotations.Nullable;
2021-08-28 22:42:51 +02:00
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
2021-09-06 01:15:13 +02:00
public abstract class TestLLDictionaryLeaks {
2021-08-28 22:42:51 +02:00
private TempDb tempDb;
private LLKeyValueDatabase db;
2021-09-06 01:15:13 +02:00
protected abstract TemporaryDbGenerator getTempDbGenerator();
2021-08-28 22:42:51 +02:00
@BeforeEach
2023-02-22 22:31:36 +01:00
public void beforeEach() throws IOException {
2023-02-28 23:10:31 +01:00
ensureNoLeaks();
2023-02-22 22:31:36 +01:00
tempDb = Objects.requireNonNull(getTempDbGenerator().openTempDb(), "TempDB");
2021-08-28 22:42:51 +02:00
db = tempDb.db();
}
2021-08-29 01:15:51 +02:00
@AfterEach
2023-02-22 22:31:36 +01:00
public void afterEach() throws IOException {
getTempDbGenerator().closeTempDb(tempDb);
2023-02-28 23:10:31 +01:00
ensureNoLeaks();
2021-08-29 01:15:51 +02:00
}
2021-08-28 22:42:51 +02:00
public static Stream<Arguments> provideArguments() {
return Arrays.stream(UpdateMode.values()).map(Arguments::of);
}
public static Stream<Arguments> providePutArguments() {
var updateModes = Arrays.stream(UpdateMode.values());
return updateModes.flatMap(updateMode -> {
var resultTypes = Arrays.stream(LLDictionaryResultType.values());
return resultTypes.map(resultType -> Arguments.of(updateMode, resultType));
});
}
public static Stream<Arguments> provideUpdateArguments() {
var updateModes = Arrays.stream(UpdateMode.values());
return updateModes.flatMap(updateMode -> {
var resultTypes = Arrays.stream(UpdateReturnMode.values());
return resultTypes.map(resultType -> Arguments.of(updateMode, resultType));
});
}
private LLDictionary getDict(UpdateMode updateMode) {
2023-02-22 22:31:36 +01:00
var dict = DbTestUtils.tempDictionary(db, updateMode);
var key1 = fromString("test-key-1");
var key2 = fromString("test-key-2");
var key3 = fromString("test-key-3");
var key4 = fromString("test-key-4");
var value = fromString("test-value");
dict.put(key1, value, LLDictionaryResultType.VOID);
dict.put(key2, value, LLDictionaryResultType.VOID);
dict.put(key3, value, LLDictionaryResultType.VOID);
dict.put(key4, value, LLDictionaryResultType.VOID);
2021-08-28 22:42:51 +02:00
return dict;
}
2023-02-22 22:31:36 +01:00
private Buf fromString(String s) {
2021-08-28 22:42:51 +02:00
var sb = s.getBytes(StandardCharsets.UTF_8);
2023-02-22 22:31:36 +01:00
var b = Buf.create(sb.length);
b.addElements(0, sb);
assert b.size() == sb.length;
return b;
2021-08-28 22:42:51 +02:00
}
@Test
public void testNoOp() {
}
2021-08-31 15:50:11 +02:00
@Test
public void testNoOpAllocation() {
for (int i = 0; i < 10; i++) {
2023-02-22 22:31:36 +01:00
var a = Buf.create(i * 512);
2021-08-31 15:50:11 +02:00
}
}
2021-08-28 22:42:51 +02:00
@ParameterizedTest
@MethodSource("provideArguments")
public void testGetDict(UpdateMode updateMode) {
var dict = getDict(updateMode);
}
@ParameterizedTest
@MethodSource("provideArguments")
public void testGetColumnName(UpdateMode updateMode) {
var dict = getDict(updateMode);
dict.getColumnName();
}
@ParameterizedTest
@MethodSource("provideArguments")
public void testGet(UpdateMode updateMode) {
var dict = getDict(updateMode);
2023-02-22 22:31:36 +01:00
var key = fromString("test");
dict.get(null, key);
dict.get(null, key);
dict.get(null, key);
2021-08-28 22:42:51 +02:00
}
@ParameterizedTest
@MethodSource("providePutArguments")
public void testPut(UpdateMode updateMode, LLDictionaryResultType resultType) {
var dict = getDict(updateMode);
2023-02-22 22:31:36 +01:00
var key = fromString("test-key");
var value = fromString("test-value");
dict.put(key, value, resultType);
2021-08-28 22:42:51 +02:00
}
@ParameterizedTest
@MethodSource("provideArguments")
public void testGetUpdateMode(UpdateMode updateMode) {
var dict = getDict(updateMode);
2022-09-12 20:14:56 +02:00
assertEquals(updateMode, dict.getUpdateMode());
2021-08-28 22:42:51 +02:00
}
@ParameterizedTest
@MethodSource("provideUpdateArguments")
public void testUpdate(UpdateMode updateMode, UpdateReturnMode updateReturnMode) {
var dict = getDict(updateMode);
2023-02-22 22:31:36 +01:00
var key = fromString("test-key");
runVoid(updateMode == UpdateMode.DISALLOW, () -> dict.update(key, this::pass, updateReturnMode));
runVoid(updateMode == UpdateMode.DISALLOW, () -> dict.update(key, this::pass, updateReturnMode));
runVoid(updateMode == UpdateMode.DISALLOW, () -> dict.update(key, this::pass, updateReturnMode));
}
private Buf pass(@Nullable Buf old) {
if (old == null) return null;
return old.copy();
2021-11-08 16:33:41 +01:00
}
2021-08-28 22:42:51 +02:00
@ParameterizedTest
@MethodSource("provideArguments")
public void testUpdateAndGetDelta(UpdateMode updateMode) {
var dict = getDict(updateMode);
2023-02-22 22:31:36 +01:00
var key = fromString("test-key");
runVoid(updateMode == UpdateMode.DISALLOW, () -> dict.updateAndGetDelta(key, this::pass));
runVoid(updateMode == UpdateMode.DISALLOW, () -> dict.updateAndGetDelta(key, this::pass));
runVoid(updateMode == UpdateMode.DISALLOW, () -> dict.updateAndGetDelta(key, this::pass));
2021-08-28 22:42:51 +02:00
}
@ParameterizedTest
@MethodSource("provideArguments")
public void testClear(UpdateMode updateMode) {
var dict = getDict(updateMode);
2023-02-22 22:31:36 +01:00
dict.clear();
2021-08-28 22:42:51 +02:00
}
@ParameterizedTest
@MethodSource("providePutArguments")
public void testRemove(UpdateMode updateMode, LLDictionaryResultType resultType) {
var dict = getDict(updateMode);
2023-02-22 22:31:36 +01:00
var key = fromString("test-key");
dict.remove(key, resultType);
2021-08-28 22:42:51 +02:00
}
}