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

Support auto detecting the misuses of TestOnly annotation #14030

Open
wants to merge 2 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
6 changes: 6 additions & 0 deletions iotdb-core/datanode/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3133,7 +3133,6 @@ public int getCachedMNodeSizeInPBTreeMode() {
return cachedMNodeSizeInPBTreeMode;
}

@TestOnly
public void setCachedMNodeSizeInPBTreeMode(int cachedMNodeSizeInPBTreeMode) {
this.cachedMNodeSizeInPBTreeMode = cachedMNodeSizeInPBTreeMode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.iotdb.commons.exception.IllegalPathException;
import org.apache.iotdb.commons.path.PartialPath;
import org.apache.iotdb.commons.schema.table.column.TsTableColumnCategory;
import org.apache.iotdb.commons.utils.TestOnly;
import org.apache.iotdb.consensus.ConsensusFactory;
import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
Expand Down Expand Up @@ -294,7 +293,6 @@ public TRegionReplicaSet getRegionReplicaSet() {
public abstract long getMinTime();

// region partial insert
@TestOnly
public void markFailedMeasurement(int index) {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.iotdb.commons.exception.MetadataException;
import org.apache.iotdb.commons.path.PartialPath;
import org.apache.iotdb.commons.utils.FileUtils;
import org.apache.iotdb.commons.utils.TestOnly;
import org.apache.iotdb.consensus.ConsensusFactory;
import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
Expand Down Expand Up @@ -444,7 +443,6 @@ public void updateAndFillSchemaCountMap(TDataNodeHeartbeatReq req, TDataNodeHear
}
}

@TestOnly
public ISchemaEngineStatistics getSchemaEngineStatistics() {
return schemaEngineStatistics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.iotdb.commons.path.MeasurementPath;
import org.apache.iotdb.commons.path.PartialPath;
import org.apache.iotdb.commons.path.PathPatternTree;
import org.apache.iotdb.commons.utils.TestOnly;
import org.apache.iotdb.db.exception.metadata.SchemaQuotaExceededException;
import org.apache.iotdb.db.queryengine.common.schematree.ClusterSchemaTree;
import org.apache.iotdb.db.queryengine.plan.relational.planner.node.schema.ConstructTableDevicesBlackListNode;
Expand Down Expand Up @@ -94,7 +93,6 @@ public interface ISchemaRegion {

void forceMlog();

@TestOnly
ISchemaRegionStatistics getSchemaRegionStatistics();

ISchemaRegionMetric getSchemaRegionMetric();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,6 @@ public ByteBuffer getRecord(String key) {
return null;
}

@TestOnly
public List<Pair<String, Short>> getKeyOffsetList() {
short[] offsets = getOffsets();
List<Pair<String, Short>> res = new ArrayList<>();
Expand All @@ -917,7 +916,6 @@ public List<Pair<String, Short>> getKeyOffsetList() {
return res;
}

@TestOnly
public short[] getOffsets() {
ByteBuffer buf = this.buffer.asReadOnlyBuffer();
short[] ofs = new short[recordNum];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ public void putTemplate(Template template) {
templateNameMap.put(template.getName(), template.getId());
}

@TestOnly
public void clear() {
templateIdMap.clear();
templateNameMap.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3940,7 +3940,6 @@ public ILastFlushTimeMap getLastFlushTimeMap() {
return lastFlushTimeMap;
}

@TestOnly
public TsFileManager getTsFileManager() {
return tsFileManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,6 @@ public boolean isEmpty() {
&& (workMemTable == null || workMemTable.getTotalPointsNum() == 0);
}

@TestOnly
public IMemTable getWorkMemTable() {
return workMemTable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.iotdb.commons.exception.StartupException;
import org.apache.iotdb.commons.service.IService;
import org.apache.iotdb.commons.service.ServiceType;
import org.apache.iotdb.commons.utils.TestOnly;
import org.apache.iotdb.consensus.ConsensusFactory;
import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
Expand Down Expand Up @@ -309,7 +308,6 @@ public void syncDeleteOutdatedFilesInWALNodes() {
}
}

@TestOnly
public void clear() {
totalDiskUsage.set(0);
walNodesManager.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

package org.apache.iotdb.db.storageengine.dataregion.wal.allocation;

import org.apache.iotdb.commons.utils.TestOnly;
import org.apache.iotdb.db.storageengine.dataregion.wal.node.IWALNode;
import org.apache.iotdb.db.storageengine.dataregion.wal.node.WALNode;

Expand All @@ -37,6 +36,5 @@ public interface NodeAllocationStrategy {
/** Get all wal nodes num. Not thread-safe, used for metrics only. */
int getNodesNum();

@TestOnly
void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.iotdb.db.tools.utils;

import org.apache.iotdb.commons.utils.TestOnly;
import org.apache.iotdb.db.storageengine.dataregion.tsfile.TsFileResource;

import org.apache.tsfile.common.conf.TSFileDescriptor;
Expand Down Expand Up @@ -352,7 +351,6 @@ public int getBadFileNum() {
return badFileNum;
}

@TestOnly
public void setBadFileNum(int badFileNum) {
this.badFileNum = badFileNum;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.iotdb.db.utils;

import org.apache.iotdb.commons.utils.TestOnly;

import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.core.importer.ImportOption.DoNotIncludeTests;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods;

public class AnnotationTest {

@Test
public void checkTestOnly() {
JavaClasses productionClasses =
new ClassFileImporter()
.withImportOption(new DoNotIncludeTests())
.importPackages("org.apache.iotdb");
JavaClasses testClasses =
new ClassFileImporter()
.withImportOption(new ImportOption.OnlyIncludeTests())
.importPackages("org.apache.iotdb");

List<Class> testReflectedClasses = new ArrayList<>();
for (JavaClass testClass : testClasses) {
testReflectedClasses.add(testClass.reflect());
}

ArchRule rule =
methods()
.that()
.areAnnotatedWith(TestOnly.class)
.should()
.onlyBeCalled()
.byClassesThat()
.belongToAnyOf(testReflectedClasses.toArray(new Class[0]))
.orShould()
.onlyBeCalled()
.byMethodsThat(
CanBeAnnotated.Predicates.annotatedWith(TestOnly.class)); // see next section

rule.check(productionClasses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
* functionality is not guaranteed and may interfere with the normal code.
*/
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.CLASS)
public @interface TestOnly {}
Loading