You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ch...@apache.org on 2018/02/20 15:46:52 UTC

[4/4] flink git commit: [hotfix][prometheus][tests] Add utility for generating port ranges

[hotfix][prometheus][tests] Add utility for generating port ranges


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/5103e572
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/5103e572
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/5103e572

Branch: refs/heads/master
Commit: 5103e57279426e783b671ae39258852925f0e6b7
Parents: 6f92435
Author: zentol <ch...@apache.org>
Authored: Tue Feb 13 10:33:31 2018 +0100
Committer: zentol <ch...@apache.org>
Committed: Tue Feb 20 16:46:19 2018 +0100

----------------------------------------------------------------------
 .../prometheus/PrometheusReporterTest.java      | 41 ++++++++++++++++++--
 1 file changed, 37 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/5103e572/flink-metrics/flink-metrics-prometheus/src/test/java/org/apache/flink/metrics/prometheus/PrometheusReporterTest.java
----------------------------------------------------------------------
diff --git a/flink-metrics/flink-metrics-prometheus/src/test/java/org/apache/flink/metrics/prometheus/PrometheusReporterTest.java b/flink-metrics/flink-metrics-prometheus/src/test/java/org/apache/flink/metrics/prometheus/PrometheusReporterTest.java
index a149900..6833a06 100644
--- a/flink-metrics/flink-metrics-prometheus/src/test/java/org/apache/flink/metrics/prometheus/PrometheusReporterTest.java
+++ b/flink-metrics/flink-metrics-prometheus/src/test/java/org/apache/flink/metrics/prometheus/PrometheusReporterTest.java
@@ -44,6 +44,8 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
 import java.util.Arrays;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
 
 import static org.apache.flink.metrics.prometheus.PrometheusReporter.ARG_PORT;
 import static org.hamcrest.Matchers.containsString;
@@ -65,6 +67,8 @@ public class PrometheusReporterTest extends TestLogger {
 	private static final String DEFAULT_LABELS = "{" + DIMENSIONS + ",}";
 	private static final String SCOPE_PREFIX = "flink_taskmanager_";
 
+	private static final PortRangeProvider portRangeProvider = new PortRangeProvider();
+
 	@Rule
 	public ExpectedException thrown = ExpectedException.none();
 
@@ -74,7 +78,7 @@ public class PrometheusReporterTest extends TestLogger {
 
 	@Before
 	public void setupReporter() {
-		registry = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test1", "9400-9500")));
+		registry = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test1", portRangeProvider.next())));
 		metricGroup = new FrontMetricGroup<>(0, new TaskManagerMetricGroup(registry, HOST_NAME, TASK_MANAGER));
 		reporter = (PrometheusReporter) registry.getReporters().get(0);
 	}
@@ -234,7 +238,7 @@ public class PrometheusReporterTest extends TestLogger {
 
 	@Test
 	public void cannotStartTwoReportersOnSamePort() {
-		final MetricRegistryImpl fixedPort1 = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test1", "9400-9500")));
+		final MetricRegistryImpl fixedPort1 = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test1", portRangeProvider.next())));
 		assertThat(fixedPort1.getReporters(), hasSize(1));
 
 		PrometheusReporter firstReporter = (PrometheusReporter) fixedPort1.getReporters().get(0);
@@ -248,8 +252,9 @@ public class PrometheusReporterTest extends TestLogger {
 
 	@Test
 	public void canStartTwoReportersWhenUsingPortRange() {
-		final MetricRegistryImpl portRange1 = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test1", "9200-9300")));
-		final MetricRegistryImpl portRange2 = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test2", "9200-9300")));
+		String portRange = portRangeProvider.next();
+		final MetricRegistryImpl portRange1 = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test1", portRange)));
+		final MetricRegistryImpl portRange2 = new MetricRegistryImpl(MetricRegistryConfiguration.fromConfiguration(createConfigWithOneReporter("test2", portRange)));
 
 		assertThat(portRange1.getReporters(), hasSize(1));
 		assertThat(portRange2.getReporters(), hasSize(1));
@@ -278,4 +283,32 @@ public class PrometheusReporterTest extends TestLogger {
 	public void closeReporterAndShutdownRegistry() {
 		registry.shutdown();
 	}
+
+	/**
+	 * Utility class providing distinct port ranges.
+	 */
+	private static class PortRangeProvider implements Iterator<String> {
+
+		private int base = 9000;
+
+		@Override
+		public boolean hasNext() {
+			return base < 14000; // arbitrary limit that should be sufficient for test purposes
+		}
+
+		/**
+		 * Returns the next port range containing exactly 100 ports.
+		 *
+		 * @return next port range
+		 */
+		public String next() {
+			if (!hasNext()) {
+				throw new NoSuchElementException();
+			}
+			int lowEnd = base;
+			int highEnd = base + 99;
+			base += 100;
+			return String.valueOf(lowEnd) + "-" + String.valueOf(highEnd);
+		}
+	}
 }