Enhanced usage of test method names in CQLTester for better test debugging

patch by Berenguer Blasi, Benjamin Lerer; reviewed by Benjamin Lerer for CASSANDRA-19050

Co-authored-by: Benjamin Lerer <b.lerer@gmail.com>
Co-authored-by: Berenguer Blasi <berenguerblasi@gmail.com>
This commit is contained in:
Bereng 2024-01-15 09:35:22 +01:00
parent f54177bb0a
commit 666f7df803
2 changed files with 45 additions and 7 deletions

View File

@ -39,6 +39,7 @@ import java.util.Iterator;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -65,11 +66,14 @@ import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -180,6 +184,11 @@ import static org.apache.cassandra.config.CassandraRelevantProperties.TEST_DRIVE
import static org.apache.cassandra.config.CassandraRelevantProperties.TEST_REUSE_PREPARED; import static org.apache.cassandra.config.CassandraRelevantProperties.TEST_REUSE_PREPARED;
import static org.apache.cassandra.config.CassandraRelevantProperties.TEST_ROW_CACHE_SIZE; import static org.apache.cassandra.config.CassandraRelevantProperties.TEST_ROW_CACHE_SIZE;
import static org.apache.cassandra.config.CassandraRelevantProperties.TEST_USE_PREPARED; import static org.apache.cassandra.config.CassandraRelevantProperties.TEST_USE_PREPARED;
import static org.apache.cassandra.cql3.SchemaElement.SchemaElementType.AGGREGATE;
import static org.apache.cassandra.cql3.SchemaElement.SchemaElementType.FUNCTION;
import static org.apache.cassandra.cql3.SchemaElement.SchemaElementType.MATERIALIZED_VIEW;
import static org.apache.cassandra.cql3.SchemaElement.SchemaElementType.TABLE;
import static org.apache.cassandra.cql3.SchemaElement.SchemaElementType.TYPE;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -197,6 +206,12 @@ public abstract class CQLTester
protected static final Logger logger = LoggerFactory.getLogger(CQLTester.class); protected static final Logger logger = LoggerFactory.getLogger(CQLTester.class);
// We make the test method name available and also use it when creating KS, table,...
@Rule
public final TestName testName = new TestName();
// Some tests use hardcoded constants so we may want to disable it
protected static volatile boolean decorateCQLWithTestNames = true;
public static final String KEYSPACE = "cql_test_keyspace"; public static final String KEYSPACE = "cql_test_keyspace";
public static final String KEYSPACE_PER_TEST = "cql_test_keyspace_alt"; public static final String KEYSPACE_PER_TEST = "cql_test_keyspace_alt";
protected static final boolean USE_PREPARED_VALUES = TEST_USE_PREPARED.getBoolean(); protected static final boolean USE_PREPARED_VALUES = TEST_USE_PREPARED.getBoolean();
@ -903,14 +918,14 @@ public abstract class CQLTester
protected String createTypeName() protected String createTypeName()
{ {
String typeName = String.format("type_%02d", seqNumber.getAndIncrement()); String typeName = createSchemaElementName(TYPE, null);
types.add(typeName); types.add(typeName);
return typeName; return typeName;
} }
protected String createFunctionName(String keyspace) protected String createFunctionName(String keyspace)
{ {
return String.format("%s.function_%02d", keyspace, seqNumber.getAndIncrement()); return createSchemaElementName(FUNCTION, keyspace);
} }
protected void registerFunction(String functionName, String argTypes) protected void registerFunction(String functionName, String argTypes)
@ -935,7 +950,7 @@ public abstract class CQLTester
protected String createAggregateName(String keyspace) protected String createAggregateName(String keyspace)
{ {
return String.format("%s.aggregate_%02d", keyspace, seqNumber.getAndIncrement()); return createSchemaElementName(AGGREGATE, keyspace);
} }
protected void registerAggregate(String aggregateName, String argTypes) protected void registerAggregate(String aggregateName, String argTypes)
@ -983,11 +998,27 @@ public abstract class CQLTester
protected String createKeyspaceName() protected String createKeyspaceName()
{ {
String currentKeyspace = String.format("keyspace_%02d", seqNumber.getAndIncrement()); String currentKeyspace = createSchemaElementName(SchemaElement.SchemaElementType.KEYSPACE, null);
keyspaces.add(currentKeyspace); keyspaces.add(currentKeyspace);
return currentKeyspace; return currentKeyspace;
} }
private String createSchemaElementName(SchemaElement.SchemaElementType type, String keyspace)
{
String prefix = keyspace == null ? "" : keyspace + '.';
String typeName = type == MATERIALIZED_VIEW ? "mv" : type.name().toLowerCase(Locale.US);
int sequence = seqNumber.getAndIncrement();
int usedSpaceSoFar = prefix.length() + typeName.length() + Math.max(2, numberOfDigits(sequence)) + 1;
String testMethodName = StringUtils.truncate(getTestMethodName(), SchemaConstants.NAME_LENGTH - usedSpaceSoFar);
return String.format("%s%s%s_%02d", prefix, typeName, testMethodName, sequence);
}
private int numberOfDigits(int i)
{
assert i >= 0;
return i == 0 ? 1 : (int) (Math.log10(i) + 1);
}
protected String createTable(String query) protected String createTable(String query)
{ {
return createTable(KEYSPACE, query); return createTable(KEYSPACE, query);
@ -1014,7 +1045,7 @@ public abstract class CQLTester
protected String createTableName(String tableName) protected String createTableName(String tableName)
{ {
String currentTable = tableName == null ? String.format("table_%02d", seqNumber.getAndIncrement()) : tableName; String currentTable = tableName == null ? createSchemaElementName(TABLE, null) : tableName;
tables.add(currentTable); tables.add(currentTable);
return currentTable; return currentTable;
} }
@ -1092,7 +1123,7 @@ public abstract class CQLTester
protected String createViewName() protected String createViewName()
{ {
String currentView = String.format("mv_%02d", seqNumber.getAndIncrement()); String currentView = createSchemaElementName(MATERIALIZED_VIEW, null);
views.add(currentView); views.add(currentView);
return currentView; return currentView;
} }
@ -2566,6 +2597,12 @@ public abstract class CQLTester
return metrics.get(metricName); return metrics.get(metricName);
} }
private String getTestMethodName()
{
return decorateCQLWithTestNames && testName.getMethodName() != null ? '_' + testName.getMethodName().toLowerCase().replaceAll("[^\\w]", "_")
: "";
}
public static class Vector<T> extends AbstractList<T> public static class Vector<T> extends AbstractList<T>
{ {
private final T[] values; private final T[] values;

View File

@ -60,7 +60,7 @@ import static com.google.common.collect.Lists.newArrayList;
* select * from table_00 where pk=42; * select * from table_00 where pk=42;
* } * }
*/ */
public class JMXCompatabilityTest extends CQLTester public class JMXCompatibilityTest extends CQLTester
{ {
private static final Map<String, String> ENV = ImmutableMap.of("JAVA_HOME", CassandraRelevantProperties.JAVA_HOME.getString()); private static final Map<String, String> ENV = ImmutableMap.of("JAVA_HOME", CassandraRelevantProperties.JAVA_HOME.getString());
@ -72,6 +72,7 @@ public class JMXCompatabilityTest extends CQLTester
@BeforeClass @BeforeClass
public static void setup() throws Exception public static void setup() throws Exception
{ {
decorateCQLWithTestNames = false;
DatabaseDescriptor.daemonInitialization(); DatabaseDescriptor.daemonInitialization();
DatabaseDescriptor.setColumnIndexSizeInKiB(0); // make sure the column index is created DatabaseDescriptor.setColumnIndexSizeInKiB(0); // make sure the column index is created