ReplicationTasksStorage: Add multi-primary unit tests

These tests examine the replication scenarios under
multi-primary setup making use of the api calls present
in ReplicationTasksStorage class similarly as done in
single primary setup.

These tests ensure that the replication compatability in
multi-primary setup is not broken.

Change-Id: Ib2d0017c4d2ac3f4cfc7262b68b09a3a357e1337
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageMPTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageMPTest.java
new file mode 100644
index 0000000..75912fa
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageMPTest.java
@@ -0,0 +1,195 @@
+// Copyright (C) 2020 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.googlesource.gerrit.plugins.replication.ReplicationTasksStorageTest.assertContainsExactly;
+import static com.googlesource.gerrit.plugins.replication.ReplicationTasksStorageTest.assertNoIncompleteTasks;
+
+import com.google.common.jimfs.Configuration;
+import com.google.common.jimfs.Jimfs;
+import java.net.URISyntaxException;
+import java.nio.file.FileSystem;
+import java.nio.file.Path;
+import org.eclipse.jgit.transport.URIish;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ReplicationTasksStorageMPTest {
+  protected static final String PROJECT = "myProject";
+  protected static final String REF = "myRef";
+  protected static final String REMOTE = "myDest";
+  protected static final URIish URISH =
+      ReplicationTasksStorageTaskTest.getUrish("http://example.com/" + PROJECT + ".git");
+  protected static final ReplicationTasksStorage.ReplicateRefUpdate REF_UPDATE =
+      new ReplicationTasksStorage.ReplicateRefUpdate(PROJECT, REF, URISH, REMOTE);
+  protected static final UriUpdates URI_UPDATES = getUriUpdates(REF_UPDATE);
+
+  protected ReplicationTasksStorage nodeA;
+  protected ReplicationTasksStorage nodeB;
+  protected ReplicationTasksStorage persistedView;
+  protected FileSystem fileSystem;
+
+  @Before
+  public void setUp() throws Exception {
+    fileSystem = Jimfs.newFileSystem(Configuration.unix());
+    Path storageSite = fileSystem.getPath("replication_site");
+    nodeA = new ReplicationTasksStorage(storageSite);
+    nodeB = new ReplicationTasksStorage(storageSite);
+    persistedView = new ReplicationTasksStorage(storageSite);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    persistedView = null;
+    nodeA = null;
+    nodeB = null;
+    fileSystem.close();
+  }
+
+  @Test
+  public void waitingTaskCanBeCompletedByOtherNode() {
+    nodeA.create(REF_UPDATE);
+
+    nodeB.start(URI_UPDATES);
+    assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeB.finish(URI_UPDATES);
+    assertNoIncompleteTasks(persistedView);
+  }
+
+  @Test
+  public void resetTaskCanBeCompletedByOtherNode() {
+    nodeA.create(REF_UPDATE);
+    nodeA.start(URI_UPDATES);
+
+    nodeA.reset(URI_UPDATES);
+    assertContainsExactly(persistedView.listWaiting(), REF_UPDATE);
+
+    nodeB.start(URI_UPDATES);
+    assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeB.finish(URI_UPDATES);
+    assertNoIncompleteTasks(persistedView);
+  }
+
+  @Test
+  public void resetTaskCanBeResetAndCompletedByOtherNode() {
+    nodeA.create(REF_UPDATE);
+    nodeA.start(URI_UPDATES);
+    nodeA.reset(URI_UPDATES);
+    nodeB.start(URI_UPDATES);
+
+    nodeB.reset(URI_UPDATES);
+    assertContainsExactly(persistedView.listWaiting(), REF_UPDATE);
+
+    nodeB.start(URI_UPDATES);
+    assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeB.finish(URI_UPDATES);
+    assertNoIncompleteTasks(persistedView);
+  }
+
+  @Test
+  public void resetTaskCanBeResetByOtherNodeAndCompletedByOriginalNode() {
+    nodeA.create(REF_UPDATE);
+    nodeA.start(URI_UPDATES);
+    nodeA.reset(URI_UPDATES);
+    nodeB.start(URI_UPDATES);
+    nodeB.reset(URI_UPDATES);
+
+    nodeA.start(URI_UPDATES);
+    assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeA.finish(URI_UPDATES);
+    assertNoIncompleteTasks(persistedView);
+  }
+
+  @Test
+  public void canBeResetAllAndCompletedByOtherNode() {
+    nodeA.create(REF_UPDATE);
+    nodeA.start(URI_UPDATES);
+
+    nodeB.resetAll();
+    assertContainsExactly(persistedView.listWaiting(), REF_UPDATE);
+
+    nodeB.start(URI_UPDATES);
+    assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeA.finish(URI_UPDATES);
+    // Bug: https://crbug.com/gerrit/12973
+    // assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeB.finish(URI_UPDATES);
+    assertNoIncompleteTasks(persistedView);
+  }
+
+  @Test
+  public void canBeResetAllAndCompletedByOtherNodeFastOriginalNode() {
+    nodeA.create(REF_UPDATE);
+    nodeA.start(URI_UPDATES);
+    nodeB.resetAll();
+
+    nodeA.finish(URI_UPDATES);
+    assertContainsExactly(persistedView.listWaiting(), REF_UPDATE);
+
+    nodeB.start(URI_UPDATES);
+    assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeB.finish(URI_UPDATES);
+    assertNoIncompleteTasks(persistedView);
+  }
+
+  @Test
+  public void canBeResetAllAndCompletedByOtherNodeSlowOriginalNode() {
+    nodeA.create(REF_UPDATE);
+    nodeA.start(URI_UPDATES);
+    nodeB.resetAll();
+
+    nodeB.start(URI_UPDATES);
+    assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeB.finish(URI_UPDATES);
+    ReplicationTasksStorageTest.assertNoIncompleteTasks(persistedView);
+
+    nodeA.finish(URI_UPDATES);
+    ReplicationTasksStorageTest.assertNoIncompleteTasks(persistedView);
+  }
+
+  @Test
+  public void multipleNodesCanReplicateSameRef() {
+    nodeA.create(REF_UPDATE);
+    nodeA.start(URI_UPDATES);
+    assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeA.finish(URI_UPDATES);
+    assertNoIncompleteTasks(persistedView);
+
+    nodeB.create(REF_UPDATE);
+    nodeB.start(URI_UPDATES);
+    assertContainsExactly(persistedView.listRunning(), REF_UPDATE);
+
+    nodeB.finish(URI_UPDATES);
+    assertNoIncompleteTasks(persistedView);
+  }
+
+  public static UriUpdates getUriUpdates(ReplicationTasksStorage.ReplicateRefUpdate refUpdate) {
+    try {
+      return TestUriUpdates.create(refUpdate);
+    } catch (URISyntaxException e) {
+      throw new RuntimeException("Cannot instantiate UriUpdates object", e);
+    }
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageTest.java
index 0c4d1a7..fecbd31 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/ReplicationTasksStorageTest.java
@@ -343,12 +343,12 @@
     assertThat(storage.listRunning()).isEmpty();
   }
 
-  private void assertNoIncompleteTasks(ReplicationTasksStorage storage) {
+  protected static void assertNoIncompleteTasks(ReplicationTasksStorage storage) {
     assertThat(storage.listWaiting()).isEmpty();
     assertThat(storage.listRunning()).isEmpty();
   }
 
-  private void assertContainsExactly(
+  protected static void assertContainsExactly(
       List<ReplicateRefUpdate> all, ReplicateRefUpdate... refUpdates) {
     assertThat(all).hasSize(refUpdates.length);
     for (int i = 0; i < refUpdates.length; i++) {
@@ -356,7 +356,7 @@
     }
   }
 
-  private boolean equals(ReplicateRefUpdate one, ReplicateRefUpdate two) {
+  public static boolean equals(ReplicateRefUpdate one, ReplicateRefUpdate two) {
     return (one == null && two == null)
         || (one != null
             && two != null