Skip to content

Commit

Permalink
add OnStopHandler again as functional interface
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Guggemos <dominik.guggemos@bosch.io>
  • Loading branch information
dguggemos committed Aug 23, 2021
1 parent 89f9bee commit 372a717
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.metrics.instruments.timer;

/**
* To be invoked when a Timer stops.
*/
@FunctionalInterface
public interface OnStopHandler {

/**
* Handles the passed {@code stoppedTimer}.
*
* @param stoppedTimer the StoppedTimer to handle.
*/
void handleStoppedTimer(final StoppedTimer stoppedTimer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class StartedKamonTimer implements StartedTimer {

private final String name;
private final Map<String, String> tags;
private final List<Consumer<StoppedTimer>> onStopHandlers;
private final List<OnStopHandler> onStopHandlers;
private final Map<String, StartedTimer> segments;
private final long startNanoTime;
private final Instant startInstant;
Expand Down Expand Up @@ -128,7 +128,7 @@ public StartedTimer startNewSegment(final String segmentName) {
}

@Override
public StartedTimer onStop(final Consumer<StoppedTimer> onStopHandler) {
public StartedTimer onStop(final OnStopHandler onStopHandler) {
onStopHandlers.add(onStopHandler);
return this;
}
Expand All @@ -154,7 +154,7 @@ public Map<String, StartedTimer> getSegments() {
}

@Override
public List<Consumer<StoppedTimer>> getOnStopHandlers() {
public List<OnStopHandler> getOnStopHandlers() {
return List.copyOf(onStopHandlers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ public interface StartedTimer extends Timer, TaggedMetricInstrument<StartedTimer
*
* @param onStopHandler the handler to invoke when this timer stops.
*/
StartedTimer onStop(Consumer<StoppedTimer> onStopHandler);
StartedTimer onStop(OnStopHandler onStopHandler);

/**
* Gets all on stop handlers of this timer.
*
* @return All on stop handlers of this timer.
*/
List<OnStopHandler> getOnStopHandlers();

/**
* @return the instant when the timer was started.
Expand All @@ -74,11 +81,4 @@ public interface StartedTimer extends Timer, TaggedMetricInstrument<StartedTimer
* @return Segments of this timer.
*/
Map<String, StartedTimer> getSegments();

/**
* Gets all on stop handlers of this timer.
*
* @return All on stop handlers of this timer.
*/
List<Consumer<StoppedTimer>> getOnStopHandlers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private StoppedKamonTimer(final StartedTimer startedTimer) {
final long durationNano = getElapsedNano();
LOGGER.trace("Timer with name <{}> and segment <{}> was stopped after <{}> nanoseconds", name,
tags.get(SEGMENT_TAG), durationNano);
startedTimer.getOnStopHandlers().forEach(stoppedTimerConsumer -> stoppedTimerConsumer.accept(this));
startedTimer.getOnStopHandlers().forEach(stoppedTimerConsumer -> stoppedTimerConsumer.handleStoppedTimer(this));
getKamonInternalTimer().record(durationNano);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ public void getName() {

@Test
public void getOnStopHandlers() {
final Consumer<StoppedTimer> onStopHandler = mock(Consumer.class);
final OnStopHandler onStopHandler = mock(OnStopHandler.class);
sut.onStop(onStopHandler);
final List<Consumer<StoppedTimer>> onStopHandlers = sut.getOnStopHandlers();
final List<OnStopHandler> onStopHandlers = sut.getOnStopHandlers();
Assertions.assertThat(onStopHandlers).hasSize(2);
Assertions.assertThat(onStopHandlers).contains(onStopHandler);
}

@Test
public void onStopIsCalled() {
final Consumer<StoppedTimer> onStopHandler = mock(Consumer.class);
final OnStopHandler onStopHandler = mock(OnStopHandler.class);
sut.onStop(onStopHandler);
final StoppedTimer stop = sut.stop();
verify(onStopHandler).accept(stop);
final StoppedTimer stopped = sut.stop();
verify(onStopHandler).handleStoppedTimer(stopped);
}

@Test
Expand Down Expand Up @@ -108,14 +108,14 @@ public void segmentsAreGettingStoppedToo() {

@Test
public void onStopHandlersOfSegmentsAreCalledToo() {
final Consumer<StoppedTimer> onStopHandler = mock(Consumer.class);
final Consumer<StoppedTimer> segmentOnStopHandler = mock(Consumer.class);
final OnStopHandler onStopHandler = mock(OnStopHandler.class);
final OnStopHandler segmentOnStopHandler = mock(OnStopHandler.class);
sut.onStop(onStopHandler);
final StartedTimer testSegment = sut.startNewSegment("TEST");
testSegment.onStop(segmentOnStopHandler);
final StoppedTimer stop = sut.stop();
verify(onStopHandler).accept(stop);
verify(segmentOnStopHandler).accept(any(StoppedTimer.class));
verify(onStopHandler).handleStoppedTimer(stop);
verify(segmentOnStopHandler).handleStoppedTimer(any(StoppedTimer.class));
}

@Test
Expand Down

0 comments on commit 372a717

Please sign in to comment.