asereda-gs commented on a change in pull request #1798: [CALCITE-2442] Remove .toDelete cassandra temp folder on Windows after tests URL: https://github.com/apache/calcite/pull/1798#discussion_r378515748
########## File path: cassandra/src/test/java/org/apache/calcite/test/CassandraExtension.java ########## @@ -0,0 +1,218 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you 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 org.apache.calcite.test; + +import org.apache.calcite.config.CalciteSystemProperty; +import org.apache.calcite.util.Bug; +import org.apache.calcite.util.Sources; +import org.apache.calcite.util.TestUtil; + +import org.apache.cassandra.concurrent.StageManager; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.WindowsFailedSnapshotTracker; +import org.apache.cassandra.service.CassandraDaemon; +import org.apache.cassandra.service.StorageService; +import org.apache.cassandra.utils.FBUtilities; +import org.apache.thrift.transport.TTransportException; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Session; +import com.google.common.collect.ImmutableMap; + +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; +import org.junit.jupiter.api.extension.ConditionEvaluationResult; +import org.junit.jupiter.api.extension.ExecutionCondition; +import org.junit.jupiter.api.extension.ExtensionConfigurationException; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.lang.reflect.Field; +import java.time.Duration; +import java.util.Locale; +import java.util.concurrent.ExecutionException; + +/** + * JUnit5 extension to start and stop embedded cassandra server. + * + * <p>Note that tests will be skipped if running on JDK11+ + * (which is not yet supported by cassandra) see + * <a href="https://issues.apache.org/jira/browse/CASSANDRA-9608">CASSANDRA-9608</a>. + */ +class CassandraExtension implements ParameterResolver, ExecutionCondition { + + private static final ExtensionContext.Namespace NAMESPACE = + ExtensionContext.Namespace.create(CassandraExtension.class); + + private static final String KEY = "cassandra"; + + @Override public boolean supportsParameter(final ParameterContext parameterContext, + final ExtensionContext extensionContext) throws ParameterResolutionException { + final Class<?> type = parameterContext.getParameter().getType(); + return Session.class.isAssignableFrom(type) || Cluster.class.isAssignableFrom(type); + } + + @Override public Object resolveParameter(final ParameterContext parameterContext, + final ExtensionContext extensionContext) throws ParameterResolutionException { + + Class<?> type = parameterContext.getParameter().getType(); + if (Session.class.isAssignableFrom(type)) { + return getOrCreate(extensionContext).session; + } else if (Cluster.class.isAssignableFrom(type)) { + return getOrCreate(extensionContext).cluster; + } + + throw new ExtensionConfigurationException( + String.format(Locale.ROOT, "%s supports only %s or %s but yours was %s", + CassandraExtension.class.getSimpleName(), + Session.class.getName(), Cluster.class.getName(), type.getName())); + } + + static ImmutableMap<String, String> getDataset(String resourcePath) { + return ImmutableMap.of("model", + Sources.of(CassandraExtension.class.getResource(resourcePath)) + .file().getAbsolutePath()); + } + + /** + * Register cassandra resource in root context so it can be shared with other tests + */ + private static CassandraResource getOrCreate(ExtensionContext context) { + // same cassandra instance should be shared across all extension instances + return context.getRoot() + .getStore(NAMESPACE) + .getOrComputeIfAbsent(KEY, key -> new CassandraResource(), CassandraResource.class); + } + + /** + * Whether to run this test. + * <p>Enabled by default, unless explicitly disabled + * from command line ({@code -Dcalcite.test.cassandra=false}) or running on incompatible JDK + * version (see below). + * + * <p>As of this wiring Cassandra 4.x is not yet released and we're using 3.x + * (which fails on JDK11+). All cassandra tests will be skipped if + * running on JDK11+. + * + * @see <a href="https://issues.apache.org/jira/browse/CASSANDRA-9608">CASSANDRA-9608</a> + * @return {@code true} if test is compatible with current environment, + * {@code false} otherwise + */ + @Override public ConditionEvaluationResult evaluateExecutionCondition( + final ExtensionContext context) { + boolean enabled = CalciteSystemProperty.TEST_CASSANDRA.value(); + Bug.upgrade("remove JDK version check once current adapter supports Cassandra 4.x"); + boolean compatibleJdk = TestUtil.getJavaMajorVersion() < 11; + if (enabled && compatibleJdk) { + return ConditionEvaluationResult.enabled("Cassandra enabled"); Review comment: Cassandra support for JDK11 was introduced in 4.x (see [CASSANDRA-9608](https://issues.apache.org/jira/browse/CASSANDRA-9608)) we still use 3.6 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
