[TESTS] Fix potential for negative sleep (elastic/x-pack-elasticsearch#1818)

Original commit: elastic/x-pack-elasticsearch@cb19908a61
This commit is contained in:
Tim Vernum 2017-06-23 11:45:09 +10:00 committed by GitHub
parent 855c63dbc7
commit 5c157fcd7b
1 changed files with 16 additions and 12 deletions

View File

@ -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<User> future = new PlainActionFuture<>();