Search in sources :

Example 1 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class ThrottlingLoggerTest method allowsTrickleOfMessages.

@Test
void allowsTrickleOfMessages() {
    TestClock clock = TestClock.create();
    ThrottlingLogger logger = new ThrottlingLogger(realLogger, clock);
    logger.log(Level.WARNING, "oh no!");
    assertThat(logs.size()).isEqualTo(1);
    logger.log(Level.WARNING, "oh no!");
    assertThat(logs.size()).isEqualTo(2);
    clock.advance(Duration.ofMillis(30_001));
    logger.log(Level.WARNING, "oh no!");
    logger.log(Level.WARNING, "oh no!");
    assertThat(logs.size()).isEqualTo(4);
    clock.advance(Duration.ofMillis(30_001));
    logger.log(Level.WARNING, "oh no 2nd minute!");
    logger.log(Level.WARNING, "oh no 2nd minute!");
    assertThat(logs.size()).isEqualTo(6);
    clock.advance(Duration.ofMillis(30_001));
    logger.log(Level.WARNING, "oh no 2nd minute!");
    logger.log(Level.WARNING, "oh no 2nd minute!");
    assertThat(logs.size()).isEqualTo(8);
    clock.advance(Duration.ofMillis(30_001));
    logger.log(Level.WARNING, "oh no 3rd minute!");
    logger.log(Level.WARNING, "oh no 3rd minute!");
    assertThat(logs.size()).isEqualTo(10);
    clock.advance(Duration.ofMillis(30_001));
    logger.log(Level.WARNING, "oh no 3rd minute!");
    logger.log(Level.WARNING, "oh no 3rd minute!");
    assertThat(logs.size()).isEqualTo(12);
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 2 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class RateLimiterTest method cantWithdrawMoreThanMax.

@Test
void cantWithdrawMoreThanMax() {
    TestClock clock = TestClock.create();
    RateLimiter limiter = new RateLimiter(1, 1.0, clock);
    assertThat(limiter.trySpend(2)).isFalse();
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 3 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class RateLimiterTest method testRateLimiterWholeNumber.

@Test
void testRateLimiterWholeNumber() {
    TestClock clock = TestClock.create();
    RateLimiter limiter = new RateLimiter(2.0, 2.0, clock);
    assertThat(limiter.trySpend(1.0)).isTrue();
    assertThat(limiter.trySpend(1.0)).isTrue();
    assertThat(limiter.trySpend(1.0)).isFalse();
    // move time 250ms forward, not enough credits to pay for 1.0 item
    clock.advance(Duration.ofNanos(TimeUnit.MILLISECONDS.toNanos(250)));
    assertThat(limiter.trySpend(1.0)).isFalse();
    // move time 500ms forward, now enough credits to pay for 1.0 item
    clock.advance(Duration.ofNanos(TimeUnit.MILLISECONDS.toNanos(500)));
    assertThat(limiter.trySpend(1.0)).isTrue();
    assertThat(limiter.trySpend(1.0)).isFalse();
    // move time 5s forward, enough to accumulate credits for 10 messages, but it should still be
    // capped at 2
    clock.advance(Duration.ofNanos(TimeUnit.MILLISECONDS.toNanos(5000)));
    assertThat(limiter.trySpend(1.0)).isTrue();
    assertThat(limiter.trySpend(1.0)).isTrue();
    assertThat(limiter.trySpend(1.0)).isFalse();
    assertThat(limiter.trySpend(1.0)).isFalse();
    assertThat(limiter.trySpend(1.0)).isFalse();
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 4 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class RateLimiterTest method testRateLimiterInitial.

/**
 * Validates rate limiter behavior with {@link System#nanoTime()}-like (non-zero) initial nano
 * ticks.
 */
@Test
void testRateLimiterInitial() {
    TestClock clock = TestClock.create();
    RateLimiter limiter = new RateLimiter(1000, 100, clock);
    // consume initial (max) balance
    assertThat(limiter.trySpend(100)).isTrue();
    assertThat(limiter.trySpend(1)).isFalse();
    // add 49 credits
    clock.advance(Duration.ofNanos(TimeUnit.MILLISECONDS.toNanos(49)));
    assertThat(limiter.trySpend(50)).isFalse();
    // add one credit
    clock.advance(Duration.ofNanos(TimeUnit.MILLISECONDS.toNanos(1)));
    // consume accrued balance
    assertThat(limiter.trySpend(50)).isTrue();
    assertThat(limiter.trySpend(1)).isFalse();
    // add a lot of credits (max out balance)
    clock.advance(Duration.ofNanos(TimeUnit.MILLISECONDS.toNanos(1_000_000)));
    // take one credit
    assertThat(limiter.trySpend(1)).isTrue();
    // add a lot of credits (max out balance)
    clock.advance(Duration.ofNanos(TimeUnit.MILLISECONDS.toNanos(1_000_000)));
    // can't consume more than max balance
    assertThat(limiter.trySpend(101)).isFalse();
    // consume max balance
    assertThat(limiter.trySpend(100)).isTrue();
    assertThat(limiter.trySpend(1)).isFalse();
}
Also used : TestClock(io.opentelemetry.sdk.testing.time.TestClock) Test(org.junit.jupiter.api.Test)

Example 5 with TestClock

use of io.opentelemetry.sdk.testing.time.TestClock in project opentelemetry-java by open-telemetry.

the class FixedSizeExemplarReservoirTest method oneMeasurement_includesTraceAndSpanIds.

@Test
public void oneMeasurement_includesTraceAndSpanIds() {
    Attributes all = Attributes.builder().put("one", 1).put("two", "two").put("three", true).build();
    Context context = Context.root().with(Span.wrap(SpanContext.createFromRemoteParent(TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TraceState.getDefault())));
    TestClock clock = TestClock.create();
    ExemplarReservoir reservoir = new FixedSizeExemplarReservoir(clock, 1, RandomSupplier.platformDefault());
    reservoir.offerMeasurement(1L, all, context);
    assertThat(reservoir.collectAndReset(Attributes.empty())).satisfiesExactly(exemplar -> assertThat(exemplar).hasEpochNanos(clock.now()).hasValue(1).hasFilteredAttributes(all).hasTraceId(TRACE_ID).hasSpanId(SPAN_ID));
}
Also used : Context(io.opentelemetry.context.Context) SpanContext(io.opentelemetry.api.trace.SpanContext) TestClock(io.opentelemetry.sdk.testing.time.TestClock) Attributes(io.opentelemetry.api.common.Attributes) Test(org.junit.jupiter.api.Test)

Aggregations

TestClock (io.opentelemetry.sdk.testing.time.TestClock)18 Test (org.junit.jupiter.api.Test)18 Attributes (io.opentelemetry.api.common.Attributes)3 SpanContext (io.opentelemetry.api.trace.SpanContext)2 AttributeKey (io.opentelemetry.api.common.AttributeKey)1 SpanKind (io.opentelemetry.api.trace.SpanKind)1 Context (io.opentelemetry.context.Context)1 Resource (io.opentelemetry.sdk.resources.Resource)1 EventData (io.opentelemetry.sdk.trace.data.EventData)1 LinkData (io.opentelemetry.sdk.trace.data.LinkData)1 SpanData (io.opentelemetry.sdk.trace.data.SpanData)1 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1