Add test to show how often SubmitRules are run

Change-Id: Ifabc3498cb39204a5924c8c62bc05e907d93e3bf
(cherry picked from commit 32e37f517da1eb763b4a82697b85fd2a2f9e0ac0)
This commit is contained in:
Patrick Hiesel
2021-04-12 15:14:42 +02:00
committed by David Ostrovsky
parent 541a9bdef8
commit 1b3c845e41

View File

@@ -24,6 +24,7 @@ import com.google.gerrit.entities.SubmitRecord;
import com.google.gerrit.extensions.annotations.Exports;
import com.google.gerrit.extensions.api.changes.ChangeApi;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.client.ListChangesOption;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.LegacySubmitRequirementInfo;
import com.google.gerrit.extensions.config.FactoryModule;
@@ -35,6 +36,7 @@ import com.google.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
public class ChangeSubmitRequirementIT extends AbstractDaemonTest {
@@ -58,7 +60,7 @@ public class ChangeSubmitRequirementIT extends AbstractDaemonTest {
};
}
@Inject CustomSubmitRule rule;
@Inject private CustomSubmitRule rule;
@Test
public void submitRequirementIsPropagated() throws Exception {
@@ -170,6 +172,38 @@ public class ChangeSubmitRequirementIT extends AbstractDaemonTest {
assertThat(result.get(0).changeId).isEqualTo(change.info().changeId);
}
@Test
public void submitRuleIsInvokedOnlyOnceWhenGettingChangeDetails() throws Exception {
PushOneCommit.Result r = createChange("Some Change", "foo.txt", "some content");
String changeId = r.getChangeId();
rule.numberOfEvaluations.set(0);
gApi.changes()
.id(changeId)
.get(ListChangesOption.ALL_REVISIONS, ListChangesOption.CURRENT_ACTIONS);
// Submit rules are invoked twice, once for populating submit requirements in the change JSON
// and once for checking if the submit button should be visible.
// TODO(hiesel): Try to avoid calling submit rules twice.
assertThat(rule.numberOfEvaluations.get()).isEqualTo(2);
}
@Test
public void submitRuleIsNotInvokedWhenQueryingChange() throws Exception {
PushOneCommit.Result r = createChange("Some Change", "foo.txt", "some content");
String changeId = r.getChangeId();
rule.numberOfEvaluations.set(0);
gApi.changes()
.query(changeId)
.withOptions(ListChangesOption.ALL_REVISIONS, ListChangesOption.CURRENT_ACTIONS)
.get();
// Submit rules are invoked to check if the submit button should be visible.
// TODO(hiesel): Change queries must not trigger submit rules.
assertThat(rule.numberOfEvaluations.get()).isEqualTo(1);
}
private List<ChangeInfo> queryIsSubmittable() throws Exception {
return gApi.changes().query("is:submittable project:" + project.get()).get();
}
@@ -189,6 +223,7 @@ public class ChangeSubmitRequirementIT extends AbstractDaemonTest {
@Singleton
private static class CustomSubmitRule implements SubmitRule {
private Optional<SubmitRecord.Status> recordStatus = Optional.empty();
private AtomicInteger numberOfEvaluations = new AtomicInteger();
public void block(boolean block) {
this.status(block ? Optional.of(SubmitRecord.Status.NOT_READY) : Optional.empty());
@@ -200,6 +235,7 @@ public class ChangeSubmitRequirementIT extends AbstractDaemonTest {
@Override
public Optional<SubmitRecord> evaluate(ChangeData changeData) {
numberOfEvaluations.incrementAndGet();
if (this.recordStatus.isPresent()) {
SubmitRecord record = new SubmitRecord();
record.labels = new ArrayList<>();