knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kris...@apache.org
Subject [knox] branch master updated: KNOX-2122 - Use ThreadLocalRandom
Date Tue, 12 Nov 2019 23:06:27 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new 62d2bf9  KNOX-2122 - Use ThreadLocalRandom
62d2bf9 is described below

commit 62d2bf9adb8d0894f039ee6dbf2d55bbaa596a6a
Author: Kevin Risden <krisden@apache.org>
AuthorDate: Tue Nov 12 18:05:42 2019 -0500

    KNOX-2122 - Use ThreadLocalRandom
    
    Signed-off-by: Kevin Risden <krisden@apache.org>
---
 .../rewrite/impl/json/JsonFilterReaderTest.java    |  4 ++--
 .../org/apache/knox/gateway/AuditLoggingTest.java  |  3 ++-
 .../vault/TestHashicorpVaultAliasService.java      | 23 +++++++++++-----------
 .../knox/gateway/GatewayCorrelationIdTest.java     | 14 ++++++-------
 .../knox/gateway/util/PasswordUtilsTest.java       |  4 ++--
 5 files changed, 23 insertions(+), 25 deletions(-)

diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReaderTest.java
b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReaderTest.java
index 9f4eb14..c659da5 100644
--- a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReaderTest.java
+++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/json/JsonFilterReaderTest.java
@@ -35,7 +35,7 @@ import java.io.StringReader;
 import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.List;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -72,7 +72,7 @@ public class JsonFilterReaderTest {
 
   @Test
   public void testNumber() throws IOException {
-    int num = new Random().nextInt();
+    int num = ThreadLocalRandom.current().nextInt();
     String inputJson = String.valueOf(num);
     StringReader inputReader = new StringReader( inputJson );
     JsonFilterReader filterReader = new TestJsonFilterReader( inputReader, null );
diff --git a/gateway-server/src/test/java/org/apache/knox/gateway/AuditLoggingTest.java b/gateway-server/src/test/java/org/apache/knox/gateway/AuditLoggingTest.java
index 861a886..13bd3fd 100644
--- a/gateway-server/src/test/java/org/apache/knox/gateway/AuditLoggingTest.java
+++ b/gateway-server/src/test/java/org/apache/knox/gateway/AuditLoggingTest.java
@@ -58,6 +58,7 @@ import java.util.Set;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -120,7 +121,7 @@ public class AuditLoggingTest {
     FilterChain chain = EasyMock.createNiceMock( FilterChain.class );
     EasyMock.replay( chain );
 
-    Random rnd = new Random();
+    Random rnd = ThreadLocalRandom.current();
 
     // Make number of total requests between 1-100
     int numberTotalRequests = rnd.nextInt(99) + 1;
diff --git a/gateway-service-hashicorp-vault/src/test/java/org/apache/knox/gateway/backend/hashicorp/vault/TestHashicorpVaultAliasService.java
b/gateway-service-hashicorp-vault/src/test/java/org/apache/knox/gateway/backend/hashicorp/vault/TestHashicorpVaultAliasService.java
index 275ebcb..5604a35 100644
--- a/gateway-service-hashicorp-vault/src/test/java/org/apache/knox/gateway/backend/hashicorp/vault/TestHashicorpVaultAliasService.java
+++ b/gateway-service-hashicorp-vault/src/test/java/org/apache/knox/gateway/backend/hashicorp/vault/TestHashicorpVaultAliasService.java
@@ -43,7 +43,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import static org.apache.knox.gateway.backend.hashicorp.vault.HashicorpVaultAliasService.VAULT_SEPARATOR;
 import static org.junit.Assert.assertEquals;
@@ -56,7 +56,6 @@ import static org.junit.Assume.assumeNoException;
 public class TestHashicorpVaultAliasService {
   private static final Logger LOG = LoggerFactory.getLogger(TestHashicorpVaultAliasService.class);
 
-  private static final Random RANDOM = new Random();
   private static final String vaultVersion = "1.2.1";
   private static final String vaultImage = "vault:" + vaultVersion;
   private static final Integer vaultPort = 8200;
@@ -88,7 +87,7 @@ public class TestHashicorpVaultAliasService {
 
   @Before
   public void setUp() throws Exception {
-    vaultSecretsEngine = "knox-secret-" + RANDOM.nextInt(100);
+    vaultSecretsEngine = "knox-secret-" + ThreadLocalRandom.current().nextInt(100);
 
     setupVaultSecretsEngine();
   }
@@ -155,7 +154,7 @@ public class TestHashicorpVaultAliasService {
     remoteAliasServiceConfiguration.put(HashicorpVaultClientAuthenticationProvider.AUTHENTICATION_TYPE_KEY,
         TokenHashicorpVaultClientAuthenticationProvider.TYPE);
     remoteAliasServiceConfiguration.put(TokenHashicorpVaultClientAuthenticationProvider.TOKEN_KEY,
-        getKnoxToken(RANDOM.nextBoolean()));
+        getKnoxToken(ThreadLocalRandom.current().nextBoolean()));
 
     EasyMock.expect(gatewayConfig.getRemoteAliasServiceConfiguration())
         .andReturn(remoteAliasServiceConfiguration).anyTimes();
@@ -167,9 +166,9 @@ public class TestHashicorpVaultAliasService {
     aliasService.init(gatewayConfig, Collections.emptyMap());
     aliasService.start();
 
-    String clusterName = "test-" + RANDOM.nextInt(100);
-    String alias = "abc-" + RANDOM.nextInt(100);
-    String aliasPassword = "def-" + RANDOM.nextInt(100);
+    String clusterName = "test-" + ThreadLocalRandom.current().nextInt(100);
+    String alias = "abc-" + ThreadLocalRandom.current().nextInt(100);
+    String aliasPassword = "def-" + ThreadLocalRandom.current().nextInt(100);
 
     assertEquals(0, aliasService.getAliasesForCluster(clusterName).size());
 
@@ -215,9 +214,9 @@ public class TestHashicorpVaultAliasService {
     aliasService.init(gatewayConfig, Collections.emptyMap());
     aliasService.start();
 
-    String clusterName = "test-" + RANDOM.nextInt(100);
-    String alias = "abc-" + RANDOM.nextInt(100);
-    String aliasPassword = "def-" + RANDOM.nextInt(100);
+    String clusterName = "test-" + ThreadLocalRandom.current().nextInt(100);
+    String alias = "abc-" + ThreadLocalRandom.current().nextInt(100);
+    String aliasPassword = "def-" + ThreadLocalRandom.current().nextInt(100);
 
     try {
       aliasService.getAliasesForCluster(clusterName);
@@ -252,9 +251,9 @@ public class TestHashicorpVaultAliasService {
 
   private String generatePathPrefix() {
     StringBuilder pathPrefix = new StringBuilder();
-    int numParts = RANDOM.nextInt(10);
+    int numParts = ThreadLocalRandom.current().nextInt(10);
     for(int i = 0; i < numParts; i++) {
-      pathPrefix.append(VAULT_SEPARATOR).append(RANDOM.nextInt(10));
+      pathPrefix.append(VAULT_SEPARATOR).append(ThreadLocalRandom.current().nextInt(10));
     }
     pathPrefix.append(VAULT_SEPARATOR);
     String result = pathPrefix.toString();
diff --git a/gateway-test/src/test/java/org/apache/knox/gateway/GatewayCorrelationIdTest.java
b/gateway-test/src/test/java/org/apache/knox/gateway/GatewayCorrelationIdTest.java
index 8d6e386..45a9dde 100644
--- a/gateway-test/src/test/java/org/apache/knox/gateway/GatewayCorrelationIdTest.java
+++ b/gateway-test/src/test/java/org/apache/knox/gateway/GatewayCorrelationIdTest.java
@@ -25,7 +25,6 @@ import org.apache.knox.gateway.audit.log4j.correlation.Log4jCorrelationService;
 import org.apache.knox.gateway.config.GatewayConfig;
 import org.apache.knox.gateway.services.DefaultGatewayServices;
 import org.apache.knox.gateway.services.ServiceLifecycleException;
-import org.apache.knox.test.TestUtils;
 import org.apache.knox.test.log.CollectAppender;
 import org.apache.log4j.spi.LoggingEvent;
 import org.hamcrest.MatcherAssert;
@@ -42,12 +41,12 @@ import java.nio.file.Files;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
-import java.util.Random;
 import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
 
 import static io.restassured.RestAssured.given;
@@ -159,17 +158,15 @@ public class GatewayCorrelationIdTest {
         .gotoRoot();
   }
 
-  @Test( timeout = TestUtils.MEDIUM_TIMEOUT )
+  @Test
   public void testTestService() throws Exception {
     LOG_ENTER();
     String username = "guest";
     String password = "guest-password";
     String serviceUrl = clusterUrl + "/test-service-path/test-service-resource";
 
-    Random rnd = new Random();
-
     // Make number of total requests between 1-100
-    int numberTotalRequests = rnd.nextInt(99) + 1;
+    int numberTotalRequests = ThreadLocalRandom.current().nextInt(99) + 1;
     Set<Callable<Void>> callables = new HashSet<>(numberTotalRequests);
     for (int i = 0; i < numberTotalRequests; i++) {
       callables.add(() -> {
@@ -185,9 +182,10 @@ public class GatewayCorrelationIdTest {
     }
 
     // Make number of concurrent requests between 1-10
-    int numberConcurrentRequests = rnd.nextInt( 9) + 1;
+    int numberConcurrentRequests = ThreadLocalRandom.current().nextInt( 9) + 1;
 
-    LOG.info("Executing %d total requests with %d concurrently", numberTotalRequests, numberConcurrentRequests);
+    LOG.info("Executing {} total requests with {} concurrently",
+        numberTotalRequests, numberConcurrentRequests);
 
     ExecutorService executor = Executors.newFixedThreadPool(numberConcurrentRequests);
     executor.invokeAll(callables);
diff --git a/gateway-util-common/src/test/java/org/apache/knox/gateway/util/PasswordUtilsTest.java
b/gateway-util-common/src/test/java/org/apache/knox/gateway/util/PasswordUtilsTest.java
index c2d9882..b5bf8ba 100644
--- a/gateway-util-common/src/test/java/org/apache/knox/gateway/util/PasswordUtilsTest.java
+++ b/gateway-util-common/src/test/java/org/apache/knox/gateway/util/PasswordUtilsTest.java
@@ -19,14 +19,14 @@ package org.apache.knox.gateway.util;
 
 import org.junit.Test;
 
-import java.util.Random;
+import java.util.concurrent.ThreadLocalRandom;
 
 import static org.junit.Assert.assertEquals;
 
 public class PasswordUtilsTest {
   @Test
   public void testGeneratePassword() {
-    int len = new Random().nextInt(20);
+    int len = ThreadLocalRandom.current().nextInt(20);
     String randomPassword = PasswordUtils.generatePassword(len);
     assertEquals(len, randomPassword.length());
   }


Mime
View raw message