Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/stable db tests #194

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions db/core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
dependencies {
api project(':types')
api project(':crypto')
api(project(':types')) {
exclude group: 'junit'
}

api(project(':crypto')) {
exclude group: 'junit'
}

api "org.rocksdb:rocksdbjni"
api "com.googlecode.concurrent-locks:concurrent-locks"

testImplementation 'org.junit.jupiter:junit-jupiter:5.5.1'

test {
useJUnitPlatform {
excludeTags 'FIX'
}
}
}
95 changes: 95 additions & 0 deletions db/core/src/test/java/org/ethereum/beacon/db/DatabaseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.ethereum.beacon.db;

import org.ethereum.beacon.db.source.DataSource;
import org.ethereum.beacon.db.source.impl.HashMapDataSource;
import org.junit.jupiter.api.Tag;
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 tech.pegasys.artemis.util.bytes.BytesValue;

import java.nio.file.InvalidPathException;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class DatabaseTest {

@Tag("FIX")
@ParameterizedTest
@MethodSource("invalidArgumentsProvider")
void testInvalidCreation(String path, long bufferLimitsInBytes) {
assertThatThrownBy(() -> Database.rocksDB(path, bufferLimitsInBytes))
.isInstanceOf(InvalidPathException.class);
}

@Test
void testInvalidCreationWithNull() {
assertThatThrownBy(() -> Database.rocksDB(null, 0)).isInstanceOf(NullPointerException.class);
}

private static Stream<Arguments> invalidArgumentsProvider() {
return Stream.of(Arguments.of("null", 0), Arguments.of("", 0), Arguments.of("0123", 0));
}

@ParameterizedTest
@MethodSource("validArgumentsProvider")
void testValidCreation(String path, long bufferLimitsInBytes) {
final Database database = Database.rocksDB(path, bufferLimitsInBytes);
assertThat(database).isNotNull();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless isNotNull test again.

}

private static Stream<Arguments> validArgumentsProvider() {
return Stream.of(
Arguments.of("/tmp", 0),
Arguments.of("/tmp", Long.MAX_VALUE),
Arguments.of("/tmp", Long.MIN_VALUE));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This likely won't work on windows.
Additionaly, RocksDB creates files. It's a good idea to clean up, i.e. remove the directories, created by RocksDB.


private static final String TEST_STORAGE_NAME = "TEST_STORAGE_NAME";

private boolean committed;
private boolean closed;
private String storageName;

@Test
void testDatabaseCommitCloseCreateStorage() {
final DataSource<BytesValue, BytesValue> dataSource = new HashMapDataSource<>();
committed = false;
closed = false;
storageName = null;

final Database db =
new Database() {
@Override
public DataSource<BytesValue, BytesValue> createStorage(String name) {
storageName = name;
return dataSource;
}

@Override
public void commit() {
committed = true;
}

@Override
public void close() {
closed = true;
}
};

final DataSource<BytesValue, BytesValue> storage = db.createStorage(TEST_STORAGE_NAME);
assertThat(storage).isEqualTo(dataSource);
assertThat(storageName).isEqualTo(TEST_STORAGE_NAME);

assertThat(committed).isFalse();
db.commit();
assertThat(committed).isTrue();

assertThat(closed).isFalse();
db.close();
assertThat(closed).isTrue();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test tests your implementation of Database interface. Useless again.

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package org.ethereum.beacon.db;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.ethereum.beacon.db.source.DataSource;
import org.ethereum.beacon.db.source.StorageEngineSource;
import org.ethereum.beacon.db.source.impl.MemSizeEvaluators;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import tech.pegasys.artemis.util.bytes.Bytes32;
import tech.pegasys.artemis.util.bytes.BytesValue;
import tech.pegasys.artemis.util.uint.UInt64;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -13,20 +19,15 @@
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import javax.annotation.Nonnull;
import org.ethereum.beacon.db.source.DataSource;
import org.ethereum.beacon.db.source.StorageEngineSource;
import org.ethereum.beacon.db.source.impl.MemSizeEvaluators;
import org.junit.Ignore;
import org.junit.Test;
import tech.pegasys.artemis.util.bytes.Bytes32;
import tech.pegasys.artemis.util.bytes.BytesValue;
import tech.pegasys.artemis.util.uint.UInt64;

public class EngineDrivenDatabaseTest {
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class EngineDrivenDatabaseTest {

@Test
public void generalCasesAreCorrect() {
void generalCasesAreCorrect() {
TestStorageSource engineSource = new TestStorageSource();
EngineDrivenDatabase db = EngineDrivenDatabase.createWithInstantFlusher(engineSource);

Expand All @@ -36,21 +37,21 @@ public void generalCasesAreCorrect() {
storage.put(wrap("TWO"), wrap("SECOND"));

assertTrue(engineSource.source.isEmpty());
assertEquals(wrap("FIRST"), storage.get(wrap("ONE")).get());
assertEquals(wrap("SECOND"), storage.get(wrap("TWO")).get());
assertFalse(storage.get(wrap("THREE")).isPresent());
assertThat(wrap("FIRST")).isEqualTo(storage.get(wrap("ONE")).get());
assertThat(wrap("SECOND")).isEqualTo(storage.get(wrap("TWO")).get());
assertThat(storage.get(wrap("THREE"))).isNotPresent();

db.commit();

assertFalse(db.getWriteBuffer().getCacheEntry(wrap("ONE")).isPresent());
assertFalse(db.getWriteBuffer().getCacheEntry(wrap("TWO")).isPresent());
assertEquals(0L, db.getWriteBuffer().evaluateSize());
assertThat(db.getWriteBuffer().getCacheEntry(wrap("ONE"))).isNotPresent();
assertThat(db.getWriteBuffer().getCacheEntry(wrap("TWO"))).isNotPresent();
assertThat(0L).isEqualTo(db.getWriteBuffer().evaluateSize());

assertTrue(engineSource.source.containsValue(wrap("FIRST")));
assertTrue(engineSource.source.containsValue(wrap("SECOND")));
assertEquals(wrap("FIRST"), storage.get(wrap("ONE")).get());
assertEquals(wrap("SECOND"), storage.get(wrap("TWO")).get());
assertFalse(storage.get(wrap("THREE")).isPresent());
assertThat(wrap("FIRST")).isEqualTo(storage.get(wrap("ONE")).get());
assertThat(wrap("SECOND")).isEqualTo(storage.get(wrap("TWO")).get());
assertThat(storage.get(wrap("THREE"))).isNotPresent();

storage.remove(wrap("SECOND"));
storage.put(wrap("THREE"), wrap("THIRD"));
Expand All @@ -60,23 +61,23 @@ public void generalCasesAreCorrect() {
assertTrue(engineSource.source.containsValue(wrap("SECOND")));
assertFalse(engineSource.source.containsValue(wrap("THIRD")));

assertEquals(wrap("FIRST"), storage.get(wrap("ONE")).get());
assertFalse(storage.get(wrap("TWO")).isPresent());
assertEquals(wrap("THIRD"), storage.get(wrap("THREE")).get());
assertThat(wrap("FIRST")).isEqualTo(storage.get(wrap("ONE")).get());
assertThat(storage.get(wrap("TWO"))).isNotPresent();
assertThat(wrap("THIRD")).isEqualTo(storage.get(wrap("THREE")).get());

db.commit();

assertTrue(engineSource.source.containsValue(wrap("FIRST")));
assertFalse(engineSource.source.containsValue(wrap("SECOND")));
assertTrue(engineSource.source.containsValue(wrap("THIRD")));

assertEquals(wrap("FIRST"), storage.get(wrap("ONE")).get());
assertFalse(storage.get(wrap("TWO")).isPresent());
assertEquals(wrap("THIRD"), storage.get(wrap("THREE")).get());
assertThat(wrap("FIRST")).isEqualTo(storage.get(wrap("ONE")).get());
assertThat(storage.get(wrap("TWO"))).isNotPresent();
assertThat(wrap("THIRD")).isEqualTo(storage.get(wrap("THREE")).get());
}

@Test
public void multipleStorageCase() {
void multipleStorageCase() {
TestStorageSource engineSource = new TestStorageSource();
EngineDrivenDatabase db = EngineDrivenDatabase.createWithInstantFlusher(engineSource);

Expand All @@ -90,39 +91,39 @@ public void multipleStorageCase() {
dos.put(wrap("FOUR"), wrap("DOS_FOURTH"));

db.commit();
assertEquals(wrap("FIRST"), uno.get(wrap("ONE")).get());
assertEquals(wrap("SECOND"), uno.get(wrap("TWO")).get());
assertEquals(wrap("SECOND"), dos.get(wrap("TWO")).get());
assertEquals(wrap("UNO_THIRD"), uno.get(wrap("THREE")).get());
assertEquals(wrap("DOS_FOURTH"), dos.get(wrap("FOUR")).get());
assertFalse(uno.get(wrap("FOUR")).isPresent());
assertFalse(dos.get(wrap("ONE")).isPresent());
assertFalse(dos.get(wrap("THREE")).isPresent());
assertThat(wrap("FIRST")).isEqualTo(uno.get(wrap("ONE")).get());
assertThat(wrap("SECOND")).isEqualTo(uno.get(wrap("TWO")).get());
assertThat(wrap("SECOND")).isEqualTo(dos.get(wrap("TWO")).get());
assertThat(wrap("UNO_THIRD")).isEqualTo(uno.get(wrap("THREE")).get());
assertThat(wrap("DOS_FOURTH")).isEqualTo(dos.get(wrap("FOUR")).get());
assertThat(uno.get(wrap("FOUR"))).isNotPresent();
assertThat(dos.get(wrap("ONE"))).isNotPresent();
assertThat(dos.get(wrap("THREE"))).isNotPresent();

uno.remove(wrap("TWO"));
dos.put(wrap("THREE"), wrap("DOS_THIRD"));

assertFalse(uno.get(wrap("TWO")).isPresent());
assertEquals(wrap("DOS_THIRD"), dos.get(wrap("THREE")).get());
assertEquals(wrap("UNO_THIRD"), uno.get(wrap("THREE")).get());
assertThat(uno.get(wrap("TWO"))).isNotPresent();
assertThat(wrap("DOS_THIRD")).isEqualTo(dos.get(wrap("THREE")).get());
assertThat(wrap("UNO_THIRD")).isEqualTo(uno.get(wrap("THREE")).get());

db.commit();
assertFalse(uno.get(wrap("TWO")).isPresent());
assertEquals(wrap("DOS_THIRD"), dos.get(wrap("THREE")).get());
assertEquals(wrap("UNO_THIRD"), uno.get(wrap("THREE")).get());
assertThat(uno.get(wrap("TWO"))).isNotPresent();
assertThat(wrap("DOS_THIRD")).isEqualTo(dos.get(wrap("THREE")).get());
assertThat(wrap("UNO_THIRD")).isEqualTo(uno.get(wrap("THREE")).get());

dos.remove(wrap("FOUR"));
uno.put(wrap("FOUR"), wrap("UNO_FOURTH"));
assertEquals(wrap("UNO_FOURTH"), uno.get(wrap("FOUR")).get());
assertFalse(dos.get(wrap("FOUR")).isPresent());
assertThat(wrap("UNO_FOURTH")).isEqualTo(uno.get(wrap("FOUR")).get());
assertThat(dos.get(wrap("FOUR"))).isNotPresent();

db.commit();
assertEquals(wrap("UNO_FOURTH"), uno.get(wrap("FOUR")).get());
assertFalse(dos.get(wrap("FOUR")).isPresent());
assertThat(wrap("UNO_FOURTH")).isEqualTo(uno.get(wrap("FOUR")).get());
assertThat(dos.get(wrap("FOUR"))).isNotPresent();
}

@Test
public void checkBufferSizeFlusher() {
void checkBufferSizeFlusher() {
TestStorageSource engineSource = new TestStorageSource();
EngineDrivenDatabase db = EngineDrivenDatabase.create(engineSource, 512);

Expand All @@ -142,29 +143,27 @@ public void checkBufferSizeFlusher() {

// should be flushed now
db.commit();
assertEquals(4, engineSource.source.size());
assertEquals(0L, db.getWriteBuffer().evaluateSize());
assertFalse(db.getWriteBuffer().getCacheEntry(wrap("ONE")).isPresent());
assertFalse(db.getWriteBuffer().getCacheEntry(wrap("TWO")).isPresent());
assertFalse(db.getWriteBuffer().getCacheEntry(wrap("THREE")).isPresent());
assertFalse(db.getWriteBuffer().getCacheEntry(wrap("FOUR")).isPresent());
assertThat(4).isEqualTo(engineSource.source.size());
assertThat(0L).isEqualTo(db.getWriteBuffer().evaluateSize());
assertThat(db.getWriteBuffer().getCacheEntry(wrap("ONE"))).isNotPresent();
assertThat(db.getWriteBuffer().getCacheEntry(wrap("TWO"))).isNotPresent();
assertThat(db.getWriteBuffer().getCacheEntry(wrap("THREE"))).isNotPresent();
assertThat(db.getWriteBuffer().getCacheEntry(wrap("FOUR"))).isNotPresent();

storage.put(wrap("FIVE"), Bytes32.random(rnd));
storage.put(wrap("SIX"), Bytes32.random(rnd));
assertEquals(
4 * MemSizeEvaluators.BytesValueEvaluator.apply(Bytes32.random(rnd)),
db.getWriteBuffer().evaluateSize());
assertThat(4 * MemSizeEvaluators.BytesValueEvaluator.apply(Bytes32.random(rnd)))
.isEqualTo(db.getWriteBuffer().evaluateSize());

storage.remove(wrap("FIVE"));

assertEquals(
2 * MemSizeEvaluators.BytesValueEvaluator.apply(Bytes32.random(rnd)),
db.getWriteBuffer().evaluateSize());
assertThat(2 * MemSizeEvaluators.BytesValueEvaluator.apply(Bytes32.random(rnd)))
.isEqualTo(db.getWriteBuffer().evaluateSize());
}

@Test
@Ignore
public void checkWithConcurrentAccessTake1() throws InterruptedException {
@Disabled
void checkWithConcurrentAccessTake1() throws InterruptedException {
TestStorageSource engineSource = new TestStorageSource();
EngineDrivenDatabase db = EngineDrivenDatabase.createWithInstantFlusher(engineSource);

Expand Down Expand Up @@ -204,12 +203,12 @@ public void checkWithConcurrentAccessTake1() throws InterruptedException {
Set<BytesValue> expectedValues = new HashSet<>(writtenToOne.values());
expectedValues.addAll(writtenToTwo.values());

assertEquals(expectedValues, sourceValues);
assertThat(expectedValues).isEqualTo(sourceValues);
}

@Test
@Ignore
public void checkWithConcurrentAccessTake2() throws InterruptedException {
@Disabled
void checkWithConcurrentAccessTake2() throws InterruptedException {
TestStorageSource engineSource = new TestStorageSource();
EngineDrivenDatabase db = EngineDrivenDatabase.createWithInstantFlusher(engineSource);

Expand Down
Loading