Merge branch 'stable-2.16' into stable-3.0

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

Change-Id: Id40baca92acc9fba8656630f725d55e5fbb6662b
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 b7841f5..0074075 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;
@@ -439,37 +440,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().getRefName();
+
+    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().getRefName();
+    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) {
@@ -498,30 +487,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));
@@ -566,6 +572,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();
   }