Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run PT with JUnit #132

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
java: ['21', '22']
java: ['23']
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
Expand Down
2 changes: 1 addition & 1 deletion .java-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
17
23
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.airlift</groupId>
<artifactId>airbase</artifactId>
<version>157</version>
<version>187</version>
</parent>

<groupId>io.trino.tempto</groupId>
Expand Down
23 changes: 17 additions & 6 deletions tempto-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
<artifactId>commons-io</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>log-manager</artifactId>
<version>275</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.trino.hive</groupId>
<artifactId>hive-apache</artifactId>
Expand Down Expand Up @@ -128,6 +140,11 @@
<artifactId>freemarker</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down Expand Up @@ -194,12 +211,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down
39 changes: 39 additions & 0 deletions tempto-core/src/main/java/io/trino/tempto/ContextAware.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.trino.tempto;

import io.trino.tempto.internal.initialization.TestInitializationListenerJUnit;
import io.trino.tempto.internal.listeners.ProgressLoggingListenerJUnit;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.parallel.Execution;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@ExtendWith({
TestInitializationListenerJUnit.class,
ProgressLoggingListenerJUnit.class
})
@TestInstance(PER_CLASS)
@Execution(CONCURRENT)
public @interface ContextAware {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.trino.tempto.internal.initialization;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import com.google.inject.Module;
import io.trino.tempto.TemptoPlugin;
import io.trino.tempto.configuration.Configuration;
import io.trino.tempto.context.TestContext;
import io.trino.tempto.fulfillment.table.TableManager;
import io.trino.tempto.fulfillment.table.TableManagerDispatcher;
import io.trino.tempto.initialization.SuiteModuleProvider;
import io.trino.tempto.internal.ReflectionHelper;
import io.trino.tempto.internal.context.GuiceTestContext;
import io.trino.tempto.internal.context.TestContextStack;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;

import java.util.List;
import java.util.ServiceLoader;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.inject.util.Modules.combine;
import static io.trino.tempto.context.ThreadLocalTestContextHolder.assertTestContextNotSet;
import static io.trino.tempto.context.ThreadLocalTestContextHolder.popAllTestContexts;
import static io.trino.tempto.context.ThreadLocalTestContextHolder.pushAllTestContexts;
import static io.trino.tempto.context.ThreadLocalTestContextHolder.testContextIfSet;
import static io.trino.tempto.internal.configuration.TestConfigurationFactory.testConfiguration;
import static org.slf4j.LoggerFactory.getLogger;

public class TestInitializationListenerJUnit
implements
BeforeAllCallback,
AfterAllCallback
{
private static final Logger LOGGER = getLogger(TestInitializationListenerJUnit.class);

private final List<? extends SuiteModuleProvider> suiteModuleProviders;

private final Configuration configuration;

public TestInitializationListenerJUnit()
{
this(ImmutableList.copyOf(ServiceLoader.load(TemptoPlugin.class).iterator()));
}

private TestInitializationListenerJUnit(List<TemptoPlugin> plugins)
{
this(
plugins.stream()
.flatMap(plugin -> plugin.getSuiteModules().stream())
.map(ReflectionHelper::instantiate)
.collect(toImmutableList()),
testConfiguration());
}

TestInitializationListenerJUnit(
List<? extends SuiteModuleProvider> suiteModuleProviders,
Configuration configuration)
{
this.suiteModuleProviders = suiteModuleProviders;
this.configuration = configuration;
}

@Override
public void beforeAll(ExtensionContext context)
{
displayConfigurationToUser();

Module suiteModule = combine(getSuiteModules());
GuiceTestContext initSuiteTestContext = new GuiceTestContext(suiteModule);
TestContextStack<TestContext> suiteTextContextStack = new TestContextStack<>();
suiteTextContextStack.push(initSuiteTestContext);
assertTestContextNotSet();
pushAllTestContexts(suiteTextContextStack);
TestContext topTestContext = suiteTextContextStack.peek();
topTestContext.injectMembers(context.getRequiredTestInstance());
}

private void displayConfigurationToUser()
{
LOGGER.info("Configuration:");
List<String> configurationKeys = Ordering.natural()
.sortedCopy(configuration.listKeys());
for (String key : configurationKeys) {
LOGGER.info(String.format("%s -> %s", key, configuration.getString(key).orElse("<NOT SET>")));
}
}

@Override
public void afterAll(ExtensionContext context)
{
if (testContextIfSet().isEmpty()) {
throw new IllegalStateException("Test context at this point may not be initialized only because of exception during initialization");
}

TestContextStack<TestContext> testContextStack = popAllTestContexts();
// we are going to close last context, so we need to close TableManager's first
testContextStack.peek().getOptionalDependency(TableManagerDispatcher.class)
.ifPresent(dispatcher -> dispatcher.getAllTableManagers().forEach(TableManager::close));

// remove close init test context too
testContextStack.peek().close();
}

private List<Module> getSuiteModules()
{
return suiteModuleProviders
.stream()
.map(provider -> provider.getModule(configuration))
.collect(toImmutableList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.trino.tempto.internal.listeners;

import io.airlift.log.Logging;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;

import static com.google.common.base.Preconditions.checkState;
import static java.lang.System.currentTimeMillis;

public class ProgressLoggingListenerJUnit
implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback, AfterAllCallback
{
private final static Logger LOGGER = LoggerFactory.getLogger(ProgressLoggingListenerJUnit.class);

static {
Logging.initialize();
}

private int started;
private int succeeded;
private int failed;
private long startTime;
private long testStartTime;

@Override
public void beforeAll(ExtensionContext context)
{
startTime = currentTimeMillis();
LOGGER.info("Starting tests");
}

@Override
public void beforeEach(ExtensionContext context)
{
testStartTime = currentTimeMillis();
started++;
LOGGER.info("[{}] Starting test: {}", started, formatTestName(context));
}

@Override
public void afterEach(ExtensionContext context)
{
long executionTime = currentTimeMillis() - testStartTime;
if (context.getExecutionException().isPresent()) {
failed++;
LOGGER.info("FAILURE: {} took {}", formatTestName(context), formatDuration(executionTime));
LOGGER.error("Failure cause:", context.getExecutionException().get());
}
else {
succeeded++;
LOGGER.info("SUCCESS: {} took {}", formatTestName(context), formatDuration(executionTime));
}
}

@Override
public void afterAll(ExtensionContext context)
{
checkState(succeeded + failed > 0, "No tests executed");
LOGGER.info("");
LOGGER.info("Completed {} tests", started);
LOGGER.info("{} SUCCEEDED / {} FAILED", succeeded, failed);
LOGGER.info("Tests execution took {}", formatDuration(currentTimeMillis() - startTime));
}

private String formatTestName(ExtensionContext context)
{
// TestMetadata testMetadata = testMetadataReader.readTestMetadata(testCase);
// String testGroups = Joiner.on(", ").join(testMetadata.testGroups);
// String testParameters = formatTestParameters(testMetadata.testParameters);
//
// return format("%s%s (Groups: %s)", testMetadata.testName, testParameters, testGroups);
return "%s#%s".formatted(context.getRequiredTestClass().getName(), context.getDisplayName());
}

private static String formatDuration(long durationInMillis)
{
BigDecimal durationSeconds = durationInSeconds(durationInMillis);
if (durationSeconds.longValue() > 60) {
long minutes = durationSeconds.longValue() / 60;
long restSeconds = durationSeconds.longValue() % 60;
return String.format("%d minutes and %d seconds", minutes, restSeconds);
}
else {
return String.format("%s seconds", durationSeconds);
}
}

private static BigDecimal durationInSeconds(long millis)
{
return new BigDecimal(millis).divide(new BigDecimal(1000), 1, RoundingMode.HALF_UP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@

package io.trino.tempto.query;

import com.google.inject.Inject;
import io.trino.tempto.context.TestContext;
import org.slf4j.Logger;

import com.google.inject.Inject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;

import static io.trino.tempto.query.QueryResult.forSingleIntegerValue;
import static io.trino.tempto.query.QueryResult.toSqlIndex;
Expand Down Expand Up @@ -87,8 +85,13 @@ public QueryResult executeQuery(String sql, QueryParam... params)
@Override
public Connection getConnection()
{
if (connection == null) {
openConnection();
try {
if (connection == null || connection.isClosed()) {
openConnection();
}
}
catch (SQLException e) {
throw new RuntimeException(e);
}
return connection;
}
Expand Down
10 changes: 10 additions & 0 deletions tempto-runner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Loading
Loading