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