Apache Lucene open-source search software
SOLR-12732: TestLogWatcher failure on Jenkins
Erick Erickson9e04375dc193
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex a975f06fdd2..78bfb33100d 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -323,6 +323,8 @@ Bug Fixes* SOLR-12733: SolrMetricReporterTest failure (Erick Erickson, David Smiley)+* SOLR-12732: TestLogWatcher failure on Jenkins (Erick Erickson)+Optimizations----------------------diff --git a/solr/core/src/test/org/apache/solr/logging/TestLogWatcher.java b/solr/core/src/test/org/apache/solr/logging/TestLogWatcher.javaindex 658bc34c31e..a93a11bec6f 100644--- a/solr/core/src/test/org/apache/solr/logging/TestLogWatcher.java+++ b/solr/core/src/test/org/apache/solr/logging/TestLogWatcher.java@@ -39,37 +39,51 @@ public class TestLogWatcher extends SolrTestCaseJ4 {// Create several log watchers and ensure that new messages go to the new watcher.@Testpublic void testLog4jWatcher() {- LogWatcher watcher = null;+ LogWatcher watcher;int lim = random().nextInt(3) + 2;for (int idx = 0; idx < lim; ++idx) {String msg = "This is a test message: " + idx;watcher = LogWatcher.newRegisteredLogWatcher(config, null);// First ensure there's nothing in the new watcher.- assertEquals(-1, watcher.getLastEvent());++ // Every time you put a message in the queue, you wait for it to come out _before_ creating+ // a new watcher so it should be fine.+ if (looper(watcher, null) == false) {+ fail("There should be no messages when a new watcher finally gets registered! In loop: " + idx);+ }// Now log a message and ensure that the new watcher sees it.log.warn(msg);// Loop to give the logger time to process the async message and notify the new watcher.- boolean foundMsg = false;- // In local testing this loop usually succeeds 1-2 tries.- for (int msgIdx = 0; msgIdx < 100; ++msgIdx) {+ if (looper(watcher, msg) == false) {+ fail("Should have found message " + msg + ". In loop: " + idx);+ }+ }+ }+ private boolean looper(LogWatcher watcher, String msg) {+ // In local testing this loop usually succeeds 1-2 tries.+ boolean success = false;+ boolean testingNew = msg == null;+ for (int msgIdx = 0; msgIdx < 100 && success == false; ++msgIdx) {+ if (testingNew) { // check that there are no entries registered for the watcher+ success = watcher.getLastEvent() == -1;+ } else { // check that the expected message is there.// Returns an empty (but non-null) list even if there are no messages yet.SolrDocumentList events = watcher.getHistory(-1, new AtomicBoolean());for (SolrDocument doc : events) {if (doc.get("message").equals(msg)) {- foundMsg = true;- break;+ success = true;}}- try {- Thread.sleep(10);- } catch (InterruptedException ie) {- ;- }}- assertTrue("Should have found message " + msg + " in loop: " + idx, foundMsg);+ try {+ Thread.sleep(10);+ } catch (InterruptedException ie) {+ ;+ }}+ return success;}}