mirror of https://github.com/apache/lucene.git
SOLR-5206: Fixed OpenExchangeRatesOrgProvider to use refreshInterval correctly
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1519858 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
31a88ab101
commit
c53362f38a
|
@ -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
|
||||
----------------------
|
||||
|
||||
|
|
|
@ -59,7 +59,8 @@ public class OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
|
|||
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 OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
|
|||
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 OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
|
|||
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 OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
|
|||
/**
|
||||
* 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 OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
|
|||
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;
|
||||
|
|
|
@ -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 OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 {
|
|||
@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 OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 {
|
|||
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)
|
||||
|
|
Loading…
Reference in New Issue