google-nomulus/javatests/google/registry/model/server/LockTest.java
guyben aee4f7acc2 Remove queueing from Lock
It was buggy (didn't work) and was never actually used.

Why never actually used: for it to be used executeWithLock has to be called
with different requesters on the same lockId. That never happend in the code.

How it was buggy: Logically, the queue is deleted on release of the lock (meaning it was
meaningless the only time it mattered - when the lock isn't taken). In
addition, a different bug meant that having items in the queue prevented the
lock from being released forcing all other tasks to have to wait for lock
timeout even if the task that acquired the lock is long done.

Alternative: fix the queue. This would mean we don't want to delete the lock on release (since we want to keep the queue). Instead, we resave the same lock with expiration date being START_OF_TIME. In addition - we need to fix the .equals used to determine if the lock the same as the acquired lock - instead use some isSame function that ignores the queue.

Note: the queue is dangerous! An item (calling class / action) in the first place of a queue means no other calling class can get that lock. Everything is waiting for the first calling class to be re-run - but that might take a long time (depending on that action's rerun policy) and even might never happen (if for some reason that action decided it was no longer needed without acquiring the lock) - causing all other actions to stall forever!

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=163705463
2017-08-01 17:06:20 -04:00

94 lines
3.4 KiB
Java

// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.model.server;
import static com.google.common.truth.Truth.assertThat;
import google.registry.model.ofy.Ofy;
import google.registry.testing.AppEngineRule;
import google.registry.testing.ExceptionRule;
import google.registry.testing.FakeClock;
import google.registry.testing.InjectRule;
import org.joda.time.Duration;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link Lock}. */
@RunWith(JUnit4.class)
public class LockTest {
private static final String RESOURCE_NAME = "foo";
private static final Duration ONE_DAY = Duration.standardDays(1);
private static final Duration TWO_MILLIS = Duration.millis(2);
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder()
.withDatastore()
.build();
@Rule
public final InjectRule inject = new InjectRule();
@Rule
public final ExceptionRule thrown = new ExceptionRule();
@Test
public void testReleasedExplicitly() throws Exception {
Lock lock = Lock.acquire(RESOURCE_NAME, "", ONE_DAY);
assertThat(lock).isNotNull();
// We can't get it again at the same time.
assertThat(Lock.acquire(RESOURCE_NAME, "", ONE_DAY)).isNull();
// But if we release it, it's available.
lock.release();
assertThat(Lock.acquire(RESOURCE_NAME, "", ONE_DAY)).isNotNull();
}
@Test
public void testReleasedAfterTimeout() throws Exception {
FakeClock clock = new FakeClock();
inject.setStaticField(Ofy.class, "clock", clock);
assertThat(Lock.acquire(RESOURCE_NAME, "", TWO_MILLIS)).isNotNull();
// We can't get it again at the same time.
assertThat(Lock.acquire(RESOURCE_NAME, "", TWO_MILLIS)).isNull();
// A second later we still can't get the lock.
clock.advanceOneMilli();
assertThat(Lock.acquire(RESOURCE_NAME, "", TWO_MILLIS)).isNull();
// But two seconds later we can get it.
clock.advanceOneMilli();
assertThat(Lock.acquire(RESOURCE_NAME, "", TWO_MILLIS)).isNotNull();
}
@Test
public void testTldsAreIndependent() throws Exception {
Lock lockA = Lock.acquire(RESOURCE_NAME, "a", ONE_DAY);
assertThat(lockA).isNotNull();
// For a different tld we can still get a lock with the same name.
Lock lockB = Lock.acquire(RESOURCE_NAME, "b", ONE_DAY);
assertThat(lockB).isNotNull();
// We can't get lockB again at the same time.
assertThat(Lock.acquire(RESOURCE_NAME, "b", ONE_DAY)).isNull();
// Releasing lockA has no effect on lockB (even though we are still using the "b" tld).
lockA.release();
assertThat(Lock.acquire(RESOURCE_NAME, "b", ONE_DAY)).isNull();
}
@Test
public void testFailure_emptyResourceName() throws Exception {
thrown.expect(IllegalArgumentException.class, "resourceName cannot be null or empty");
Lock.acquire("", "", TWO_MILLIS);
}
}