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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: fix DatabaseTest after code review
  • Loading branch information
eustimenko committed Sep 18, 2019

Verified

This commit was signed with the committer’s verified signature.
theoludwig Théo LUDWIG
commit 4fdb06d52171510294be9332259e0e52742ad5f2
78 changes: 19 additions & 59 deletions db/core/src/test/java/org/ethereum/beacon/db/DatabaseTest.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,55 @@
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.ethereum.beacon.db.rocksdb.RocksDbSource;
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 org.rocksdb.RocksDBException;
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);
void testInvalidCreation() {
final EngineDrivenDatabase database = (EngineDrivenDatabase) Database.rocksDB("", 0);
final RocksDbSource upstream = (RocksDbSource) database.getWriteBuffer().getUpstream();
assertThatThrownBy(upstream::open)
.isInstanceOf(RuntimeException.class)
.hasCauseExactlyInstanceOf(RocksDBException.class);
}

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

@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();
final EngineDrivenDatabase database = (EngineDrivenDatabase) Database.rocksDB(path, bufferLimitsInBytes);
final RocksDbSource upstream = (RocksDbSource) database.getWriteBuffer().getUpstream();
upstream.open();
upstream.put(BytesValue.of(123), BytesValue.of(1, 2, 3));
assertThat(database.getWriteBuffer().get(BytesValue.of(123))).isPresent().hasValue(BytesValue.of(1, 2, 3));
upstream.close();
}

private static Stream<Arguments> validArgumentsProvider() {
return Stream.of(
Arguments.of("", 0),
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();
}
}