ReplicationIT: Split waitUntil to a separate class and fix it The waitUntil method does not fail the test if the expected condition is not satisfied within the timeout; it just returns. Split it out to a separate class, to make it easier to test, fix it, and add tests. Change-Id: I6cbd6801bcfd07869dd56d3ab6b54c5ac7c8a691
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java index 745375c..45d8506 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
@@ -17,7 +17,6 @@ import static com.google.common.truth.Truth.assertThat; import static java.util.stream.Collectors.toList; -import com.google.common.base.Stopwatch; import com.google.common.flogger.FluentLogger; import com.google.gerrit.acceptance.LightweightPluginDaemonTest; import com.google.gerrit.acceptance.PushOneCommit.Result; @@ -43,7 +42,6 @@ import java.util.Arrays; import java.util.List; import java.util.Optional; -import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import java.util.regex.Pattern; import org.eclipse.jgit.lib.Constants; @@ -257,10 +255,7 @@ } private void waitUntil(Supplier<Boolean> waitCondition) throws InterruptedException { - Stopwatch stopwatch = Stopwatch.createStarted(); - while (!waitCondition.get() && stopwatch.elapsed().compareTo(TEST_TIMEOUT) < 0) { - TimeUnit.SECONDS.sleep(1); - } + WaitUtil.waitUntil(waitCondition, TEST_TIMEOUT); } private void reloadConfig() {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/WaitUtil.java b/src/test/java/com/googlesource/gerrit/plugins/replication/WaitUtil.java new file mode 100644 index 0000000..8bd8147 --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/WaitUtil.java
@@ -0,0 +1,34 @@ +// Copyright (C) 2019 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.replication; + +import static java.util.concurrent.TimeUnit.SECONDS; + +import com.google.common.base.Stopwatch; +import java.time.Duration; +import java.util.function.Supplier; + +public class WaitUtil { + public static void waitUntil(Supplier<Boolean> waitCondition, Duration timeout) + throws InterruptedException { + Stopwatch stopwatch = Stopwatch.createStarted(); + while (!waitCondition.get()) { + if (stopwatch.elapsed().compareTo(timeout) > 0) { + throw new InterruptedException(); + } + SECONDS.sleep(1); + } + } +}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/WaitUtilTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/WaitUtilTest.java new file mode 100644 index 0000000..0ccb0af --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/WaitUtilTest.java
@@ -0,0 +1,40 @@ +// Copyright (C) 2019 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.replication; + +import static com.google.gerrit.testing.GerritJUnit.assertThrows; +import static com.googlesource.gerrit.plugins.replication.WaitUtil.waitUntil; + +import java.time.Duration; +import org.junit.Test; + +public class WaitUtilTest { + + @Test + public void shouldFailWhenConditionNotMetWithinTimeout() throws Exception { + assertThrows( + InterruptedException.class, + () -> waitUntil(() -> returnTrue() == false, Duration.ofSeconds(1))); + } + + @Test + public void shouldNotFailWhenConditionIsMetWithinTimeout() throws Exception { + waitUntil(() -> returnTrue() == true, Duration.ofSeconds(1)); + } + + private static boolean returnTrue() { + return true; + } +}