Search in sources :

Example 1 with BoundedState

use of reactor.core.scheduler.BoundedElasticScheduler.BoundedState in project reactor-core by reactor.

the class BoundedElasticSchedulerTest method regrowFromEviction.

@Test
public void regrowFromEviction() {
    MockUtils.VirtualClock virtualClock = new MockUtils.VirtualClock();
    BoundedElasticScheduler scheduler = afterTest.autoDispose(new BoundedElasticScheduler(1, Integer.MAX_VALUE, r -> new Thread(r, "regrowFromEviction"), 1000, virtualClock));
    scheduler.start();
    Worker worker = scheduler.createWorker();
    List<BoundedState> beforeEviction = new ArrayList<>(Arrays.asList(scheduler.boundedServices.busyArray));
    assertThat(scheduler.estimateSize()).as("before eviction").isEqualTo(scheduler.estimateBusy()).isEqualTo(beforeEviction.size()).isEqualTo(1);
    worker.dispose();
    assertThat(scheduler.estimateSize()).as("once disposed").isEqualTo(scheduler.estimateIdle()).isEqualTo(1);
    // simulate an eviction 1s in the future
    virtualClock.advanceTimeBy(Duration.ofSeconds(1));
    scheduler.boundedServices.eviction();
    assertThat(scheduler.estimateSize()).as("after eviction").isEqualTo(scheduler.estimateIdle()).isEqualTo(scheduler.estimateBusy()).isZero();
    afterTest.autoDispose(scheduler.createWorker());
    assertThat(scheduler.boundedServices.busyArray).as("after regrowth").isNotEmpty().hasSize(1).doesNotContainAnyElementsOf(beforeEviction);
}
Also used : Offset(org.assertj.core.data.Offset) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) AfterAll(org.junit.jupiter.api.AfterAll) BoundedServices(reactor.core.scheduler.BoundedElasticScheduler.BoundedServices) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Logger(reactor.util.Logger) BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) Assertions(org.assertj.core.api.Assertions) Tag(org.junit.jupiter.api.Tag) Disposables(reactor.core.Disposables) AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Set(java.util.Set) Worker(reactor.core.scheduler.Scheduler.Worker) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) ThrowingSupplier(com.pivovarit.function.ThrowingSupplier) Awaitility(org.awaitility.Awaitility) CsvSource(org.junit.jupiter.params.provider.CsvSource) Disposable(reactor.core.Disposable) Scannable(reactor.core.Scannable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MonoSink(reactor.core.publisher.MonoSink) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ThrowingRunnable(com.pivovarit.function.ThrowingRunnable) MockUtils(reactor.test.MockUtils) LinkedList(java.util.LinkedList) RaceTestUtils(reactor.test.util.RaceTestUtils) ExecutorService(java.util.concurrent.ExecutorService) BoundedScheduledExecutorService(reactor.core.scheduler.BoundedElasticScheduler.BoundedScheduledExecutorService) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Clock(java.time.Clock) Comparator(java.util.Comparator) BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) ArrayList(java.util.ArrayList) Worker(reactor.core.scheduler.Scheduler.Worker) MockUtils(reactor.test.MockUtils) Test(org.junit.jupiter.api.Test)

Example 2 with BoundedState

use of reactor.core.scheduler.BoundedElasticScheduler.BoundedState in project reactor-core by reactor.

the class BoundedElasticSchedulerTest method whenCapReachedPicksLeastBusyExecutor.

@Test
public void whenCapReachedPicksLeastBusyExecutor() throws InterruptedException {
    BoundedElasticScheduler s = scheduler();
    // reach the cap of workers
    BoundedState state1 = afterTest.autoDispose(s.boundedServices.pick());
    BoundedState state2 = afterTest.autoDispose(s.boundedServices.pick());
    BoundedState state3 = afterTest.autoDispose(s.boundedServices.pick());
    BoundedState state4 = afterTest.autoDispose(s.boundedServices.pick());
    assertThat(new HashSet<>(Arrays.asList(state1, state2, state3, state4))).as("4 distinct").hasSize(4);
    // cheat to make some look like more busy
    state1.markPicked();
    state1.markPicked();
    state1.markPicked();
    state2.markPicked();
    state2.markPicked();
    state3.markPicked();
    assertThat(s.boundedServices.pick()).as("picked least busy state4").isSameAs(state4);
    // at this point state4 and state3 both are backing 1
    assertThat(Arrays.asList(s.boundedServices.pick(), s.boundedServices.pick())).as("next 2 picks picked state4 and state3").containsExactlyInAnyOrder(state4, state3);
}
Also used : BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) Test(org.junit.jupiter.api.Test)

Example 3 with BoundedState

use of reactor.core.scheduler.BoundedElasticScheduler.BoundedState in project reactor-core by reactor.

the class BoundedElasticSchedulerTest method extraWorkersShareBackingExecutorAndBoundedState.

@Test
public void extraWorkersShareBackingExecutorAndBoundedState() throws InterruptedException {
    Scheduler s = schedulerNotCached();
    ExecutorServiceWorker worker1 = (ExecutorServiceWorker) afterTest.autoDispose(s.createWorker());
    ExecutorServiceWorker worker2 = (ExecutorServiceWorker) afterTest.autoDispose(s.createWorker());
    ExecutorServiceWorker worker3 = (ExecutorServiceWorker) afterTest.autoDispose(s.createWorker());
    ExecutorServiceWorker worker4 = (ExecutorServiceWorker) afterTest.autoDispose(s.createWorker());
    ExecutorServiceWorker worker5 = (ExecutorServiceWorker) afterTest.autoDispose(s.createWorker());
    assertThat(worker1.exec).as("worker1").isNotSameAs(worker2.exec).isNotSameAs(worker3.exec).isNotSameAs(worker4.exec).isSameAs(worker5.exec);
    assertThat(worker2.exec).as("worker2").isNotSameAs(worker3.exec).isNotSameAs(worker4.exec);
    assertThat(worker3.exec).as("worker3").isNotSameAs(worker4.exec);
    BoundedState worker1BoundedState = Scannable.from(worker1.disposables).inners().findFirst().map(o -> (BoundedState) o).get();
    BoundedState worker5BoundedState = Scannable.from(worker5.disposables).inners().findFirst().map(o -> (BoundedState) o).get();
    assertThat(worker1BoundedState).as("w1 w5 same BoundedState in tasks").isSameAs(worker5BoundedState);
}
Also used : Offset(org.assertj.core.data.Offset) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) AfterAll(org.junit.jupiter.api.AfterAll) BoundedServices(reactor.core.scheduler.BoundedElasticScheduler.BoundedServices) Loggers(reactor.util.Loggers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Logger(reactor.util.Logger) BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) Assertions(org.assertj.core.api.Assertions) Tag(org.junit.jupiter.api.Tag) Disposables(reactor.core.Disposables) AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Set(java.util.Set) Worker(reactor.core.scheduler.Scheduler.Worker) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) ThrowingSupplier(com.pivovarit.function.ThrowingSupplier) Awaitility(org.awaitility.Awaitility) CsvSource(org.junit.jupiter.params.provider.CsvSource) Disposable(reactor.core.Disposable) Scannable(reactor.core.Scannable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MonoSink(reactor.core.publisher.MonoSink) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ThrowingRunnable(com.pivovarit.function.ThrowingRunnable) MockUtils(reactor.test.MockUtils) LinkedList(java.util.LinkedList) RaceTestUtils(reactor.test.util.RaceTestUtils) ExecutorService(java.util.concurrent.ExecutorService) BoundedScheduledExecutorService(reactor.core.scheduler.BoundedElasticScheduler.BoundedScheduledExecutorService) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Mono(reactor.core.publisher.Mono) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flux(reactor.core.publisher.Flux) Clock(java.time.Clock) Comparator(java.util.Comparator) BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) Test(org.junit.jupiter.api.Test)

Example 4 with BoundedState

use of reactor.core.scheduler.BoundedElasticScheduler.BoundedState in project reactor-core by reactor.

the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityWithUnobservableOnly.

@Test
public void estimateRemainingTaskCapacityWithUnobservableOnly() {
    // 3 workers, 1 not observable
    BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(3, 5, Thread::new, 10));
    boundedElasticScheduler.start();
    boundedElasticScheduler.boundedServices.setBusy(new BoundedState(boundedElasticScheduler.boundedServices, Executors.newSingleThreadScheduledExecutor()));
    boundedElasticScheduler.boundedServices.setBusy(new BoundedState(boundedElasticScheduler.boundedServices, Executors.newSingleThreadScheduledExecutor()));
    boundedElasticScheduler.boundedServices.setBusy(new BoundedState(boundedElasticScheduler.boundedServices, Executors.newSingleThreadScheduledExecutor()));
    assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("non-computable capacity").isEqualTo(-1);
}
Also used : BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) Test(org.junit.jupiter.api.Test)

Example 5 with BoundedState

use of reactor.core.scheduler.BoundedElasticScheduler.BoundedState in project reactor-core by reactor.

the class BoundedElasticSchedulerTest method estimateRemainingTaskCapacityWithSomeUnobservableWorkers.

@Test
public void estimateRemainingTaskCapacityWithSomeUnobservableWorkers() {
    // 3 workers, 1 not observable
    BoundedElasticScheduler boundedElasticScheduler = afterTest.autoDispose(new BoundedElasticScheduler(3, 5, Thread::new, 10));
    boundedElasticScheduler.start();
    afterTest.autoDispose(boundedElasticScheduler.createWorker());
    afterTest.autoDispose(boundedElasticScheduler.createWorker());
    boundedElasticScheduler.boundedServices.setBusy(new BoundedState(boundedElasticScheduler.boundedServices, Executors.newSingleThreadScheduledExecutor()));
    assertThat(boundedElasticScheduler.estimateRemainingTaskCapacity()).as("partially computable capacity").isEqualTo(-1);
}
Also used : BoundedState(reactor.core.scheduler.BoundedElasticScheduler.BoundedState) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)5 BoundedState (reactor.core.scheduler.BoundedElasticScheduler.BoundedState)5 ThrowingRunnable (com.pivovarit.function.ThrowingRunnable)2 ThrowingSupplier (com.pivovarit.function.ThrowingSupplier)2 Clock (java.time.Clock)2 Duration (java.time.Duration)2 Instant (java.time.Instant)2 ZoneId (java.time.ZoneId)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Comparator (java.util.Comparator)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Objects (java.util.Objects)2 Set (java.util.Set)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 Executors (java.util.concurrent.Executors)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2