test_class.file: oauth-filter/src/test/java/se/curity/examples/oauth/jwt/TimeBasedCacheTest.java
test_class.identifier: TimeBasedCacheTest
test_case.body: @Test
      public void doesNotReloadCacheWithinTimeLimit()
              throws Exception
      {
          // fake clock returns current time, then the same thing again,
          // then 4 seconds later, then 8 seconds later.
          Clock fakeClock = mock(Clock.class);
          Instant now = Instant.now();
          when(fakeClock.millis()).thenCallRealMethod();
          when(fakeClock.instant()).thenReturn(
                  now, // get("0") - first map loading
                  now, // cache asks when the latest loading happened
                  now.plus(Duration.ofSeconds(4)), // get("1") - no reloading
                  // get("0") - should not even ask about the time as entry was found
                  now.plus(Duration.ofSeconds(4)), // get("1") - no reloading
                  now.plus(Duration.ofSeconds(8))); // get("1") - reload
  
          @SuppressWarnings("unchecked")
          Supplier<ImmutableMap> fakeSupplier = mock(Supplier.class);
          when(fakeSupplier.get()).thenReturn(
                  ImmutableMap.of("0", 0),
                  ImmutableMap.of("1", 1));
  
          // the map will always contain a single entry with the number of reloads as in
          // ("reloads" -> reloads)
          AtomicInteger reloads = new AtomicInteger(0);
  
          TimeBasedCache<String, Integer> cache = new TimeBasedCache<>(Duration.ofSeconds(5),
                  () -> ImmutableMap.copyOf(ImmutableMap.of(Integer.toString(reloads.get()),
                          reloads.getAndIncrement())),
                  ImmutableMap.of(), fakeClock);
  
          // should have only ("0" -> 0) in the map in the beginning
          assertNotNull(cache.get("0"));
          assertNull(cache.get("1"));
  
          // second time we try, the map should not reload
          assertNotNull(cache.get("0"));
          assertNull(cache.get("1"));
  
          // when the first reload happens, the cache contains ("1" -> 1)
          assertNotNull(cache.get("1"));
          assertNull(cache.get("0"));
      }
test_case.class_method_signature: TimeBasedCacheTest.doesNotReloadCacheWithinTimeLimit()
test_case.full_signature: @Test public void doesNotReloadCacheWithinTimeLimit()
test_case.identifier: doesNotReloadCacheWithinTimeLimit
test_case.invocations: mock now thenCallRealMethod when millis thenReturn when instant plus ofSeconds plus ofSeconds plus ofSeconds mock thenReturn when get of of ofSeconds copyOf of toString get getAndIncrement of assertNotNull get assertNull get assertNotNull get assertNull get assertNotNull get assertNull get
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void doesNotReloadCacheWithinTimeLimit()
focal_class.declarator: _cacheLock = new Object()
focal_class.modifier: private final
focal_class.original_string: private final Object _cacheLock = new Object();
focal_class.type: Object
focal_class.var_name: _cacheLock
focal_class.declarator: _cache
focal_class.modifier: private volatile
focal_class.original_string: private volatile ImmutableMap<K, V> _cache;
focal_class.type: ImmutableMap<K, V>
focal_class.var_name: _cache
focal_class.declarator: _nextLoadingEarliestTime = Instant.MIN
focal_class.modifier: private volatile
focal_class.original_string: private volatile Instant _nextLoadingEarliestTime = Instant.MIN;
focal_class.type: Instant
focal_class.var_name: _nextLoadingEarliestTime
focal_class.declarator: _lastLoading = Optional.empty()
focal_class.modifier: private volatile
focal_class.original_string: private volatile Optional<Instant> _lastLoading = Optional.empty();
focal_class.type: Optional<Instant>
focal_class.var_name: _lastLoading
focal_class.declarator: _minTimeBetweenReloads
focal_class.modifier: private final
focal_class.original_string: private final Duration _minTimeBetweenReloads;
focal_class.type: Duration
focal_class.var_name: _minTimeBetweenReloads
focal_class.declarator: _valuesSupplier
focal_class.modifier: private final
focal_class.original_string: private final Supplier<ImmutableMap<K, V>> _valuesSupplier;
focal_class.type: Supplier<ImmutableMap<K, V>>
focal_class.var_name: _valuesSupplier
focal_class.declarator: _clock
focal_class.modifier: private final
focal_class.original_string: private final Clock _clock;
focal_class.type: Clock
focal_class.var_name: _clock
focal_class.file: oauth-filter/src/main/java/se/curity/examples/oauth/jwt/TimeBasedCache.java
focal_class.identifier: TimeBasedCache
focal_class.class_method_signature: TimeBasedCache.TimeBasedCache(Duration minTimeBetweenReloads, Supplier<ImmutableMap<K, V>> valuesSupplier)
focal_class.full_signature: public  TimeBasedCache(Duration minTimeBetweenReloads, Supplier<ImmutableMap<K, V>> valuesSupplier)
focal_class.identifier: TimeBasedCache
focal_class.modifiers: public
focal_class.parameters: (Duration minTimeBetweenReloads, Supplier<ImmutableMap<K, V>> valuesSupplier)
focal_class.signature:  TimeBasedCache(Duration minTimeBetweenReloads, Supplier<ImmutableMap<K, V>> valuesSupplier)
focal_class.class_method_signature: TimeBasedCache.TimeBasedCache(Duration minTimeBetweenReloads,
                            Supplier<ImmutableMap<K, V>> valuesSupplier,
                            ImmutableMap<K, V> initialCache,
                            Clock clock)
focal_class.full_signature: public  TimeBasedCache(Duration minTimeBetweenReloads,
                            Supplier<ImmutableMap<K, V>> valuesSupplier,
                            ImmutableMap<K, V> initialCache,
                            Clock clock)
focal_class.identifier: TimeBasedCache
focal_class.modifiers: public
focal_class.parameters: (Duration minTimeBetweenReloads,
                            Supplier<ImmutableMap<K, V>> valuesSupplier,
                            ImmutableMap<K, V> initialCache,
                            Clock clock)
focal_class.signature:  TimeBasedCache(Duration minTimeBetweenReloads,
                            Supplier<ImmutableMap<K, V>> valuesSupplier,
                            ImmutableMap<K, V> initialCache,
                            Clock clock)
focal_class.class_method_signature: TimeBasedCache.mayReload()
focal_class.full_signature: private boolean mayReload()
focal_class.identifier: mayReload
focal_class.modifiers: private
focal_class.parameters: ()
focal_class.return: boolean
focal_class.signature: boolean mayReload()
focal_class.class_method_signature: TimeBasedCache.get(K key)
focal_class.full_signature: public V get(K key)
focal_class.identifier: get
focal_class.modifiers: public
focal_class.parameters: (K key)
focal_class.return: V
focal_class.signature: V get(K key)
focal_class.class_method_signature: TimeBasedCache.clear()
focal_class.full_signature: public void clear()
focal_class.identifier: clear
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void clear()
focal_class.class_method_signature: TimeBasedCache.reloadCache()
focal_class.full_signature: private void reloadCache()
focal_class.identifier: reloadCache
focal_class.modifiers: private
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void reloadCache()
focal_class.class_method_signature: TimeBasedCache.getLastReloadInstant()
focal_class.full_signature: public Optional<Instant> getLastReloadInstant()
focal_class.identifier: getLastReloadInstant
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: Optional<Instant>
focal_class.signature: Optional<Instant> getLastReloadInstant()
focal_class.class_method_signature: TimeBasedCache.getMinTimeBetweenReloads()
focal_class.full_signature: public Duration getMinTimeBetweenReloads()
focal_class.identifier: getMinTimeBetweenReloads
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: Duration
focal_class.signature: Duration getMinTimeBetweenReloads()
focal_method.body: public V get(K key)
      {
          // optimistically try to get the key without locking
          V value = _cache.get(key);
          if (value == null && mayReload())
          {
              reloadCache();
              value = _cache.get(key);
          }
  
          return value;
      }
focal_method.class_method_signature: TimeBasedCache.get(K key)
focal_method.full_signature: public V get(K key)
focal_method.identifier: get
focal_method.invocations: get mayReload reloadCache get
focal_method.modifiers: public
focal_method.parameters: (K key)
focal_method.return: V
focal_method.signature: V get(K key)