Search in sources :

Example 21 with IgniteAtomicLong

use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.

the class IgniteAtomicLongApiAbstractSelfTest method testGetAndSetInTx.

/**
     * @throws Exception If failed.
     */
public void testGetAndSetInTx() throws Exception {
    info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']');
    Ignite ignite = grid(0);
    IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true);
    IgniteCache<Object, Object> cache = ignite.cache(TRANSACTIONAL_CACHE_NAME);
    try (Transaction tx = ignite.transactions().txStart()) {
        cache.put(1, 1);
        long newVal = RND.nextLong();
        long curAtomicVal = atomic.get();
        assert curAtomicVal == atomic.getAndSet(newVal);
        assert newVal == atomic.get();
    }
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Example 22 with IgniteAtomicLong

use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.

the class IgniteAtomicLongApiAbstractSelfTest method testGetAndIncrement.

/**
     * @throws Exception If failed.
     */
public void testGetAndIncrement() throws Exception {
    info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']');
    Ignite ignite = grid(0);
    IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true);
    long curAtomicVal = atomic.get();
    assert curAtomicVal == atomic.getAndIncrement();
    assert curAtomicVal + 1 == atomic.get();
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Example 23 with IgniteAtomicLong

use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.

the class IgniteAtomicLongApiAbstractSelfTest method testCompareAndSet.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "NullableProblems", "ConstantConditions" })
public void testCompareAndSet() throws Exception {
    info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']');
    Ignite ignite = grid(0);
    IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true);
    long newVal = RND.nextLong();
    final long oldVal = atomic.get();
    // Don't set new value.
    assert !atomic.compareAndSet(oldVal - 1, newVal);
    assert oldVal == atomic.get();
    // Set new value.
    assert atomic.compareAndSet(oldVal, newVal);
    assert newVal == atomic.get();
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Example 24 with IgniteAtomicLong

use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.

the class IgniteAtomicLongApiAbstractSelfTest method testDecrementAndGet.

/**
     * @throws Exception If failed.
     */
public void testDecrementAndGet() throws Exception {
    info("Running test [name=" + getName() + ", cacheMode=" + atomicsCacheMode() + ']');
    Ignite ignite = grid(0);
    IgniteAtomicLong atomic = ignite.atomicLong("atomic", 0, true);
    long curAtomicVal = atomic.get();
    assert curAtomicVal - 1 == atomic.decrementAndGet();
    assert curAtomicVal - 1 == atomic.get();
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) Ignite(org.apache.ignite.Ignite)

Example 25 with IgniteAtomicLong

use of org.apache.ignite.IgniteAtomicLong in project ignite by apache.

the class IgniteClientDataStructuresAbstractTest method testAtomicLong.

/**
     * @param creator Creator node.
     * @param other Other node.
     * @throws Exception If failed.
     */
private void testAtomicLong(Ignite creator, Ignite other) throws Exception {
    assertNull(creator.atomicLong("long1", 1L, false));
    assertNull(other.atomicLong("long1", 1L, false));
    try (IgniteAtomicLong cntr = creator.atomicLong("long1", 1L, true)) {
        assertNotNull(cntr);
        assertEquals(1L, cntr.get());
        assertEquals(1L, cntr.getAndAdd(1));
        assertEquals(2L, cntr.get());
        IgniteAtomicLong cntr0 = other.atomicLong("long1", 1L, false);
        assertNotNull(cntr0);
        assertEquals(2L, cntr0.get());
        assertEquals(3L, cntr0.incrementAndGet());
        assertEquals(3L, cntr.get());
    }
    assertNull(creator.atomicLong("long1", 1L, false));
    assertNull(other.atomicLong("long1", 1L, false));
}
Also used : IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong)

Aggregations

IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)31 Ignite (org.apache.ignite.Ignite)23 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)6 IgniteException (org.apache.ignite.IgniteException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 EntryProcessorException (javax.cache.processor.EntryProcessorException)2 IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)2 IgniteQueue (org.apache.ignite.IgniteQueue)2 IgniteSet (org.apache.ignite.IgniteSet)2 Transaction (org.apache.ignite.transactions.Transaction)2 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 Callable (java.util.concurrent.Callable)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1