Merge branch 'stable-3.0' into stable-3.1

* stable-3.0:
  Improve readability of shouldFirePendingOnlyToStoredUri test
  Fix flakiness in ReplicationIT for pending events firing

Change-Id: I58ce5a03d7b1094ef859c4cd2bd08a3ead00ba30
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 60f55ac..31cd75d 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationIT.java
@@ -46,6 +46,7 @@
 import java.util.Optional;
 import java.util.function.Supplier;
 import java.util.regex.Pattern;
+import java.util.stream.Stream;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
@@ -455,37 +456,25 @@
     Project.NameKey target2 = createTestProject(project + suffix2);
     String remote1 = "foo1";
     String remote2 = "foo2";
+    setReplicationDestination(remote1, suffix1, ALL_PROJECTS, Integer.MAX_VALUE, false);
+    setReplicationDestination(remote2, suffix2, ALL_PROJECTS, Integer.MAX_VALUE, false);
+    reloadConfig();
+
+    String changeRef = createChange().getPatchSet().refName();
+
+    tasksStorage.disableDeleteForTesting(false);
+    changeReplicationTasksForRemote(changeRef, remote1).forEach(tasksStorage::delete);
+    tasksStorage.disableDeleteForTesting(true);
+
     setReplicationDestination(remote1, suffix1, ALL_PROJECTS);
     setReplicationDestination(remote2, suffix2, ALL_PROJECTS);
     reloadConfig();
 
-    Result pushResult = createChange();
-    String sourceRef = pushResult.getPatchSet().refName();
+    assertThat(changeReplicationTasksForRemote(changeRef, remote2).count()).isEqualTo(1);
+    assertThat(changeReplicationTasksForRemote(changeRef, remote1).count()).isEqualTo(0);
 
-    replicationQueueStop();
-
-    tasksStorage.disableDeleteForTesting(false);
-    listReplicationTasks("refs/changes/\\d*/\\d*/\\d*").stream()
-        .filter(task -> remote1.equals(task.remote))
-        .forEach(u -> tasksStorage.delete(u));
-    tasksStorage.disableDeleteForTesting(true);
-
-    assertThat(
-            listReplicationTasks("refs/changes/\\d*/\\d*/\\d*").stream()
-                .filter(task -> remote2.equals(task.remote))
-                .collect(toList()))
-        .hasSize(1);
-
-    assertThat(
-            listReplicationTasks("refs/changes/\\d*/\\d*/\\d*").stream()
-                .filter(task -> remote1.equals(task.remote))
-                .collect(toList()))
-        .hasSize(0);
-
-    replicationQueueStart();
-
-    assertThat(isPushCompleted(target2, sourceRef, TEST_TIMEOUT)).isEqualTo(true);
-    assertThat(isPushCompleted(target1, sourceRef, TEST_TIMEOUT)).isEqualTo(false);
+    assertThat(isPushCompleted(target2, changeRef, TEST_TIMEOUT)).isEqualTo(true);
+    assertThat(isPushCompleted(target1, changeRef, TEST_TIMEOUT)).isEqualTo(false);
   }
 
   public boolean isPushCompleted(Project.NameKey project, String ref, Duration timeOut) {
@@ -514,30 +503,47 @@
 
   private void setReplicationDestination(
       String remoteName, String replicaSuffix, Optional<String> project) throws IOException {
-    setReplicationDestination(remoteName, Arrays.asList(replicaSuffix), project, false);
+    setReplicationDestination(
+        remoteName, Arrays.asList(replicaSuffix), project, TEST_REPLICATION_DELAY, false);
   }
 
   private void setReplicationDestination(
       String remoteName, String replicaSuffix, Optional<String> project, boolean mirror)
       throws IOException {
-    setReplicationDestination(remoteName, Arrays.asList(replicaSuffix), project, mirror);
+    setReplicationDestination(
+        remoteName, Arrays.asList(replicaSuffix), project, TEST_REPLICATION_DELAY, mirror);
+  }
+
+  private void setReplicationDestination(
+      String remoteName,
+      String replicaSuffix,
+      Optional<String> project,
+      int replicationDelay,
+      boolean mirror)
+      throws IOException {
+    setReplicationDestination(
+        remoteName, Arrays.asList(replicaSuffix), project, replicationDelay, mirror);
   }
 
   private void setReplicationDestination(
       String remoteName, List<String> replicaSuffixes, Optional<String> project)
       throws IOException {
-    setReplicationDestination(remoteName, replicaSuffixes, project, false);
+    setReplicationDestination(remoteName, replicaSuffixes, project, TEST_REPLICATION_DELAY, false);
   }
 
   private void setReplicationDestination(
-      String remoteName, List<String> replicaSuffixes, Optional<String> project, boolean mirror)
+      String remoteName,
+      List<String> replicaSuffixes,
+      Optional<String> project,
+      int replicationDelay,
+      boolean mirror)
       throws IOException {
     List<String> replicaUrls =
         replicaSuffixes.stream()
             .map(suffix -> gitPath.resolve("${name}" + suffix + ".git").toString())
             .collect(toList());
     config.setStringList("remote", remoteName, "url", replicaUrls);
-    config.setInt("remote", remoteName, "replicationDelay", TEST_REPLICATION_DELAY);
+    config.setInt("remote", remoteName, "replicationDelay", replicationDelay);
     config.setInt("remote", remoteName, "replicationRetry", TEST_REPLICATION_RETRY);
     config.setBoolean("remote", remoteName, "mirror", mirror);
     project.ifPresent(prj -> config.setString("remote", remoteName, "projects", prj));
@@ -583,6 +589,13 @@
     return plugin.getSysInjector().getInstance(classObj);
   }
 
+  private Stream<ReplicateRefUpdate> changeReplicationTasksForRemote(
+      String changeRef, String remote) {
+    return tasksStorage.list().stream()
+        .filter(task -> changeRef.equals(task.ref))
+        .filter(task -> remote.equals(task.remote));
+  }
+
   private Project.NameKey createTestProject(String name) throws Exception {
     return projectOperations.newProject().name(name).create();
   }