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 @@ -31,15 +31,24 @@
class PeriodicRecordingFlusher {
private static final Logger logger = Logger.getLogger(PeriodicRecordingFlusher.class.getName());

private final ScheduledExecutorService executor =
HelpfulExecutors.newSingleThreadedScheduledExecutor("JFR Recording Flusher");
private final ScheduledExecutorService executor;
private final Duration recordingDuration;
private final JfrRecorder recorder;
private ScheduledFuture<?> scheduledFlushFuture = null;

PeriodicRecordingFlusher(JfrRecorder recorder, Duration recordingDuration) {
this(
recorder,
recordingDuration,
HelpfulExecutors.newSingleThreadedScheduledExecutor("JFR Recording Flusher"));
}

@VisibleForTesting
PeriodicRecordingFlusher(
JfrRecorder recorder, Duration recordingDuration, ScheduledExecutorService executor) {
this.recordingDuration = recordingDuration;
this.recorder = recorder;
this.executor = executor;
}

public void start() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.time.Duration;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -36,6 +39,8 @@ class PeriodicRecordingFlusherTest {

@Mock JfrRecorder recorder;
@Mock RecordingFileNamingConvention namingConvention;
@Mock ScheduledExecutorService executor;
@Mock ScheduledFuture<?> scheduledFlushFuture;

@Test
void canContinueNotStarted() {
Expand Down Expand Up @@ -66,15 +71,24 @@ void startThroughFlushSequence() throws Exception {

@Test
void stopCancelsScheduledFutureAndStopsRecorder() {
@SuppressWarnings("unchecked")
PeriodicRecordingFlusher recordingFlusher = buildRecordingFlusher();
doReturn(scheduledFlushFuture)
.when(executor)
.scheduleAtFixedRate(
any(Runnable.class), eq(0L), eq(duration.toMillis()), eq(TimeUnit.MILLISECONDS));
PeriodicRecordingFlusher recordingFlusher =
new PeriodicRecordingFlusher(recorder, duration, executor);

recordingFlusher.start();
recordingFlusher.stop();

verify(recorder).start();
verify(recorder).stop();
verify(executor)
.scheduleAtFixedRate(
any(Runnable.class), eq(0L), eq(duration.toMillis()), eq(TimeUnit.MILLISECONDS));
verify(scheduledFlushFuture).cancel(false);
verifyNoMoreInteractions(recorder);
verifyNoMoreInteractions(executor, scheduledFlushFuture);
}

private PeriodicRecordingFlusher buildRecordingFlusher() {
Expand Down
Loading