JsRunnerProvider implements Provider<JsRunner> { public JsRunner get() { JsRunnerRequestCache cache = cacheProvider.get(); Date updated = cache.getUpdated(); if (updated == null) { updated = rulesCurator.getUpdated(); cache.setUpdated(updated); } if (!updated.equals(this.currentRulesUpdated)) { compileRules(); } Scriptable rulesScope; scriptLock.readLock().lock(); try { Context context = Context.enter(); rulesScope = context.newObject(scope); rulesScope.setPrototype(scope); rulesScope.setParentScope(null); Context.exit(); } finally { scriptLock.readLock().unlock(); } return new JsRunner(rulesScope); } @Inject  JsRunnerProvider(RulesCurator rulesCurator, Provider<JsRunnerRequestCache> cacheProvider); void compileRules(); void compileRules(boolean forceRefresh); JsRunner get(); String getRulesVersion(); RulesSourceEnum getRulesSource();  }
@Test public void valueGetsCachedMockedTest() { when(cacheProvider.get()).thenReturn(mockedCache); when(mockedCache.getUpdated()).thenReturn(null).thenReturn(time1); provider.get(); verify(mockedCache).getUpdated(); verify(mockedCache).setUpdated(time1); provider.get(); verify(mockedCache, times(2)).getUpdated(); verifyNoMoreInteractions(mockedCache); }
@Test public void rulesCuratorIsNotHitMultipleTimes() { when(cacheProvider.get()).thenReturn(new JsRunnerRequestCache()); provider.get(); provider.get(); provider.get(); provider.get(); provider.get(); verify(rulesCurator, times(2)).getUpdated(); }
@Test public void cacheInvalidationOnNewRequest() { when(cacheProvider.get()).thenReturn(new JsRunnerRequestCache()); provider.get(); provider.get(); provider.get(); JsRunnerRequestCache newCache = new JsRunnerRequestCache(); when(cacheProvider.get()).thenReturn(newCache); Assert.assertEquals(null, newCache.getUpdated()); provider.get(); Assert.assertEquals(time1, newCache.getUpdated()); provider.get(); verify(rulesCurator, times(3)).getUpdated(); }