From 5c157fcd7bc87de8d4c62f39c3953c6a7f30300e Mon Sep 17 00:00:00 2001 From: Tim Vernum Date: Fri, 23 Jun 2017 11:45:09 +1000 Subject: [PATCH] [TESTS] Fix potential for negative sleep (elastic/x-pack-elasticsearch#1818) Original commit: elastic/x-pack-elasticsearch@cb19908a61d4e9bff863a63891a1cbb13034b4e7 --- .../CachingUsernamePasswordRealmTests.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/plugin/src/test/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealmTests.java b/plugin/src/test/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealmTests.java index 9be5577c99b..18c1080e143 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealmTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/security/authc/support/CachingUsernamePasswordRealmTests.java @@ -5,26 +5,23 @@ */ package org.elasticsearch.xpack.security.authc.support; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; + import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.common.settings.SecureString; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.security.authc.Realm; import org.elasticsearch.xpack.security.authc.RealmConfig; import org.elasticsearch.xpack.security.user.User; -import org.elasticsearch.test.ESTestCase; import org.junit.Before; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - import static java.util.Collections.emptyMap; import static org.hamcrest.Matchers.arrayContaining; import static org.hamcrest.Matchers.containsString; @@ -263,19 +260,19 @@ public class CachingUsernamePasswordRealmTests extends ESTestCase { assertThat(realm.authInvocationCounter.intValue(), is(1)); // After 100 ms (from the original start time), authenticate (read from cache). We don't care about the result - Thread.sleep(start + 100 - System.currentTimeMillis()); + sleepUntil(start + 100); future = new PlainActionFuture<>(); realm.authenticate(authToken, future); future.actionGet(); // After 200 ms (from the original start time), authenticate (read from cache). We don't care about the result - Thread.sleep(start + 200 - System.currentTimeMillis()); + sleepUntil(start + 200); future = new PlainActionFuture<>(); realm.authenticate(authToken, future); future.actionGet(); // After 300 ms (from the original start time), authenticate again. The cache entry should have expired (despite the previous reads) - Thread.sleep(start + 300 - System.currentTimeMillis()); + sleepUntil(start + 300); future = new PlainActionFuture<>(); realm.authenticate(authToken, future); final User user2 = future.actionGet(); @@ -285,6 +282,13 @@ public class CachingUsernamePasswordRealmTests extends ESTestCase { assertThat(realm.authInvocationCounter.intValue(), greaterThan(1)); } + private void sleepUntil(long until) throws InterruptedException { + final long sleep = until - System.currentTimeMillis(); + if (sleep > 0) { + Thread.sleep(sleep); + } + } + public void testAuthenticateContract() throws Exception { Realm realm = new FailingAuthenticationRealm(Settings.EMPTY, globalSettings); PlainActionFuture future = new PlainActionFuture<>();