From 1a08736ff64d8c5fc3f880f24a44d0022828cc11 Mon Sep 17 00:00:00 2001 From: gjoranv Date: Fri, 22 Jan 2021 16:13:17 +0100 Subject: Rewrite tests to not use the defunct StateMetricConsumer. --- .../jdisc/state/MetricsPacketsHandlerTest.java | 61 +++-- .../jdisc/state/MockSnapshotProvider.java | 23 ++ .../container/jdisc/state/StateHandlerTest.java | 299 ++++++--------------- .../jdisc/state/StateHandlerTestBase.java | 109 +++----- 4 files changed, 175 insertions(+), 317 deletions(-) create mode 100644 container-core/src/test/java/com/yahoo/container/jdisc/state/MockSnapshotProvider.java diff --git a/container-core/src/test/java/com/yahoo/container/jdisc/state/MetricsPacketsHandlerTest.java b/container-core/src/test/java/com/yahoo/container/jdisc/state/MetricsPacketsHandlerTest.java index ef700597537..89bde3eeb5b 100644 --- a/container-core/src/test/java/com/yahoo/container/jdisc/state/MetricsPacketsHandlerTest.java +++ b/container-core/src/test/java/com/yahoo/container/jdisc/state/MetricsPacketsHandlerTest.java @@ -3,12 +3,13 @@ package com.yahoo.container.jdisc.state; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.yahoo.jdisc.Metric; +import com.yahoo.container.jdisc.RequestHandlerTestDriver; +import org.junit.Before; import org.junit.Test; import java.util.ArrayList; -import java.util.Collections; import java.util.List; +import java.util.Map; import static com.yahoo.container.jdisc.state.MetricsPacketsHandler.APPLICATION_KEY; import static com.yahoo.container.jdisc.state.MetricsPacketsHandler.DIMENSIONS_KEY; @@ -25,9 +26,21 @@ import static org.junit.Assert.assertTrue; */ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { + private static final String APPLICATION_NAME = "state-handler-test-base"; + + private static MetricsPacketsHandler metricsPacketsHandler; + + @Before + public void setupHandler() { + metricsPacketsHandlerConfig = new MetricsPacketsHandlerConfig(new MetricsPacketsHandlerConfig.Builder() + .application(APPLICATION_NAME)); + metricsPacketsHandler = new MetricsPacketsHandler(monitor, timer, snapshotProviderRegistry, + metricsPresentationConfig, metricsPacketsHandlerConfig); + testDriver = new RequestHandlerTestDriver(metricsPacketsHandler); + } + @Test - public void only_status_packet_is_returned_prior_to_first_snapshot() throws Exception { - metric.add("not_included", 1, null); + public void status_packet_is_returned_prior_to_first_snapshot() throws Exception { String response = requestAsString("http://localhost/metrics-packets"); List packets = toJsonPackets(response); @@ -41,7 +54,7 @@ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { @Test public void metrics_are_included_after_snapshot() throws Exception { - metric.add("counter", 1, null); + createSnapshotWithCountMetric("counter", 1, null); List packets = incrementTimeAndGetJsonPackets(); assertEquals(2, packets.size()); @@ -51,7 +64,7 @@ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { @Test public void metadata_is_included_in_each_metrics_packet() throws Exception { - metric.add("counter", 1, null); + createSnapshotWithCountMetric("counter", 1, null); List packets = incrementTimeAndGetJsonPackets(); JsonNode counterPacket = packets.get(1); @@ -62,7 +75,7 @@ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { @Test public void timestamp_resolution_is_in_seconds() throws Exception { - metric.add("counter", 1, null); + createSnapshotWithCountMetric("counter", 1, null); List packets = incrementTimeAndGetJsonPackets(); JsonNode counterPacket = packets.get(1); @@ -71,8 +84,10 @@ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { @Test public void expected_aggregators_are_output_for_gauge_metrics() throws Exception{ - Metric.Context context = metric.createContext(Collections.singletonMap("dim1", "value1")); - metric.set("gauge", 0.2, null); + var context = StateMetricContext.newInstance(Map.of("dim1", "value1")); + var snapshot = new MetricSnapshot(); + snapshot.set(context, "gauge", 0.2); + snapshotProvider.setSnapshot(snapshot); List packets = incrementTimeAndGetJsonPackets(); JsonNode gaugeMetric = packets.get(1).get(METRICS_KEY); @@ -84,8 +99,8 @@ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { @Test public void dimensions_from_context_are_included() throws Exception { - Metric.Context context = metric.createContext(Collections.singletonMap("dim1", "value1")); - metric.add("counter", 1, context); + var context = StateMetricContext.newInstance(Map.of("dim1", "value1")); + createSnapshotWithCountMetric("counter", 1, context); List packets = incrementTimeAndGetJsonPackets(); JsonNode counterPacket = packets.get(1); @@ -97,9 +112,11 @@ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { @Test public void metrics_with_identical_dimensions_are_contained_in_the_same_packet() throws Exception { - Metric.Context context = metric.createContext(Collections.singletonMap("dim1", "value1")); - metric.add("counter1", 1, context); - metric.add("counter2", 2, context); + var context = StateMetricContext.newInstance(Map.of("dim1", "value1")); + var snapshot = new MetricSnapshot(); + snapshot.add(context, "counter1", 1); + snapshot.add(context, "counter2", 2); + snapshotProvider.setSnapshot(snapshot); List packets = incrementTimeAndGetJsonPackets(); assertEquals(2, packets.size()); @@ -112,10 +129,12 @@ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { @Test public void metrics_with_different_dimensions_get_separate_packets() throws Exception { - Metric.Context context1 = metric.createContext(Collections.singletonMap("dim1", "value1")); - Metric.Context context2 = metric.createContext(Collections.singletonMap("dim2", "value2")); - metric.add("counter1", 1, context1); - metric.add("counter2", 1, context2); + var context1 = StateMetricContext.newInstance(Map.of("dim1", "value1")); + var context2 = StateMetricContext.newInstance(Map.of("dim2", "value2")); + var snapshot = new MetricSnapshot(); + snapshot.add(context1, "counter1", 1); + snapshot.add(context2, "counter2", 2); + snapshotProvider.setSnapshot(snapshot); List packets = incrementTimeAndGetJsonPackets(); assertEquals(3, packets.size()); @@ -145,4 +164,10 @@ public class MetricsPacketsHandlerTest extends StateHandlerTestBase { assertEquals(expected, counterMetrics.get(metricName).asLong()); } + private void createSnapshotWithCountMetric(String name, Number value, MetricDimensions context) { + var snapshot = new MetricSnapshot(); + snapshot.add(context, name, value); + snapshotProvider.setSnapshot(snapshot); + } + } diff --git a/container-core/src/test/java/com/yahoo/container/jdisc/state/MockSnapshotProvider.java b/container-core/src/test/java/com/yahoo/container/jdisc/state/MockSnapshotProvider.java new file mode 100644 index 00000000000..5cc65be855d --- /dev/null +++ b/container-core/src/test/java/com/yahoo/container/jdisc/state/MockSnapshotProvider.java @@ -0,0 +1,23 @@ +package com.yahoo.container.jdisc.state; + +import java.io.PrintStream; + +/** + * @author gjoranv + */ +public class MockSnapshotProvider implements SnapshotProvider { + + private MetricSnapshot snapshot; + + public void setSnapshot(MetricSnapshot snapshot) { + this.snapshot = snapshot; + } + + @Override + public MetricSnapshot latestSnapshot() { + return snapshot; + } + + @Override + public void histogram(PrintStream output) { } +} diff --git a/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java b/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java index 26a2f817acc..1258ecdc46f 100644 --- a/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java +++ b/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTest.java @@ -3,14 +3,13 @@ package com.yahoo.container.jdisc.state; import com.fasterxml.jackson.databind.JsonNode; import com.yahoo.component.Vtag; -import com.yahoo.jdisc.Metric; +import com.yahoo.container.jdisc.RequestHandlerTestDriver; import com.yahoo.vespa.defaults.Defaults; +import org.junit.Before; import org.junit.Test; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; -import java.util.TreeMap; +import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -21,98 +20,82 @@ import static org.junit.Assert.assertTrue; */ public class StateHandlerTest extends StateHandlerTestBase { + private static final String V1_URI = URI_BASE + "/state/v1/"; + private static StateHandler stateHandler; + + @Before + public void setupHandler() { + stateHandler = new StateHandler(monitor, timer, applicationMetadataConfig, snapshotProviderRegistry , metricsPresentationConfig); + testDriver = new RequestHandlerTestDriver(stateHandler); + } + @Test - public void testReportPriorToFirstSnapshot() throws Exception { - metric.add("foo", 1, null); - metric.set("bar", 4, null); - JsonNode json = requestAsJson("http://localhost/state/v1/all"); + public void testStatusReportPriorToFirstSnapshot() throws Exception { + JsonNode json = requestAsJson(V1_URI + "/all"); assertEquals(json.toString(), "up", json.get("status").get("code").asText()); assertFalse(json.toString(), json.get("metrics").has("values")); } @Test public void testReportIncludesMetricsAfterSnapshot() throws Exception { - metric.add("foo", 1, null); - metric.set("bar", 4, null); - advanceToNextSnapshot(); - JsonNode json1 = requestAsJson("http://localhost/state/v1/metrics"); + var snapshot = new MetricSnapshot(); + snapshot.add(null, "foo", 1); + snapshot.set(null, "bar", 4); + snapshotProvider.setSnapshot(snapshot); + + JsonNode json1 = requestAsJson(V1_URI + "metrics"); assertEquals(json1.toString(), "up", json1.get("status").get("code").asText()); assertEquals(json1.toString(), 2, json1.get("metrics").get("values").size()); - - metric.add("fuz", 1, metric.createContext(new HashMap<>(0))); - advanceToNextSnapshot(); - JsonNode json2 = requestAsJson("http://localhost/state/v1/metrics"); - assertEquals(json2.toString(), "up", json2.get("status").get("code").asText()); - assertEquals(json2.toString(), 3, json2.get("metrics").get("values").size()); } - /** - * Tests that we restart an metric when it changes type from gauge to counter or back. - * This may happen in practice on config reloads. - */ @Test - public void testMetricTypeChangeIsAllowed() { - String metricName = "myMetric"; - Metric.Context metricContext = null; - - { - // Add a count metric - metric.add(metricName, 1, metricContext); - metric.add(metricName, 2, metricContext); - // Change it to a gauge metric - metric.set(metricName, 9, metricContext); - advanceToNextSnapshot(); - MetricValue resultingMetric = monitor.snapshot().iterator().next().getValue().get(metricName); - assertEquals(GaugeMetric.class, resultingMetric.getClass()); - assertEquals("Value was reset and produces the last gauge value", - 9.0, ((GaugeMetric) resultingMetric).getLast(), 0.000001); - } - - { - // Add a gauge metric - metric.set(metricName, 9, metricContext); - // Change it to a count metric - metric.add(metricName, 1, metricContext); - metric.add(metricName, 2, metricContext); - advanceToNextSnapshot(); - MetricValue resultingMetric = monitor.snapshot().iterator().next().getValue().get(metricName); - assertEquals(CountMetric.class, resultingMetric.getClass()); - assertEquals("Value was reset, and changed to add semantics giving 1+2", - 3, ((CountMetric) resultingMetric).getCount()); - } - } + public void testCountMetricCount() throws Exception { + var snapshot = new MetricSnapshot(); + snapshot.add(null, "foo", 4); + snapshot.add(null, "foo", 2); + snapshotProvider.setSnapshot(snapshot); - @Test - public void testAverageAggregationOfValues() throws Exception { - metric.set("bar", 4, null); - metric.set("bar", 5, null); - metric.set("bar", 7, null); - metric.set("bar", 2, null); - advanceToNextSnapshot(); - JsonNode json = requestAsJson("http://localhost/state/v1/all"); + JsonNode json = requestAsJson(V1_URI + "all"); assertEquals(json.toString(), "up", json.get("status").get("code").asText()); assertEquals(json.toString(), 1, json.get("metrics").get("values").size()); - assertEquals(json.toString(), 4.5, - json.get("metrics").get("values").get(0).get("values").get("average").asDouble(), 0.001); + assertEquals(json.toString(), 6, + json.get("metrics").get("values").get(0).get("values").get("count").asDouble(), 0.001); } @Test - public void testSumAggregationOfCounts() throws Exception { - metric.add("foo", 1, null); - metric.add("foo", 1, null); - metric.add("foo", 2, null); - metric.add("foo", 1, null); - advanceToNextSnapshot(); - JsonNode json = requestAsJson("http://localhost/state/v1/all"); - assertEquals(json.toString(), "up", json.get("status").get("code").asText()); - assertEquals(json.toString(), 1, json.get("metrics").get("values").size()); - assertEquals(json.toString(), 5, - json.get("metrics").get("values").get(0).get("values").get("count").asDouble(), 0.001); + public void gaugeSnapshotsTracksCountMinMaxAvgPerPeriod() throws Exception { + var snapshot = new MetricSnapshot(); + snapshot.set(null, "bar", 20); + snapshot.set(null, "bar", 40); + snapshotProvider.setSnapshot(snapshot); + + JsonNode json = requestAsJson(V1_URI + "all"); + JsonNode metricValues = getFirstMetricValueNode(json); + assertEquals(json.toString(), 40, metricValues.get("last").asDouble(), 0.001); + // Last snapshot had explicit values set + assertEquals(json.toString(), 30, metricValues.get("average").asDouble(), 0.001); + assertEquals(json.toString(), 20, metricValues.get("min").asDouble(), 0.001); + assertEquals(json.toString(), 40, metricValues.get("max").asDouble(), 0.001); + assertEquals(json.toString(), 2, metricValues.get("count").asInt()); + } + + private JsonNode getFirstMetricValueNode(JsonNode root) { + assertEquals(root.toString(), 1, root.get("metrics").get("values").size()); + JsonNode metricValues = root.get("metrics").get("values").get(0).get("values"); + assertTrue(root.toString(), metricValues.has("last")); + return metricValues; } @Test - public void testReadabilityOfJsonReport() throws Exception { - metric.add("foo", 1, null); + public void testReadabilityOfJsonReport() { + var snapshot = new MetricSnapshot(0L, SNAPSHOT_INTERVAL, TimeUnit.MILLISECONDS); + snapshot.add(null, "foo", 1); + var ctx = StateMetricContext.newInstance(Map.of("component", "test")); + snapshot.set(ctx, "bar", 2); + snapshot.set(ctx, "bar", 3); + snapshot.set(ctx, "bar", 4); + snapshot.set(ctx, "bar", 5); + snapshotProvider.setSnapshot(snapshot); advanceToNextSnapshot(); assertEquals("{\n" + " \"metrics\": {\n" + @@ -120,37 +103,12 @@ public class StateHandlerTest extends StateHandlerTestBase { " \"from\": 0,\n" + " \"to\": 300\n" + " },\n" + - " \"values\": [{\n" + - " \"name\": \"foo\",\n" + - " \"values\": {\n" + - " \"count\": 1,\n" + - " \"rate\": 0.0033333333333333335\n" + - " }\n" + - " }]\n" + - " },\n" + - " \"status\": {\"code\": \"up\"},\n" + - " \"time\": 300000\n" + - "}", - requestAsString("http://localhost/state/v1/all")); - - Metric.Context ctx = metric.createContext(Collections.singletonMap("component", "test")); - metric.set("bar", 2, ctx); - metric.set("bar", 3, ctx); - metric.set("bar", 4, ctx); - metric.set("bar", 5, ctx); - advanceToNextSnapshot(); - assertEquals("{\n" + - " \"metrics\": {\n" + - " \"snapshot\": {\n" + - " \"from\": 300,\n" + - " \"to\": 600\n" + - " },\n" + " \"values\": [\n" + " {\n" + " \"name\": \"foo\",\n" + " \"values\": {\n" + - " \"count\": 0,\n" + - " \"rate\": 0\n" + + " \"count\": 1,\n" + + " \"rate\": 0.0033333333333333335\n" + " }\n" + " },\n" + " {\n" + @@ -169,131 +127,24 @@ public class StateHandlerTest extends StateHandlerTestBase { " ]\n" + " },\n" + " \"status\": {\"code\": \"up\"},\n" + - " \"time\": 600000\n" + + " \"time\": 300000\n" + "}", - requestAsString("http://localhost/state/v1/all")); - } - - @Test - public void testNotAggregatingCountsBeyondSnapshots() throws Exception { - metric.add("foo", 1, null); - metric.add("foo", 1, null); - advanceToNextSnapshot(); - metric.add("foo", 2, null); - metric.add("foo", 1, null); - advanceToNextSnapshot(); - JsonNode json = requestAsJson("http://localhost/state/v1/all"); - assertEquals(json.toString(), "up", json.get("status").get("code").asText()); - assertEquals(json.toString(), 1, json.get("metrics").get("values").size()); - assertEquals(json.toString(), 3, - json.get("metrics").get("values").get(0).get("values").get("count").asDouble(), 0.001); - } - - @Test - public void testSnapshottingTimes() throws Exception { - metric.add("foo", 1, null); - metric.set("bar", 3, null); - // At this time we should not have done any snapshotting - { - JsonNode json = requestAsJson("http://localhost/state/v1/all"); - assertFalse(json.toString(), json.get("metrics").has("snapshot")); - } - // At this time first snapshot should have been generated - advanceToNextSnapshot(); - { - JsonNode json = requestAsJson("http://localhost/state/v1/all"); - assertTrue(json.toString(), json.get("metrics").has("snapshot")); - assertEquals(0.0, json.get("metrics").get("snapshot").get("from").asDouble(), 0.00001); - assertEquals(300.0, json.get("metrics").get("snapshot").get("to").asDouble(), 0.00001); - } - // No new snapshot - { - JsonNode json = requestAsJson("http://localhost/state/v1/all"); - assertTrue(json.toString(), json.get("metrics").has("snapshot")); - assertEquals(0.0, json.get("metrics").get("snapshot").get("from").asDouble(), 0.00001); - assertEquals(300.0, json.get("metrics").get("snapshot").get("to").asDouble(), 0.00001); - } - // A new snapshot - advanceToNextSnapshot(); - { - JsonNode json = requestAsJson("http://localhost/state/v1/all"); - assertTrue(json.toString(), json.get("metrics").has("snapshot")); - assertEquals(300.0, json.get("metrics").get("snapshot").get("from").asDouble(), 0.00001); - assertEquals(600.0, json.get("metrics").get("snapshot").get("to").asDouble(), 0.00001); - } - } - - @Test - public void testFreshStartOfValuesBeyondSnapshot() throws Exception { - metric.set("bar", 4, null); - metric.set("bar", 5, null); - advanceToNextSnapshot(); - metric.set("bar", 4, null); - metric.set("bar", 2, null); - advanceToNextSnapshot(); - JsonNode json = requestAsJson("http://localhost/state/v1/all"); - assertEquals(json.toString(), "up", json.get("status").get("code").asText()); - assertEquals(json.toString(), 1, json.get("metrics").get("values").size()); - assertEquals(json.toString(), 3, - json.get("metrics").get("values").get(0).get("values").get("average").asDouble(), 0.001); - } - - @Test - public void snapshotsPreserveLastGaugeValue() throws Exception { - metric.set("bar", 4, null); - advanceToNextSnapshot(); - advanceToNextSnapshot(); - JsonNode json = requestAsJson("http://localhost/state/v1/all"); - JsonNode metricValues = getFirstMetricValueNode(json); - assertEquals(json.toString(), 4, metricValues.get("last").asDouble(), 0.001); - // Use 'last' as avg/min/max when none has been set explicitly during snapshot period - assertEquals(json.toString(), 4, metricValues.get("average").asDouble(), 0.001); - assertEquals(json.toString(), 4, metricValues.get("min").asDouble(), 0.001); - assertEquals(json.toString(), 4, metricValues.get("max").asDouble(), 0.001); - // Count is tracked per period. - assertEquals(json.toString(), 0, metricValues.get("count").asInt()); - } - - private JsonNode getFirstMetricValueNode(JsonNode root) { - assertEquals(root.toString(), 1, root.get("metrics").get("values").size()); - JsonNode metricValues = root.get("metrics").get("values").get(0).get("values"); - assertTrue(root.toString(), metricValues.has("last")); - return metricValues; - } - - @Test - public void gaugeSnapshotsTracksCountMinMaxAvgPerPeriod() throws Exception { - metric.set("bar", 10000, null); // Ensure any cross-snapshot noise is visible - advanceToNextSnapshot(); - metric.set("bar", 20, null); - metric.set("bar", 40, null); - advanceToNextSnapshot(); - JsonNode json = requestAsJson("http://localhost/state/v1/all"); - JsonNode metricValues = getFirstMetricValueNode(json); - assertEquals(json.toString(), 40, metricValues.get("last").asDouble(), 0.001); - // Last snapshot had explicit values set - assertEquals(json.toString(), 30, metricValues.get("average").asDouble(), 0.001); - assertEquals(json.toString(), 20, metricValues.get("min").asDouble(), 0.001); - assertEquals(json.toString(), 40, metricValues.get("max").asDouble(), 0.001); - assertEquals(json.toString(), 2, metricValues.get("count").asInt()); + requestAsString(V1_URI + "all")); } @Test public void testHealthAggregation() throws Exception { - Map dimensions1 = new TreeMap<>(); - dimensions1.put("port", String.valueOf(Defaults.getDefaults().vespaWebServicePort())); - Metric.Context context1 = metric.createContext(dimensions1); - Map dimensions2 = new TreeMap<>(); - dimensions2.put("port", "80"); - Metric.Context context2 = metric.createContext(dimensions2); - - metric.add("serverNumSuccessfulResponses", 4, context1); - metric.add("serverNumSuccessfulResponses", 2, context2); - metric.set("serverTotalSuccessfulResponseLatency", 20, context1); - metric.set("serverTotalSuccessfulResponseLatency", 40, context2); - metric.add("random", 3, context1); - advanceToNextSnapshot(); - JsonNode json = requestAsJson("http://localhost/state/v1/health"); + var context1 = StateMetricContext.newInstance(Map.of("port", Defaults.getDefaults().vespaWebServicePort())); + var context2 = StateMetricContext.newInstance(Map.of("port", 80)); + var snapshot = new MetricSnapshot(); + snapshot.add(context1, "serverNumSuccessfulResponses", 4); + snapshot.add(context2, "serverNumSuccessfulResponses", 2); + snapshot.set(context1, "serverTotalSuccessfulResponseLatency", 20); + snapshot.set(context2, "serverTotalSuccessfulResponseLatency", 40); + snapshot.add(context1, "random", 3); + snapshotProvider.setSnapshot(snapshot); + + JsonNode json = requestAsJson(V1_URI + "health"); assertEquals(json.toString(), "up", json.get("status").get("code").asText()); assertEquals(json.toString(), 2, json.get("metrics").get("values").size()); assertEquals(json.toString(), "requestsPerSecond", @@ -308,7 +159,7 @@ public class StateHandlerTest extends StateHandlerTestBase { @Test public void testStateConfig() throws Exception { - JsonNode root = requestAsJson("http://localhost/state/v1/config"); + JsonNode root = requestAsJson(V1_URI + "config"); JsonNode config = root.get("config"); JsonNode container = config.get("container"); @@ -317,7 +168,7 @@ public class StateHandlerTest extends StateHandlerTestBase { @Test public void testStateVersion() throws Exception { - JsonNode root = requestAsJson("http://localhost/state/v1/version"); + JsonNode root = requestAsJson(V1_URI + "version"); JsonNode version = root.get("version"); assertEquals(Vtag.currentVersion.toString(), version.asText()); diff --git a/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTestBase.java b/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTestBase.java index 57a9ba4abdb..6f1eb5359d0 100644 --- a/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTestBase.java +++ b/container-core/src/test/java/com/yahoo/container/jdisc/state/StateHandlerTestBase.java @@ -3,103 +3,63 @@ package com.yahoo.container.jdisc.state; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.inject.AbstractModule; +import com.yahoo.component.ComponentId; +import com.yahoo.component.provider.ComponentRegistry; import com.yahoo.container.core.ApplicationMetadataConfig; +import com.yahoo.container.jdisc.RequestHandlerTestDriver; import com.yahoo.container.jdisc.config.HealthMonitorConfig; -import com.yahoo.jdisc.Metric; -import com.yahoo.jdisc.Response; import com.yahoo.jdisc.Timer; -import com.yahoo.jdisc.application.ContainerBuilder; -import com.yahoo.jdisc.application.MetricConsumer; -import com.yahoo.jdisc.handler.BufferedContentChannel; -import com.yahoo.jdisc.handler.ContentChannel; -import com.yahoo.jdisc.handler.ResponseHandler; -import com.yahoo.jdisc.test.TestDriver; import com.yahoo.metrics.MetricsPresentationConfig; -import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; /** - * @author Simon Thoresen Hult * @author gjoranv */ public class StateHandlerTestBase { final static long SNAPSHOT_INTERVAL = TimeUnit.SECONDS.toMillis(300); final static long META_GENERATION = 69; - static final String APPLICATION_NAME = "state-handler-test-base"; - TestDriver driver; - StateMonitor monitor; - Metric metric; + + static final String URI_BASE = "http://localhost"; + + static StateMonitor monitor; + static RequestHandlerTestDriver testDriver; + + static HealthMonitorConfig healthMonitorConfig; + static ApplicationMetadataConfig applicationMetadataConfig; + static MetricsPresentationConfig metricsPresentationConfig; + static MetricsPacketsHandlerConfig metricsPacketsHandlerConfig; + final AtomicLong currentTimeMillis = new AtomicLong(0); + Timer timer; - @Before - public void startTestDriver() { - Timer timer = this.currentTimeMillis::get; - this.driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(new AbstractModule() { - @Override - protected void configure() { - bind(Timer.class).toInstance(timer); - } - }); - ContainerBuilder builder = driver.newContainerBuilder(); - HealthMonitorConfig healthMonitorConfig = - new HealthMonitorConfig( - new HealthMonitorConfig.Builder() - .snapshot_interval(TimeUnit.MILLISECONDS.toSeconds(SNAPSHOT_INTERVAL)) - .initialStatus("up")); - this.monitor = new StateMonitor(healthMonitorConfig, timer, null); - builder.guiceModules().install(new AbstractModule() { + MockSnapshotProvider snapshotProvider; + ComponentRegistry snapshotProviderRegistry; - @Override - protected void configure() { - bind(StateMonitor.class).toInstance(monitor); - bind(MetricConsumer.class).toProvider(MetricConsumerProviders.wrap(monitor)); - bind(ApplicationMetadataConfig.class).toInstance(new ApplicationMetadataConfig( - new ApplicationMetadataConfig.Builder().generation(META_GENERATION))); - bind(MetricsPresentationConfig.class) - .toInstance(new MetricsPresentationConfig(new MetricsPresentationConfig.Builder())); - bind(MetricsPacketsHandlerConfig.class).toInstance(new MetricsPacketsHandlerConfig( - new MetricsPacketsHandlerConfig.Builder().application(APPLICATION_NAME))); - } - }); - builder.serverBindings().bind("http://*/*", builder.getInstance(StateHandler.class)); - builder.serverBindings().bind("http://*/metrics-packets", builder.getInstance(MetricsPacketsHandler.class)); - driver.activateContainer(builder); - metric = builder.getInstance(Metric.class); + @BeforeClass + public static void setupClass() { + metricsPresentationConfig = new MetricsPresentationConfig(new MetricsPresentationConfig.Builder()); + healthMonitorConfig = new HealthMonitorConfig(new HealthMonitorConfig.Builder() + .initialStatus("up")); + applicationMetadataConfig = new ApplicationMetadataConfig(new ApplicationMetadataConfig.Builder() + .generation(META_GENERATION)); } - @After - public void stopTestDriver() { - assertTrue(driver.close()); + @Before + public void setupSnapshotProvider() { + timer = currentTimeMillis::get; + snapshotProvider = new MockSnapshotProvider(); + snapshotProviderRegistry = new ComponentRegistry<>(); + snapshotProviderRegistry.register(new ComponentId("foo"), snapshotProvider); + monitor = new StateMonitor(healthMonitorConfig, timer, null); } - String requestAsString(String requestUri) throws Exception { - final BufferedContentChannel content = new BufferedContentChannel(); - Response response = driver.dispatchRequest(requestUri, new ResponseHandler() { - - @Override - public ContentChannel handleResponse(Response response) { - return content; - } - }).get(60, TimeUnit.SECONDS); - assertNotNull(response); - assertEquals(Response.Status.OK, response.getStatus()); - StringBuilder str = new StringBuilder(); - Reader in = new InputStreamReader(content.toStream(), StandardCharsets.UTF_8); - for (int c; (c = in.read()) != -1; ) { - str.append((char)c); - } - return str.toString(); + String requestAsString(String requestUri) { + return testDriver.sendRequest(requestUri).readAll(); } JsonNode requestAsJson(String requestUri) throws Exception { @@ -109,7 +69,6 @@ public class StateHandlerTestBase { void advanceToNextSnapshot() { currentTimeMillis.addAndGet(SNAPSHOT_INTERVAL); - monitor.updateSnapshot(); } } -- cgit v1.2.3