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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations