Skip to content

Commit

Permalink
Run the test in the runner
Browse files Browse the repository at this point in the history
  • Loading branch information
acm19 committed Jan 31, 2024
1 parent 7396f79 commit e2bb1ad
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,11 @@ public static class FailingHttpClient extends DummyHttpClient {

@Override
public LokiResponse send(ByteBuffer batch) throws Exception {
// usually used to wait until the assertion is executed to run the next send
sendCount++;
var response = super.send(batch);
await();
await();
if (fail.get() && !rateLimited.get())
throw new ConnectException("Text exception");
else if (fail.get() && rateLimited.get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.concurrent.atomic.AtomicReference;

import org.junit.Ignore;
import org.junit.Test;

import com.github.loki4j.logback.Generators.FailingHttpClient;
Expand All @@ -18,8 +17,6 @@
import ch.qos.logback.classic.spi.ILoggingEvent;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiFunction;

Expand Down Expand Up @@ -238,8 +235,7 @@ public void testRetry() throws InterruptedException, BrokenBarrierException, Tim
var sender = new WrappingHttpSender<FailingHttpClient>(failingHttpClient);
var encoder = defaultToStringEncoder();
var appender = appender(1, 4000L, encoder, sender);
var sleep = new CyclicSleep();
appender.setPipelineBuilder(PipelineConfig.builder().setSleep(sleep));
appender.setPipelineBuilder(PipelineConfig.builder().setSleep((a, b) -> true));
appender.start();

sender.client.fail.set(true);
Expand All @@ -251,16 +247,17 @@ public void testRetry() throws InterruptedException, BrokenBarrierException, Tim
"ts=100 l=INFO c=test.TestApp t=thread-1 | Test message 1 ")
.build();

failingHttpClient.await();
failingHttpClient.await();
assertEquals("send", 1, sender.client.sendCount);
assertEquals("send", expectedPayload, StringPayload.parse(sender.client.lastBatch, encoder.charset));

sleep.retry.await();
failingHttpClient.await();
failingHttpClient.await();
assertEquals("retry1", 2, sender.client.sendCount);
assertEquals("retry1", expectedPayload, StringPayload.parse(sender.client.lastBatch, encoder.charset));

sleep.retry.await();
failingHttpClient.await();
failingHttpClient.await();
assertEquals("retry2", 3, sender.client.sendCount);
assertEquals("retry2", expectedPayload, StringPayload.parse(sender.client.lastBatch, encoder.charset));
Expand All @@ -272,10 +269,11 @@ public void testRetry() throws InterruptedException, BrokenBarrierException, Tim
.build();
appender.append(events[1]);

failingHttpClient.await();
failingHttpClient.await();
assertEquals("send-2", 4, sender.client.sendCount);
assertEquals("send-2", expected2, StringPayload.parse(sender.client.lastBatch, encoder.charset));
sleep.retry.await();
failingHttpClient.await();

sender.client.fail.set(false);

Expand All @@ -294,8 +292,7 @@ public void testRateLimitedRetry() throws InterruptedException, BrokenBarrierExc

// retries rate limited requests by default
var appender = buildRateLimitedAppender(false, encoder, sender);
var sleep = new CyclicSleep();
appender.setPipelineBuilder(PipelineConfig.builder().setSleep(sleep));
appender.setPipelineBuilder(PipelineConfig.builder().setSleep((a, b) -> true));
appender.start();
appender.append(events[0]);

Expand All @@ -305,16 +302,17 @@ public void testRateLimitedRetry() throws InterruptedException, BrokenBarrierExc
"ts=100 l=INFO c=test.TestApp t=thread-1 | Test message 1 ")
.build();

failingHttpClient.await();
failingHttpClient.await();
assertEquals("send", 1, sender.client.sendCount);
assertEquals("send", expectedPayload, StringPayload.parse(sender.client.lastBatch, encoder.charset));

sleep.retry.await();
failingHttpClient.await();
failingHttpClient.await();
assertEquals("retry1", 2, sender.client.sendCount);
assertEquals("retry1", expectedPayload, StringPayload.parse(sender.client.lastBatch, encoder.charset));

sleep.retry.await();
failingHttpClient.await();
failingHttpClient.await();
assertEquals("retry2", 3, sender.client.sendCount);
assertEquals("retry2", expectedPayload, StringPayload.parse(sender.client.lastBatch, encoder.charset));
Expand Down Expand Up @@ -346,6 +344,7 @@ public void testRateLimitedNoRetries() throws InterruptedException, BrokenBarrie
failingHttpClient.await();
assertEquals("send-2", 1, sender.client.sendCount);
assertEquals("send-2", expectedPayload, StringPayload.parse(sender.client.lastBatch, encoder.charset));
failingHttpClient.await();

appender.stop();
}
Expand All @@ -363,20 +362,4 @@ private Loki4jAppender buildRateLimitedAppender(
return appender;
}

private static class CyclicSleep implements BiFunction<Integer, Long, Boolean> {
private CyclicBarrier retry = new CyclicBarrier(2);

@Override
public Boolean apply(Integer t, Long u) {
try {
retry.await(1L, TimeUnit.MINUTES);
return true;
} catch (InterruptedException | BrokenBarrierException | TimeoutException ex) {
Thread.currentThread().interrupt();
return false;
}
}

}

}

0 comments on commit e2bb1ad

Please sign in to comment.