Introduce tests for ReplicationTaskId thread-local Ensure that the ReplicationTaskId introduced in Iacacda97 preserves nesting of task-ids with or without exceptions. Also assures that the withTaskId() is used in conjunction with refs filtering. Change-Id: Id2af833f94b68bde20124cca695524a254b44790
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/FetchOneTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/FetchOneTest.java index 50d91ff..5547b60 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/FetchOneTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/FetchOneTest.java
@@ -369,6 +369,25 @@ } @Test + public void shouldSetReplicationTaskIdDuringRefsFiltering() throws Exception { + assertThat(ReplicationTaskId.get()).isNull(); + + setupObjectUnderTestMocksForFetchingTestRef(); + + objectUnderTest.setReplicationFetchFilter(replicationFilter); + when(replicationFilter.get()) + .thenReturn( + (projectName, fetchRefs) -> { + assertThat(ReplicationTaskId.get()).isEqualTo(objectUnderTest.getTaskIdHex()); + return Set.of(TEST_REF); + }); + + objectUnderTest.run(); + + assertThat(ReplicationTaskId.get()).isNull(); + } + + @Test public void fetchWithoutDelta_shouldPassNewRefsToFilter() throws Exception { setupMocks(true); String REMOTE_REF = "refs/heads/remote"; @@ -890,6 +909,15 @@ assertThat(refsToDelete(fetchRefSpecs)).isEqualTo(Set.of("refs/something/someref")); } + private void setupObjectUnderTestMocksForFetchingTestRef() throws Exception { + setupMocks(true); + Set<FetchRefSpec> refSpecs = Set.of(TEST_REF_SPEC); + setupFetchFactoryMock( + List.of(new FetchFactoryEntry.Builder().refSpecNameWithDefaults(TEST_REF).build()), + Optional.of(List.of(TEST_REF))); + objectUnderTest.addRefs(refSpecs); + } + private void setupRequestScopeMock() { when(scoper.scope(any())) .thenAnswer(
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationTaskIdTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationTaskIdTest.java new file mode 100644 index 0000000..4889d2a --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationTaskIdTest.java
@@ -0,0 +1,69 @@ +// Copyright (C) 2026 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.pull; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.gerrit.testing.GerritJUnit.assertThrows; +import static com.googlesource.gerrit.plugins.replication.pull.ReplicationTaskId.withTaskId; + +import java.io.IOException; +import org.junit.Test; + +public class ReplicationTaskIdTest { + String OUTER_TASK_ID = "outer"; + String INNER_TASK_ID = "inner"; + + @Test + public void withTaskIdShouldPreserveIdUponNestedCalls() throws IOException { + String unused = + withTaskId( + OUTER_TASK_ID, + () -> { + withTaskId( + INNER_TASK_ID, + () -> { + assertThat(ReplicationTaskId.get()).isEqualTo(INNER_TASK_ID); + return "unused"; + }); + assertThat(ReplicationTaskId.get()).isEqualTo(OUTER_TASK_ID); + return "unused"; + }); + assertThat(ReplicationTaskId.get()).isNull(); + } + + @Test + public void withTaskIdShouldPreserveIdUponNestedCallsWithExceptions() throws IOException { + assertThrows( + IOException.class, + () -> + withTaskId( + OUTER_TASK_ID, + () -> { + try { + withTaskId( + INNER_TASK_ID, + () -> { + assertThat(ReplicationTaskId.get()).isEqualTo(INNER_TASK_ID); + throw new IOException("exception"); + }); + } catch (IOException e) { + assertThat(ReplicationTaskId.get()).isEqualTo(OUTER_TASK_ID); + throw e; + } + return "unused"; + })); + assertThat(ReplicationTaskId.get()).isNull(); + } +}