@ -56,18 +56,20 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void twoStoresAreIndependent ( ) throws Exception {
final KeyValueStorage store1 = createStore ( ) ;
final KeyValueStorage store2 = createStore ( ) ;
try ( final KeyValueStorage store1 = createStore ( ) ) {
try ( final KeyValueStorage store2 = createStore ( ) ) {
final KeyValueStorageTransaction tx = store1 . startTransaction ( ) ;
final byte [ ] key = bytesFromHexString ( "0001" ) ;
final byte [ ] value = bytesFromHexString ( "0FFF" ) ;
final KeyValueStorageTransaction tx = store1 . startTransaction ( ) ;
final byte [ ] key = bytesFromHexString ( "0001" ) ;
final byte [ ] value = bytesFromHexString ( "0FFF" ) ;
tx . put ( key , value ) ;
tx . commit ( ) ;
tx . put ( key , value ) ;
tx . commit ( ) ;
final Optional < byte [ ] > result = store2 . get ( key ) ;
assertThat ( result ) . isEmpty ( ) ;
final Optional < byte [ ] > result = store2 . get ( key ) ;
assertThat ( result ) . isEmpty ( ) ;
}
}
}
/ * *
@ -77,20 +79,21 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void put ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
final byte [ ] key = bytesFromHexString ( "0F" ) ;
final byte [ ] firstValue = bytesFromHexString ( "0ABC" ) ;
final byte [ ] secondValue = bytesFromHexString ( "0DEF" ) ;
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( key , firstValue ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . contains ( firstValue ) ;
tx = store . startTransaction ( ) ;
tx . put ( key , secondValue ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . contains ( secondValue ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final byte [ ] key = bytesFromHexString ( "0F" ) ;
final byte [ ] firstValue = bytesFromHexString ( "0ABC" ) ;
final byte [ ] secondValue = bytesFromHexString ( "0DEF" ) ;
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( key , firstValue ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . contains ( firstValue ) ;
tx = store . startTransaction ( ) ;
tx . put ( key , secondValue ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . contains ( secondValue ) ;
}
}
/ * *
@ -100,16 +103,17 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void streamKeys ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
final List < byte [ ] > keys =
Stream . of ( "0F" , "10" , "11" , "12" )
. map ( this : : bytesFromHexString )
. collect ( toUnmodifiableList ( ) ) ;
keys . forEach ( key - > tx . put ( key , bytesFromHexString ( "0ABC" ) ) ) ;
tx . commit ( ) ;
assertThat ( store . stream ( ) . map ( Pair : : getKey ) . collect ( toUnmodifiableSet ( ) ) )
. containsExactlyInAnyOrder ( keys . toArray ( new byte [ ] [ ] { } ) ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
final List < byte [ ] > keys =
Stream . of ( "0F" , "10" , "11" , "12" )
. map ( this : : bytesFromHexString )
. collect ( toUnmodifiableList ( ) ) ;
keys . forEach ( key - > tx . put ( key , bytesFromHexString ( "0ABC" ) ) ) ;
tx . commit ( ) ;
assertThat ( store . stream ( ) . map ( Pair : : getKey ) . collect ( toUnmodifiableSet ( ) ) )
. containsExactlyInAnyOrder ( keys . toArray ( new byte [ ] [ ] { } ) ) ;
}
}
/ * *
@ -119,18 +123,19 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void getAllKeysThat ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( bytesFromHexString ( "0F" ) , bytesFromHexString ( "0ABC" ) ) ;
tx . put ( bytesFromHexString ( "10" ) , bytesFromHexString ( "0ABC" ) ) ;
tx . put ( bytesFromHexString ( "11" ) , bytesFromHexString ( "0ABC" ) ) ;
tx . put ( bytesFromHexString ( "12" ) , bytesFromHexString ( "0ABC" ) ) ;
tx . commit ( ) ;
Set < byte [ ] > keys = store . getAllKeysThat ( bv - > Bytes . wrap ( bv ) . toString ( ) . contains ( "1" ) ) ;
assertThat ( keys . size ( ) ) . isEqualTo ( 3 ) ;
assertThat ( keys )
. containsExactlyInAnyOrder (
bytesFromHexString ( "10" ) , bytesFromHexString ( "11" ) , bytesFromHexString ( "12" ) ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( bytesFromHexString ( "0F" ) , bytesFromHexString ( "0ABC" ) ) ;
tx . put ( bytesFromHexString ( "10" ) , bytesFromHexString ( "0ABC" ) ) ;
tx . put ( bytesFromHexString ( "11" ) , bytesFromHexString ( "0ABC" ) ) ;
tx . put ( bytesFromHexString ( "12" ) , bytesFromHexString ( "0ABC" ) ) ;
tx . commit ( ) ;
Set < byte [ ] > keys = store . getAllKeysThat ( bv - > Bytes . wrap ( bv ) . toString ( ) . contains ( "1" ) ) ;
assertThat ( keys . size ( ) ) . isEqualTo ( 3 ) ;
assertThat ( keys )
. containsExactlyInAnyOrder (
bytesFromHexString ( "10" ) , bytesFromHexString ( "11" ) , bytesFromHexString ( "12" ) ) ;
}
}
/ * *
@ -140,17 +145,18 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void containsKey ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
final byte [ ] key = bytesFromHexString ( "ABCD" ) ;
final byte [ ] value = bytesFromHexString ( "DEFF" ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final byte [ ] key = bytesFromHexString ( "ABCD" ) ;
final byte [ ] value = bytesFromHexString ( "DEFF" ) ;
assertThat ( store . containsKey ( key ) ) . isFalse ( ) ;
assertThat ( store . containsKey ( key ) ) . isFalse ( ) ;
final KeyValueStorageTransaction transaction = store . startTransaction ( ) ;
transaction . put ( key , value ) ;
transaction . commit ( ) ;
final KeyValueStorageTransaction transaction = store . startTransaction ( ) ;
transaction . put ( key , value ) ;
transaction . commit ( ) ;
assertThat ( store . containsKey ( key ) ) . isTrue ( ) ;
assertThat ( store . containsKey ( key ) ) . isTrue ( ) ;
}
}
/ * *
@ -160,18 +166,19 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void removeExisting ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
final byte [ ] key = bytesFromHexString ( "0F" ) ;
final byte [ ] value = bytesFromHexString ( "0ABC" ) ;
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( key , value ) ;
tx . commit ( ) ;
tx = store . startTransaction ( ) ;
tx . remove ( key ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . isEmpty ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final byte [ ] key = bytesFromHexString ( "0F" ) ;
final byte [ ] value = bytesFromHexString ( "0ABC" ) ;
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( key , value ) ;
tx . commit ( ) ;
tx = store . startTransaction ( ) ;
tx . remove ( key ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . isEmpty ( ) ;
}
}
/ * *
@ -181,15 +188,16 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void removeExistingSameTransaction ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
final byte [ ] key = bytesFromHexString ( "0F" ) ;
final byte [ ] value = bytesFromHexString ( "0ABC" ) ;
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( key , value ) ;
tx . remove ( key ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . isEmpty ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final byte [ ] key = bytesFromHexString ( "0F" ) ;
final byte [ ] value = bytesFromHexString ( "0ABC" ) ;
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( key , value ) ;
tx . remove ( key ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . isEmpty ( ) ;
}
}
/ * *
@ -199,13 +207,14 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void removeNonExistent ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
final byte [ ] key = bytesFromHexString ( "0F" ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final byte [ ] key = bytesFromHexString ( "0F" ) ;
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . remove ( key ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . isEmpty ( ) ;
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . remove ( key ) ;
tx . commit ( ) ;
assertThat ( store . get ( key ) ) . isEmpty ( ) ;
}
}
/ * *
@ -216,39 +225,38 @@ public abstract class AbstractKeyValueStorageTest {
@Test
public void concurrentUpdate ( ) throws Exception {
final int keyCount = 1000 ;
final KeyValueStorage store = createStore ( ) ;
final CountDownLatch finishedLatch = new CountDownLatch ( 2 ) ;
final Function < byte [ ] , Thread > updater =
( value ) - >
new Thread (
( ) - > {
try {
for ( int i = 0 ; i < keyCount ; i + + ) {
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( Bytes . minimalBytes ( i ) . toArrayUnsafe ( ) , value ) ;
tx . commit ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final CountDownLatch finishedLatch = new CountDownLatch ( 2 ) ;
final Function < byte [ ] , Thread > updater =
( value ) - >
new Thread (
( ) - > {
try {
for ( int i = 0 ; i < keyCount ; i + + ) {
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( Bytes . minimalBytes ( i ) . toArrayUnsafe ( ) , value ) ;
tx . commit ( ) ;
}
} finally {
finishedLatch . countDown ( ) ;
}
} finally {
finishedLatch . countDown ( ) ;
}
} ) ;
} ) ;
// Run 2 concurrent transactions that write a bunch of values to the same keys
final byte [ ] a = Bytes . of ( 10 ) . toArrayUnsafe ( ) ;
final byte [ ] b = Bytes . of ( 20 ) . toArrayUnsafe ( ) ;
updater . apply ( a ) . start ( ) ;
updater . apply ( b ) . start ( ) ;
// Run 2 concurrent transactions that write a bunch of values to the same keys
final byte [ ] a = Bytes . of ( 10 ) . toArrayUnsafe ( ) ;
final byte [ ] b = Bytes . of ( 20 ) . toArrayUnsafe ( ) ;
updater . apply ( a ) . start ( ) ;
updater . apply ( b ) . start ( ) ;
finishedLatch . await ( ) ;
finishedLatch . await ( ) ;
for ( int i = 0 ; i < keyCount ; i + + ) {
final byte [ ] key = Bytes . minimalBytes ( i ) . toArrayUnsafe ( ) ;
final byte [ ] actual = store . get ( key ) . get ( ) ;
assertThat ( Arrays . equals ( actual , a ) | | Arrays . equals ( actual , b ) ) . isTrue ( ) ;
for ( int i = 0 ; i < keyCount ; i + + ) {
final byte [ ] key = Bytes . minimalBytes ( i ) . toArrayUnsafe ( ) ;
final byte [ ] actual = store . get ( key ) . get ( ) ;
assertThat ( Arrays . equals ( actual , a ) | | Arrays . equals ( actual , b ) ) . isTrue ( ) ;
}
}
store . close ( ) ;
}
/ * *
@ -258,34 +266,35 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void transactionCommit ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
// Add some values
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 2 ) ) ;
tx . put ( bytesOf ( 3 ) , bytesOf ( 3 ) ) ;
tx . commit ( ) ;
// Start transaction that adds, modifies, and removes some values
tx = store . startTransaction ( ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 3 ) ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 4 ) ) ;
tx . remove ( bytesOf ( 3 ) ) ;
tx . put ( bytesOf ( 4 ) , bytesOf ( 8 ) ) ;
// Check values before committing have not changed
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 2 ) ) ;
assertThat ( store . get ( bytesOf ( 3 ) ) ) . contains ( bytesOf ( 3 ) ) ;
assertThat ( store . get ( bytesOf ( 4 ) ) ) . isEmpty ( ) ;
tx . commit ( ) ;
// Check that values have been updated after commit
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 4 ) ) ;
assertThat ( store . get ( bytesOf ( 3 ) ) ) . isEmpty ( ) ;
assertThat ( store . get ( bytesOf ( 4 ) ) ) . contains ( bytesOf ( 8 ) ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
// Add some values
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 2 ) ) ;
tx . put ( bytesOf ( 3 ) , bytesOf ( 3 ) ) ;
tx . commit ( ) ;
// Start transaction that adds, modifies, and removes some values
tx = store . startTransaction ( ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 3 ) ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 4 ) ) ;
tx . remove ( bytesOf ( 3 ) ) ;
tx . put ( bytesOf ( 4 ) , bytesOf ( 8 ) ) ;
// Check values before committing have not changed
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 2 ) ) ;
assertThat ( store . get ( bytesOf ( 3 ) ) ) . contains ( bytesOf ( 3 ) ) ;
assertThat ( store . get ( bytesOf ( 4 ) ) ) . isEmpty ( ) ;
tx . commit ( ) ;
// Check that values have been updated after commit
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 4 ) ) ;
assertThat ( store . get ( bytesOf ( 3 ) ) ) . isEmpty ( ) ;
assertThat ( store . get ( bytesOf ( 4 ) ) ) . contains ( bytesOf ( 8 ) ) ;
}
}
/ * *
@ -295,34 +304,35 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void transactionRollback ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
// Add some values
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 2 ) ) ;
tx . put ( bytesOf ( 3 ) , bytesOf ( 3 ) ) ;
tx . commit ( ) ;
// Start transaction that adds, modifies, and removes some values
tx = store . startTransaction ( ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 3 ) ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 4 ) ) ;
tx . remove ( bytesOf ( 3 ) ) ;
tx . put ( bytesOf ( 4 ) , bytesOf ( 8 ) ) ;
// Check values before committing have not changed
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 2 ) ) ;
assertThat ( store . get ( bytesOf ( 3 ) ) ) . contains ( bytesOf ( 3 ) ) ;
assertThat ( store . get ( bytesOf ( 4 ) ) ) . isEmpty ( ) ;
tx . rollback ( ) ;
// Check that values have not changed after rollback
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 2 ) ) ;
assertThat ( store . get ( bytesOf ( 3 ) ) ) . contains ( bytesOf ( 3 ) ) ;
assertThat ( store . get ( bytesOf ( 4 ) ) ) . isEmpty ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
// Add some values
KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 2 ) ) ;
tx . put ( bytesOf ( 3 ) , bytesOf ( 3 ) ) ;
tx . commit ( ) ;
// Start transaction that adds, modifies, and removes some values
tx = store . startTransaction ( ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 3 ) ) ;
tx . put ( bytesOf ( 2 ) , bytesOf ( 4 ) ) ;
tx . remove ( bytesOf ( 3 ) ) ;
tx . put ( bytesOf ( 4 ) , bytesOf ( 8 ) ) ;
// Check values before committing have not changed
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 2 ) ) ;
assertThat ( store . get ( bytesOf ( 3 ) ) ) . contains ( bytesOf ( 3 ) ) ;
assertThat ( store . get ( bytesOf ( 4 ) ) ) . isEmpty ( ) ;
tx . rollback ( ) ;
// Check that values have not changed after rollback
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 2 ) ) ;
assertThat ( store . get ( bytesOf ( 3 ) ) ) . contains ( bytesOf ( 3 ) ) ;
assertThat ( store . get ( bytesOf ( 4 ) ) ) . isEmpty ( ) ;
}
}
/ * *
@ -332,9 +342,10 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void transactionCommitEmpty ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
}
}
/ * *
@ -344,143 +355,120 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void transactionRollbackEmpty ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
}
}
/ * *
* Transaction put after commit .
*
* @throws Exception the exception
* /
/** Transaction put after commit. */
@Test
public void transactionPutAfterCommit ( ) throws Exception {
public void transactionPutAfterCommit ( ) {
Assertions . assertThatThrownBy (
( ) - > {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
tx . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
tx . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
}
} )
. isInstanceOf ( IllegalStateException . class ) ;
}
/ * *
* Transaction remove after commit .
*
* @throws Exception the exception
* /
/** Transaction remove after commit. */
@Test
public void transactionRemoveAfterCommit ( ) throws Exception {
public void transactionRemoveAfterCommit ( ) {
Assertions . assertThatThrownBy (
( ) - > {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
tx . remove ( bytesOf ( 1 ) ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
tx . remove ( bytesOf ( 1 ) ) ;
}
} )
. isInstanceOf ( IllegalStateException . class ) ;
}
/ * *
* Transaction put after rollback .
*
* @throws Exception the exception
* /
/** Transaction put after rollback. */
@Test
public void transactionPutAfterRollback ( ) throws Exception {
public void transactionPutAfterRollback ( ) {
Assertions . assertThatThrownBy (
( ) - > {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
tx . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
tx . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
}
} )
. isInstanceOf ( IllegalStateException . class ) ;
}
/ * *
* Transaction remove after rollback .
*
* @throws Exception the exception
* /
/** Transaction remove after rollback. */
@Test
public void transactionRemoveAfterRollback ( ) throws Exception {
public void transactionRemoveAfterRollback ( ) {
Assertions . assertThatThrownBy (
( ) - > {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
tx . remove ( bytesOf ( 1 ) ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
tx . remove ( bytesOf ( 1 ) ) ;
}
} )
. isInstanceOf ( IllegalStateException . class ) ;
}
/ * *
* Transaction commit after rollback .
*
* @throws Exception the exception
* /
/** Transaction commit after rollback. */
@Test
public void transactionCommitAfterRollback ( ) throws Exception {
public void transactionCommitAfterRollback ( ) {
Assertions . assertThatThrownBy (
( ) - > {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
tx . commit ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
tx . commit ( ) ;
}
} )
. isInstanceOf ( IllegalStateException . class ) ;
}
/ * *
* Transaction commit twice .
*
* @throws Exception the exception
* /
/** Transaction commit twice. */
@Test
public void transactionCommitTwice ( ) throws Exception {
public void transactionCommitTwice ( ) {
Assertions . assertThatThrownBy (
( ) - > {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
tx . commit ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
tx . commit ( ) ;
}
} )
. isInstanceOf ( IllegalStateException . class ) ;
}
/ * *
* Transaction rollback after commit .
*
* @throws Exception the exception
* /
/** Transaction rollback after commit. */
@Test
public void transactionRollbackAfterCommit ( ) throws Exception {
public void transactionRollbackAfterCommit ( ) {
Assertions . assertThatThrownBy (
( ) - > {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
tx . rollback ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . commit ( ) ;
tx . rollback ( ) ;
}
} )
. isInstanceOf ( IllegalStateException . class ) ;
}
/ * *
* Transaction rollback twice .
*
* @throws Exception the exception
* /
/** Transaction rollback twice. */
@Test
public void transactionRollbackTwice ( ) throws Exception {
public void transactionRollbackTwice ( ) {
Assertions . assertThatThrownBy (
( ) - > {
final KeyValueStorage store = createStore ( ) ;
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
tx . rollback ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx = store . startTransaction ( ) ;
tx . rollback ( ) ;
tx . rollback ( ) ;
}
} )
. isInstanceOf ( IllegalStateException . class ) ;
}
@ -492,19 +480,20 @@ public abstract class AbstractKeyValueStorageTest {
* /
@Test
public void twoTransactions ( ) throws Exception {
final KeyValueStorage store = createStore ( ) ;
try ( final KeyValueStorage store = createStore ( ) ) {
final KeyValueStorageTransaction tx1 = store . startTransaction ( ) ;
final KeyValueStorageTransaction tx2 = store . startTransaction ( ) ;
final KeyValueStorageTransaction tx1 = store . startTransaction ( ) ;
final KeyValueStorageTransaction tx2 = store . startTransaction ( ) ;
tx1 . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
tx2 . put ( bytesOf ( 2 ) , bytesOf ( 2 ) ) ;
tx1 . put ( bytesOf ( 1 ) , bytesOf ( 1 ) ) ;
tx2 . put ( bytesOf ( 2 ) , bytesOf ( 2 ) ) ;
tx1 . commit ( ) ;
tx2 . commit ( ) ;
tx1 . commit ( ) ;
tx2 . commit ( ) ;
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 2 ) ) ;
assertThat ( store . get ( bytesOf ( 1 ) ) ) . contains ( bytesOf ( 1 ) ) ;
assertThat ( store . get ( bytesOf ( 2 ) ) ) . contains ( bytesOf ( 2 ) ) ;
}
}
/ * *