You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by dp...@apache.org on 2018/10/30 15:38:10 UTC
ignite git commit: IGNITE-10023 Improve ListeningTestLogger for wait
conditions. - Fixes #5191.
Repository: ignite
Updated Branches:
refs/heads/master 99bb28f21 -> 5397d67af
IGNITE-10023 Improve ListeningTestLogger for wait conditions. - Fixes #5191.
Signed-off-by: Dmitriy Pavlov <dp...@apache.org>
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/5397d67a
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/5397d67a
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/5397d67a
Branch: refs/heads/master
Commit: 5397d67af2c52d1acb37234c5f5247c5e5811001
Parents: 99bb28f
Author: NSAmelchev <ns...@gmail.com>
Authored: Tue Oct 30 18:37:51 2018 +0300
Committer: Dmitriy Pavlov <dp...@apache.org>
Committed: Tue Oct 30 18:37:51 2018 +0300
----------------------------------------------------------------------
.../ignite/testframework/LogListener.java | 76 +++++---------------
.../test/ListeningTestLoggerTest.java | 71 +++++++++---------
2 files changed, 52 insertions(+), 95 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ignite/blob/5397d67a/modules/core/src/test/java/org/apache/ignite/testframework/LogListener.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/LogListener.java b/modules/core/src/test/java/org/apache/ignite/testframework/LogListener.java
index d349f06..485e3fa 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/LogListener.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/LogListener.java
@@ -28,7 +28,6 @@ import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
/**
* The basic listener for custom log contents checking in {@link ListeningTestLogger}.<br><br>
@@ -66,9 +65,9 @@ public abstract class LogListener implements Consumer<String> {
/**
* Checks that all conditions are met.
*
- * @throws AssertionError If some condition failed.
+ * @return {@code True} if all conditions are met.
*/
- public abstract void check() throws AssertionError;
+ public abstract boolean check();
/**
* Reset listener state.
@@ -122,7 +121,7 @@ public abstract class LogListener implements Consumer<String> {
* @return current builder instance.
*/
public Builder andMatches(String substr) {
- addLast(new Node(substr, msg -> {
+ addLast(new Node(msg -> {
if (substr.isEmpty())
return msg.isEmpty() ? 1 : 0;
@@ -144,7 +143,7 @@ public abstract class LogListener implements Consumer<String> {
* @return current builder instance.
*/
public Builder andMatches(Pattern regexp) {
- addLast(new Node(regexp.toString(), msg -> {
+ addLast(new Node(msg -> {
int cnt = 0;
Matcher matcher = regexp.matcher(msg);
@@ -165,7 +164,7 @@ public abstract class LogListener implements Consumer<String> {
* @return current builder instance.
*/
public Builder andMatches(Predicate<String> pred) {
- addLast(new Node(null, msg -> pred.test(msg) ? 1 : 0));
+ addLast(new Node(msg -> pred.test(msg) ? 1 : 0));
return this;
}
@@ -222,19 +221,6 @@ public abstract class LogListener implements Consumer<String> {
}
/**
- * Set custom message for assertion error.
- *
- * @param msg Custom message.
- * @return current builder instance.
- */
- public Builder orError(String msg) {
- if (prev != null)
- prev.msg = msg;
-
- return this;
- }
-
- /**
* Constructs message listener.
*
* @return Log message listener.
@@ -263,15 +249,9 @@ public abstract class LogListener implements Consumer<String> {
*/
static final class Node {
/** */
- final String subj;
-
- /** */
final Function<String, Integer> func;
/** */
- String msg;
-
- /** */
Integer min;
/** */
@@ -281,8 +261,7 @@ public abstract class LogListener implements Consumer<String> {
Integer cnt;
/** */
- Node(String subj, Function<String, Integer> func) {
- this.subj = subj;
+ Node(Function<String, Integer> func) {
this.func = func;
}
@@ -297,7 +276,7 @@ public abstract class LogListener implements Consumer<String> {
else
range = ValueRange.of(min == null ? 0 : min, max == null ? Integer.MAX_VALUE : max);
- return new LogMessageListener(func, range, subj, msg);
+ return new LogMessageListener(func, range);
}
}
}
@@ -316,28 +295,13 @@ public abstract class LogListener implements Consumer<String> {
/** */
private final ValueRange exp;
- /** */
- private final String subj;
-
- /** */
- private final String errMsg;
-
/**
- * @param subj Search subject.
* @param exp Expected occurrences.
* @param func Function of counting matches in the message.
- * @param errMsg Custom error message.
*/
- private LogMessageListener(
- @NotNull Function<String, Integer> func,
- @NotNull ValueRange exp,
- @Nullable String subj,
- @Nullable String errMsg
- ) {
+ private LogMessageListener(@NotNull Function<String, Integer> func, @NotNull ValueRange exp) {
this.func = func;
this.exp = exp;
- this.subj = subj == null ? func.toString() : subj;
- this.errMsg = errMsg;
}
/** {@inheritDoc} */
@@ -350,7 +314,8 @@ public abstract class LogListener implements Consumer<String> {
if (cnt > 0)
matches.addAndGet(cnt);
- } catch (Throwable t) {
+ }
+ catch (Throwable t) {
err.compareAndSet(null, t);
if (t instanceof VirtualMachineError)
@@ -359,18 +324,12 @@ public abstract class LogListener implements Consumer<String> {
}
/** {@inheritDoc} */
- @Override public void check() {
+ @Override public boolean check() {
errCheck();
int matchesCnt = matches.get();
- if (!exp.isValidIntValue(matchesCnt)) {
- String err = errMsg != null ? errMsg :
- "\"" + subj + "\" matches " + matchesCnt + " times, expected: " +
- (exp.getMaximum() == exp.getMinimum() ? exp.getMinimum() : exp) + ".";
-
- throw new AssertionError(err);
- }
+ return exp.isValidIntValue(matchesCnt);
}
/** {@inheritDoc} */
@@ -385,10 +344,10 @@ public abstract class LogListener implements Consumer<String> {
Throwable t = err.get();
if (t instanceof Error)
- throw (Error) t;
+ throw (Error)t;
if (t instanceof RuntimeException)
- throw (RuntimeException) t;
+ throw (RuntimeException)t;
assert t == null : t;
}
@@ -400,9 +359,12 @@ public abstract class LogListener implements Consumer<String> {
private final List<LogMessageListener> lsnrs = new ArrayList<>();
/** {@inheritDoc} */
- @Override public void check() {
+ @Override public boolean check() {
for (LogMessageListener lsnr : lsnrs)
- lsnr.check();
+ if (!lsnr.check())
+ return false;
+
+ return true;
}
/** {@inheritDoc} */
http://git-wip-us.apache.org/repos/asf/ignite/blob/5397d67a/modules/core/src/test/java/org/apache/ignite/testframework/test/ListeningTestLoggerTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testframework/test/ListeningTestLoggerTest.java b/modules/core/src/test/java/org/apache/ignite/testframework/test/ListeningTestLoggerTest.java
index a888017..8043f7e 100644
--- a/modules/core/src/test/java/org/apache/ignite/testframework/test/ListeningTestLoggerTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/testframework/test/ListeningTestLoggerTest.java
@@ -29,7 +29,6 @@ import org.apache.ignite.testframework.ListeningTestLogger;
import org.apache.ignite.testframework.LogListener;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
-import static org.apache.ignite.testframework.GridTestUtils.assertThrows;
import static org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause;
/**
@@ -64,7 +63,7 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
try {
startGridsMultiThreaded(gridCnt);
- lsnr.check();
+ assertTrue(lsnr.check());
} finally {
stopAllGrids();
}
@@ -88,8 +87,8 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info(msg);
- lsnr1.check();
- lsnr2.check();
+ assertTrue(lsnr1.check());
+ assertTrue(lsnr2.check());
// Repeat these steps to ensure that the state is cleared during registration.
log.registerListener(lsnr1);
@@ -101,8 +100,8 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info(msg);
- lsnr1.check();
- lsnr2.check();
+ assertTrue(lsnr1.check());
+ assertTrue(lsnr2.check());
}
/**
@@ -125,24 +124,18 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
* Checks basic API.
*/
public void testBasicApi() {
- String errMsg = "Word started with \"a\" not found.";
-
- LogListener lsnr = LogListener.matches(Pattern.compile("a[a-z]+")).orError(errMsg)
+ LogListener lsnr = LogListener.matches(Pattern.compile("a[a-z]+"))
.andMatches("Exception message.").andMatches(".java:").build();
log.registerListener(lsnr);
log.info("Something new.");
- assertThrows(log(), () -> {
- lsnr.check();
-
- return null;
- }, AssertionError.class, errMsg);
+ assertFalse(lsnr.check());
log.error("There was an error.", new RuntimeException("Exception message."));
- lsnr.check();
+ assertTrue(lsnr.check());
}
/**
@@ -155,7 +148,7 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info("");
- emptyLineLsnr.check();
+ assertTrue(emptyLineLsnr.check());
}
/** */
@@ -176,7 +169,7 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
// Check custom exception.
LogListener lsnr2 = LogListener.matches(msg -> {
throw new IllegalStateException("Illegal state");
- }).orError("ignored blah-blah").build();
+ }).build();
log.registerListener(lsnr2);
@@ -201,18 +194,18 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info(msg);
log.info(msg);
- lsnr2.check();
- lsnr2_3.check();
+ assertTrue(lsnr2.check());
+ assertTrue(lsnr2_3.check());
log.info(msg);
- assertThrowsWithCause(lsnr2::check, AssertionError.class);
+ assertFalse(lsnr2.check());
- lsnr2_3.check();
+ assertTrue(lsnr2_3.check());
log.info(msg);
- assertThrowsWithCause(lsnr2_3::check, AssertionError.class);
+ assertFalse(lsnr2_3.check());
}
/**
@@ -227,11 +220,11 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info("1");
- notPresent.check();
+ assertTrue(notPresent.check());
log.info(msg);
- assertThrowsWithCause(notPresent::check, AssertionError.class);
+ assertFalse(notPresent.check());
}
/**
@@ -246,11 +239,11 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info(msg);
- assertThrowsWithCause(atLeast2::check, AssertionError.class);
+ assertFalse(atLeast2.check());
log.info(msg);
- atLeast2.check();
+ assertTrue(atLeast2.check());
}
/**
@@ -263,16 +256,16 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.registerListener(atMost2);
- atMost2.check();
+ assertTrue(atMost2.check());
log.info(msg);
log.info(msg);
- atMost2.check();
+ assertTrue(atMost2.check());
log.info(msg);
- assertThrowsWithCause(atMost2::check, AssertionError.class);
+ assertFalse(atMost2.check());
}
/**
@@ -287,9 +280,9 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
for (int i = 0; i < 6; i++) {
if (i < 4)
- atMost3.check();
+ assertTrue(atMost3.check());
else
- assertThrowsWithCause(atMost3::check, AssertionError.class);
+ assertFalse(atMost3.check());
log.info(msg);
}
@@ -302,9 +295,9 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info(msg);
if (i == 4)
- lsnr4.check();
+ assertTrue(lsnr4.check());
else
- assertThrowsWithCause(lsnr4::check, AssertionError.class);
+ assertFalse(lsnr4.check());
}
}
@@ -319,7 +312,7 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info("aabaab");
log.info("abaaab");
- lsnr.check();
+ assertTrue(lsnr.check());
LogListener newLineLsnr = LogListener.matches("\n").times(5).build();
@@ -327,7 +320,7 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info("\n1\n2\n\n3\n");
- newLineLsnr.check();
+ assertTrue(newLineLsnr.check());
LogListener regexpLsnr = LogListener.matches(Pattern.compile("(?i)hi|hello")).times(3).build();
@@ -336,7 +329,7 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
log.info("Hi! Hello!");
log.info("Hi folks");
- regexpLsnr.check();
+ assertTrue(regexpLsnr.check());
}
/**
@@ -350,6 +343,8 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
int total = threadCnt * iterCnt;
int rndNum = ThreadLocalRandom.current().nextInt(iterCnt);
+ ListeningTestLogger log = new ListeningTestLogger();
+
LogListener lsnr = LogListener.matches("abba").times(total)
.andMatches(Pattern.compile("(?i)abba")).times(total * 2)
.andMatches("ab").times(total)
@@ -369,8 +364,8 @@ public class ListeningTestLoggerTest extends GridCommonAbstractTest {
}
}, threadCnt, "test-listening-log");
- lsnr.check();
- mtLsnr.check();
+ assertTrue(lsnr.check());
+ assertTrue(mtLsnr.check());
}
/**