|
|
@ -14,7 +14,10 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package org.hyperledger.besu.ethereum.api.jsonrpc.internal; |
|
|
|
package org.hyperledger.besu.ethereum.api.jsonrpc.internal; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
|
|
|
|
|
|
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeoutException; |
|
|
|
|
|
|
|
|
|
|
|
import io.vertx.core.Vertx; |
|
|
|
import io.vertx.core.Vertx; |
|
|
|
import io.vertx.ext.unit.Async; |
|
|
|
import io.vertx.ext.unit.Async; |
|
|
@ -32,63 +35,46 @@ public class QosTimerTest { |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldExecuteConsecutivelyAtTimeout(final TestContext ctx) { |
|
|
|
public void shouldExecuteConsecutivelyAtTimeout(final TestContext ctx) { |
|
|
|
final long TEST_QOS_TIMEOUT = 100L; |
|
|
|
final long TEST_QOS_TIMEOUT = 100L; |
|
|
|
final Async async = ctx.async(); |
|
|
|
final int INVOCATIONS_COUNT = 2; |
|
|
|
final AtomicInteger execCount = new AtomicInteger(0); |
|
|
|
final Async async = ctx.async(INVOCATIONS_COUNT); |
|
|
|
new QosTimer(vertx, TEST_QOS_TIMEOUT, z -> execCount.incrementAndGet()); |
|
|
|
final long startTime = System.currentTimeMillis(); |
|
|
|
|
|
|
|
new QosTimer(vertx, TEST_QOS_TIMEOUT, z -> async.countDown()); |
|
|
|
vertx.setTimer( |
|
|
|
async.awaitSuccess(); |
|
|
|
250L, |
|
|
|
final long executionTime = System.currentTimeMillis() - startTime; |
|
|
|
z -> { |
|
|
|
assertThat(executionTime) |
|
|
|
ctx.assertEquals(2, execCount.get()); |
|
|
|
.as("Execution ended ahead of time") |
|
|
|
async.complete(); |
|
|
|
.isGreaterThanOrEqualTo(TEST_QOS_TIMEOUT * INVOCATIONS_COUNT); |
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldExecuteOnceAtTimeout(final TestContext ctx) { |
|
|
|
public void shouldExecuteOnceAtTimeout(final TestContext ctx) { |
|
|
|
final long TEST_QOS_TIMEOUT = 75L; |
|
|
|
final long TEST_QOS_TIMEOUT = 75L; |
|
|
|
final Async async = ctx.async(); |
|
|
|
final Async async = ctx.async(); |
|
|
|
final AtomicInteger execCount = new AtomicInteger(0); |
|
|
|
final long startTime = System.currentTimeMillis(); |
|
|
|
new QosTimer(vertx, TEST_QOS_TIMEOUT, z -> execCount.incrementAndGet()); |
|
|
|
new QosTimer(vertx, TEST_QOS_TIMEOUT, z -> async.countDown()); |
|
|
|
|
|
|
|
async.awaitSuccess(); |
|
|
|
vertx.setTimer( |
|
|
|
final long executionTime = System.currentTimeMillis() - startTime; |
|
|
|
100L, |
|
|
|
assertThat(executionTime) |
|
|
|
z -> { |
|
|
|
.as("Execution ended ahead of time") |
|
|
|
ctx.assertEquals(1, execCount.get()); |
|
|
|
.isGreaterThanOrEqualTo(TEST_QOS_TIMEOUT); |
|
|
|
async.complete(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldNotExecuteBeforeTimeout(final TestContext ctx) { |
|
|
|
public void shouldNotExecuteBeforeTimeout(final TestContext ctx) { |
|
|
|
final long TEST_QOS_TIMEOUT = 200L; |
|
|
|
final long TEST_QOS_TIMEOUT = 200L; |
|
|
|
final Async async = ctx.async(); |
|
|
|
final Async async = ctx.async(); |
|
|
|
final AtomicInteger execCount = new AtomicInteger(0); |
|
|
|
new QosTimer(vertx, TEST_QOS_TIMEOUT, z -> async.countDown()); |
|
|
|
new QosTimer(vertx, TEST_QOS_TIMEOUT, z -> execCount.incrementAndGet()); |
|
|
|
assertThatThrownBy(() -> async.awaitSuccess(50L)).isInstanceOf(TimeoutException.class); |
|
|
|
|
|
|
|
|
|
|
|
vertx.setTimer( |
|
|
|
|
|
|
|
50L, |
|
|
|
|
|
|
|
z -> { |
|
|
|
|
|
|
|
ctx.assertEquals(0, execCount.get()); |
|
|
|
|
|
|
|
async.complete(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void shouldNotExecuteWhenReset(final TestContext ctx) { |
|
|
|
public void shouldNotExecuteWhenReset(final TestContext ctx) { |
|
|
|
final long TEST_QOS_TIMEOUT = 50L; |
|
|
|
final long TEST_QOS_TIMEOUT = 50L; |
|
|
|
final Async async = ctx.async(); |
|
|
|
final Async async = ctx.async(); |
|
|
|
final AtomicInteger execCount = new AtomicInteger(0); |
|
|
|
final var timer = new QosTimer(vertx, TEST_QOS_TIMEOUT, z -> async.countDown()); |
|
|
|
final var timer = new QosTimer(vertx, TEST_QOS_TIMEOUT, z -> execCount.incrementAndGet()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reset QoS timer every 25 millis
|
|
|
|
// reset QoS timer every 25 millis
|
|
|
|
vertx.setPeriodic(25L, z -> timer.resetTimer()); |
|
|
|
vertx.setPeriodic(25L, z -> timer.resetTimer()); |
|
|
|
|
|
|
|
assertThatThrownBy(() -> async.awaitSuccess(200L)).isInstanceOf(TimeoutException.class); |
|
|
|
vertx.setTimer( |
|
|
|
async.complete(); |
|
|
|
200L, |
|
|
|
|
|
|
|
z -> { |
|
|
|
|
|
|
|
ctx.assertEquals(0, execCount.get()); |
|
|
|
|
|
|
|
async.complete(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|