Skip to content

Commit

Permalink
GG-20495 Rework GridCommandHandlerTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkalenko committed Aug 20, 2019
1 parent 5b12ba8 commit 932b66b
Show file tree
Hide file tree
Showing 14 changed files with 2,655 additions and 2,281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class CommandHandler {
public static final int EXIT_CODE_UNEXPECTED_ERROR = 4;

/** */
public static final int EXIT_CODE_ILLEGAL_SATE_ERROR = 5;
public static final int EXIT_CODE_ILLEGAL_STATE_ERROR = 5;

/** */
private static final long DFLT_PING_INTERVAL = 5000L;
Expand Down Expand Up @@ -320,9 +320,9 @@ public int execute(List<String> rawArgs) {
VisorIllegalStateException vise = X.cause(e, VisorIllegalStateException.class);

logger.severe(CommandLogger.errorMessage(vise));
logger.info("Command [" + commandName + "] finished with code: " + EXIT_CODE_ILLEGAL_SATE_ERROR);
logger.info("Command [" + commandName + "] finished with code: " + EXIT_CODE_ILLEGAL_STATE_ERROR);

return EXIT_CODE_ILLEGAL_SATE_ERROR;
return EXIT_CODE_ILLEGAL_STATE_ERROR;
}

logger.severe(CommandLogger.errorMessage(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import org.apache.ignite.internal.visor.VisorDataTransferObject;
import org.jetbrains.annotations.NotNull;

import static java.util.Objects.nonNull;
import static org.apache.ignite.internal.visor.verify.CacheFilterEnum.DEFAULT;

/**
* Arguments for task {@link VisorIdleVerifyTask}. <br/>
*/
Expand All @@ -45,7 +48,7 @@ public class VisorIdleVerifyTaskArg extends VisorDataTransferObject {
private boolean skipZeros;

/** Cache kind. */
private CacheFilterEnum cacheFilterEnum;
private CacheFilterEnum cacheFilterEnum = DEFAULT;

/**
* Default constructor.
Expand All @@ -58,7 +61,7 @@ public VisorIdleVerifyTaskArg() {
* @param caches Caches.
* @param excludeCaches Exclude caches or group.
* @param skipZeros Skip zeros partitions.
* @param cacheFilterEnum Cache kind.
* @param cacheFilterEnum Cache kind, require non null.
* @param checkCrc Check CRC sum on stored pages on disk.
*/
public VisorIdleVerifyTaskArg(
Expand All @@ -68,28 +71,28 @@ public VisorIdleVerifyTaskArg(
CacheFilterEnum cacheFilterEnum,
boolean checkCrc
) {
assert nonNull(cacheFilterEnum) : "Cache filter can't be null";

this.caches = caches;
this.excludeCaches = excludeCaches;
this.skipZeros = skipZeros;
this.checkCrc = checkCrc;

cacheFilterEnum(cacheFilterEnum);
this.cacheFilterEnum = cacheFilterEnum;
}

/**
* @param caches Caches.
* @param checkCrc Check CRC sum on stored pages on disk.
*/
public VisorIdleVerifyTaskArg(Set<String> caches, boolean checkCrc) {
this.caches = caches;
this.checkCrc = checkCrc;
this(caches, null, false, DEFAULT, false);
}

/**
* @param caches Caches.
*/
public VisorIdleVerifyTaskArg(Set<String> caches) {
this.caches = caches;
this(caches, null, false, DEFAULT, false);
}

/** */
Expand Down Expand Up @@ -162,8 +165,6 @@ public Set<String> excludeCaches() {

cacheFilterEnum = CacheFilterEnum.fromOrdinal(in.readByte());
}

cacheFilterEnum(cacheFilterEnum);
}
}

Expand Down Expand Up @@ -191,7 +192,9 @@ protected void skipZeros(boolean skipZeros) {

/** */
protected void cacheFilterEnum(CacheFilterEnum cacheFilterEnum) {
this.cacheFilterEnum = (cacheFilterEnum == null ? CacheFilterEnum.DEFAULT : cacheFilterEnum);
assert nonNull(cacheFilterEnum);

this.cacheFilterEnum = cacheFilterEnum;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
import org.apache.ignite.internal.processors.cache.persistence.CheckpointReadLockFailureTest;
import org.apache.ignite.internal.processors.cache.persistence.SingleNodePersistenceSslTest;
import org.apache.ignite.marshaller.GridMarshallerMappingConsistencyTest;
import org.apache.ignite.util.GridCommandHandlerSslTest;
import org.apache.ignite.util.GridCommandHandlerClusterByClassTest;
import org.apache.ignite.util.GridCommandHandlerTest;
import org.apache.ignite.util.GridCommandHandlerSslTest;
import org.apache.ignite.util.GridInternalTaskUnusedWalSegmentsTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
Expand All @@ -53,6 +54,7 @@
CheckpointReadLockFailureTest.class,

GridCommandHandlerTest.class,
GridCommandHandlerClusterByClassTest.class,
GridCommandHandlerSslTest.class,
GridInternalTaskUnusedWalSegmentsTest.class,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteDataStreamer;
import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
import org.apache.ignite.configuration.AtomicConfiguration;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.ConnectorConfiguration;
import org.apache.ignite.configuration.DataRegionConfiguration;
import org.apache.ignite.configuration.DataStorageConfiguration;
Expand All @@ -45,31 +48,48 @@
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.G;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.testframework.GridTestUtils;
import org.apache.ignite.testframework.junits.SystemPropertiesRule;
import org.apache.ignite.testframework.junits.WithSystemProperty;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.junit.ClassRule;
import org.junit.rules.TestRule;

import static java.nio.file.Files.delete;
import static java.nio.file.Files.newDirectoryStream;
import static java.util.Arrays.asList;
import static java.util.Objects.nonNull;
import static org.apache.ignite.IgniteSystemProperties.IGNITE_BASELINE_AUTO_ADJUST_ENABLED;
import static org.apache.ignite.IgniteSystemProperties.IGNITE_ENABLE_EXPERIMENTAL_COMMAND;
import static org.apache.ignite.configuration.DataStorageConfiguration.DFLT_CHECKPOINT_FREQ;
import static org.apache.ignite.internal.processors.cache.verify.VerifyBackupPartitionsDumpTask.IDLE_DUMP_FILE_PREFIX;
import static org.apache.ignite.testframework.GridTestUtils.cleanIdleVerifyLogFiles;

/**
*
* Common abstract class for testing {@link CommandHandler}.
* I advise you to look at the heirs classes:
* {@link GridCommandHandlerClusterPerMethodAbstractTest}
* {@link GridCommandHandlerClusterByClassAbstractTest}
*/
public class GridCommandHandlerAbstractTest extends GridCommonAbstractTest {
@WithSystemProperty(key = IGNITE_BASELINE_AUTO_ADJUST_ENABLED, value = "false")
@WithSystemProperty(key = IGNITE_ENABLE_EXPERIMENTAL_COMMAND, value = "true")
public abstract class GridCommandHandlerAbstractTest extends GridCommonAbstractTest {
/** */
@ClassRule public static final TestRule classRule = new SystemPropertiesRule();

/** */
protected static final String CLIENT_NODE_NAME_PREFIX = "client";

/** Option is used for auto confirmation. */
protected static final String CMD_AUTO_CONFIRMATION = "--yes";

/** System out. */
protected PrintStream sysOut;
protected static PrintStream sysOut;

/**
* Test out - can be injected via {@link #injectTestSystemOut()} instead of System.out and analyzed in test.
* Will be as well passed as a handler output for an anonymous logger in the test.
*/
protected ByteArrayOutputStream testOut;
protected static ByteArrayOutputStream testOut;

/** Atomic configuration. */
protected AtomicConfiguration atomicConfiguration;
Expand All @@ -78,69 +98,40 @@ public class GridCommandHandlerAbstractTest extends GridCommonAbstractTest {
protected DataRegionConfiguration dataRegionConfiguration;

/** Checkpoint frequency. */
protected long checkpointFreq;

/** {@inheritDoc} */
@Override protected void afterTestsStopped() throws Exception {
super.afterTestsStopped();

GridTestUtils.cleanIdleVerifyLogFiles();

System.clearProperty(IGNITE_BASELINE_AUTO_ADJUST_ENABLED);
}
protected long checkpointFreq = DFLT_CHECKPOINT_FREQ;

/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
System.setProperty(IGNITE_BASELINE_AUTO_ADJUST_ENABLED, "false");

super.beforeTestsStarted();
}

/** {@inheritDoc} */
@Override protected void beforeTest() throws Exception {
System.setProperty(IGNITE_ENABLE_EXPERIMENTAL_COMMAND, "true");

cleanPersistenceDir();

stopAllGrids();

sysOut = System.out;

testOut = new ByteArrayOutputStream(16 * 1024);

checkpointFreq = DataStorageConfiguration.DFLT_CHECKPOINT_FREQ;
sysOut = System.out;
}

/**
* @return True if system out was already injected on test initialization.
*/
protected boolean isSystemOutAlreadyInjected() {
return false;
/** {@inheritDoc} */
@Override protected void afterTestsStopped() throws Exception {
super.afterTestsStopped();

cleanIdleVerifyLogFiles();
}

/** {@inheritDoc} */
@Override protected void afterTest() throws Exception {
super.afterTest();

log.info("Test output for " + currentTestMethod());
log.info("----------------------------------------");

System.setOut(sysOut);

if (testOut != null)
System.out.println(testOut.toString());

testOut = null;

stopAllGrids();
log.info(testOut.toString());

cleanPersistenceDir();

// Delete idle-verify dump files.
try (DirectoryStream<Path> files = newDirectoryStream(Paths.get(U.defaultWorkDirectory()), this::idleVerifyRes)) {
for (Path path : files)
delete(path);
}
testOut.reset();
}

System.clearProperty(IGNITE_ENABLE_EXPERIMENTAL_COMMAND);
/** {@inheritDoc} */
@Override public String getTestIgniteInstanceName() {
return "gridCommandHandlerTest";
}

/**
Expand All @@ -156,7 +147,7 @@ private Logger createTestLogger() {
}

/** */
private boolean idleVerifyRes(Path p) {
protected boolean idleVerifyRes(Path p) {
return p.toFile().getName().startsWith(IDLE_DUMP_FILE_PREFIX);
}

Expand Down Expand Up @@ -186,12 +177,24 @@ private boolean idleVerifyRes(Path p) {

cfg.setConsistentId(igniteInstanceName);

cfg.setClientMode(igniteInstanceName.startsWith("client"));
cfg.setClientMode(igniteInstanceName.startsWith(CLIENT_NODE_NAME_PREFIX));

return cfg;
}

/** {@inheritDoc} */
@Override protected void cleanPersistenceDir() throws Exception {
super.cleanPersistenceDir();

try (DirectoryStream<Path> files = newDirectoryStream(Paths.get(U.defaultWorkDirectory()), this::idleVerifyRes)) {
for (Path path : files)
delete(path);
}
}

/**
* Before command executed {@link #testOut} reset.
*
* @param args Arguments.
* @return Result of execution.
*/
Expand All @@ -200,6 +203,8 @@ protected int execute(String... args) {
}

/**
* Before command executed {@link #testOut} reset.
*
* @param args Arguments.
* @return Result of execution
*/
Expand All @@ -208,6 +213,8 @@ protected int execute(List<String> args) {
}

/**
* Before command executed {@link #testOut} reset.
*
* @param hnd Handler.
* @param args Arguments.
* @return Result of execution
Expand All @@ -216,11 +223,15 @@ protected int execute(CommandHandler hnd, String... args) {
return execute(hnd, new ArrayList<>(asList(args)));
}

/** */
/**
* Before command executed {@link #testOut} reset.
*/
protected int execute(CommandHandler hnd, List<String> args) {
if (!F.isEmpty(args) && !"--help".equalsIgnoreCase(args.get(0)))
addExtraArguments(args);

testOut.reset();

int exitCode = hnd.execute(args);

// Flush all Logger handlers to make log data available to test.
Expand Down Expand Up @@ -288,4 +299,41 @@ protected void checkUserFutures() {
fail("Some transaction are not finished");
}
}

/**
* Creates default cache and preload some data entries.
* <br/>
* <table class="doctable">
* <th>Cache parameter</th>
* <th>Value</th>
* <tr>
* <td>Name</td>
* <td>{@link #DEFAULT_CACHE_NAME}</td>
* </tr>
* <tr>
* <td>Affinity</td>
* <td>{@link RendezvousAffinityFunction} with exclNeighbors = false, parts = 32</td>
* </tr>
* <tr>
* <td>Number of backup</td>
* <td>1</td>
* </tr>
*
* </table>
*
* @param ignite Ignite.
* @param countEntries Count of entries.
*/
protected void createCacheAndPreload(Ignite ignite, int countEntries) {
assert nonNull(ignite);

ignite.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME)
.setAffinity(new RendezvousAffinityFunction(false, 32))
.setBackups(1));

try (IgniteDataStreamer streamer = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
for (int i = 0; i < countEntries; i++)
streamer.addData(i, i);
}
}
}

0 comments on commit 932b66b

Please sign in to comment.