@Override public boolean hasBeenInUse() { return cachedSupplier.get().hasBeenInUse(); }
private void fitListToCacheSize() { List<String> itemsToRemove = new ArrayList<>(); int maxIterations = config.getJgitCacheOverflowCleanupSize(); Object[] entries = this.entrySet().toArray(); for (int i = this.size() - 1; (i >= 0 && (this.size() - i < maxIterations)); i--) { Map.Entry<String, Supplier<JGitFileSystem>> entry = (Map.Entry) entries[i]; JGitFileSystem targetFS = (JGitFileSystem) ((MemoizedFileSystemsSupplier) entry.getValue()).get(); if (!targetFS.hasBeenInUse()) { itemsToRemove.add(entry.getKey()); } } itemsToRemove.forEach(item -> this.remove(item)); }
@Override public boolean removeEldestEntry(Map.Entry eldest) { if (removeEldestEntryIterations > config.getJgitRemoveEldestEntryIterations()) { removeEldestEntryIterations = 0; return false; } if (size() > config.getJgitFileSystemsInstancesCache()) { JGitFileSystem targetFS = (JGitFileSystem) ((MemoizedFileSystemsSupplier) eldest.getValue()).get(); if (targetFS.hasBeenInUse()) { removeEldestEntryIterations++; this.remove(eldest.getKey()); this.put((String) eldest.getKey(), (MemoizedFileSystemsSupplier) eldest.getValue()); return false; } else { return true; } } return false; } });
when(fs1.hasBeenInUse()).thenReturn(true); Supplier<JGitFileSystem> fsSupplier1 = getSupplierSpy(fs1); cache.addSupplier("fs1", when(fs2.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs2", fs2Supplier); when(fs3.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs3", fs3Supplier); when(fs4.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs4", fs3Supplier); assertTrue(cache.memoizedSuppliers.containsKey("fs4")); when(fs1.hasBeenInUse()).thenReturn(false); when(fs2.hasBeenInUse()).thenReturn(false); when(fs4.hasBeenInUse()).thenReturn(false); when(fs5.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs5", fs5Supplier);
when(fs1.hasBeenInUse()).thenReturn(true); Supplier<JGitFileSystem> fsSupplier1 = getSupplierSpy(fs1); cache.addSupplier("fs1", when(fs2.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs2", fs2Supplier); when(fs3.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs3", fs3Supplier); when(fs4.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs4", fs3Supplier); assertTrue(cache.memoizedSuppliers.containsKey("fs4")); when(fs1.hasBeenInUse()).thenReturn(false); when(fs2.hasBeenInUse()).thenReturn(false); when(fs4.hasBeenInUse()).thenReturn(false); when(fs5.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs5", fs5Supplier);
@Test public void removeEldestEntryTest() { JGitFileSystemProviderConfiguration config = setupConfigMock(); //no fs is on use setupCacheToTestOrder(config, "fs1", "fs2", "fs3"); assertFalse(cache.memoizedSuppliers.containsKey("fs1")); //fs1 is on use cache = new JGitFileSystemsCache(config); JGitFileSystem fs1 = mock(JGitFileSystem.class); when(fs1.hasBeenInUse()).thenReturn(true); Supplier<JGitFileSystem> fsSupplier1 = getSupplierSpy(fs1); cache.addSupplier("fs1", fsSupplier1); JGitFileSystem fs2 = mock(JGitFileSystem.class); Supplier<JGitFileSystem> fsSupplier2 = getSupplierSpy(fs2); when(fs2.hasBeenInUse()).thenReturn(false); cache.addSupplier("fs2", fsSupplier2); JGitFileSystem fs3 = mock(JGitFileSystem.class); Supplier<JGitFileSystem> fsSupplier = getSupplierSpy(fs3); when(fs3.hasBeenInUse()).thenReturn(false); cache.addSupplier("fs3", fsSupplier); assertTrue(cache.memoizedSuppliers.containsKey("fs1")); assertTrue(cache.memoizedSuppliers.containsKey("fs3")); assertFalse(cache.memoizedSuppliers.containsKey("fs2")); }
@Test public void removeEldestEntryTest() { JGitFileSystemProviderConfiguration config = setupConfigMock(); //no fs is on use setupCacheToTestOrder(config, "fs1", "fs2", "fs3"); assertFalse(cache.memoizedSuppliers.containsKey("fs1")); //fs1 is on use cache = new JGitFileSystemsCache(config); JGitFileSystem fs1 = mock(JGitFileSystem.class); when(fs1.hasBeenInUse()).thenReturn(true); Supplier<JGitFileSystem> fsSupplier1 = getSupplierSpy(fs1); cache.addSupplier("fs1", fsSupplier1); JGitFileSystem fs2 = mock(JGitFileSystem.class); Supplier<JGitFileSystem> fsSupplier2 = getSupplierSpy(fs2); when(fs2.hasBeenInUse()).thenReturn(false); cache.addSupplier("fs2", fsSupplier2); JGitFileSystem fs3 = mock(JGitFileSystem.class); Supplier<JGitFileSystem> fsSupplier = getSupplierSpy(fs3); when(fs3.hasBeenInUse()).thenReturn(false); cache.addSupplier("fs3", fsSupplier); assertTrue(cache.memoizedSuppliers.containsKey("fs1")); assertTrue(cache.memoizedSuppliers.containsKey("fs3")); assertFalse(cache.memoizedSuppliers.containsKey("fs2")); }
when(fs2.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs2", fs2Supplier); when(fs3.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs5", fs3Supplier);
when(fs2.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs2", fs2Supplier); when(fs3.hasBeenInUse()).thenReturn(true); cache.addSupplier("fs5", fs3Supplier);