Skip to content

Commit 873ad84

Browse files
committed
Make this test run again
1 parent f71a1f9 commit 873ad84

File tree

1 file changed

+19
-45
lines changed

1 file changed

+19
-45
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.elasticsearch.test.ESTestCase;
4444
import org.elasticsearch.test.IndexSettingsModule;
4545
import org.elasticsearch.test.MockLog;
46-
import org.mockito.Mockito;
4746

4847
import java.io.Closeable;
4948
import java.io.IOException;
@@ -72,7 +71,6 @@
7271
import static org.hamcrest.Matchers.notNullValue;
7372
import static org.hamcrest.Matchers.nullValue;
7473
import static org.hamcrest.Matchers.sameInstance;
75-
import static org.mockito.ArgumentMatchers.any;
7674
import static org.mockito.Mockito.mock;
7775
import static org.mockito.Mockito.when;
7876

@@ -377,61 +375,37 @@ public void testCacheUnderConcurrentAccess() throws Exception {
377375
cache.verifyInternalConsistencyKeysToCache();
378376
}
379377

380-
@AwaitsFix(bugUrl = "todo")
381-
public void testCleanupWorksWhenIndexIsClosing() throws Exception {
378+
public void testCleanupWorksWhenIndexIsClosed() throws Exception {
382379
// Enough to hold slightly more than 1 bit-set in the cache
383380
final long maxCacheBytes = EXPECTED_BYTES_PER_BIT_SET + EXPECTED_BYTES_PER_BIT_SET / 2;
384381
final Settings settings = Settings.builder()
385382
.put(DocumentSubsetBitsetCache.CACHE_SIZE_SETTING.getKey(), maxCacheBytes + "b")
386383
.build();
387-
final ExecutorService threads = Executors.newFixedThreadPool(1);
388-
final ExecutorService cleanupExecutor = Mockito.mock(ExecutorService.class);
389-
final CountDownLatch cleanupReadyLatch = new CountDownLatch(1);
390-
final CountDownLatch cleanupCompleteLatch = new CountDownLatch(1);
391-
final CountDownLatch indexCloseLatch = new CountDownLatch(1);
392-
final AtomicReference<Throwable> cleanupException = new AtomicReference<>();
393-
when(cleanupExecutor.submit(any(Runnable.class))).thenAnswer(inv -> {
394-
final Runnable runnable = (Runnable) inv.getArguments()[0];
395-
return threads.submit(() -> {
396-
try {
397-
cleanupReadyLatch.countDown();
398-
assertTrue("index close did not completed in expected time", indexCloseLatch.await(1, TimeUnit.SECONDS));
399-
runnable.run();
400-
} catch (Throwable e) {
401-
logger.warn("caught error in cleanup thread", e);
402-
cleanupException.compareAndSet(null, e);
403-
} finally {
404-
cleanupCompleteLatch.countDown();
405-
}
406-
return null;
407-
});
408-
});
409384

410385
final DocumentSubsetBitsetCache cache = new DocumentSubsetBitsetCache(settings);
411386
assertThat(cache.entryCount(), equalTo(0));
412387
assertThat(cache.ramBytesUsed(), equalTo(0L));
413388

414-
try {
415-
runTestOnIndex((searchExecutionContext, leafContext) -> {
416-
final Query query1 = QueryBuilders.termQuery("field-1", "value-1").toQuery(searchExecutionContext);
417-
final BitSet bitSet1 = cache.getBitSet(query1, leafContext);
418-
assertThat(bitSet1, notNullValue());
389+
runTestOnIndex((searchExecutionContext, leafContext) -> {
390+
final Query query1 = QueryBuilders.termQuery("field-1", "value-1").toQuery(searchExecutionContext);
391+
final BitSet bitSet1 = cache.getBitSet(query1, leafContext);
392+
assertThat(bitSet1, notNullValue());
393+
cache.verifyInternalConsistency();
419394

420-
// Second query should trigger a cache eviction
421-
final Query query2 = QueryBuilders.termQuery("field-2", "value-2").toQuery(searchExecutionContext);
422-
final BitSet bitSet2 = cache.getBitSet(query2, leafContext);
423-
assertThat(bitSet2, notNullValue());
395+
// Second query should trigger a cache eviction
396+
final Query query2 = QueryBuilders.termQuery("field-2", "value-2").toQuery(searchExecutionContext);
397+
final BitSet bitSet2 = cache.getBitSet(query2, leafContext);
398+
assertThat(bitSet2, notNullValue());
399+
cache.verifyInternalConsistency();
424400

425-
final IndexReader.CacheKey indexKey = leafContext.reader().getCoreCacheHelper().getKey();
426-
assertTrue("cleanup did not trigger in expected time", cleanupReadyLatch.await(1, TimeUnit.SECONDS));
427-
cache.onClose(indexKey);
428-
indexCloseLatch.countDown();
429-
assertTrue("cleanup did not complete in expected time", cleanupCompleteLatch.await(1, TimeUnit.SECONDS));
430-
assertThat("caught error in cleanup thread: " + cleanupException.get(), cleanupException.get(), nullValue());
431-
});
432-
} finally {
433-
threads.shutdown();
434-
}
401+
final IndexReader.CacheKey indexKey = leafContext.reader().getCoreCacheHelper().getKey();
402+
cache.onClose(indexKey);
403+
cache.verifyInternalConsistency();
404+
405+
// closing an index results in the associated entries being removed from the cache (at least when single threaded)
406+
assertThat(cache.entryCount(), equalTo(0));
407+
assertThat(cache.ramBytesUsed(), equalTo(0L));
408+
});
435409
}
436410

437411
public void testCacheIsPerIndex() throws Exception {

0 commit comments

Comments
 (0)