Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,21 @@ public MemoryMode getMemoryMode() {

@Override
public CompletableResultCode forceFlush() {
return scheduled.doRun();
CompletableResultCode result = new CompletableResultCode();
CompletableResultCode doRunResult = scheduled.doRun();
doRunResult.whenComplete(
() -> {
CompletableResultCode flushResult = exporter.flush();
flushResult.whenComplete(
() -> {
if (doRunResult.isSuccess() && flushResult.isSuccess()) {
result.succeed();
} else {
result.fail();
}
});
});
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,35 @@ void flush() throws Exception {
}
}

@Test
void forceflush_callsFlush() {
MetricExporter metricExporter = mock(MetricExporter.class);
when(metricExporter.export(any()))
.thenReturn(CompletableResultCode.ofSuccess())
.thenReturn(CompletableResultCode.ofSuccess())
.thenThrow(new RuntimeException("Export Failed!"));
when(metricExporter.flush())
.thenReturn(CompletableResultCode.ofSuccess())
.thenReturn(CompletableResultCode.ofFailure())
.thenReturn(CompletableResultCode.ofSuccess());
when(metricExporter.shutdown()).thenReturn(CompletableResultCode.ofSuccess());

PeriodicMetricReader reader =
PeriodicMetricReader.builder(metricExporter)
.setInterval(Duration.ofNanos(Long.MAX_VALUE))
.build();

try {
reader.register(collectionRegistration);
assertThat(reader.forceFlush().join(10, TimeUnit.SECONDS).isSuccess()).isTrue();
assertThat(reader.forceFlush().join(10, TimeUnit.SECONDS).isSuccess()).isFalse();
assertThat(reader.forceFlush().join(10, TimeUnit.SECONDS).isSuccess()).isFalse();
} finally {
reader.shutdown();
}
verify(metricExporter, times(3)).flush();
}

@Test
@Timeout(2)
@SuppressLogger(PeriodicMetricReader.class)
Expand Down
Loading