Search in sources :

Example 76 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project reactor-core by reactor.

the class BoundedElasticSchedulerTest method whenCapReachedPicksLeastBusyExecutorWithContention.

@ParameterizedTestWithName
@CsvSource(value = { "4, 1", "4, 100", "4, 1000", "100, 1", "100, 100", "100, 1000", "1000, 1", "1000, 100", "1000, 1000", "10000, 1", "10000, 100", "10000, 1000" })
void whenCapReachedPicksLeastBusyExecutorWithContention(int maxThreads, int contention) throws InterruptedException {
    BoundedElasticScheduler s = scheduler(maxThreads);
    HashSet<BoundedElasticScheduler.BoundedState> boundedStates = new HashSet<>();
    // reach the cap of workers and keep track of boundedStates
    for (int i = 0; i < maxThreads; i++) {
        boundedStates.add(afterTest.autoDispose(s.boundedServices.pick()));
    }
    assertThat(boundedStates).as("all distinct").hasSize(maxThreads);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicInteger countDown = new AtomicInteger(contention);
    AtomicInteger errors = new AtomicInteger();
    ExecutorService executorService = Executors.newFixedThreadPool(contention);
    AtomicIntegerArray busyPattern = new AtomicIntegerArray(Math.max(5, contention));
    for (int i = 0; i < contention; i++) {
        executorService.submit(() -> {
            if (countDown.get() <= 0) {
                return;
            }
            try {
                BoundedElasticScheduler.BoundedState bs = s.boundedServices.pick();
                assertThat(boundedStates).as("picked a busy one").contains(bs);
                assertThat(bs.markPicked()).isTrue();
                int business = bs.markCount;
                busyPattern.incrementAndGet(business);
            } catch (Throwable t) {
                errors.incrementAndGet();
                t.printStackTrace();
                countDown.set(0);
                latch.countDown();
            } finally {
                if (countDown.decrementAndGet() <= 0) {
                    latch.countDown();
                }
            }
        });
    }
    assertThat(latch.await(10, TimeUnit.SECONDS)).as("latch").isTrue();
    executorService.shutdownNow();
    assertThat(errors).as("errors").hasValue(0);
    int maxPicks = 0;
    for (int businessFactor = 0; businessFactor < busyPattern.length(); businessFactor++) {
        int pickCount = busyPattern.get(businessFactor);
        if (pickCount == 0)
            continue;
        if (pickCount > maxPicks)
            maxPicks = pickCount;
        LOGGER.trace("Picked {} times at business level {}", pickCount, businessFactor);
    }
    final int expectedMaxPick = Math.min(contention, maxThreads);
    Offset<Integer> offset;
    if (expectedMaxPick < 10) {
        offset = Offset.offset(1);
    } else if (expectedMaxPick < 1000) {
        offset = Offset.offset(10);
    } else {
        offset = Offset.offset(50);
    }
    assertThat(maxPicks).as("maxPicks").isCloseTo(expectedMaxPick, offset);
    LOGGER.info("Max picks {} ", maxPicks);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) BoundedScheduledExecutorService(reactor.core.scheduler.BoundedElasticScheduler.BoundedScheduledExecutorService) HashSet(java.util.HashSet) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Example 77 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project MyPerf4J by ThinkpadNC5.

the class AtomicIntArrayBench method setup.

@Setup
public void setup() {
    jdkArray = new AtomicIntegerArray(1024);
    myArray = new MyAtomicIntArray(1024);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) MyAtomicIntArray(cn.myperf4j.base.util.concurrent.MyAtomicIntArray) Setup(org.openjdk.jmh.annotations.Setup)

Example 78 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project android_packages_apps_Settings by omnirom.

the class SelectableSubscriptionsTest method atomicToList_zeroLengthInput_getEmptyList.

@Test
public void atomicToList_zeroLengthInput_getEmptyList() {
    List<Integer> result = SelectableSubscriptions.atomicToList(new AtomicIntegerArray(0));
    assertThat(result.size()).isEqualTo(0);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Test(org.junit.Test)

Example 79 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project android_packages_apps_Settings by omnirom.

the class SelectableSubscriptionsTest method atomicToList_subIdInArray_getList.

@Test
public void atomicToList_subIdInArray_getList() {
    AtomicIntegerArray array = new AtomicIntegerArray(3);
    array.set(0, 3);
    array.set(1, 7);
    array.set(2, 4);
    List<Integer> result = SelectableSubscriptions.atomicToList(array);
    assertThat(result.size()).isEqualTo(3);
    assertThat(result.get(0)).isEqualTo(3);
    assertThat(result.get(1)).isEqualTo(7);
    assertThat(result.get(2)).isEqualTo(4);
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Test(org.junit.Test)

Example 80 with AtomicIntegerArray

use of java.util.concurrent.atomic.AtomicIntegerArray in project assertj-core by joel-costigliola.

the class RecursiveComparisonDifferenceCalculator method compareAtomicIntegerArray.

private static void compareAtomicIntegerArray(DualValue dualValue, ComparisonState comparisonState) {
    if (!dualValue.isActualFieldAnAtomicIntegerArray()) {
        comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an AtomicIntegerArray"));
        return;
    }
    AtomicIntegerArray actual = (AtomicIntegerArray) dualValue.actual;
    AtomicIntegerArray expected = (AtomicIntegerArray) dualValue.expected;
    // both values in dualValue are arrays
    int actualArrayLength = actual.length();
    int expectedArrayLength = expected.length();
    if (actualArrayLength != expectedArrayLength) {
        comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "AtomicIntegerArrays", actualArrayLength, expectedArrayLength));
        // no need to inspect elements, arrays are not equal as they don't have the same size
        return;
    }
    // register each pair of actual/expected elements for recursive comparison
    FieldLocation arrayFieldLocation = dualValue.fieldLocation;
    for (int i = 0; i < actualArrayLength; i++) {
        Object actualElement = actual.get(i);
        Object expectedElement = expected.get(i);
        FieldLocation elementFieldLocation = arrayFieldLocation.field(format(ARRAY_FIELD_NAME + "[%d]", i));
        comparisonState.registerForComparison(new DualValue(elementFieldLocation, actualElement, expectedElement));
    }
}
Also used : AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) FieldLocation.rootFieldLocation(org.assertj.core.api.recursive.comparison.FieldLocation.rootFieldLocation)

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