test: fix concurrency bug in SSLConfigurationReloaderTests

The SSLConfigurationReloaderTests rarely failed during some local runs. This turned out to be due to
signaling that the reload happened before we actually reloaded. This led to a race condition where we
attempted to validate the config was reloaded properly and actually reloading. This change fixes the
ordering of operations and uses a CountDownLatch instead of a AtomicInteger and awaitBusy.

Original commit: elastic/x-pack-elasticsearch@9615f225d6
This commit is contained in:
jaymode 2016-08-12 08:50:53 -04:00
parent a3e7536205
commit 72f580c82d
1 changed files with 5 additions and 5 deletions

View File

@ -35,7 +35,7 @@ import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.CountDownLatch;
import java.util.function.BiFunction;
import static org.hamcrest.Matchers.containsString;
@ -469,14 +469,14 @@ public class SSLConfigurationReloaderTests extends ESTestCase {
BiFunction<X509ExtendedTrustManager, SSLConfiguration, Void> trustManagerPostChecks)
throws Exception {
final AtomicInteger counter = new AtomicInteger(0);
final CountDownLatch reloadLatch = new CountDownLatch(1);
final SSLService sslService = new SSLService(settings, env);
final SSLConfiguration config = sslService.sslConfiguration(Settings.EMPTY);
new SSLConfigurationReloader(settings, env, sslService, resourceWatcherService) {
@Override
void reloadSSLContext(SSLConfiguration configuration) {
counter.incrementAndGet();
super.reloadSSLContext(configuration);
reloadLatch.countDown();
}
};
@ -504,11 +504,11 @@ public class SSLConfigurationReloaderTests extends ESTestCase {
trustManagerPreChecks.apply(trustManager, config);
}
assertEquals("nothing should have called get", 0, counter.get());
assertEquals("nothing should have called reload", 1, reloadLatch.getCount());
// modify
modificationFunction.run();
assertTrue(awaitBusy(() -> counter.get() > 0));
reloadLatch.await();
// check key manager
if (checkKeys) {