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

143 lines
4.5 KiB
Java
Raw Normal View History

2021-05-08 03:09:00 +02:00
package it.cavallium.dbengine;
2021-09-06 01:15:13 +02:00
import static it.cavallium.dbengine.DbTestUtils.BIG_STRING;
2021-08-29 01:15:51 +02:00
import static it.cavallium.dbengine.DbTestUtils.destroyAllocator;
2021-08-28 22:42:51 +02:00
import static it.cavallium.dbengine.DbTestUtils.ensureNoLeaks;
2021-09-10 13:34:26 +02:00
import static it.cavallium.dbengine.DbTestUtils.isCIMode;
2021-08-29 01:15:51 +02:00
import static it.cavallium.dbengine.DbTestUtils.newAllocator;
2021-05-08 03:09:00 +02:00
import static it.cavallium.dbengine.DbTestUtils.tempDatabaseMapDictionaryDeepMapHashMap;
import static it.cavallium.dbengine.DbTestUtils.tempDb;
import static it.cavallium.dbengine.DbTestUtils.tempDictionary;
2021-08-29 01:15:51 +02:00
import it.cavallium.dbengine.DbTestUtils.TestAllocator;
2021-05-08 03:09:00 +02:00
import it.cavallium.dbengine.database.UpdateMode;
import java.util.Arrays;
2021-09-10 13:34:26 +02:00
import java.util.List;
2021-05-08 03:09:00 +02:00
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
2021-08-28 22:42:51 +02:00
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
2021-05-08 03:09:00 +02:00
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import reactor.test.StepVerifier.Step;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuple3;
import reactor.util.function.Tuple4;
import reactor.util.function.Tuples;
2021-09-06 01:15:13 +02:00
public abstract class TestDictionaryMapDeepHashMap {
2021-05-08 03:09:00 +02:00
2021-08-29 01:15:51 +02:00
private TestAllocator allocator;
2021-09-08 00:22:39 +02:00
private boolean checkLeaks = true;
2021-08-29 01:15:51 +02:00
2021-05-08 03:09:00 +02:00
private static boolean isTestBadKeysEnabled() {
2021-09-10 13:34:26 +02:00
return !isCIMode() && System.getProperty("badkeys", "true").equalsIgnoreCase("true");
2021-05-08 03:09:00 +02:00
}
2021-09-06 01:15:13 +02:00
protected abstract TemporaryDbGenerator getTempDbGenerator();
2021-05-08 03:09:00 +02:00
private static Stream<Arguments> provideArgumentsPut() {
2021-09-10 13:34:26 +02:00
var goodKeys1 = isCIMode() ? List.of("12345") : List.of("12345", "zebra");
List<String> badKeys1;
2021-05-08 03:09:00 +02:00
if (isTestBadKeysEnabled()) {
2021-09-10 13:34:26 +02:00
badKeys1 = List.of("", "a", "aaaa", "aaaaaa");
2021-05-08 03:09:00 +02:00
} else {
2021-09-10 13:34:26 +02:00
badKeys1 = List.of();
2021-05-08 03:09:00 +02:00
}
2021-09-10 13:34:26 +02:00
var goodKeys2 = isCIMode() ? List.of("123456") : List.of("123456", "anatra", "", "a", "aaaaa", "aaaaaaa");
2021-05-08 03:09:00 +02:00
2021-09-10 13:34:26 +02:00
var values = isCIMode() ? List.of("val") : List.of("a", "", "\0", "\0\0", "z", "azzszgzczqz", BIG_STRING);
2021-05-08 03:09:00 +02:00
Flux<Tuple4<String, String, String, Boolean>> failOnKeys1 = Flux
.fromIterable(badKeys1)
.map(badKey1 -> Tuples.of(
badKey1,
goodKeys2.stream().findAny().orElseThrow(),
values.stream().findAny().orElseThrow(),
true
));
Flux<Tuple4<String, String, String, Boolean>> goodKeys1And2 = Flux
.fromIterable(values)
.map(value -> Tuples.of(
goodKeys1.stream().findAny().orElseThrow(),
goodKeys2.stream().findAny().orElseThrow(),
value,
false
));
Flux<Tuple4<String, String, String, Boolean>> keys1And2 = Flux
.concat(
goodKeys1And2,
failOnKeys1
);
return keys1And2
.flatMap(entryTuple -> Flux
.fromArray(UpdateMode.values())
.map(updateMode -> Tuples.of(updateMode,
entryTuple.getT1(),
entryTuple.getT2(),
entryTuple.getT3(),
entryTuple.getT4()
))
)
.map(fullTuple -> Arguments.of(fullTuple.getT1(),
fullTuple.getT2(),
fullTuple.getT3(),
fullTuple.getT4(),
fullTuple.getT1() != UpdateMode.ALLOW || fullTuple.getT5()
))
.toStream();
}
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-08-28 22:42:51 +02:00
}
@AfterEach
public void afterEach() {
2021-09-10 13:42:02 +02:00
if (!isCIMode() && checkLeaks) {
2021-09-08 00:22:39 +02:00
ensureNoLeaks(allocator.allocator(), true, false);
}
2021-08-29 01:15:51 +02:00
destroyAllocator(allocator);
2021-08-28 22:42:51 +02:00
}
2021-05-08 03:09:00 +02:00
@ParameterizedTest
@MethodSource("provideArgumentsPut")
public void testAtPutValueGetAllValues(UpdateMode updateMode, String key1, String key2, String value, boolean shouldFail) {
var stpVer = StepVerifier
2021-09-06 01:15:13 +02:00
.create(tempDb(getTempDbGenerator(), allocator, db -> tempDictionary(db, updateMode)
2021-05-08 03:09:00 +02:00
.map(dict -> tempDatabaseMapDictionaryDeepMapHashMap(dict, 5))
.flatMapMany(map -> map
2021-09-23 20:57:28 +02:00
.at(null, key1).flatMap(v -> v.putValue(key2, value).doFinally(s -> v.close()))
2021-05-08 03:09:00 +02:00
.thenMany(map
.getAllValues(null)
.map(Entry::getValue)
.flatMap(maps -> Flux.fromIterable(maps.entrySet()))
.map(Entry::getValue)
)
2021-09-23 20:57:28 +02:00
.doFinally(s -> map.close())
2021-05-08 03:09:00 +02:00
)
));
if (shouldFail) {
2021-09-08 00:22:39 +02:00
this.checkLeaks = false;
2021-05-08 03:09:00 +02:00
stpVer.verifyError();
} else {
stpVer.expectNext(value).verifyComplete();
}
}
}