From e2832b37d7e130103370fd3c854cf2cff3aecf7c Mon Sep 17 00:00:00 2001 From: Andrzej Bialecki Date: Tue, 11 Jun 2019 14:09:18 +0200 Subject: [PATCH] Improve this test - instead of waiting a fixed amount of time wait until an update arrives. Also, deal with partial updates. --- .../solr/util/MockSearchableSolrClient.java | 15 ++++++ .../rrd/SolrRrdBackendFactoryTest.java | 53 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/solr/core/src/java/org/apache/solr/util/MockSearchableSolrClient.java b/solr/core/src/java/org/apache/solr/util/MockSearchableSolrClient.java index c4d1d0c3910..646d0088017 100644 --- a/solr/core/src/java/org/apache/solr/util/MockSearchableSolrClient.java +++ b/solr/core/src/java/org/apache/solr/util/MockSearchableSolrClient.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrRequest; @@ -41,6 +42,9 @@ import org.apache.solr.common.util.NamedList; public class MockSearchableSolrClient extends SolrClient { public Map> docs = new ConcurrentHashMap<>(); + private AtomicLong numUpdates = new AtomicLong(); + private AtomicLong numQueries = new AtomicLong(); + public void clear() { docs.clear(); } @@ -64,6 +68,7 @@ public class MockSearchableSolrClient extends SolrClient { String id = (String) doc.getFieldValue("id"); Objects.requireNonNull(id, doc.toString()); docs.computeIfAbsent(collection, c -> new LinkedHashMap<>()).put(id, doc); + numUpdates.incrementAndGet(); }); } } else if (request instanceof QueryRequest) { @@ -75,6 +80,7 @@ public class MockSearchableSolrClient extends SolrClient { final SolrDocumentList lst = new SolrDocumentList(); if (query != null) { if (query.startsWith("{!term f=id}") || query.startsWith("id:")) { + numQueries.incrementAndGet(); String id; if (query.startsWith("{!")) { id = query.substring(12); @@ -92,6 +98,7 @@ public class MockSearchableSolrClient extends SolrClient { } } } else if (query.equals("*:*")) { + numQueries.incrementAndGet(); Map collDocs = docs.get(collection); if (collDocs != null) { lst.setNumFound(collDocs.size()); @@ -110,6 +117,14 @@ public class MockSearchableSolrClient extends SolrClient { return res; } + public long getNumUpdates() { + return numUpdates.get(); + } + + public long getNumQueries() { + return numQueries.get(); + } + @Override public void close() throws IOException { diff --git a/solr/core/src/test/org/apache/solr/metrics/rrd/SolrRrdBackendFactoryTest.java b/solr/core/src/test/org/apache/solr/metrics/rrd/SolrRrdBackendFactoryTest.java index 955c256f6b6..2c7ea9d7242 100644 --- a/solr/core/src/test/org/apache/solr/metrics/rrd/SolrRrdBackendFactoryTest.java +++ b/solr/core/src/test/org/apache/solr/metrics/rrd/SolrRrdBackendFactoryTest.java @@ -20,6 +20,7 @@ package org.apache.solr.metrics.rrd; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.SolrInputDocument; @@ -29,6 +30,7 @@ import org.apache.solr.common.util.TimeSource; import org.apache.solr.common.util.Utils; import org.apache.solr.util.LogLevel; import org.apache.solr.util.MockSearchableSolrClient; +import org.apache.solr.util.TimeOut; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -86,16 +88,25 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 { public void testBasic() throws Exception { long startTime = 1000000000; RrdDb db = new RrdDb(createDef(startTime), factory); + long lastNumUpdates = solrClient.getNumUpdates(); List> list = factory.list(100); assertEquals(list.toString(), 1, list.size()); assertEquals(list.toString(), "foo", list.get(0).first()); + timeSource.sleep(4000); + lastNumUpdates = waitForUpdates(lastNumUpdates); + + // wait until updates stop coming - the first update could have been partial + lastNumUpdates = waitForUpdatesToStop(lastNumUpdates); + // there should be one sync data assertEquals(solrClient.docs.toString(), 1, solrClient.docs.size()); String id = SolrRrdBackendFactory.ID_PREFIX + SolrRrdBackendFactory.ID_SEP + "foo"; SolrInputDocument doc = solrClient.docs.get(CollectionAdminParams.SYSTEM_COLL).get(id); long timestamp = (Long)doc.getFieldValue("timestamp_l"); + timeSource.sleep(4000); + SolrInputDocument newDoc = solrClient.docs.get(CollectionAdminParams.SYSTEM_COLL).get(id); assertEquals(newDoc.toString(), newDoc, doc); // make sure the update doesn't race with the sampling boundaries @@ -110,6 +121,8 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 { lastTime = lastTime + 60; } timeSource.sleep(3000); + lastNumUpdates = waitForUpdates(lastNumUpdates); + newDoc = solrClient.docs.get(CollectionAdminParams.SYSTEM_COLL).get(id); assertFalse(newDoc.toString(), newDoc.equals(doc)); long newTimestamp = (Long)newDoc.getFieldValue("timestamp_l"); @@ -138,6 +151,8 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 { assertEquals(list.toString(), 1, list.size()); assertEquals(list.toString(), "foo", list.get(0).first()); + lastNumUpdates = solrClient.getNumUpdates(); + // re-open read-write db = new RrdDb("solr:foo", factory); s = db.createSample(); @@ -146,6 +161,8 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 { s.setValue("two", 100); s.update(); timeSource.sleep(3000); + lastNumUpdates = waitForUpdates(lastNumUpdates); + // should update timestamp = newTimestamp; doc = newDoc; @@ -200,4 +217,40 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 { return Utils.toJSONString(map); } + private long waitForUpdates(long lastNumUpdates) throws Exception { + TimeOut timeOut = new TimeOut(30, TimeUnit.SECONDS, timeSource); + while (!timeOut.hasTimedOut()) { + timeOut.sleep(1000); + if (solrClient.getNumUpdates() > lastNumUpdates) { + return solrClient.getNumUpdates(); + } + } + if (solrClient.getNumUpdates() > lastNumUpdates) { + return solrClient.getNumUpdates(); + } + throw new Exception("time out waiting for updates"); + } + + + private long waitForUpdatesToStop(long lastNumUpdates) throws Exception { + TimeOut timeOut = new TimeOut(30, TimeUnit.SECONDS, timeSource); + int stopped = 0; + while (!timeOut.hasTimedOut()) { + timeOut.sleep(1000); + if (solrClient.getNumUpdates() > lastNumUpdates) { + stopped = 0; + lastNumUpdates = solrClient.getNumUpdates(); + continue; + } else { + stopped++; + if (stopped > 2) { + return lastNumUpdates; + } + } + timeOut.sleep(1000); + } + throw new Exception("time out waiting for updates"); + } + + }