Support disabling NoteDb reads for tests

In I0a9eb0ad we fixed a NoteDb-specific issue where loading the
default search results page caused ChangeNotes to get loaded, despite
not causing changes to load from ReviewDb.

Support this configuration in tests by adding a method to
NotesMigration that is called in AbstractChangeNotes#load(). In
production this method always returns false so the cost is negligible.

Change-Id: I22622cb3c834eeb1391d5b80247beafe36fffedf
This commit is contained in:
Dave Borowitz
2016-04-22 12:31:38 -04:00
parent 680495c014
commit af71861280
6 changed files with 52 additions and 11 deletions

View File

@@ -529,6 +529,16 @@ public abstract class AbstractDaemonTest {
atrScope.newContext(reviewDbProvider, null, anonymousUser.get()));
}
protected Context disableDb() {
notesMigration.setFailOnLoad(true);
return atrScope.disableDb();
}
protected void enableDb(Context preDisableContext) {
notesMigration.setFailOnLoad(false);
atrScope.set(preDisableContext);
}
protected static Gson newGson() {
return OutputFormat.JSON_COMPACT.newGson();
}

View File

@@ -31,6 +31,7 @@ import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.AcceptanceTestRequestScope;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.TestProjectInput;
@@ -983,16 +984,20 @@ public class ChangeIT extends AbstractDaemonTest {
createChange();
setApiUserAnonymous(); // Identified user may async get stars from DB.
atrScope.disableDb();
assertThat(gApi.changes().query()
.withQuery(
"project:{" + project.get() + "} (status:open OR status:closed)")
// Options should match defaults in AccountDashboardScreen.
.withOption(ListChangesOption.LABELS)
.withOption(ListChangesOption.DETAILED_ACCOUNTS)
.withOption(ListChangesOption.REVIEWED)
.get())
.hasSize(2);
AcceptanceTestRequestScope.Context ctx = disableDb();
try {
assertThat(gApi.changes().query()
.withQuery(
"project:{" + project.get() + "} (status:open OR status:closed)")
// Options should match defaults in AccountDashboardScreen.
.withOption(ListChangesOption.LABELS)
.withOption(ListChangesOption.DETAILED_ACCOUNTS)
.withOption(ListChangesOption.REVIEWED)
.get())
.hasSize(2);
} finally {
enableDb(ctx);
}
}
@Test

View File

@@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.AcceptanceTestRequestScope;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.common.data.AccessSection;
@@ -77,7 +78,6 @@ public class VisibleRefFilterIT extends AbstractDaemonTest {
.getGroupUUID();
setUpPermissions();
setUpChanges();
atrScope.disableDb();
}
private void setUpPermissions() throws Exception {
@@ -269,6 +269,7 @@ public class VisibleRefFilterIT extends AbstractDaemonTest {
}
}
AcceptanceTestRequestScope.Context ctx = disableDb();
try (Repository repo = repoManager.openRepository(project)) {
ProjectControl ctl = projectControlFactory.controlFor(project,
identifiedUserFactory.create(Providers.of(db), user.getId()));
@@ -277,6 +278,8 @@ public class VisibleRefFilterIT extends AbstractDaemonTest {
Map<String, Ref> all = repo.getAllRefs();
assertThat(filter.filter(all, false).keySet())
.containsExactlyElementsIn(expected);
} finally {
enableDb(ctx);
}
}
}

View File

@@ -124,6 +124,9 @@ public abstract class AbstractChangeNotes<T> {
loadDefaults();
return self();
}
if (args.migration.failOnLoad()) {
throw new OrmException("Reading from NoteDb is disabled");
}
try (Timer1.Context timer = args.metrics.readLatency.start(CHANGES);
Repository repo = args.repoManager.openRepository(getProjectName());
LoadHandle handle = openHandle(repo)) {

View File

@@ -42,6 +42,15 @@ public abstract class NotesMigration {
public abstract boolean writeAccounts();
/**
* Whether to fail when reading any data from NoteDb.
* <p>
* Used in conjunction with {@link #readChanges()} for tests.
*/
public boolean failOnLoad() {
return false;
}
public boolean enabled() {
return writeChanges() || readChanges()
|| writeAccounts() || readAccounts();

View File

@@ -22,6 +22,7 @@ import com.google.inject.Singleton;
public class TestNotesMigration extends NotesMigration {
private volatile boolean readChanges;
private volatile boolean writeChanges;
private volatile boolean failOnLoad;
@Override
public boolean readChanges() {
@@ -43,6 +44,11 @@ public class TestNotesMigration extends NotesMigration {
return false;
}
@Override
public boolean failOnLoad() {
return failOnLoad;
}
public TestNotesMigration setReadChanges(boolean readChanges) {
this.readChanges = readChanges;
return this;
@@ -53,6 +59,11 @@ public class TestNotesMigration extends NotesMigration {
return this;
}
public TestNotesMigration setFailOnLoad(boolean failOnLoad) {
this.failOnLoad = failOnLoad;
return this;
}
public TestNotesMigration setAllEnabled(boolean enabled) {
return setReadChanges(enabled).setWriteChanges(enabled);
}