summaryrefslogtreecommitdiffstats
path: root/messagebus
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-09-14 09:37:53 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2022-09-14 09:37:53 +0200
commit782962c90a4e52eda6666de98881871bb8ef7879 (patch)
treed2cce2aeea938224b557df2d4781144b81ebb3c7 /messagebus
parentbb54881376421e09daa8287173e06458b0c1f45a (diff)
Simplify and unify the use of Timers.
Diffstat (limited to 'messagebus')
-rw-r--r--messagebus/src/test/java/com/yahoo/messagebus/CustomTimer.java17
-rw-r--r--messagebus/src/test/java/com/yahoo/messagebus/DynamicThrottlePolicyTest.java19
-rw-r--r--messagebus/src/test/java/com/yahoo/messagebus/RateThrottlingTestCase.java7
-rw-r--r--messagebus/src/test/java/com/yahoo/messagebus/ThrottlerTestCase.java27
-rwxr-xr-xmessagebus/src/test/java/com/yahoo/messagebus/network/rpc/TargetPoolTestCase.java28
5 files changed, 38 insertions, 60 deletions
diff --git a/messagebus/src/test/java/com/yahoo/messagebus/CustomTimer.java b/messagebus/src/test/java/com/yahoo/messagebus/CustomTimer.java
deleted file mode 100644
index 539897d6485..00000000000
--- a/messagebus/src/test/java/com/yahoo/messagebus/CustomTimer.java
+++ /dev/null
@@ -1,17 +0,0 @@
-// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.messagebus;
-
-import com.yahoo.concurrent.Timer;
-
-/**
- * @author <a href="mailto:thomasg@yahoo-inc.com">Thomas Gundersen</a>
- */
-class CustomTimer implements Timer {
-
- long millis = 0;
-
- @Override
- public long milliTime() {
- return millis;
- }
-}
diff --git a/messagebus/src/test/java/com/yahoo/messagebus/DynamicThrottlePolicyTest.java b/messagebus/src/test/java/com/yahoo/messagebus/DynamicThrottlePolicyTest.java
index 8f9ed2323d7..8414f6588ea 100644
--- a/messagebus/src/test/java/com/yahoo/messagebus/DynamicThrottlePolicyTest.java
+++ b/messagebus/src/test/java/com/yahoo/messagebus/DynamicThrottlePolicyTest.java
@@ -1,6 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;
+import com.yahoo.concurrent.ManualTimer;
import com.yahoo.messagebus.test.SimpleMessage;
import com.yahoo.messagebus.test.SimpleReply;
import org.junit.jupiter.api.Test;
@@ -46,7 +47,7 @@ public class DynamicThrottlePolicyTest {
{ // This setup is lucky with the artificial local maxima for latency, and gives good results. See below for counter-examples.
int workPerSuccess = 8;
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer).setMinWindowSize(1)
.setWindowSizeIncrement(0.1)
.setResizeRate(100);
@@ -64,7 +65,7 @@ public class DynamicThrottlePolicyTest {
{ // This setup is not so lucky, and the artificial behaviour pushes it into overload.
int workPerSuccess = 5;
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer).setMinWindowSize(1)
.setWindowSizeIncrement(0.1)
.setResizeRate(100);
@@ -80,7 +81,7 @@ public class DynamicThrottlePolicyTest {
{ // This setup is not so lucky either, and the artificial behaviour keeps it far below a good throughput.
int workPerSuccess = 4;
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer).setMinWindowSize(1)
.setWindowSizeIncrement(0.1)
.setResizeRate(100);
@@ -98,7 +99,7 @@ public class DynamicThrottlePolicyTest {
@Test
void singlePolicySingleWorkerWithIncreasingParallelism() {
for (int exponent = 0; exponent < 4; exponent++) {
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);
int scaleFactor = (int) Math.pow(10, exponent);
long operations = 3_000L * scaleFactor;
@@ -121,7 +122,7 @@ public class DynamicThrottlePolicyTest {
@Test
void singlePolicyIncreasingWorkersWithNoParallelism() {
for (int exponent = 0; exponent < 4; exponent++) {
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);
int scaleFactor = (int) Math.pow(10, exponent);
long operations = 2_000L * scaleFactor;
@@ -156,7 +157,7 @@ public class DynamicThrottlePolicyTest {
int numberOfWorkers = 1 + (int) (10 * Math.random());
int maximumTasksPerWorker = 100_000;
int workerParallelism = 32;
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy1 = new DynamicThrottlePolicy(timer);
DynamicThrottlePolicy policy2 = new DynamicThrottlePolicy(timer).setWeight(0.5);
Summary summary = run(operations, workPerSuccess, numberOfWorkers, maximumTasksPerWorker, workerParallelism, timer, policy1, policy2);
@@ -180,7 +181,7 @@ public class DynamicThrottlePolicyTest {
int numberOfWorkers = 6;
int maximumTasksPerWorker = 180 + (int) (120 * Math.random());
int workerParallelism = 60 + (int) (40 * Math.random());
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
int p = 10;
DynamicThrottlePolicy[] policies = IntStream.range(0, p)
.mapToObj(j -> new DynamicThrottlePolicy(timer)
@@ -213,7 +214,7 @@ public class DynamicThrottlePolicyTest {
}
private Summary run(long operations, int workPerSuccess, int numberOfWorkers, int maximumTasksPerWorker,
- int workerParallelism, CustomTimer timer, DynamicThrottlePolicy... policies) {
+ int workerParallelism, ManualTimer timer, DynamicThrottlePolicy... policies) {
System.err.printf("\n### Running %d operations of %d ticks each against %d workers with parallelism %d and queue size %d\n",
operations, workPerSuccess, numberOfWorkers, workerParallelism, maximumTasksPerWorker);
@@ -250,7 +251,7 @@ public class DynamicThrottlePolicyTest {
++ticks;
totalPending += resource.pending();
resource.tick();
- ++timer.millis;
+ timer.advance(1);
}
for (int i = 0; i < windows.length; i++)
diff --git a/messagebus/src/test/java/com/yahoo/messagebus/RateThrottlingTestCase.java b/messagebus/src/test/java/com/yahoo/messagebus/RateThrottlingTestCase.java
index 5e579f5d622..b4ca923c4ff 100644
--- a/messagebus/src/test/java/com/yahoo/messagebus/RateThrottlingTestCase.java
+++ b/messagebus/src/test/java/com/yahoo/messagebus/RateThrottlingTestCase.java
@@ -1,6 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;
+import com.yahoo.concurrent.ManualTimer;
import com.yahoo.messagebus.test.SimpleMessage;
import org.junit.jupiter.api.Test;
@@ -11,7 +12,7 @@ public class RateThrottlingTestCase {
@Test
void testPending() {
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
RateThrottlingPolicy policy = new RateThrottlingPolicy(5.0, timer);
policy.setMaxPendingCount(200);
@@ -20,7 +21,7 @@ public class RateThrottlingTestCase {
}
public int getActualRate(double desiredRate) {
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
RateThrottlingPolicy policy = new RateThrottlingPolicy(desiredRate, timer);
int ok = 0;
@@ -28,7 +29,7 @@ public class RateThrottlingTestCase {
if (policy.canSend(new SimpleMessage("test"), 0)) {
ok++;
}
- timer.millis += 10;
+ timer.advance(10);
}
return ok;
diff --git a/messagebus/src/test/java/com/yahoo/messagebus/ThrottlerTestCase.java b/messagebus/src/test/java/com/yahoo/messagebus/ThrottlerTestCase.java
index 045dc1177db..2e50d561778 100644
--- a/messagebus/src/test/java/com/yahoo/messagebus/ThrottlerTestCase.java
+++ b/messagebus/src/test/java/com/yahoo/messagebus/ThrottlerTestCase.java
@@ -1,6 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;
+import com.yahoo.concurrent.ManualTimer;
import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.slobrok.server.Slobrok;
import com.yahoo.messagebus.network.rpc.test.TestServer;
@@ -14,7 +15,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import java.util.Arrays;
+import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@@ -29,8 +30,8 @@ public class ThrottlerTestCase {
@BeforeEach
public void setUp() throws ListenFailedException {
RoutingTableSpec table = new RoutingTableSpec(SimpleProtocol.NAME);
- table.addHop("dst", "test/dst/session", Arrays.asList("test/dst/session"));
- table.addRoute("test", Arrays.asList("dst"));
+ table.addHop("dst", "test/dst/session", List.of("test/dst/session"));
+ table.addRoute("test", List.of("dst"));
slobrok = new Slobrok();
src = new TestServer("test/src", table, slobrok, null);
dst = new TestServer("test/dst", table, slobrok, null);
@@ -127,7 +128,7 @@ public class ThrottlerTestCase {
@Test
void testDynamicWindowSize() {
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);
policy.setWindowSizeIncrement(5)
@@ -151,7 +152,7 @@ public class ThrottlerTestCase {
@Test
void testIdleTimePeriod() {
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);
policy.setWindowSizeIncrement(5)
@@ -162,15 +163,15 @@ public class ThrottlerTestCase {
assertTrue(windowSize >= 90 && windowSize <= 110);
Message msg = new SimpleMessage("foo");
- timer.millis += 30 * 1000;
+ timer.advance(30 * 1000);
assertTrue(policy.canSend(msg, 0));
assertTrue(windowSize >= 90 && windowSize <= 110);
- timer.millis += 60 * 1000 + 1;
+ timer.advance(60 * 1000 + 1);
assertTrue(policy.canSend(msg, 50));
assertEquals(55, policy.getMaxPendingCount());
- timer.millis += 60 * 1000 + 1;
+ timer.advance(60 * 1000 + 1);
assertTrue(policy.canSend(msg, 0));
assertEquals(5, policy.getMaxPendingCount());
@@ -178,7 +179,7 @@ public class ThrottlerTestCase {
@Test
void testMinWindowSize() {
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);
policy.setWindowSizeIncrement(5)
@@ -191,7 +192,7 @@ public class ThrottlerTestCase {
@Test
void testMaxWindowSize() {
- CustomTimer timer = new CustomTimer();
+ ManualTimer timer = new ManualTimer();
DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer);
policy.setWindowSizeIncrement(5);
@@ -202,7 +203,7 @@ public class ThrottlerTestCase {
assertTrue(windowSize >= 40 && windowSize <= 50);
}
- private int getWindowSize(DynamicThrottlePolicy policy, CustomTimer timer, int maxPending) {
+ private int getWindowSize(DynamicThrottlePolicy policy, ManualTimer timer, int maxPending) {
Message msg = new SimpleMessage("foo");
Reply reply = new SimpleReply("bar");
reply.setContext(1);
@@ -213,8 +214,8 @@ public class ThrottlerTestCase {
++numPending;
}
- long tripTime = (numPending < maxPending) ? 1000 : 1000 + (numPending - maxPending) * 1000;
- timer.millis += tripTime;
+ long tripTime = (numPending < maxPending) ? 1000L : 1000 + (numPending - maxPending) * 1000L;
+ timer.advance(tripTime);
while (--numPending >= 0) {
policy.processReply(reply);
diff --git a/messagebus/src/test/java/com/yahoo/messagebus/network/rpc/TargetPoolTestCase.java b/messagebus/src/test/java/com/yahoo/messagebus/network/rpc/TargetPoolTestCase.java
index afeaa1304a1..9fb817ad12f 100755
--- a/messagebus/src/test/java/com/yahoo/messagebus/network/rpc/TargetPoolTestCase.java
+++ b/messagebus/src/test/java/com/yahoo/messagebus/network/rpc/TargetPoolTestCase.java
@@ -6,6 +6,7 @@ import com.yahoo.jrt.Supervisor;
import com.yahoo.jrt.Transport;
import com.yahoo.jrt.slobrok.server.Slobrok;
import com.yahoo.concurrent.Timer;
+import com.yahoo.concurrent.ManualTimer;
import com.yahoo.messagebus.network.rpc.test.TestServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@@ -46,7 +47,7 @@ public class TargetPoolTestCase {
// Necessary setup to be able to resolve targets.
RPCServiceAddress adr1 = registerServer();
- PoolTimer timer = new PoolTimer();
+ Timer timer = new ManualTimer();
RPCTargetPool pool1 = new RPCTargetPool(timer, 0.666, 1);
RPCTarget target1 = pool1.getTarget(orb, adr1);
@@ -80,7 +81,7 @@ public class TargetPoolTestCase {
RPCServiceAddress adr2 = registerServer();
RPCServiceAddress adr3 = registerServer();
- PoolTimer timer = new PoolTimer();
+ ManualTimer timer = new ManualTimer();
RPCTargetPool pool = new RPCTargetPool(timer, 0.666, 1);
// Assert that all connections expire.
@@ -96,7 +97,7 @@ public class TargetPoolTestCase {
pool.flushTargets(false);
assertEquals(3, pool.size());
}
- timer.millis += 999;
+ timer.advance(999);
pool.flushTargets(false);
assertEquals(0, pool.size());
@@ -108,7 +109,7 @@ public class TargetPoolTestCase {
assertNotNull(target = pool.getTarget(orb, adr3));
target.subRef();
assertEquals(3, pool.size());
- timer.millis += 444;
+ timer.advance(444);
pool.flushTargets(false);
assertEquals(3, pool.size());
assertNotNull(target = pool.getTarget(orb, adr2));
@@ -116,15 +117,15 @@ public class TargetPoolTestCase {
assertNotNull(target = pool.getTarget(orb, adr3));
target.subRef();
assertEquals(3, pool.size());
- timer.millis += 444;
+ timer.advance(444);
pool.flushTargets(false);
assertEquals(2, pool.size());
assertNotNull(target = pool.getTarget(orb, adr3));
target.subRef();
- timer.millis += 444;
+ timer.advance(444);
pool.flushTargets(false);
assertEquals(1, pool.size());
- timer.millis += 444;
+ timer.advance(444);
pool.flushTargets(false);
assertEquals(0, pool.size());
@@ -132,12 +133,12 @@ public class TargetPoolTestCase {
assertNotNull(target = pool.getTarget(orb, adr1));
assertEquals(1, pool.size());
for (int i = 0; i < 10; ++i) {
- timer.millis += 999;
+ timer.advance(999);
pool.flushTargets(false);
assertEquals(1, pool.size());
}
target.subRef();
- timer.millis += 999;
+ timer.advance(999);
pool.flushTargets(false);
assertEquals(0, pool.size());
}
@@ -147,13 +148,4 @@ public class TargetPoolTestCase {
return new RPCServiceAddress("foo/bar", servers.get(servers.size() - 1).mb.getConnectionSpec());
}
- private static class PoolTimer implements Timer {
- long millis = 0;
-
- @Override
- public long milliTime() {
- return millis;
- }
- }
-
}