Skip to content

Commit

Permalink
[PAN-2574] Fix failed tests on Windows (PegaSysEng#1332)
Browse files Browse the repository at this point in the history
- remove usage of `Lock.parkNanos`
- replace tests using Mockito
  • Loading branch information
AbdelStark authored and notlesh committed May 14, 2019
1 parent ad7e26b commit 062ea7a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,29 @@ public abstract class AbstractEthTask<T> implements EthTask<T> {
private final Collection<CompletableFuture<?>> subTaskFutures = new ConcurrentLinkedDeque<>();

protected AbstractEthTask(final MetricsSystem metricsSystem) {
this(buildOperationTimer(metricsSystem));
}

protected AbstractEthTask(final OperationTimer taskTimer) {
this.taskTimer = taskTimer;
}

private static OperationTimer buildOperationTimer(final MetricsSystem metricsSystem) {
final LabelledMetric<OperationTimer> ethTasksTimer =
metricsSystem.createLabelledTimer(
MetricCategory.SYNCHRONIZER, "task", "Internal processing tasks", "taskName");
if (ethTasksTimer == NoOpMetricsSystem.NO_OP_LABELLED_1_OPERATION_TIMER) {
taskTimer =
() ->
new OperationTimer.TimingContext() {
final Stopwatch stopwatch = Stopwatch.createStarted();

@Override
public double stopTimer() {
return stopwatch.elapsed(TimeUnit.MILLISECONDS) / 1000.0;
}
};
return () ->
new OperationTimer.TimingContext() {
final Stopwatch stopwatch = Stopwatch.createStarted();

@Override
public double stopTimer() {
return stopwatch.elapsed(TimeUnit.MILLISECONDS) / 1000.0;
}
};
} else {
taskTimer = ethTasksTimer.labels(getClass().getSimpleName());
return ethTasksTimer.labels(AbstractEthTask.class.getSimpleName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,29 @@
package tech.pegasys.pantheon.ethereum.eth.manager.task;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import tech.pegasys.pantheon.metrics.OperationTimer;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;
import tech.pegasys.pantheon.metrics.prometheus.MetricsConfiguration;
import tech.pegasys.pantheon.metrics.prometheus.PrometheusMetricsSystem;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.LockSupport;

import com.google.common.collect.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class AbstractEthTaskTest {

@Mock private OperationTimer mockOperationTimer;
@Mock private OperationTimer.TimingContext mockTimingContext;

@Test
public void shouldCancelAllIncompleteSubtasksWhenMultipleIncomplete() {
final CompletableFuture<Void> subtask1 = new CompletableFuture<>();
Expand Down Expand Up @@ -83,35 +90,16 @@ public void shouldCompleteWhenCancelNotCalled() {
}

@Test
public void shouldTakeTimeToExecuteNoOpMetrics() {
final AbstractEthTask<Void> waitTask =
new AbstractEthTask<Void>(new NoOpMetricsSystem()) {
public void shouldInvokeTimingMethods() {
final AbstractEthTask<Void> task =
new AbstractEthTask<Void>(mockOperationTimer) {
@Override
protected void executeTask() {
LockSupport.parkNanos(1_000_000);
}
protected void executeTask() {}
};

waitTask.run();

assertThat(waitTask.getTaskTimeInSec()).isGreaterThan(0.0);
}

@Test
public void shouldTakeTimeToExecutePrometheusMetrics() {
final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault();
metricsConfiguration.setEnabled(true);
final AbstractEthTask<Void> waitTask =
new AbstractEthTask<Void>(PrometheusMetricsSystem.init(metricsConfiguration)) {
@Override
protected void executeTask() {
LockSupport.parkNanos(1_000_000);
}
};

waitTask.run();

assertThat(waitTask.getTaskTimeInSec()).isGreaterThan(0.0);
when(mockOperationTimer.startTimer()).thenReturn(mockTimingContext);
task.run();
verify(mockOperationTimer).startTimer();
verify(mockTimingContext).stopTimer();
}

private class EthTaskWithMultipleSubtasks extends AbstractEthTask<Void> {
Expand Down

0 comments on commit 062ea7a

Please sign in to comment.