package org.multiverse.stms.gamma.transactionalobjects.txnlong; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.multiverse.api.LockMode; import org.multiverse.api.TxnFactory; import org.multiverse.api.exceptions.*; import org.multiverse.api.exceptions.DeadTxnException; import org.multiverse.api.exceptions.PreparedTxnException; import org.multiverse.api.references.TxnLong; import org.multiverse.stms.gamma.GammaConstants; import org.multiverse.stms.gamma.GammaStm; import org.multiverse.stms.gamma.transactionalobjects.GammaTxnLong; import org.multiverse.stms.gamma.transactions.GammaTxn; import org.multiverse.stms.gamma.transactions.GammaTxnFactory; import org.multiverse.stms.gamma.transactions.fat.FatFixedLengthGammaTxnFactory; import org.multiverse.stms.gamma.transactions.fat.FatMonoGammaTxnFactory; import org.multiverse.stms.gamma.transactions.fat.FatVariableLengthGammaTxnFactory; import java.util.Collection; import static java.util.Arrays.asList; import static org.junit.Assert.*; import static org.multiverse.TestUtils.*; import static org.multiverse.api.TxnThreadLocal.*; import static org.multiverse.stms.gamma.GammaTestUtils.*; @RunWith(Parameterized.class) public class GammaTxnLong_getAndIncrement1Test implements GammaConstants { private final GammaTxnFactory transactionFactory; private final GammaStm stm; public GammaTxnLong_getAndIncrement1Test(GammaTxnFactory transactionFactory) { this.transactionFactory = transactionFactory; this.stm = transactionFactory.getConfig().getStm(); } @Before public void setUp() { clearThreadLocalTxn(); } @Parameterized.Parameters public static Collection<TxnFactory[]> configs() { return asList( new TxnFactory[]{new FatVariableLengthGammaTxnFactory(new GammaStm())}, new TxnFactory[]{new FatFixedLengthGammaTxnFactory(new GammaStm())}, new TxnFactory[]{new FatMonoGammaTxnFactory(new GammaStm())} ); } @Test public void whenPreparedTransactionAvailable_thenPreparedTxnException() { GammaTxnLong ref = new GammaTxnLong(stm, 10); long version = ref.getVersion(); GammaTxn tx = transactionFactory.newTxn(); tx.prepare(); setThreadLocalTxn(tx); try { ref.getAndIncrement(30); fail(); } catch (PreparedTxnException expected) { } assertIsAborted(tx); assertVersionAndValue(ref, version, 10); } @Test public void whenActiveTransactionAvailable() { TxnLong ref = new GammaTxnLong(stm, 10); GammaTxn tx = transactionFactory.newTxn(); setThreadLocalTxn(tx); long value = ref.getAndIncrement(20); tx.commit(); assertEquals(10, value); assertIsCommitted(tx); assertEquals(30, ref.atomicGet()); assertSame(tx, getThreadLocalTxn()); } @Test public void whenNoChange() { GammaTxnLong ref = new GammaTxnLong(stm, 10); long version = ref.getVersion(); GammaTxn tx = transactionFactory.newTxn(); setThreadLocalTxn(tx); long value = ref.getAndIncrement(0); tx.commit(); assertEquals(10, value); assertIsCommitted(tx); assertEquals(10, ref.atomicGet()); assertSame(tx, getThreadLocalTxn()); assertVersionAndValue(ref, version, 10); } @Test public void whenListenersAvailable() { long initialValue = 10; GammaTxnLong ref = new GammaTxnLong(stm, initialValue); long initialVersion = ref.getVersion(); long amount = 2; TxnLongAwaitThread thread = new TxnLongAwaitThread(ref, initialValue + amount); thread.start(); sleepMs(500); GammaTxn tx = transactionFactory.newTxn(); setThreadLocalTxn(tx); long result = ref.getAndIncrement(amount); tx.commit(); joinAll(thread); assertEquals(initialValue, result); assertRefHasNoLocks(ref); assertVersionAndValue(ref, initialVersion + 1, initialValue + amount); } @Test public void whenLocked_thenReadWriteConflict() { long initialValue = 10; GammaTxnLong ref = new GammaTxnLong(stm, initialValue); long version = ref.getVersion(); GammaTxn otherTx = transactionFactory.newTxn(); ref.getLock().acquire(otherTx, LockMode.Exclusive); GammaTxn tx = transactionFactory.newTxn(); setThreadLocalTxn(tx); try { ref.getAndIncrement(1); fail(); } catch (ReadWriteConflict expected) { } assertIsAborted(tx); assertVersionAndValue(ref, version, initialValue); assertSurplus(ref, 1); assertRefHasExclusiveLock(ref, otherTx); } @Test public void whenNoTransactionAvailable_thenNoTransactionFoundException() { long initialValue = 10; GammaTxnLong ref = new GammaTxnLong(stm, initialValue); long initialVersion = ref.getVersion(); try { ref.getAndIncrement(1); fail(); } catch (TxnMandatoryException expected) { } assertSurplus(ref, 0); assertRefHasNoLocks(ref); assertVersionAndValue(ref, initialVersion, initialValue); assertNull(getThreadLocalTxn()); } @Test public void whenCommittedTransactionAvailable_thenDeadTxnException() { long initialValue = 10; GammaTxnLong ref = new GammaTxnLong(stm, initialValue); long initialVersion = ref.getVersion(); GammaTxn tx = transactionFactory.newTxn(); setThreadLocalTxn(tx); tx.commit(); try { ref.getAndIncrement(2); fail(); } catch (DeadTxnException expected) { } assertSurplus(ref, 0); assertRefHasNoLocks(ref); assertVersionAndValue(ref, initialVersion, initialValue); assertSame(tx, getThreadLocalTxn()); assertIsCommitted(tx); } @Test public void whenAbortedTransactionAvailable_thenDeadTxnException() { long initialValue = 10; GammaTxnLong ref = new GammaTxnLong(stm, initialValue); long initialVersion = ref.getVersion(); GammaTxn tx = transactionFactory.newTxn(); setThreadLocalTxn(tx); tx.abort(); try { ref.getAndIncrement(1); fail(); } catch (DeadTxnException expected) { } assertSurplus(ref, 0); assertRefHasNoLocks(ref); assertVersionAndValue(ref, initialVersion, initialValue); assertSame(tx, getThreadLocalTxn()); assertIsAborted(tx); } }