Search in sources :

Example 36 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.

the class AtomicIntegerArrayTest method testCompareAndSet.

/**
 * compareAndSet succeeds in changing value if equal to expected else fails
 */
public void testCompareAndSet() {
    AtomicIntegerArray aa = new AtomicIntegerArray(SIZE);
    for (int i = 0; i < SIZE; i++) {
        aa.set(i, 1);
        assertTrue(aa.compareAndSet(i, 1, 2));
        assertTrue(aa.compareAndSet(i, 2, -4));
        assertEquals(-4, aa.get(i));
        assertFalse(aa.compareAndSet(i, -5, 7));
        assertEquals(-4, aa.get(i));
        assertTrue(aa.compareAndSet(i, -4, 7));
        assertEquals(7, aa.get(i));
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 37 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.

the class AtomicIntegerArrayTest method testToString.

/**
 * toString returns current value.
 */
public void testToString() {
    int[] a = { 17, 3, -42, 99, -7 };
    AtomicIntegerArray aa = new AtomicIntegerArray(a);
    assertEquals(Arrays.toString(a), aa.toString());
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 38 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.

the class AtomicIntegerArrayTest method testConstructor2.

/**
 * constructor with array is of same size and has all elements
 */
public void testConstructor2() {
    int[] a = { 17, 3, -42, 99, -7 };
    AtomicIntegerArray aa = new AtomicIntegerArray(a);
    assertEquals(a.length, aa.length());
    for (int i = 0; i < a.length; i++) assertEquals(a[i], aa.get(i));
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 39 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project j2objc by google.

the class Atomic8Test method testIntArrayAccumulateAndGet.

/**
 * AtomicIntegerArray accumulateAndGet updates with supplied function and
 * returns result.
 */
public void testIntArrayAccumulateAndGet() {
    AtomicIntegerArray a = new AtomicIntegerArray(1);
    a.set(0, 1);
    assertEquals(7, a.accumulateAndGet(0, 6, Integer::sum));
    assertEquals(10, a.accumulateAndGet(0, 3, Integer::sum));
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray)

Example 40 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project elasticsearch by elastic.

the class IndexActionIT method testCreatedFlagParallelExecution.

public void testCreatedFlagParallelExecution() throws Exception {
    createIndex("test");
    ensureGreen();
    int threadCount = 20;
    final int docCount = 300;
    int taskCount = docCount * threadCount;
    final AtomicIntegerArray createdCounts = new AtomicIntegerArray(docCount);
    ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
    List<Callable<Void>> tasks = new ArrayList<>(taskCount);
    final Random random = random();
    for (int i = 0; i < taskCount; i++) {
        tasks.add(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                int docId = random.nextInt(docCount);
                IndexResponse indexResponse = index("test", "type", Integer.toString(docId), "field1", "value");
                if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
                    createdCounts.incrementAndGet(docId);
                }
                return null;
            }
        });
    }
    threadPool.invokeAll(tasks);
    for (int i = 0; i < docCount; i++) {
        assertThat(createdCounts.get(i), lessThanOrEqualTo(1));
    }
    terminate(threadPool);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Random(java.util.Random) IndexResponse(org.elasticsearch.action.index.IndexResponse) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) InvalidIndexNameException(org.elasticsearch.indices.InvalidIndexNameException)

Aggregations

AtomicIntegerArray (java.util.concurrent.atomic.AtomicIntegerArray)85 Test (org.junit.Test)24 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 Test (org.junit.jupiter.api.Test)11 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 List (java.util.List)6 AtomicLongArray (java.util.concurrent.atomic.AtomicLongArray)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 JSONArray (com.alibaba.fastjson.JSONArray)3 ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 CacheException (javax.cache.CacheException)3 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)3 IgniteEx (org.apache.ignite.internal.IgniteEx)3 GridRandom (org.apache.ignite.internal.util.GridRandom)3 CAX (org.apache.ignite.internal.util.typedef.CAX)3