-
Notifications
You must be signed in to change notification settings - Fork 9
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
eustimenko
wants to merge
16
commits into
develop
Choose a base branch
from
test/stable-db-tests
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Test/stable db tests #194
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0882595
test: add junit5 into gradle
eustimenko ab80471
test: replace junit4 with junit5 in java code
eustimenko 173a95c
test: db/core/src/test/java/org/ethereum/beacon/db/DatabaseTest
eustimenko 01478a7
test: db/core/src/test/java/org/ethereum/beacon/db/DatabaseTest
eustimenko 0f07f8d
test: add junit4 dependency into test module
eustimenko 2b935f6
test: fix junit 4/5 dependencies
eustimenko 042ee95
Community tests updated to be in line with develop
zilm13 aac0059
test: reformat code
eustimenko 01e17cb
test: update subproject
eustimenko 4fdb06d
test: fix DatabaseTest after code review
eustimenko 23994a6
test: fix DatabaseTest after code review
eustimenko 29245fb
test: delete directories
eustimenko 355bc81
test: add relative directories
eustimenko b2a8186
test: add additional test for Database
eustimenko b575797
test: correct InMemoryDatabaseTest
eustimenko 1cb4db6
test: add Database tests
eustimenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
} | ||
} |
191 changes: 191 additions & 0 deletions
191
db/core/src/test/java/org/ethereum/beacon/db/DatabaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package org.ethereum.beacon.db; | ||
|
||
import org.ethereum.beacon.db.source.DataSource; | ||
import org.ethereum.beacon.db.util.FileUtil; | ||
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 org.rocksdb.RocksDBException; | ||
import tech.pegasys.artemis.util.bytes.BytesValue; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.stream.Stream; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class DatabaseTest { | ||
|
||
private static final String TEST_DB_NAME = "rocksdb"; | ||
private static final BytesValue KEY = BytesValue.of(123); | ||
private static final BytesValue VALUE = BytesValue.of(1, 2, 3); | ||
|
||
@BeforeEach | ||
@AfterEach | ||
void setUp() throws IOException { | ||
FileUtil.removeRecursively(TEST_DB_NAME); | ||
} | ||
|
||
@Test | ||
void testInvalidCreation() { | ||
final Database database = Database.rocksDB("", 0); | ||
assertThatThrownBy(() -> database.createStorage("test-db")) | ||
.isInstanceOf(RuntimeException.class) | ||
.hasCauseExactlyInstanceOf(RocksDBException.class); | ||
} | ||
|
||
@Test | ||
void testInvalidCreationWithNull() { | ||
assertThatThrownBy(() -> Database.rocksDB(null, 0)).isInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
void testCreateNotClosedDatabase() { | ||
final Database database = Database.rocksDB(TEST_DB_NAME, 0); | ||
database.createStorage("test-db"); | ||
|
||
final Database newDatabase = Database.rocksDB(TEST_DB_NAME, 0); | ||
assertThatThrownBy(() -> newDatabase.createStorage("test-db")) | ||
.isInstanceOf(RuntimeException.class) | ||
.hasCauseExactlyInstanceOf(RocksDBException.class); | ||
|
||
database.close(); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("validArgumentsProvider") | ||
void testValidCreation(String path, long bufferLimitsInBytes) throws IOException { | ||
final Database database = Database.rocksDB(path, bufferLimitsInBytes); | ||
final DataSource<BytesValue, BytesValue> storage = database.createStorage("test-db"); | ||
assertThat(Files.exists(Paths.get(path))).isTrue(); | ||
|
||
storage.put(KEY, VALUE); | ||
assertThat(storage.get(KEY)).isPresent().hasValue(VALUE); | ||
storage.remove(KEY); | ||
assertThat(storage.get(KEY)).isNotPresent(); | ||
|
||
database.close(); | ||
} | ||
|
||
private static Stream<Arguments> validArgumentsProvider() { | ||
return Stream.of( | ||
Arguments.of(TEST_DB_NAME, 0), | ||
Arguments.of(TEST_DB_NAME, Long.MAX_VALUE), | ||
Arguments.of(TEST_DB_NAME, Long.MIN_VALUE)); | ||
} | ||
|
||
@Test | ||
void testCommitAfterClose() { | ||
final Database database = Database.rocksDB(TEST_DB_NAME, Long.MAX_VALUE); | ||
final DataSource<BytesValue, BytesValue> storage = database.createStorage("test-db"); | ||
|
||
storage.put(KEY, VALUE); | ||
database.close(); | ||
|
||
//TODO: something should throw an Exception after close | ||
assertThatThrownBy(() -> storage.remove(KEY)).isInstanceOf(Exception.class); | ||
assertThatThrownBy(database::commit).isInstanceOf(Exception.class); | ||
} | ||
|
||
@Test | ||
void testCreateDuplicateStorage() { | ||
final Database database = Database.rocksDB(TEST_DB_NAME, Long.MAX_VALUE); | ||
final DataSource<BytesValue, BytesValue> s1 = database.createStorage("test-db"); | ||
final DataSource<BytesValue, BytesValue> s2 = database.createStorage("test-db"); | ||
|
||
s1.put(KEY, VALUE); | ||
assertThat(s1.get(KEY)).isPresent().hasValue(VALUE); | ||
assertThat(s1.get(KEY)).isEqualTo(s2.get(KEY)); | ||
|
||
s2.put(KEY, BytesValue.of(3, 2, 1)); | ||
assertThat(s2.get(KEY)).isPresent().hasValue(BytesValue.of(3, 2, 1)); | ||
assertThat(s1.get(KEY)).isNotEqualTo(VALUE); | ||
assertThat(s1.get(KEY)).isEqualTo(s2.get(KEY)); | ||
|
||
s1.remove(KEY); | ||
assertThat(s1.get(KEY)).isNotPresent(); | ||
assertThat(s2.get(KEY)).isNotPresent(); | ||
|
||
database.close(); | ||
} | ||
|
||
@Test | ||
void testCreateDifferentStorage() { | ||
final Database database = Database.rocksDB(TEST_DB_NAME, Long.MAX_VALUE); | ||
final DataSource<BytesValue, BytesValue> s1 = database.createStorage("test-db"); | ||
final DataSource<BytesValue, BytesValue> s2 = database.createStorage("test-db2"); | ||
|
||
s1.put(KEY, VALUE); | ||
assertThat(s1.get(KEY)).isPresent().hasValue(VALUE); | ||
assertThat(s2.get(KEY)).isNotPresent(); | ||
|
||
final BytesValue s2Value = BytesValue.of(3, 2, 1); | ||
s2.put(KEY, s2Value); | ||
assertThat(s2.get(KEY)).isPresent().hasValue(s2Value); | ||
assertThat(s1.get(KEY)).isNotEqualTo(s2Value); | ||
|
||
s1.remove(KEY); | ||
assertThat(s1.get(KEY)).isNotPresent(); | ||
assertThat(s2.get(KEY)).isPresent().hasValue(s2Value); | ||
|
||
database.close(); | ||
} | ||
|
||
@Test | ||
void testCreateDifferentDatabase_SameStorage() throws IOException { | ||
final Database db1 = Database.rocksDB(TEST_DB_NAME, Long.MAX_VALUE); | ||
final DataSource<BytesValue, BytesValue> s1 = db1.createStorage("test-db"); | ||
|
||
final Database db2 = Database.rocksDB("rocksdb2", Long.MAX_VALUE); | ||
final DataSource<BytesValue, BytesValue> s2 = db2.createStorage("test-db2"); | ||
|
||
s1.put(KEY, VALUE); | ||
assertThat(s1.get(KEY)).isPresent().hasValue(VALUE); | ||
assertThat(s2.get(KEY)).isNotPresent(); | ||
|
||
final BytesValue s2Value = BytesValue.of(3, 2, 1); | ||
s2.put(KEY, s2Value); | ||
assertThat(s2.get(KEY)).isPresent().hasValue(s2Value); | ||
assertThat(s1.get(KEY)).isNotEqualTo(s2Value); | ||
|
||
s1.remove(KEY); | ||
assertThat(s1.get(KEY)).isNotPresent(); | ||
assertThat(s2.get(KEY)).isPresent().hasValue(s2Value); | ||
|
||
db1.close(); | ||
db2.close(); | ||
FileUtil.removeRecursively("rocksdb2"); | ||
} | ||
|
||
@Test | ||
void testSavedDataAfterReopen() { | ||
Database database = Database.rocksDB(TEST_DB_NAME, Long.MAX_VALUE); | ||
DataSource<BytesValue, BytesValue> storage = database.createStorage("test-db"); | ||
storage.put(KEY, VALUE); | ||
database.commit(); | ||
database.close(); | ||
|
||
database = Database.rocksDB(TEST_DB_NAME, Long.MAX_VALUE); | ||
storage = database.createStorage("test-db"); | ||
assertThat(storage.get(KEY)).isPresent().hasValue(VALUE); | ||
database.close(); | ||
} | ||
|
||
@Test | ||
void testNotSaveUncommittedDataAfterReopen() { | ||
Database database = Database.rocksDB(TEST_DB_NAME, Long.MAX_VALUE); | ||
DataSource<BytesValue, BytesValue> storage = database.createStorage("test-db"); | ||
storage.put(KEY, VALUE); | ||
database.close(); | ||
|
||
database = Database.rocksDB(TEST_DB_NAME, Long.MAX_VALUE); | ||
storage = database.createStorage("test-db"); | ||
assertThat(storage.get(KEY)).isNotPresent(); //TODO: data were not committed before | ||
database.close(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test tests your implementation of Database interface. Useless again. |
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.