Run both the AppEngineRule and JpaTransactionManagerRule in tests (#356)

* Run both the AppEngineRule and JpaTransactionManagerRule in tests

As previously written, the AppEngineRule wraps the infinite loop of the
server handling requests. This changes the code so that we'll have a
chain that does both the AppEngine management and the JPA transaction
manager management so we can actually use SQL in tests and in the test
server.

* remove log line
This commit is contained in:
gbrodman 2019-11-11 11:26:20 -05:00 committed by GitHub
parent f0345ddf89
commit ae89a8b76f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 30 deletions

View file

@ -19,6 +19,7 @@ import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.net.HostAndPort; import com.google.common.net.HostAndPort;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.testing.AppEngineRule; import google.registry.testing.AppEngineRule;
import google.registry.testing.UserInfo; import google.registry.testing.UserInfo;
import google.registry.tools.params.HostAndPortParameter; import google.registry.tools.params.HostAndPortParameter;
@ -134,36 +135,44 @@ public final class RegistryTestServerMain {
LIGHT_PURPLE, ORANGE, PINK, RESET); LIGHT_PURPLE, ORANGE, PINK, RESET);
final RegistryTestServer server = new RegistryTestServer(address); final RegistryTestServer server = new RegistryTestServer(address);
Statement runner = new Statement() { Statement runner =
@Override new Statement() {
public void evaluate() throws InterruptedException { @Override
System.out.printf("%sLoading Datastore fixtures...%s\n", BLUE, RESET); public void evaluate() throws InterruptedException {
for (Fixture fixture : fixtures) { System.out.printf("%sLoading Datastore fixtures...%s\n", BLUE, RESET);
fixture.load(); for (Fixture fixture : fixtures) {
} fixture.load();
System.out.printf("%sStarting Jetty6 HTTP Server...%s\n", BLUE, RESET); }
server.start(); System.out.printf("%sStarting Jetty6 HTTP Server...%s\n", BLUE, RESET);
System.out.printf("%sListening on: %s%s\n", PURPLE, server.getUrl("/"), RESET); server.start();
try { System.out.printf("%sListening on: %s%s\n", PURPLE, server.getUrl("/"), RESET);
while (true) { try {
server.process(); while (true) {
server.process();
}
} finally {
server.stop();
}
} }
} finally { };
server.stop();
}
}};
System.out.printf("%sLoading AppEngineRule...%s\n", BLUE, RESET); Statement withAppEngine =
AppEngineRule.builder() AppEngineRule.builder()
.withDatastore() .withDatastore()
.withUrlFetch() .withUrlFetch()
.withTaskQueue() .withTaskQueue()
.withLocalModules() .withLocalModules()
.withUserService(loginIsAdmin .withUserService(
? UserInfo.createAdmin(loginEmail, loginUserId) loginIsAdmin
: UserInfo.create(loginEmail, loginUserId)) ? UserInfo.createAdmin(loginEmail, loginUserId)
: UserInfo.create(loginEmail, loginUserId))
.build()
.apply(runner, Description.EMPTY);
System.out.printf("%sLoading SQL fixtures and AppEngineRule...%s\n", BLUE, RESET);
new JpaTransactionManagerRule.Builder()
.build() .build()
.apply(runner, Description.EMPTY) .apply(withAppEngine, Description.EMPTY)
.evaluate(); .evaluate();
} }

View file

@ -23,6 +23,7 @@ import static google.registry.util.NetworkUtils.pickUnusedPort;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.net.HostAndPort; import com.google.common.net.HostAndPort;
import google.registry.model.transaction.JpaTransactionManagerRule;
import google.registry.request.auth.AuthenticatedRegistrarAccessor; import google.registry.request.auth.AuthenticatedRegistrarAccessor;
import google.registry.server.Fixture; import google.registry.server.Fixture;
import google.registry.server.Route; import google.registry.server.Route;
@ -53,6 +54,7 @@ public final class TestServerRule extends ExternalResource {
private final ImmutableList<Fixture> fixtures; private final ImmutableList<Fixture> fixtures;
private final AppEngineRule appEngineRule; private final AppEngineRule appEngineRule;
private final JpaTransactionManagerRule jpaTransactionManagerRule;
private final BlockingQueue<FutureTask<?>> jobs = new LinkedBlockingDeque<>(); private final BlockingQueue<FutureTask<?>> jobs = new LinkedBlockingDeque<>();
private final ImmutableMap<String, Path> runfiles; private final ImmutableMap<String, Path> runfiles;
private final ImmutableList<Route> routes; private final ImmutableList<Route> routes;
@ -80,6 +82,7 @@ public final class TestServerRule extends ExternalResource {
.withTaskQueue() .withTaskQueue()
.withUserService(UserInfo.createAdmin(email, THE_REGISTRAR_GAE_USER_ID)) .withUserService(UserInfo.createAdmin(email, THE_REGISTRAR_GAE_USER_ID))
.build(); .build();
this.jpaTransactionManagerRule = new JpaTransactionManagerRule.Builder().build();
} }
@Override @Override
@ -164,9 +167,8 @@ public final class TestServerRule extends ExternalResource {
@Override @Override
public void run() { public void run() {
try { try {
appEngineRule Statement appEngineStatement = appEngineRule.apply(this, Description.EMPTY);
.apply(this, Description.EMPTY) jpaTransactionManagerRule.apply(appEngineStatement, Description.EMPTY).evaluate();
.evaluate();
} catch (InterruptedException e) { } catch (InterruptedException e) {
// This is what we expect to happen. // This is what we expect to happen.
} catch (Throwable e) { } catch (Throwable e) {