You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ho...@apache.org on 2013/09/03 23:39:51 UTC
svn commit: r1519858 - in /lucene/dev/trunk/solr: CHANGES.txt
core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java
core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java
Author: hossman
Date: Tue Sep 3 21:39:51 2013
New Revision: 1519858
URL: http://svn.apache.org/r1519858
Log:
SOLR-5206: Fixed OpenExchangeRatesOrgProvider to use refreshInterval correctly
Modified:
lucene/dev/trunk/solr/CHANGES.txt
lucene/dev/trunk/solr/core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java
lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java
Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1519858&r1=1519857&r2=1519858&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Tue Sep 3 21:39:51 2013
@@ -176,6 +176,9 @@ Bug Fixes
* SOLR-3852: Fixed ZookeeperInfoServlet so that the SolrCloud Admin UI pages will
work even if ZK contains nodes with data which are not utf8 text. (hossman)
+* SOLR-5206: Fixed OpenExchangeRatesOrgProvider to use refreshInterval correctly
+ (Catalin, hossman)
+
Optimizations
----------------------
Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java?rev=1519858&r1=1519857&r2=1519858&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/schema/OpenExchangeRatesOrgProvider.java Tue Sep 3 21:39:51 2013
@@ -59,7 +59,8 @@ public class OpenExchangeRatesOrgProvide
protected static final String DEFAULT_REFRESH_INTERVAL = "1440";
protected String ratesFileLocation;
- protected int refreshInterval;
+ // configured in minutes, but stored in seconds for quicker math
+ protected int refreshIntervalSeconds;
protected ResourceLoader resourceLoader;
protected OpenExchangeRates rates;
@@ -84,7 +85,7 @@ public class OpenExchangeRatesOrgProvide
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cannot get exchange rate; currency was null.");
}
- if (rates.getTimestamp() + refreshInterval*60*1000 > System.currentTimeMillis()) {
+ if ((rates.getTimestamp() + refreshIntervalSeconds)*1000 < System.currentTimeMillis()) {
log.debug("Refresh interval has expired. Refreshing exchange rates.");
reload();
}
@@ -159,13 +160,14 @@ public class OpenExchangeRatesOrgProvide
if (null == ratesFileLocation) {
throw new SolrException(ErrorCode.SERVER_ERROR, "Init param must be specified: " + PARAM_RATES_FILE_LOCATION);
}
- refreshInterval = Integer.parseInt(getParam(params.get(PARAM_REFRESH_INTERVAL), DEFAULT_REFRESH_INTERVAL));
+ int refreshInterval = Integer.parseInt(getParam(params.get(PARAM_REFRESH_INTERVAL), DEFAULT_REFRESH_INTERVAL));
// Force a refresh interval of minimum one hour, since the API does not offer better resolution
if (refreshInterval < 60) {
refreshInterval = 60;
log.warn("Specified refreshInterval was too small. Setting to 60 minutes which is the update rate of openexchangerates.org");
}
log.info("Initialized with rates="+ratesFileLocation+", refreshInterval="+refreshInterval+".");
+ refreshIntervalSeconds = refreshInterval * 60;
} catch (SolrException e1) {
throw e1;
} catch (Exception e2) {
@@ -191,7 +193,7 @@ public class OpenExchangeRatesOrgProvide
/**
* A simple class encapsulating the JSON data from openexchangerates.org
*/
- class OpenExchangeRates {
+ static class OpenExchangeRates {
private Map<String, Double> rates;
private String baseCurrency;
private long timestamp;
@@ -261,6 +263,12 @@ public class OpenExchangeRatesOrgProvide
public long getTimestamp() {
return timestamp;
}
+ /** Package protected method for test purposes
+ * @lucene.internal
+ */
+ void setTimestamp(long timestamp) {
+ this.timestamp = timestamp;
+ }
public String getDisclaimer() {
return disclaimer;
Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java?rev=1519858&r1=1519857&r2=1519858&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/schema/OpenExchangeRatesOrgProviderTest.java Tue Sep 3 21:39:51 2013
@@ -30,10 +30,13 @@ import java.util.Map;
* Tests currency field type.
*/
public class OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 {
+ private final static long HARDCODED_TEST_TIMESTAMP = 1332070464L;
+
OpenExchangeRatesOrgProvider oerp;
ResourceLoader loader;
private final Map<String,String> mockParams = new HashMap<String,String>();
+
@Override
@Before
public void setUp() throws Exception {
@@ -50,18 +53,22 @@ public class OpenExchangeRatesOrgProvide
@Test
public void testInit() throws Exception {
oerp.init(mockParams);
+ // don't inform, we don't want to hit any of these URLs
+
assertEquals("Wrong url",
"open-exchange-rates.json", oerp.ratesFileLocation);
- assertEquals("Wrong default interval", 1440, oerp.refreshInterval);
+ assertEquals("Wrong default interval", (1440*60), oerp.refreshIntervalSeconds);
Map<String,String> params = new HashMap<String,String>();
params.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION,
"http://foo.bar/baz");
params.put(OpenExchangeRatesOrgProvider.PARAM_REFRESH_INTERVAL, "100");
+
oerp.init(params);
assertEquals("Wrong param set url",
"http://foo.bar/baz", oerp.ratesFileLocation);
- assertEquals("Wrong param interval", 100, oerp.refreshInterval);
+ assertEquals("Wrong param interval", (100*60), oerp.refreshIntervalSeconds);
+
}
@Test
@@ -76,15 +83,31 @@ public class OpenExchangeRatesOrgProvide
oerp.init(mockParams);
oerp.inform(loader);
assertEquals(81.29D, oerp.getExchangeRate("USD", "JPY"), 0.0D);
+ assertEquals("USD", oerp.rates.getBaseCurrency());
}
@Test
public void testReload() {
+ // reminder: interval is in minutes
+ mockParams.put(OpenExchangeRatesOrgProvider.PARAM_REFRESH_INTERVAL, "100");
oerp.init(mockParams);
oerp.inform(loader);
- assertTrue(oerp.reload());
- assertEquals("USD", oerp.rates.getBaseCurrency());
- assertEquals(new Long(1332070464L), new Long(oerp.rates.getTimestamp()));
+
+ // reminder: timestamp is in seconds
+ assertEquals(HARDCODED_TEST_TIMESTAMP, oerp.rates.getTimestamp());
+
+ // modify the timestamp to be "current" then fetch a rate and ensure no reload
+ final long currentTimestamp = (long) (System.currentTimeMillis() / 1000);
+ oerp.rates.setTimestamp(currentTimestamp);
+ assertEquals(81.29D, oerp.getExchangeRate("USD", "JPY"), 0.0D);
+ assertEquals(currentTimestamp, oerp.rates.getTimestamp());
+
+ // roll back clock on timestamp and ensure rate fetch does reload
+ oerp.rates.setTimestamp(currentTimestamp - (101 * 60));
+ assertEquals(81.29D, oerp.getExchangeRate("USD", "JPY"), 0.0D);
+ assertEquals("timestamp wasn't reset to hardcoded value, indicating no reload",
+ HARDCODED_TEST_TIMESTAMP, oerp.rates.getTimestamp());
+
}
@Test(expected=SolrException.class)