Search in sources :

Example 1 with TAG_SCHEDULER_ID

use of reactor.core.scheduler.SchedulerMetricDecorator.TAG_SCHEDULER_ID in project reactor-core by reactor.

the class SchedulersMetricsTest method shouldReportExecutionTimes.

@ParameterizedTestWithName
@MethodSource("metricsSchedulers")
@Timeout(10)
void shouldReportExecutionTimes(Supplier<Scheduler> schedulerType) {
    Scheduler scheduler = afterTest.autoDispose(schedulerType.get());
    final int taskCount = 3;
    Phaser phaser = new Phaser(1);
    for (int i = 1; i <= taskCount; i++) {
        phaser.register();
        // bumped delay from 20ms to make actual scheduling times more precise
        int delay = i * 200;
        scheduler.schedule(() -> {
            try {
                Thread.sleep(delay);
                phaser.arriveAndDeregister();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }
    phaser.arriveAndAwaitAdvance();
    Collection<Timer> timers = registry.find("executor").tag(TAG_SCHEDULER_ID, scheduler.toString()).timers();
    // Use Awaitility because "count" is reported "eventually"
    await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
        assertThat(timers.stream().reduce(0d, (time, timer) -> time + timer.totalTime(TimeUnit.MILLISECONDS), Double::sum)).as("total durations").isEqualTo(600 + 400 + 200, offset(50d));
        assertThat(timers.stream().mapToLong(Timer::count).sum()).as("count").isEqualTo(taskCount);
    });
}
Also used : Metrics(reactor.util.Metrics) TAG_SCHEDULER_ID(reactor.core.scheduler.SchedulerMetricDecorator.TAG_SCHEDULER_ID) BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Supplier(java.util.function.Supplier) Timer(io.micrometer.core.instrument.Timer) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Assertions.offset(org.assertj.core.api.Assertions.offset) Arguments.arguments(org.junit.jupiter.params.provider.Arguments.arguments) MethodSource(org.junit.jupiter.params.provider.MethodSource) Counter(io.micrometer.core.instrument.Counter) AutoDisposingExtension(reactor.test.AutoDisposingExtension) Awaitility.await(org.awaitility.Awaitility.await) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Meter(io.micrometer.core.instrument.Meter) Arguments(org.junit.jupiter.params.provider.Arguments) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Stream(java.util.stream.Stream) Named(org.junit.jupiter.api.Named) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Phaser(java.util.concurrent.Phaser) FunctionCounter(io.micrometer.core.instrument.FunctionCounter) Timeout(org.junit.jupiter.api.Timeout) Timer(io.micrometer.core.instrument.Timer) Phaser(java.util.concurrent.Phaser) Timeout(org.junit.jupiter.api.Timeout) MethodSource(org.junit.jupiter.params.provider.MethodSource) ParameterizedTestWithName(reactor.test.ParameterizedTestWithName)

Aggregations

Counter (io.micrometer.core.instrument.Counter)1 FunctionCounter (io.micrometer.core.instrument.FunctionCounter)1 Meter (io.micrometer.core.instrument.Meter)1 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 Timer (io.micrometer.core.instrument.Timer)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 Collection (java.util.Collection)1 List (java.util.List)1 Executors (java.util.concurrent.Executors)1 Phaser (java.util.concurrent.Phaser)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 TimeUnit (java.util.concurrent.TimeUnit)1 Predicate (java.util.function.Predicate)1 Supplier (java.util.function.Supplier)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.offset (org.assertj.core.api.Assertions.offset)1 Awaitility.await (org.awaitility.Awaitility.await)1 AfterEach (org.junit.jupiter.api.AfterEach)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1