mirror of
https://github.com/google/nomulus.git
synced 2025-07-21 18:26:12 +02:00
Break circular dependency between core and util (#379)
* Break circular dependency between core and util Created a new :common project and moved a minimum number of classes to break the circular dependency between the two projects. This gets rid of the gradle lint dependency warnings. Also separated api classes and testing helpers into separate source sets in :common so that testing classes may be restricted to test configurations.
This commit is contained in:
parent
1f85613c84
commit
9359f40665
62 changed files with 487 additions and 2742 deletions
|
@ -1,34 +0,0 @@
|
|||
// 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.util;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/**
|
||||
* A clock that tells the current time in milliseconds or nanoseconds.
|
||||
*
|
||||
* <p>Clocks are technically serializable because they are either a stateless wrapper around the
|
||||
* system clock, or for testing, are just a wrapper around a DateTime. This means that if you
|
||||
* serialize a clock and deserialize it elsewhere, you won't necessarily get the same time or time
|
||||
* zone -- what you will get is a functioning clock.
|
||||
*/
|
||||
@ThreadSafe
|
||||
public interface Clock extends Serializable {
|
||||
|
||||
/** Returns current time in UTC timezone. */
|
||||
DateTime nowUtc();
|
||||
}
|
|
@ -1,111 +0,0 @@
|
|||
// 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.util;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Ordering;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.TimeZone;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
/** Utilities methods and constants related to Joda {@link DateTime} objects. */
|
||||
public class DateTimeUtils {
|
||||
|
||||
/** The start of the epoch, in a convenient constant. */
|
||||
public static final DateTime START_OF_TIME = new DateTime(0, DateTimeZone.UTC);
|
||||
|
||||
/**
|
||||
* A date in the far future that we can treat as infinity.
|
||||
*
|
||||
* <p>This value is (2^63-1)/1000 rounded down. AppEngine stores dates as 64 bit microseconds, but
|
||||
* Java uses milliseconds, so this is the largest representable date that will survive a
|
||||
* round-trip through Datastore.
|
||||
*/
|
||||
public static final DateTime END_OF_TIME = new DateTime(Long.MAX_VALUE / 1000, DateTimeZone.UTC);
|
||||
|
||||
/** Returns the earliest of a number of given {@link DateTime} instances. */
|
||||
public static DateTime earliestOf(DateTime first, DateTime... rest) {
|
||||
return earliestOf(Lists.asList(first, rest));
|
||||
}
|
||||
|
||||
/** Returns the earliest element in a {@link DateTime} iterable. */
|
||||
public static DateTime earliestOf(Iterable<DateTime> dates) {
|
||||
checkArgument(!Iterables.isEmpty(dates));
|
||||
return Ordering.<DateTime>natural().min(dates);
|
||||
}
|
||||
|
||||
/** Returns the latest of a number of given {@link DateTime} instances. */
|
||||
public static DateTime latestOf(DateTime first, DateTime... rest) {
|
||||
return latestOf(Lists.asList(first, rest));
|
||||
}
|
||||
|
||||
/** Returns the latest element in a {@link DateTime} iterable. */
|
||||
public static DateTime latestOf(Iterable<DateTime> dates) {
|
||||
checkArgument(!Iterables.isEmpty(dates));
|
||||
return Ordering.<DateTime>natural().max(dates);
|
||||
}
|
||||
|
||||
/** Returns whether the first {@link DateTime} is equal to or earlier than the second. */
|
||||
public static boolean isBeforeOrAt(DateTime timeToCheck, DateTime timeToCompareTo) {
|
||||
return !timeToCheck.isAfter(timeToCompareTo);
|
||||
}
|
||||
|
||||
/** Returns whether the first {@link DateTime} is equal to or later than the second. */
|
||||
public static boolean isAtOrAfter(DateTime timeToCheck, DateTime timeToCompareTo) {
|
||||
return !timeToCheck.isBefore(timeToCompareTo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds years to a date, in the {@code Duration} sense of semantic years. Use this instead of
|
||||
* {@link DateTime#plusYears} to ensure that we never end up on February 29.
|
||||
*/
|
||||
public static DateTime leapSafeAddYears(DateTime now, int years) {
|
||||
checkArgument(years >= 0);
|
||||
return years == 0 ? now : now.plusYears(1).plusYears(years - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtracts years from a date, in the {@code Duration} sense of semantic years. Use this instead
|
||||
* of {@link DateTime#minusYears} to ensure that we never end up on February 29.
|
||||
*/
|
||||
public static DateTime leapSafeSubtractYears(DateTime now, int years) {
|
||||
checkArgument(years >= 0);
|
||||
return years == 0 ? now : now.minusYears(1).minusYears(years - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Joda {@link DateTime} object to an equivalent java.time {@link ZonedDateTime}
|
||||
* object.
|
||||
*/
|
||||
public static ZonedDateTime toZonedDateTime(DateTime dateTime) {
|
||||
java.time.Instant instant = java.time.Instant.ofEpochMilli(dateTime.getMillis());
|
||||
return ZonedDateTime.ofInstant(instant, ZoneId.of(dateTime.getZone().getID()).normalized());
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a java.time {@link ZonedDateTime} object to an equivalent Joda {@link DateTime}
|
||||
* object.
|
||||
*/
|
||||
public static DateTime toJodaDateTime(ZonedDateTime zonedDateTime) {
|
||||
return new DateTime(
|
||||
zonedDateTime.toInstant().toEpochMilli(),
|
||||
DateTimeZone.forTimeZone(TimeZone.getTimeZone(zonedDateTime.getZone())));
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
// 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.util;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
/** A simple clock that always returns a fixed time. */
|
||||
public class FixedClock implements Clock {
|
||||
private final DateTime nowUtc;
|
||||
|
||||
public FixedClock(long millisSinceEpoch) {
|
||||
this.nowUtc = new DateTime(millisSinceEpoch, DateTimeZone.UTC);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DateTime nowUtc() {
|
||||
return nowUtc;
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
// 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.util;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import org.joda.time.ReadableDuration;
|
||||
|
||||
/**
|
||||
* An object which accepts requests to put the current thread to sleep.
|
||||
*
|
||||
* @see SystemSleeper
|
||||
* @see google.registry.testing.FakeSleeper
|
||||
*/
|
||||
@ThreadSafe
|
||||
public interface Sleeper {
|
||||
|
||||
/**
|
||||
* Puts the current thread to sleep.
|
||||
*
|
||||
* @throws InterruptedException if this thread was interrupted
|
||||
*/
|
||||
void sleep(ReadableDuration duration) throws InterruptedException;
|
||||
|
||||
/**
|
||||
* Puts the current thread to sleep, ignoring interrupts.
|
||||
*
|
||||
* <p>If {@link InterruptedException} was caught, then {@code Thread.currentThread().interrupt()}
|
||||
* will be called at the end of the {@code duration}.
|
||||
*
|
||||
* @see com.google.common.util.concurrent.Uninterruptibles#sleepUninterruptibly
|
||||
*/
|
||||
void sleepUninterruptibly(ReadableDuration duration);
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
// 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.util;
|
||||
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.inject.Inject;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
/** Clock implementation that proxies to the real system clock. */
|
||||
@ThreadSafe
|
||||
public class SystemClock implements Clock {
|
||||
|
||||
private static final long serialVersionUID = 5165372013848947515L;
|
||||
|
||||
@Inject
|
||||
public SystemClock() {}
|
||||
|
||||
/** Returns the current time. */
|
||||
@Override
|
||||
public DateTime nowUtc() {
|
||||
return DateTime.now(UTC);
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
// 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.util;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import com.google.common.util.concurrent.Uninterruptibles;
|
||||
import java.io.Serializable;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.inject.Inject;
|
||||
import org.joda.time.ReadableDuration;
|
||||
|
||||
/** Implementation of {@link Sleeper} for production use. */
|
||||
@ThreadSafe
|
||||
public final class SystemSleeper implements Sleeper, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 2003215961965322843L;
|
||||
|
||||
@Inject
|
||||
public SystemSleeper() {}
|
||||
|
||||
@Override
|
||||
public void sleep(ReadableDuration duration) throws InterruptedException {
|
||||
checkArgument(duration.getMillis() >= 0);
|
||||
Thread.sleep(duration.getMillis());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sleepUninterruptibly(ReadableDuration duration) {
|
||||
checkArgument(duration.getMillis() >= 0);
|
||||
Uninterruptibles.sleepUninterruptibly(duration.getMillis(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
// 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.util;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.testing.NullPointerTester;
|
||||
import com.google.common.util.concurrent.UncheckedExecutionException;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import java.util.function.Function;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link Concurrent}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class ConcurrentTest {
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder()
|
||||
.withDatastore()
|
||||
.build();
|
||||
|
||||
@Test
|
||||
public void testTransform_emptyList_returnsEmptyList() {
|
||||
assertThat(Concurrent.transform(ImmutableList.of(), x -> x)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransform_addIntegers() {
|
||||
assertThat(Concurrent.transform(ImmutableList.of(1, 2, 3), input -> input + 1))
|
||||
.containsExactly(2, 3, 4)
|
||||
.inOrder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransform_throwsException_isSinglyWrappedByUee() {
|
||||
UncheckedExecutionException e =
|
||||
assertThrows(
|
||||
UncheckedExecutionException.class,
|
||||
() ->
|
||||
Concurrent.transform(
|
||||
ImmutableList.of(1, 2, 3),
|
||||
input -> {
|
||||
throw new RuntimeException("hello");
|
||||
}));
|
||||
assertThat(e).hasCauseThat().isInstanceOf(RuntimeException.class);
|
||||
assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("hello");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullness() {
|
||||
NullPointerTester tester = new NullPointerTester().setDefault(Function.class, x -> x);
|
||||
tester.testAllPublicStaticMethods(Concurrent.class);
|
||||
}
|
||||
}
|
|
@ -1,125 +0,0 @@
|
|||
// 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.util;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.testing.LogsSubject.assertAboutLogs;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.api.log.LogQuery;
|
||||
import com.google.appengine.api.log.LogService;
|
||||
import com.google.appengine.api.log.RequestLogs;
|
||||
import com.google.apphosting.api.ApiProxy;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.flogger.LoggerConfig;
|
||||
import com.google.common.testing.TestLogHandler;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import java.util.logging.Level;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link RequestStatusCheckerImpl}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public final class RequestStatusCheckerImplTest {
|
||||
|
||||
private static final TestLogHandler logHandler = new TestLogHandler();
|
||||
|
||||
private static final RequestStatusChecker requestStatusChecker = new RequestStatusCheckerImpl();
|
||||
|
||||
/**
|
||||
* Matcher for the expected LogQuery in {@link RequestStatusCheckerImpl#isRunning}.
|
||||
*
|
||||
* Because LogQuery doesn't have a .equals function, we have to create an actual matcher to make
|
||||
* sure we have the right argument in our mocks.
|
||||
*/
|
||||
private static LogQuery expectedLogQuery(final String requestLogId) {
|
||||
return argThat(
|
||||
object -> {
|
||||
assertThat(object).isInstanceOf(LogQuery.class);
|
||||
assertThat(object.getRequestIds()).containsExactly(requestLogId);
|
||||
assertThat(object.getIncludeAppLogs()).isFalse();
|
||||
assertThat(object.getIncludeIncomplete()).isTrue();
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@Rule
|
||||
public AppEngineRule appEngineRule = AppEngineRule.builder().build();
|
||||
|
||||
@Before public void setUp() {
|
||||
LoggerConfig.getConfig(RequestStatusCheckerImpl.class).addHandler(logHandler);
|
||||
RequestStatusCheckerImpl.logService = mock(LogService.class);
|
||||
}
|
||||
|
||||
@After public void tearDown() {
|
||||
LoggerConfig.getConfig(RequestStatusCheckerImpl.class).removeHandler(logHandler);
|
||||
}
|
||||
|
||||
// If a logId is unrecognized, it could be that the log hasn't been uploaded yet - so we assume
|
||||
// it's a request that has just started running recently.
|
||||
@Test public void testIsRunning_unrecognized() {
|
||||
when(RequestStatusCheckerImpl.logService.fetch(expectedLogQuery("12345678")))
|
||||
.thenReturn(ImmutableList.of());
|
||||
assertThat(requestStatusChecker.isRunning("12345678")).isTrue();
|
||||
assertAboutLogs()
|
||||
.that(logHandler)
|
||||
.hasLogAtLevelWithMessage(Level.INFO, "Queried an unrecognized requestLogId");
|
||||
}
|
||||
|
||||
@Test public void testIsRunning_notFinished() {
|
||||
RequestLogs requestLogs = new RequestLogs();
|
||||
requestLogs.setFinished(false);
|
||||
|
||||
when(RequestStatusCheckerImpl.logService.fetch(expectedLogQuery("12345678")))
|
||||
.thenReturn(ImmutableList.of(requestLogs));
|
||||
|
||||
assertThat(requestStatusChecker.isRunning("12345678")).isTrue();
|
||||
assertAboutLogs()
|
||||
.that(logHandler)
|
||||
.hasLogAtLevelWithMessage(Level.INFO, "isFinished: false");
|
||||
}
|
||||
|
||||
@Test public void testIsRunning_finished() {
|
||||
RequestLogs requestLogs = new RequestLogs();
|
||||
requestLogs.setFinished(true);
|
||||
|
||||
when(RequestStatusCheckerImpl.logService.fetch(expectedLogQuery("12345678")))
|
||||
.thenReturn(ImmutableList.of(requestLogs));
|
||||
|
||||
assertThat(requestStatusChecker.isRunning("12345678")).isFalse();
|
||||
assertAboutLogs()
|
||||
.that(logHandler)
|
||||
.hasLogAtLevelWithMessage(Level.INFO, "isFinished: true");
|
||||
}
|
||||
|
||||
@Test public void testGetLogId_returnsRequestLogId() {
|
||||
String expectedLogId = ApiProxy.getCurrentEnvironment().getAttributes().get(
|
||||
"com.google.appengine.runtime.request_log_id").toString();
|
||||
assertThat(requestStatusChecker.getLogId()).isEqualTo(expectedLogId);
|
||||
}
|
||||
|
||||
@Test public void testGetLogId_createsLog() {
|
||||
requestStatusChecker.getLogId();
|
||||
assertAboutLogs()
|
||||
.that(logHandler)
|
||||
.hasLogAtLevelWithMessage(Level.INFO, "Current requestLogId: ");
|
||||
}
|
||||
}
|
|
@ -1,145 +0,0 @@
|
|||
// Copyright 2018 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.util;
|
||||
|
||||
import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withUrl;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
import static google.registry.testing.TaskQueueHelper.getQueueInfo;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.google.appengine.api.taskqueue.Queue;
|
||||
import com.google.appengine.api.taskqueue.QueueFactory;
|
||||
import com.google.appengine.api.taskqueue.TaskHandle;
|
||||
import com.google.appengine.api.taskqueue.TaskOptions;
|
||||
import com.google.appengine.api.taskqueue.TransientFailureException;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.FakeClock;
|
||||
import google.registry.testing.FakeSleeper;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link TaskQueueUtils}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public final class TaskQueueUtilsTest {
|
||||
|
||||
private static final int MAX_RETRIES = 3;
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine =
|
||||
AppEngineRule.builder().withDatastore().withTaskQueue().build();
|
||||
|
||||
private int origBatchSize;
|
||||
|
||||
private final FakeClock clock = new FakeClock(DateTime.parse("2000-01-01TZ"));
|
||||
private final FakeSleeper sleeper = new FakeSleeper(clock);
|
||||
private final TaskQueueUtils taskQueueUtils =
|
||||
new TaskQueueUtils(new Retrier(sleeper, MAX_RETRIES));
|
||||
private final Queue queue = mock(Queue.class);
|
||||
private final TaskOptions task = withUrl("url").taskName("name");
|
||||
private final TaskHandle handle = new TaskHandle(task, "handle");
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
origBatchSize = TaskQueueUtils.BATCH_SIZE;
|
||||
TaskQueueUtils.BATCH_SIZE = 2;
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
TaskQueueUtils.BATCH_SIZE = origBatchSize;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnqueue_worksOnFirstTry_doesntSleep() {
|
||||
when(queue.add(ImmutableList.of(task))).thenReturn(ImmutableList.of(handle));
|
||||
assertThat(taskQueueUtils.enqueue(queue, task)).isSameInstanceAs(handle);
|
||||
verify(queue).add(ImmutableList.of(task));
|
||||
assertThat(clock.nowUtc()).isEqualTo(DateTime.parse("2000-01-01TZ"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnqueue_twoTransientErrorsThenSuccess_stillWorksAfterSleeping() {
|
||||
when(queue.add(ImmutableList.of(task)))
|
||||
.thenThrow(new TransientFailureException(""))
|
||||
.thenThrow(new TransientFailureException(""))
|
||||
.thenReturn(ImmutableList.of(handle));
|
||||
assertThat(taskQueueUtils.enqueue(queue, task)).isSameInstanceAs(handle);
|
||||
verify(queue, times(3)).add(ImmutableList.of(task));
|
||||
assertThat(clock.nowUtc()).isEqualTo(DateTime.parse("2000-01-01T00:00:00.6Z")); // 200 + 400ms
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnqueue_multiple() {
|
||||
TaskOptions taskA = withUrl("a").taskName("a");
|
||||
TaskOptions taskB = withUrl("b").taskName("b");
|
||||
ImmutableList<TaskHandle> handles =
|
||||
ImmutableList.of(new TaskHandle(taskA, "a"), new TaskHandle(taskB, "b"));
|
||||
when(queue.add(ImmutableList.of(taskA, taskB))).thenReturn(handles);
|
||||
assertThat(taskQueueUtils.enqueue(queue, ImmutableList.of(taskA, taskB)))
|
||||
.isSameInstanceAs(handles);
|
||||
assertThat(clock.nowUtc()).isEqualTo(DateTime.parse("2000-01-01TZ"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnqueue_maxRetries_givesUp() {
|
||||
when(queue.add(ImmutableList.of(task)))
|
||||
.thenThrow(new TransientFailureException("one"))
|
||||
.thenThrow(new TransientFailureException("two"))
|
||||
.thenThrow(new TransientFailureException("three"))
|
||||
.thenThrow(new TransientFailureException("four"));
|
||||
TransientFailureException thrown =
|
||||
assertThrows(TransientFailureException.class, () -> taskQueueUtils.enqueue(queue, task));
|
||||
assertThat(thrown).hasMessageThat().contains("three");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEnqueue_transientErrorThenInterrupt_throwsTransientError() {
|
||||
when(queue.add(ImmutableList.of(task))).thenThrow(new TransientFailureException(""));
|
||||
try {
|
||||
Thread.currentThread().interrupt();
|
||||
assertThrows(TransientFailureException.class, () -> taskQueueUtils.enqueue(queue, task));
|
||||
} finally {
|
||||
Thread.interrupted(); // Clear interrupt state so it doesn't pwn other tests.
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteTasks_usesMultipleBatches() {
|
||||
Queue defaultQ = QueueFactory.getQueue("default");
|
||||
TaskOptions taskOptA = withUrl("/a").taskName("a");
|
||||
TaskOptions taskOptB = withUrl("/b").taskName("b");
|
||||
TaskOptions taskOptC = withUrl("/c").taskName("c");
|
||||
taskQueueUtils.enqueue(defaultQ, ImmutableList.of(taskOptA, taskOptB, taskOptC));
|
||||
assertThat(getQueueInfo("default").getTaskInfo()).hasSize(3);
|
||||
|
||||
taskQueueUtils.deleteTasks(
|
||||
defaultQ,
|
||||
ImmutableList.of(
|
||||
new TaskHandle(taskOptA, "default"),
|
||||
new TaskHandle(taskOptB, "default"),
|
||||
new TaskHandle(taskOptC, "default")));
|
||||
assertThat(getQueueInfo("default").getTaskInfo()).hasSize(0);
|
||||
}
|
||||
}
|
|
@ -1,109 +0,0 @@
|
|||
// 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.util;
|
||||
|
||||
import static com.google.common.net.HttpHeaders.CONTENT_LENGTH;
|
||||
import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
|
||||
import static com.google.common.net.MediaType.CSV_UTF_8;
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
import static google.registry.util.UrlFetchUtils.setPayloadMultipart;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
import com.google.appengine.api.urlfetch.HTTPHeader;
|
||||
import com.google.appengine.api.urlfetch.HTTPRequest;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
/** Unit tests for {@link UrlFetchUtils}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class UrlFetchUtilsTest {
|
||||
|
||||
@Rule
|
||||
public final AppEngineRule appEngine = AppEngineRule.builder()
|
||||
.build();
|
||||
|
||||
private final Random random = mock(Random.class);
|
||||
|
||||
@Before
|
||||
public void setupRandomZeroes() {
|
||||
doAnswer(
|
||||
info -> {
|
||||
Arrays.fill((byte[]) info.getArguments()[0], (byte) 0);
|
||||
return null;
|
||||
})
|
||||
.when(random)
|
||||
.nextBytes(any(byte[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetPayloadMultipart() {
|
||||
HTTPRequest request = mock(HTTPRequest.class);
|
||||
setPayloadMultipart(
|
||||
request,
|
||||
"lol",
|
||||
"cat",
|
||||
CSV_UTF_8,
|
||||
"The nice people at the store say hello. ヘ(◕。◕ヘ)",
|
||||
random);
|
||||
ArgumentCaptor<HTTPHeader> headerCaptor = ArgumentCaptor.forClass(HTTPHeader.class);
|
||||
verify(request, times(2)).addHeader(headerCaptor.capture());
|
||||
List<HTTPHeader> addedHeaders = headerCaptor.getAllValues();
|
||||
assertThat(addedHeaders.get(0).getName()).isEqualTo(CONTENT_TYPE);
|
||||
assertThat(addedHeaders.get(0).getValue())
|
||||
.isEqualTo(
|
||||
"multipart/form-data; "
|
||||
+ "boundary=\"------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\"");
|
||||
assertThat(addedHeaders.get(1).getName()).isEqualTo(CONTENT_LENGTH);
|
||||
assertThat(addedHeaders.get(1).getValue()).isEqualTo("294");
|
||||
String payload = "--------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\n"
|
||||
+ "Content-Disposition: form-data; name=\"lol\"; filename=\"cat\"\r\n"
|
||||
+ "Content-Type: text/csv; charset=utf-8\r\n"
|
||||
+ "\r\n"
|
||||
+ "The nice people at the store say hello. ヘ(◕。◕ヘ)\r\n"
|
||||
+ "--------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA--\r\n";
|
||||
verify(request).setPayload(payload.getBytes(UTF_8));
|
||||
verifyNoMoreInteractions(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetPayloadMultipart_boundaryInPayload() {
|
||||
HTTPRequest request = mock(HTTPRequest.class);
|
||||
String payload = "I screamed------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHHH";
|
||||
IllegalStateException thrown =
|
||||
assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> setPayloadMultipart(request, "lol", "cat", CSV_UTF_8, payload, random));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains(
|
||||
"Multipart data contains autogenerated boundary: "
|
||||
+ "------------------------------AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue