From 1d7803586a74ab9688b32d64f9df15527fafbfde Mon Sep 17 00:00:00 2001 From: Diego Zambelli Sessona Date: Thu, 21 Mar 2024 19:23:05 +0100 Subject: Add test paginationType NONE to query only if there are more results If the first query to the index did not fill the full limit the caller wants, we paginate to obtain more results. However, subsequent queries are not needed if there are no more results from the previous query. This add a test for the change 413358 [1], that has reintroduced the issue fixed in change 344695. [1]: https://gerrit-review.googlesource.com/c/gerrit/+/413358/comment/85bca522_2b3552b2/ Bug: Issue 330195358 Release-Notes: skip Change-Id: If578ee877587488fdf3715331bd8d23e9d86f750 --- .../server/query/change/FakeQueryChangesTest.java | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/javatests/com/google/gerrit/server/query/change/FakeQueryChangesTest.java b/javatests/com/google/gerrit/server/query/change/FakeQueryChangesTest.java index af14683e8b..2f5ae17e31 100644 --- a/javatests/com/google/gerrit/server/query/change/FakeQueryChangesTest.java +++ b/javatests/com/google/gerrit/server/query/change/FakeQueryChangesTest.java @@ -22,8 +22,11 @@ import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS import static org.junit.Assume.assumeFalse; import com.google.gerrit.acceptance.UseClockStep; +import com.google.gerrit.entities.Account; +import com.google.gerrit.entities.Change; import com.google.gerrit.entities.Permission; import com.google.gerrit.entities.Project; +import com.google.gerrit.extensions.common.ChangeInfo; import com.google.gerrit.index.PaginationType; import com.google.gerrit.index.testing.AbstractFakeIndex; import com.google.gerrit.server.config.AllProjectsName; @@ -34,6 +37,7 @@ import com.google.gerrit.testing.InMemoryRepositoryManager.Repo; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; +import java.util.List; import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.lib.Config; import org.junit.Test; @@ -81,6 +85,41 @@ public abstract class FakeQueryChangesTest extends AbstractQueryChangesTest { assertThat(idx.getQueryCount()).isEqualTo(1); } + @Test + @UseClockStep + @SuppressWarnings("unchecked") + public void queryRightNumberOfTimes() throws Exception { + TestRepository repo = createProject("repo"); + Account.Id user2 = + accountManager.authenticate(authRequestFactory.createForUser("anotheruser")).getAccountId(); + + // create 1 visible change + Change visibleChange1 = insert(repo, newChangeWithStatus(repo, Change.Status.NEW)); + + // create 4 private changes + Change invisibleChange2 = insert(repo, newChangeWithStatus(repo, Change.Status.NEW), user2); + Change invisibleChange3 = insert(repo, newChangeWithStatus(repo, Change.Status.NEW), user2); + Change invisibleChange4 = insert(repo, newChangeWithStatus(repo, Change.Status.NEW), user2); + Change invisibleChange5 = insert(repo, newChangeWithStatus(repo, Change.Status.NEW), user2); + gApi.changes().id(invisibleChange2.getChangeId()).setPrivate(true, null); + gApi.changes().id(invisibleChange3.getChangeId()).setPrivate(true, null); + gApi.changes().id(invisibleChange4.getChangeId()).setPrivate(true, null); + gApi.changes().id(invisibleChange5.getChangeId()).setPrivate(true, null); + + AbstractFakeIndex idx = (AbstractFakeIndex) changeIndexCollection.getSearchIndex(); + int queriesBeforeExecution = idx.getQueryCount(); + List queryResult = newQuery("status:new").withLimit(2).get(); + assertThat(queryResult).hasSize(1); + assertThat(queryResult.get(0).changeId).isEqualTo(visibleChange1.getKey().get()); + + // Since the limit of the query (i.e. 2), 2 index searches are expected in fact: + // 1: The first query will return invisibleChange5, invisibleChange4 and invisibleChange3, + // 2: Another query is needed to back-fill the limit requested by the user. + // even if one result in the second query is skipped because it is not visible, + // there are no more results to query. + assertThat(idx.getQueryCount() - queriesBeforeExecution).isEqualTo(2); + } + @Test @UseClockStep @SuppressWarnings("unchecked") -- cgit v1.2.3