Skip to content

Commit

Permalink
Eliminate duplicate random suffix generation code
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Nov 14, 2022
1 parent 74d239a commit cf0bae8
Show file tree
Hide file tree
Showing 51 changed files with 372 additions and 382 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -61,9 +60,7 @@
import static io.trino.spi.testing.InterfaceTestUtils.assertAllMethodsOverridden;
import static io.trino.spi.type.IntegerType.INTEGER;
import static io.trino.testing.TestingConnectorSession.builder;
import static java.lang.Character.MAX_RADIX;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
Expand All @@ -78,10 +75,6 @@
@Test(singleThreaded = true)
public class TestCachingJdbcClient
{
private static final SecureRandom random = new SecureRandom();
// The suffix needs to be long enough to "prevent" collisions in practice. The length of 5 was proven not to be long enough
private static final int RANDOM_SUFFIX_LENGTH = 10;

private static final Duration FOREVER = Duration.succinctDuration(1, DAYS);
private static final Duration ZERO = Duration.succinctDuration(0, MILLISECONDS);

Expand Down Expand Up @@ -758,7 +751,7 @@ public void testConcurrentSchemaCreateAndDrop()
List<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < 5; i++) {
futures.add(executor.submit(() -> {
String schemaName = "schema_" + randomSuffix();
String schemaName = "schema_" + randomNameSuffix();
assertThat(cachingJdbcClient.getSchemaNames(session)).doesNotContain(schemaName);
cachingJdbcClient.createSchema(session, schemaName);
assertThat(cachingJdbcClient.getSchemaNames(session)).contains(schemaName);
Expand Down Expand Up @@ -895,12 +888,6 @@ public void testEverythingImplemented()
assertAllMethodsOverridden(JdbcClient.class, CachingJdbcClient.class);
}

private static String randomSuffix()
{
String randomSuffix = Long.toString(abs(random.nextLong()), MAX_RADIX);
return randomSuffix.substring(0, min(RANDOM_SUFFIX_LENGTH, randomSuffix.length()));
}

private static SingleJdbcCacheStatsAssertions assertTableNamesCache(CachingJdbcClient client)
{
return assertCacheStats(client, CachingJdbcCache.TABLE_NAMES_CACHE);
Expand Down
6 changes: 6 additions & 0 deletions plugin/trino-bigquery/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-tpch</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions plugin/trino-cassandra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-tpch</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,18 @@
import com.google.common.collect.ImmutableList;
import io.trino.testing.sql.SqlExecutor;

import java.security.SecureRandom;
import java.util.List;
import java.util.function.Function;

import static com.google.common.base.Verify.verify;
import static java.lang.Character.MAX_RADIX;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static java.lang.String.format;
import static java.lang.String.join;
import static java.util.stream.Collectors.joining;

public class TestCassandraTable
implements AutoCloseable
{
private static final SecureRandom random = new SecureRandom();
// The suffix needs to be long enough to "prevent" collisions in practice. The length of 5 was proven not to be long enough
private static final int RANDOM_SUFFIX_LENGTH = 10;

private final SqlExecutor sqlExecutor;
private final String keyspace;
private final String tableName;
Expand All @@ -57,7 +50,7 @@ public TestCassandraTable(
{
this.sqlExecutor = sqlExecutor;
this.keyspace = keyspace;
this.tableName = namePrefix + randomTableSuffix();
this.tableName = namePrefix + randomNameSuffix();
sqlExecutor.execute(format("CREATE TABLE %s.%s %s", keyspace, tableName, tableDefinition(columnDefinitions)));
String columns = columnDefinitions.stream()
.map(columnDefinition -> columnDefinition.name)
Expand Down Expand Up @@ -116,12 +109,6 @@ public void close()
sqlExecutor.execute("DROP TABLE " + getTableName());
}

private static String randomTableSuffix()
{
String randomSuffix = Long.toString(abs(random.nextLong()), MAX_RADIX);
return randomSuffix.substring(0, min(RANDOM_SUFFIX_LENGTH, randomSuffix.length()));
}

public static ColumnDefinition partitionColumn(String name, String type)
{
return new ColumnDefinition(name, type, PrimaryKeyType.PARTITION);
Expand Down
6 changes: 6 additions & 0 deletions plugin/trino-clickhouse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-tpch</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions plugin/trino-druid/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-tpch</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions plugin/trino-elasticsearch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,19 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<!-- conflicts with elasticsearch client dependency -->
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-tpch</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions plugin/trino-hive-hadoop2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>testing</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions plugin/trino-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-tpch</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions plugin/trino-oracle/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-tpch</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,15 @@
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static java.lang.Character.MAX_RADIX;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

final class LdapUtil
{
private static final SecureRandom random = new SecureRandom();
private static final int RANDOM_SUFFIX_LENGTH = 10;

public static final String MEMBER = "member";

private LdapUtil()
Expand Down Expand Up @@ -120,10 +113,4 @@ public static LdapObjectDefinition buildLdapUserObject(String organizationName,
.setObjectClasses(Arrays.asList("person", "inetOrgPerson"))
.build();
}

public static String randomSuffix()
{
String randomSuffix = Long.toString(abs(random.nextLong()), MAX_RADIX);
return randomSuffix.substring(0, min(RANDOM_SUFFIX_LENGTH, randomSuffix.length()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import static io.trino.plugin.password.ldap.LdapUtil.buildLdapGroupObject;
import static io.trino.plugin.password.ldap.LdapUtil.buildLdapOrganizationObject;
import static io.trino.plugin.password.ldap.LdapUtil.buildLdapUserObject;
import static io.trino.plugin.password.ldap.LdapUtil.randomSuffix;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -82,13 +82,13 @@ public String getLdapUrl()
public DisposableSubContext createOrganization()
throws NamingException
{
return createDisposableSubContext(buildLdapOrganizationObject("organization_" + randomSuffix(), BASE_DISTINGUISED_NAME));
return createDisposableSubContext(buildLdapOrganizationObject("organization_" + randomNameSuffix(), BASE_DISTINGUISED_NAME));
}

public DisposableSubContext createGroup(DisposableSubContext organization)
throws Exception
{
return createDisposableSubContext(buildLdapGroupObject(organization.getDistinguishedName(), "group_" + randomSuffix()));
return createDisposableSubContext(buildLdapGroupObject(organization.getDistinguishedName(), "group_" + randomNameSuffix()));
}

public DisposableSubContext createUser(DisposableSubContext organization, String userName, String password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@
import io.trino.tests.product.launcher.docker.DockerFiles.ResourceProvider;

import java.nio.file.Path;
import java.security.SecureRandom;

import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.Character.MAX_RADIX;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static org.testcontainers.utility.MountableFile.forHostPath;

public final class EnvironmentContainers
{
private static final SecureRandom random = new SecureRandom();
private static final int RANDOM_SUFFIX_LENGTH = 5;

public static final String TRINO = "presto";
public static final String COORDINATOR = TRINO + "-master";
public static final String WORKER = TRINO + "-worker";
Expand Down Expand Up @@ -56,7 +50,7 @@ public static void configureTempto(Environment.Builder builder, ResourceProvider
{
builder.configureContainer(TESTS, dockerContainer -> {
Path path = configDir.getPath("tempto-configuration.yaml");
String suffix = getParentDirectoryName(path) + "-" + randomSuffix();
String suffix = getParentDirectoryName(path) + "-" + randomNameSuffix();
String temptoConfig = "/docker/presto-product-tests/conf/tempto/tempto-configuration-for-" + suffix + ".yaml";
dockerContainer
.withCopyFileToContainer(forHostPath(path), temptoConfig)
Expand All @@ -72,10 +66,4 @@ private static String getParentDirectoryName(Path path)
checkArgument(path.getNameCount() >= 2, "Cannot determine parent directory of: %s", path);
return path.getName(path.getNameCount() - 2).toString();
}

private static String randomSuffix()
{
String randomSuffix = Long.toString(abs(random.nextLong()), MAX_RADIX);
return randomSuffix.substring(0, min(RANDOM_SUFFIX_LENGTH, randomSuffix.length()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import static io.trino.tempto.assertions.QueryAssert.assertQueryFailure;
import static io.trino.tempto.assertions.QueryAssert.assertThat;
import static io.trino.testing.TestingNames.randomNameSuffix;
import static io.trino.tests.product.TestGroups.CREATE_TABLE;
import static io.trino.tests.product.hive.util.TemporaryHiveTable.randomTableSuffix;
import static io.trino.tests.product.utils.QueryExecutors.onTrino;
import static java.lang.String.format;

Expand Down Expand Up @@ -53,8 +53,8 @@ public void shouldCreateTableAsEmptySelect()
@Test(groups = CREATE_TABLE)
public void shouldNotCreateTableInNonExistentSchema()
{
String schemaName = "test_schema_" + randomTableSuffix();
String table = schemaName + ".test_create_no_schema_" + randomTableSuffix();
String schemaName = "test_schema_" + randomNameSuffix();
String table = schemaName + ".test_create_no_schema_" + randomNameSuffix();
assertQueryFailure(() -> onTrino().executeQuery("CREATE TABLE " + table + " (a bigint)"))
.hasMessageMatching("\\QQuery failed (#\\E\\S+\\Q): Schema " + schemaName + " not found");

Expand All @@ -66,8 +66,8 @@ public void shouldNotCreateTableInNonExistentSchema()
@Test(groups = CREATE_TABLE)
public void shouldNotCreateExternalTableInNonExistentSchema()
{
String schemaName = "test_schema_" + randomTableSuffix();
String table = schemaName + ".test_create_no_schema_" + randomTableSuffix();
String schemaName = "test_schema_" + randomNameSuffix();
String table = schemaName + ".test_create_no_schema_" + randomNameSuffix();
assertQueryFailure(() -> onTrino().executeQuery("CREATE TABLE " + table + " (a bigint) WITH (external_location = '/tmp')"))
.hasMessageMatching("\\QQuery failed (#\\E\\S+\\Q): Schema " + schemaName + " not found");

Expand Down
Loading

0 comments on commit cf0bae8

Please sign in to comment.