diff options
author | Bjørn Christian Seime <bjorncs@yahooinc.com> | 2023-08-29 15:06:39 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@yahooinc.com> | 2023-08-30 14:10:39 +0200 |
commit | 0ccdbed4b8e90e3f07da11cf31f25f654b200216 (patch) | |
tree | 4a3051501899fccd7cdc653272ebe231f9170709 /container-core | |
parent | 08802d7adf9a80a0ead56fe5dcd7ddb89d14e6db (diff) |
Mocking native methods no longer possible
Diffstat (limited to 'container-core')
2 files changed, 19 insertions, 5 deletions
diff --git a/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ConnectionThrottler.java b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ConnectionThrottler.java index 06d1a707be9..06a9986e996 100644 --- a/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ConnectionThrottler.java +++ b/container-core/src/main/java/com/yahoo/jdisc/http/server/jetty/ConnectionThrottler.java @@ -46,11 +46,11 @@ class ConnectionThrottler extends ContainerLifeCycle implements SelectorManager. private boolean isThrottling = false; ConnectionThrottler(AbstractConnector connector, ConnectorConfig.Throttling config) { - this(Runtime.getRuntime(), new RateStatistic(1, TimeUnit.SECONDS), connector.getScheduler(), connector, config); + this(Jvm.fromRuntime(), new RateStatistic(1, TimeUnit.SECONDS), connector.getScheduler(), connector, config); } // Intended for unit testing - ConnectionThrottler(Runtime runtime, + ConnectionThrottler(Jvm runtime, RateStatistic rateStatistic, Scheduler scheduler, AbstractConnector connector, @@ -150,10 +150,10 @@ class ConnectionThrottler extends ContainerLifeCycle implements SelectorManager. * Note: implementation inspired by Jetty's {@link LowResourceMonitor} */ private static class HeapResourceLimit extends AbstractLifeCycle implements ResourceLimit { - private final Runtime runtime; + private final Jvm runtime; private final double maxHeapUtilization; - HeapResourceLimit(Runtime runtime, double maxHeapUtilization) { + HeapResourceLimit(Jvm runtime, double maxHeapUtilization) { this.runtime = runtime; this.maxHeapUtilization = maxHeapUtilization; } @@ -269,4 +269,18 @@ class ConnectionThrottler extends ContainerLifeCycle implements SelectorManager. } } } + + interface Jvm { + long maxMemory(); + long freeMemory(); + + static Jvm fromRuntime() { + return new Jvm() { + final Runtime rt = Runtime.getRuntime(); + + @Override public long maxMemory() { return rt.maxMemory(); } + @Override public long freeMemory() { return rt.freeMemory(); } + }; + } + } } diff --git a/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/ConnectionThrottlerTest.java b/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/ConnectionThrottlerTest.java index a65231db2b7..cc73ab52aa1 100644 --- a/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/ConnectionThrottlerTest.java +++ b/container-core/src/test/java/com/yahoo/jdisc/http/server/jetty/ConnectionThrottlerTest.java @@ -24,7 +24,7 @@ public class ConnectionThrottlerTest { @Test void throttles_when_any_resource_check_exceeds_configured_threshold() { - Runtime runtime = mock(Runtime.class); + var runtime = mock(ConnectionThrottler.Jvm.class); when(runtime.maxMemory()).thenReturn(100l); RateStatistic rateStatistic = new RateStatistic(1, TimeUnit.HOURS); MockScheduler scheduler = new MockScheduler(); |