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

212 lines
6.8 KiB
Java
Raw Normal View History

2021-08-28 22:42:51 +02:00
package it.cavallium.dbengine;
2021-08-29 01:15:51 +02:00
import static it.cavallium.dbengine.DbTestUtils.destroyAllocator;
import static it.cavallium.dbengine.DbTestUtils.ensureNoLeaks;
import static it.cavallium.dbengine.DbTestUtils.newAllocator;
import static it.cavallium.dbengine.SyncUtils.*;
2021-08-28 22:42:51 +02:00
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
2022-03-16 13:47:56 +01:00
import io.netty5.buffer.api.Buffer;
2022-07-15 02:44:50 +02:00
import io.netty5.util.Send;
2021-08-28 22:42:51 +02:00
import it.cavallium.dbengine.DbTestUtils.TempDb;
2021-08-29 01:15:51 +02:00
import it.cavallium.dbengine.DbTestUtils.TestAllocator;
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;
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;
import reactor.core.publisher.Mono;
2021-09-06 01:15:13 +02:00
public abstract class TestLLDictionaryLeaks {
2021-08-28 22:42:51 +02:00
2021-08-29 01:15:51 +02:00
private TestAllocator allocator;
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
public void beforeEach() {
2021-08-29 01:15:51 +02:00
this.allocator = newAllocator();
ensureNoLeaks(allocator.allocator(), false, false);
2021-09-06 01:15:13 +02:00
tempDb = Objects.requireNonNull(getTempDbGenerator().openTempDb(allocator).block(), "TempDB");
2021-08-28 22:42:51 +02:00
db = tempDb.db();
}
2021-08-29 01:15:51 +02:00
@AfterEach
public void afterEach() {
2021-09-06 01:15:13 +02:00
getTempDbGenerator().closeTempDb(tempDb).block();
ensureNoLeaks(allocator.allocator(), true, false);
2021-08-29 01:15:51 +02:00
destroyAllocator(allocator);
}
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) {
2021-08-31 15:50:11 +02:00
var dict = DbTestUtils.tempDictionary(db, updateMode).blockOptional().orElseThrow();
2021-08-28 22:42:51 +02:00
var key1 = Mono.fromCallable(() -> fromString("test-key-1"));
var key2 = Mono.fromCallable(() -> fromString("test-key-2"));
var key3 = Mono.fromCallable(() -> fromString("test-key-3"));
var key4 = Mono.fromCallable(() -> fromString("test-key-4"));
var value = Mono.fromCallable(() -> fromString("test-value"));
dict.put(key1, value, LLDictionaryResultType.VOID).block();
dict.put(key2, value, LLDictionaryResultType.VOID).block();
dict.put(key3, value, LLDictionaryResultType.VOID).block();
dict.put(key4, value, LLDictionaryResultType.VOID).block();
return dict;
}
private Buffer fromString(String s) {
2021-08-28 22:42:51 +02:00
var sb = s.getBytes(StandardCharsets.UTF_8);
var b = db.getAllocator().allocate(sb.length);
try {
b.writeBytes(sb);
assert b.readableBytes() == sb.length;
return b;
} catch (Throwable ex) {
b.close();
throw ex;
2021-08-31 15:50:11 +02:00
}
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++) {
var a = allocator.allocator().allocate(i * 512);
a.send().receive().close();
}
}
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 testGetAllocator(UpdateMode updateMode) {
var dict = getDict(updateMode);
dict.getAllocator();
}
@ParameterizedTest
@MethodSource("provideArguments")
public void testGet(UpdateMode updateMode) {
var dict = getDict(updateMode);
var key = Mono.fromCallable(() -> fromString("test"));
2022-01-26 14:22:54 +01:00
runVoid(dict.get(null, key).then());
2022-03-24 21:14:17 +01:00
runVoid(dict.get(null, key).then());
runVoid(dict.get(null, key).then());
2021-08-28 22:42:51 +02:00
}
@ParameterizedTest
@MethodSource("providePutArguments")
public void testPut(UpdateMode updateMode, LLDictionaryResultType resultType) {
var dict = getDict(updateMode);
var key = Mono.fromCallable(() -> fromString("test-key"));
var value = Mono.fromCallable(() -> fromString("test-value"));
runVoid(dict.put(key, value, resultType).then().doOnDiscard(Buffer.class, Buffer::close));
2021-08-28 22:42:51 +02:00
}
@ParameterizedTest
@MethodSource("provideArguments")
public void testGetUpdateMode(UpdateMode updateMode) {
var dict = getDict(updateMode);
assertEquals(updateMode, run(dict.getUpdateMode()));
2021-08-28 22:42:51 +02:00
}
@ParameterizedTest
@MethodSource("provideUpdateArguments")
public void testUpdate(UpdateMode updateMode, UpdateReturnMode updateReturnMode) {
var dict = getDict(updateMode);
var key = Mono.fromCallable(() -> fromString("test-key"));
runVoid(updateMode == UpdateMode.DISALLOW,
2022-03-02 18:33:58 +01:00
dict.update(key, this::pass, updateReturnMode).then()
2021-08-28 22:42:51 +02:00
);
runVoid(updateMode == UpdateMode.DISALLOW,
2022-03-02 18:33:58 +01:00
dict.update(key, this::pass, updateReturnMode).then()
2021-08-28 22:42:51 +02:00
);
runVoid(updateMode == UpdateMode.DISALLOW,
2022-01-26 14:22:54 +01:00
dict.update(key, this::pass, updateReturnMode).then()
2021-08-28 22:42:51 +02:00
);
}
private Buffer pass(@Nullable Buffer 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);
var key = Mono.fromCallable(() -> fromString("test-key"));
runVoid(updateMode == UpdateMode.DISALLOW,
2022-03-02 18:33:58 +01:00
dict.updateAndGetDelta(key, this::pass).then()
2021-08-28 22:42:51 +02:00
);
runVoid(updateMode == UpdateMode.DISALLOW,
2022-03-02 18:33:58 +01:00
dict.updateAndGetDelta(key, this::pass).then()
2021-08-28 22:42:51 +02:00
);
runVoid(updateMode == UpdateMode.DISALLOW,
2022-01-26 14:22:54 +01:00
dict.updateAndGetDelta(key, this::pass).then()
2021-08-28 22:42:51 +02:00
);
}
@ParameterizedTest
@MethodSource("provideArguments")
public void testClear(UpdateMode updateMode) {
var dict = getDict(updateMode);
runVoid(dict.clear());
2021-08-28 22:42:51 +02:00
}
@ParameterizedTest
@MethodSource("providePutArguments")
public void testRemove(UpdateMode updateMode, LLDictionaryResultType resultType) {
var dict = getDict(updateMode);
var key = Mono.fromCallable(() -> fromString("test-key"));
runVoid(dict.remove(key, resultType).then().doOnDiscard(Buffer.class, Buffer::close));
2021-08-28 22:42:51 +02:00
}
}