Create an injectable LockHandler

We create an injectable LockHandler that just calls the static
Lock.executeWithLocks function.

I'm not sure what's the correct place to put the LockHandler. I think
model/server is only appropriate for the actual datastore lock. This is a "per request" lock, so maybe request/lock?

-----------------------------

This is the initial step in adding the "lock implicitly released on request death" feature, but it's also useful on its own - easier to test Actions when we can use a fake lock.

To keep this CL simple, we keep using the old Lock as is in most places. We just choose a single example to convert to LockHandler to showcase it. Converting all other uses will be in a subsequent CL.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=167357564
This commit is contained in:
guyben 2017-09-01 21:32:37 -07:00 committed by jianglai
parent 67276888d2
commit 978149e677
7 changed files with 85 additions and 17 deletions

View file

@ -24,12 +24,12 @@ import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import com.google.common.base.Optional;
import google.registry.model.server.Lock;
import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeLockHandler;
import google.registry.testing.FakeResponse;
import java.util.concurrent.Callable;
import javax.annotation.Nullable;
import org.joda.time.Duration;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -48,16 +48,23 @@ public class SyncRegistrarsSheetActionTest {
private final FakeResponse response = new FakeResponse();
private final SyncRegistrarsSheet syncRegistrarsSheet = mock(SyncRegistrarsSheet.class);
private SyncRegistrarsSheetAction action;
private void runAction(@Nullable String idConfig, @Nullable String idParam) {
SyncRegistrarsSheetAction action = new SyncRegistrarsSheetAction();
action.response = response;
action.syncRegistrarsSheet = syncRegistrarsSheet;
action.idConfig = Optional.fromNullable(idConfig);
action.idParam = Optional.fromNullable(idParam);
action.timeout = Duration.standardHours(1);
action.run();
}
@Before
public void setUp() {
action = new SyncRegistrarsSheetAction();
action.response = response;
action.syncRegistrarsSheet = syncRegistrarsSheet;
action.timeout = Duration.standardHours(1);
action.lockHandler = new FakeLockHandler(true);
}
@Test
public void testPost_withoutParamsOrSystemProperty_dropsTask() throws Exception {
runAction(null, null);
@ -96,14 +103,8 @@ public class SyncRegistrarsSheetActionTest {
@Test
public void testPost_failToAquireLock_servletDoesNothingAndReturns() throws Exception {
String lockName = "Synchronize registrars sheet: foobar";
Lock.executeWithLocks(new Callable<Void>() {
@Override
public Void call() throws Exception {
runAction(null, "foobar");
return null;
}
}, null, Duration.standardHours(1), lockName);
action.lockHandler = new FakeLockHandler(false);
runAction(null, "foobar");
assertThat(response.getPayload()).startsWith("LOCKED");
verifyZeroInteractions(syncRegistrarsSheet);
}