jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject [jmeter] 02/02: Extract common code into helper methods
Date Mon, 09 Sep 2019 19:10:59 GMT
This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git

commit 7148f87e686f5c1276c70cbfe15445e0feef79f7
Author: Felix Schumacher <felix.schumacher@internetallee.de>
AuthorDate: Mon Sep 9 21:09:11 2019 +0200

    Extract common code into helper methods
---
 .../backend/influxdb/HttpMetricsSenderTest.java    | 82 +++++++++-------------
 1 file changed, 35 insertions(+), 47 deletions(-)

diff --git a/src/components/src/test/java/org/apache/jmeter/visualizers/backend/influxdb/HttpMetricsSenderTest.java
b/src/components/src/test/java/org/apache/jmeter/visualizers/backend/influxdb/HttpMetricsSenderTest.java
index e20009d..9737df0 100644
--- a/src/components/src/test/java/org/apache/jmeter/visualizers/backend/influxdb/HttpMetricsSenderTest.java
+++ b/src/components/src/test/java/org/apache/jmeter/visualizers/backend/influxdb/HttpMetricsSenderTest.java
@@ -23,6 +23,8 @@ import static org.junit.Assert.assertNull;
 
 import java.io.IOException;
 import java.util.Objects;
+import java.util.concurrent.BlockingDeque;
+import java.util.concurrent.LinkedBlockingDeque;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.IntStream;
 
@@ -38,13 +40,13 @@ import org.junit.Test;
 public class HttpMetricsSenderTest {
 
     private HttpServer server;
-    private HttpRequest request;
+    private BlockingDeque<HttpRequest> resultQueue = new LinkedBlockingDeque<>();
 
     @Before
     public void startServer() {
 
         HttpRequestHandler requestHandler = (request, response, context) -> {
-            HttpMetricsSenderTest.this.request = request;
+            HttpMetricsSenderTest.this.resultQueue.add(request);
             response.setStatusCode(HttpStatus.SC_NO_CONTENT);
         };
 
@@ -75,70 +77,56 @@ public class HttpMetricsSenderTest {
 
     @Test
     public void checkTokenDoesNotPresentInHeader() throws Exception {
-        String influxdbUrl = String.format("http://localhost:%s/api/v2/write", server.getLocalPort());
-        HttpMetricsSender metricsSender = new HttpMetricsSender();
-        metricsSender.setup(influxdbUrl, null);
-        metricsSender.addMetric("measurement", "location=west", "size=10");
-        metricsSender.writeAndSendMetrics();
+        String influxdbUrl = getInfluxDbUrl();
+        setupSenderAndSendMetric(influxdbUrl, null);
 
-        do {
-            Thread.sleep(100);
-        } while (request == null);
-
-        assertNull(
-                "The authorization header shouldn't be defined.",
-                request.getFirstHeader("Authorization"));
+        assertNoAuthHeader(resultQueue.take());
     }
 
+
     @Test
     public void checkEmptyTokenDoesNotPresentInHeader() throws Exception {
-        String influxdbUrl = String.format("http://localhost:%s/api/v2/write", server.getLocalPort());
-        HttpMetricsSender metricsSender = new HttpMetricsSender();
-        metricsSender.setup(influxdbUrl, "");
-        metricsSender.addMetric("measurement", "location=west", "size=10");
-        metricsSender.writeAndSendMetrics();
-
-        do {
-            Thread.sleep(100);
-        } while (request == null);
+        String influxdbUrl = getInfluxDbUrl();
+        setupSenderAndSendMetric(influxdbUrl, "");
 
-        assertNull(
-                "The authorization header shouldn't be defined.",
-                request.getFirstHeader("Authorization"));
+        assertNoAuthHeader(resultQueue.take());
     }
 
     @Test
     public void checkEmptyOnlyWhitespaceTokenDoesNotPresentInHeader() throws Exception {
-        String influxdbUrl = String.format("http://localhost:%s/api/v2/write", server.getLocalPort());
-        HttpMetricsSender metricsSender = new HttpMetricsSender();
-        metricsSender.setup(influxdbUrl, "  ");
-        metricsSender.addMetric("measurement", "location=west", "size=10");
-        metricsSender.writeAndSendMetrics();
+        String influxdbUrl = getInfluxDbUrl();
+        setupSenderAndSendMetric(influxdbUrl, "  ");
 
-        do {
-            Thread.sleep(100);
-        } while (request == null);
-
-        assertNull(
-                "The authorization header shouldn't be defined.",
-                request.getFirstHeader("Authorization"));
+        assertNoAuthHeader(resultQueue.take());
     }
 
     @Test
     public void checkTokenPresentInHeader() throws Exception {
-        String influxdbUrl = String.format("http://localhost:%s/api/v2/write", server.getLocalPort());
-        HttpMetricsSender metricsSender = new HttpMetricsSender();
-        metricsSender.setup(influxdbUrl, "my-token");
-        metricsSender.addMetric("measurement", "location=west", "size=10");
-        metricsSender.writeAndSendMetrics();
-
-        do {
-            Thread.sleep(100);
-        } while (request == null);
+        String influxdbUrl = getInfluxDbUrl();
+        setupSenderAndSendMetric(influxdbUrl, "my-token");
 
+        HttpRequest request = resultQueue.take();
         assertEquals(
                 "The authorization header should be: 'Token my-token'",
                 "Token my-token",
                 request.getFirstHeader("Authorization").getValue());
     }
+
+    private void setupSenderAndSendMetric(String influxdbUrl, String influxDBToken) throws
Exception {
+        HttpMetricsSender metricsSender = new HttpMetricsSender();
+        metricsSender.setup(influxdbUrl, influxDBToken);
+        metricsSender.addMetric("measurement", "location=west", "size=10");
+        metricsSender.writeAndSendMetrics();
+    }
+
+    private void assertNoAuthHeader(HttpRequest request) {
+        assertNull(
+                "The authorization header shouldn't be defined.",
+                request.getFirstHeader("Authorization"));
+    }
+
+    private String getInfluxDbUrl() {
+        return String.format("http://localhost:%s/api/v2/write", Integer.valueOf(server.getLocalPort()));
+    }
+
 }


Mime
View raw message