Skip to content

Instantly share code, notes, and snippets.

@rjernst
Created April 19, 2015 01:08
Show Gist options
  • Save rjernst/4aca1452913e4066414f to your computer and use it in GitHub Desktop.
Save rjernst/4aca1452913e4066414f to your computer and use it in GitHub Desktop.
// -----------------------------------------------------------------
// Suite and test case setup/cleanup.
// -----------------------------------------------------------------
// TODO: Parent/child and other things does not work with the query cache
// We must disable query cache for both suite and test to override lucene, but LTC resets it after the suite
@BeforeClass
public static void disableQueryCacheSuite() {
IndexSearcher.setDefaultQueryCache(null);
}
@Before
public void disableQueryCache() {
IndexSearcher.setDefaultQueryCache(null);
}
// setup mock filesystems for this test run. we change PathUtils
// so that all accesses are plumbed thru any mock wrappers
@BeforeClass
public static void setFileSystem() throws Exception {
Field field = PathUtils.class.getDeclaredField("DEFAULT");
field.setAccessible(true);
field.set(null, LuceneTestCase.getBaseTempDirForTestClass().getFileSystem());
}
@AfterClass
public static void restoreFileSystem() throws Exception {
Field field1 = PathUtils.class.getDeclaredField("ACTUAL_DEFAULT");
field1.setAccessible(true);
Field field2 = PathUtils.class.getDeclaredField("DEFAULT");
field2.setAccessible(true);
field2.set(null, field1.get(null));
}
// setup a default exception handler which knows when and how to print a stacktrace
private static Thread.UncaughtExceptionHandler defaultHandler;
@BeforeClass
public static void setDefaultExceptionHandler() throws Exception {
defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new ElasticsearchUncaughtExceptionHandler(defaultHandler));
}
@AfterClass
public static void restoreDefaultExceptionHandler() throws Exception {
Thread.setDefaultUncaughtExceptionHandler(defaultHandler);
}
// randomize content type for request builders
@BeforeClass
public static void setContentType() throws Exception {
Requests.CONTENT_TYPE = randomFrom(XContentType.values());
Requests.INDEX_CONTENT_TYPE = randomFrom(XContentType.values());
}
@AfterClass
public static void restoreContentType() {
Requests.CONTENT_TYPE = XContentType.SMILE;
Requests.INDEX_CONTENT_TYPE = XContentType.JSON;
}
// randomize and override the number of cpus so tests reproduce regardless of real number of cpus
@BeforeClass
public static void setProcessors() {
int numCpu = TestUtil.nextInt(random(), 1, 4);
System.setProperty(EsExecutors.DEFAULT_SYSPROP, Integer.toString(numCpu));
assertEquals(numCpu, EsExecutors.boundedNumberOfProcessors(ImmutableSettings.EMPTY));
}
@AfterClass
public static void restoreProcessors() {
System.clearProperty(EsExecutors.DEFAULT_SYSPROP);
}
@BeforeClass
public static void setAfterSuiteAssertions() throws Exception {
closeAfterSuite(new Closeable() {
@Override
public void close() throws IOException {
assertAllFilesClosed();
}
});
closeAfterSuite(new Closeable() {
@Override
public void close() throws IOException {
assertAllSearchersClosed();
}
});
}
@After
public void ensureCleanedUp() throws Exception {
MockPageCacheRecycler.ensureAllPagesAreReleased();
MockBigArrays.ensureAllArraysAreReleased();
// field cache should NEVER get loaded.
String[] entries = UninvertingReader.getUninvertedStats();
assertEquals("fieldcache must never be used, got=" + Arrays.toString(entries), 0, entries.length);
}
@After
public void ensureAllSearchContextsReleased() throws Exception {
assertBusy(new Runnable() {
@Override
public void run() {
MockSearchService.assertNoInFLightContext();
}
});
}
/** MockFSDirectoryService sets this: */
public static boolean checkIndexFailed;
@Before
public void resetCheckIndexStatus() throws Exception {
checkIndexFailed = false;
}
@After
public void ensureCheckIndexPassed() throws Exception {
assertFalse("at least one shard failed CheckIndex", checkIndexFailed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment