diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/edit/ChangeEditIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/edit/ChangeEditIT.java index 896eb061f3..f4e7cb74ea 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/edit/ChangeEditIT.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/edit/ChangeEditIT.java @@ -17,7 +17,6 @@ package com.google.gerrit.acceptance.edit; import static com.google.common.collect.Iterables.getOnlyElement; import static com.google.common.truth.Truth.assertThat; import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import com.google.common.base.Optional; @@ -53,6 +52,7 @@ import com.google.gerrit.server.edit.UnchangedCommitMessageException; import com.google.gerrit.server.git.ProjectConfig; import com.google.gerrit.server.project.InvalidChangeOperationException; import com.google.gerrit.server.project.Util; +import com.google.gerrit.testutil.TestTimeUtil; import com.google.gson.stream.JsonReader; import com.google.gwtorm.server.SchemaFactory; import com.google.inject.Inject; @@ -61,9 +61,6 @@ import org.apache.commons.codec.binary.StringUtils; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.RefUpdate; -import org.joda.time.DateTime; -import org.joda.time.DateTimeUtils; -import org.joda.time.DateTimeUtils.MillisProvider; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -76,7 +73,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; -import java.util.concurrent.atomic.AtomicLong; public class ChangeEditIT extends AbstractDaemonTest { @@ -109,20 +105,12 @@ public class ChangeEditIT extends AbstractDaemonTest { @BeforeClass public static void setTimeForTesting() { - final long clockStepMs = MILLISECONDS.convert(1, SECONDS); - final AtomicLong clockMs = new AtomicLong( - new DateTime(2009, 9, 30, 17, 0, 0).getMillis()); - DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { - @Override - public long getMillis() { - return clockMs.getAndAdd(clockStepMs); - } - }); + TestTimeUtil.resetWithClockStep(1, SECONDS); } @AfterClass public static void restoreTime() { - DateTimeUtils.setCurrentMillisSystem(); + TestTimeUtil.useSystemTime(); } @Before diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/git/AbstractPushForReview.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/git/AbstractPushForReview.java index 21fd1a8f50..73a02a505a 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/git/AbstractPushForReview.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/git/AbstractPushForReview.java @@ -18,7 +18,6 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.TruthJUnit.assume; import static com.google.gerrit.acceptance.GitUtil.pushHead; import static com.google.gerrit.server.group.SystemGroupBackend.ANONYMOUS_USERS; -import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import com.google.common.collect.ImmutableSet; @@ -40,18 +39,15 @@ import com.google.gerrit.server.git.MetaDataUpdate; import com.google.gerrit.server.git.ProjectConfig; import com.google.gerrit.server.group.SystemGroupBackend; import com.google.gerrit.server.project.Util; +import com.google.gerrit.testutil.TestTimeUtil; import org.eclipse.jgit.revwalk.RevCommit; -import org.joda.time.DateTime; -import org.joda.time.DateTimeUtils; -import org.joda.time.DateTimeUtils.MillisProvider; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import java.util.Set; -import java.util.concurrent.atomic.AtomicLong; public abstract class AbstractPushForReview extends AbstractDaemonTest { protected enum Protocol { @@ -64,20 +60,12 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest { @BeforeClass public static void setTimeForTesting() { - final long clockStepMs = MILLISECONDS.convert(1, SECONDS); - final AtomicLong clockMs = new AtomicLong( - new DateTime(2009, 9, 30, 17, 0, 0).getMillis()); - DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { - @Override - public long getMillis() { - return clockMs.getAndAdd(clockStepMs); - } - }); + TestTimeUtil.resetWithClockStep(1, SECONDS); } @AfterClass public static void restoreTime() { - DateTimeUtils.setCurrentMillisSystem(); + TestTimeUtil.useSystemTime(); } @Before diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/change/ChangeMessagesIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/change/ChangeMessagesIT.java index d726d70828..91f2962360 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/change/ChangeMessagesIT.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/change/ChangeMessagesIT.java @@ -15,7 +15,6 @@ package com.google.gerrit.acceptance.rest.change; import static com.google.common.truth.Truth.assertThat; -import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import com.google.gerrit.acceptance.AbstractDaemonTest; @@ -23,41 +22,28 @@ import com.google.gerrit.extensions.api.changes.ReviewInput; import com.google.gerrit.extensions.common.ChangeInfo; import com.google.gerrit.extensions.common.ChangeMessageInfo; import com.google.gerrit.testutil.ConfigSuite; +import com.google.gerrit.testutil.TestTimeUtil; -import org.joda.time.DateTime; -import org.joda.time.DateTimeUtils; -import org.joda.time.DateTimeUtils.MillisProvider; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import java.util.Iterator; -import java.util.concurrent.atomic.AtomicLong; @RunWith(ConfigSuite.class) public class ChangeMessagesIT extends AbstractDaemonTest { private String systemTimeZone; - private volatile long clockStepMs; @Before public void setTimeForTesting() { systemTimeZone = System.setProperty("user.timezone", "US/Eastern"); - clockStepMs = MILLISECONDS.convert(1, SECONDS); - final AtomicLong clockMs = new AtomicLong( - new DateTime(2009, 9, 30, 17, 0, 0).getMillis()); - - DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { - @Override - public long getMillis() { - return clockMs.getAndAdd(clockStepMs); - } - }); + TestTimeUtil.resetWithClockStep(1, SECONDS); } @After public void resetTime() { - DateTimeUtils.setCurrentMillisSystem(); + TestTimeUtil.useSystemTime(); System.setProperty("user.timezone", systemTimeZone); } diff --git a/gerrit-server/BUCK b/gerrit-server/BUCK index b192432585..b8fb4d241b 100644 --- a/gerrit-server/BUCK +++ b/gerrit-server/BUCK @@ -98,6 +98,7 @@ TESTUTIL_DEPS = [ '//lib/guice:guice-servlet', '//lib/jgit:jgit', '//lib/jgit:junit', + '//lib/joda:joda-time', '//lib/log:api', '//lib/log:impl_log4j', '//lib/log:log4j', @@ -175,7 +176,6 @@ java_test( '//gerrit-common:annotations', '//gerrit-server/src/main/prolog:common', '//lib/antlr:java_runtime', - '//lib/joda:joda-time', ], source_under_test = [':server'], ) @@ -197,7 +197,6 @@ java_test( '//lib:guava', '//lib/dropwizard:dropwizard-core', '//lib/guice:guice-assistedinject', - '//lib/joda:joda-time', '//lib/prolog:runtime', ], source_under_test = [':server'], diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/notedb/AbstractChangeNotesTest.java b/gerrit-server/src/test/java/com/google/gerrit/server/notedb/AbstractChangeNotesTest.java index 4a4b1167d2..07624a424a 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/server/notedb/AbstractChangeNotesTest.java +++ b/gerrit-server/src/test/java/com/google/gerrit/server/notedb/AbstractChangeNotesTest.java @@ -15,7 +15,6 @@ package com.google.gerrit.server.notedb; import static com.google.inject.Scopes.SINGLETON; -import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import com.google.common.collect.ImmutableList; @@ -53,6 +52,7 @@ import com.google.gerrit.testutil.FakeAccountCache; import com.google.gerrit.testutil.GerritBaseTests; import com.google.gerrit.testutil.InMemoryRepositoryManager; import com.google.gerrit.testutil.TestChanges; +import com.google.gerrit.testutil.TestTimeUtil; import com.google.gwtorm.client.KeyUtil; import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.StandardKeyEncoder; @@ -64,15 +64,11 @@ import com.google.inject.util.Providers; import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.PersonIdent; -import org.joda.time.DateTime; -import org.joda.time.DateTimeUtils; -import org.joda.time.DateTimeUtils.MillisProvider; import org.junit.After; import org.junit.Before; import java.sql.Timestamp; import java.util.TimeZone; -import java.util.concurrent.atomic.AtomicLong; public class AbstractChangeNotesTest extends GerritBaseTests { private static final TimeZone TZ = @@ -93,7 +89,6 @@ public class AbstractChangeNotesTest extends GerritBaseTests { private Injector injector; private String systemTimeZone; - private volatile long clockStepMs; @Inject private AllUsersNameProvider allUsers; @@ -155,21 +150,12 @@ public class AbstractChangeNotesTest extends GerritBaseTests { private void setTimeForTesting() { systemTimeZone = System.setProperty("user.timezone", "US/Eastern"); - clockStepMs = MILLISECONDS.convert(1, SECONDS); - final AtomicLong clockMs = new AtomicLong( - new DateTime(2009, 9, 30, 17, 0, 0).getMillis()); - - DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { - @Override - public long getMillis() { - return clockMs.getAndAdd(clockStepMs); - } - }); + TestTimeUtil.resetWithClockStep(1, SECONDS); } @After public void resetTime() { - DateTimeUtils.setCurrentMillisSystem(); + TestTimeUtil.useSystemTime(); System.setProperty("user.timezone", systemTimeZone); } diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java b/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java index fcaa738a95..06bfafcd9e 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java +++ b/gerrit-server/src/test/java/com/google/gerrit/server/query/change/AbstractQueryChangesTest.java @@ -74,6 +74,7 @@ import com.google.gerrit.testutil.GerritServerTests; import com.google.gerrit.testutil.InMemoryDatabase; import com.google.gerrit.testutil.InMemoryRepositoryManager; import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo; +import com.google.gerrit.testutil.TestTimeUtil; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Provider; @@ -83,9 +84,6 @@ import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.revwalk.RevCommit; -import org.joda.time.DateTime; -import org.joda.time.DateTimeUtils; -import org.joda.time.DateTimeUtils.MillisProvider; import org.junit.After; import org.junit.Before; import org.junit.Ignore; @@ -94,7 +92,6 @@ import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.atomic.AtomicLong; @Ignore public abstract class AbstractQueryChangesTest extends GerritServerTests { @@ -129,7 +126,6 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests { protected ReviewDb db; protected Account.Id userId; protected CurrentUser user; - protected volatile long clockStepMs; private String systemTimeZone; @@ -185,21 +181,12 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests { @Before public void setTimeForTesting() { systemTimeZone = System.setProperty("user.timezone", "US/Eastern"); - clockStepMs = 1; - final AtomicLong clockMs = new AtomicLong( - new DateTime(2009, 9, 30, 17, 0, 0).getMillis()); - - DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { - @Override - public long getMillis() { - return clockMs.getAndAdd(clockStepMs); - } - }); + TestTimeUtil.resetWithClockStep(1, MILLISECONDS); } @After public void resetTime() { - DateTimeUtils.setCurrentMillisSystem(); + TestTimeUtil.useSystemTime(); System.setProperty("user.timezone", systemTimeZone); } @@ -697,7 +684,7 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests { @Test public void updateOrder() throws Exception { - clockStepMs = MILLISECONDS.convert(2, MINUTES); + TestTimeUtil.resetWithClockStep(2, MINUTES); TestRepository repo = createProject("repo"); List inserters = Lists.newArrayList(); List changes = Lists.newArrayList(); @@ -722,7 +709,7 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests { @Test public void updatedOrderWithMinuteResolution() throws Exception { - clockStepMs = MILLISECONDS.convert(2, MINUTES); + TestTimeUtil.resetWithClockStep(2, MINUTES); TestRepository repo = createProject("repo"); ChangeInserter ins1 = newChange(repo, null, null, null, null); Change change1 = insert(ins1); @@ -876,16 +863,17 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests { @Test public void byAge() throws Exception { - long thirtyHours = MILLISECONDS.convert(30, HOURS); - clockStepMs = thirtyHours; + long thirtyHoursInMs = MILLISECONDS.convert(30, HOURS); + TestTimeUtil.resetWithClockStep(thirtyHoursInMs, MILLISECONDS); TestRepository repo = createProject("repo"); Change change1 = insert(newChange(repo, null, null, null, null)); Change change2 = insert(newChange(repo, null, null, null, null)); - clockStepMs = 0; // Queried by AgePredicate constructor. + // Queried by AgePredicate constructor. + TestTimeUtil.setClockStep(0, MILLISECONDS); long now = TimeUtil.nowMs(); assertThat(lastUpdatedMs(change2) - lastUpdatedMs(change1)) - .isEqualTo(thirtyHours); - assertThat(now - lastUpdatedMs(change2)).isEqualTo(thirtyHours); + .isEqualTo(thirtyHoursInMs); + assertThat(now - lastUpdatedMs(change2)).isEqualTo(thirtyHoursInMs); assertThat(TimeUtil.nowMs()).isEqualTo(now); assertQuery("-age:1d"); @@ -899,11 +887,11 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests { @Test public void byBefore() throws Exception { - clockStepMs = MILLISECONDS.convert(30, HOURS); + TestTimeUtil.resetWithClockStep(30, HOURS); TestRepository repo = createProject("repo"); Change change1 = insert(newChange(repo, null, null, null, null)); Change change2 = insert(newChange(repo, null, null, null, null)); - clockStepMs = 0; + TestTimeUtil.setClockStep(0, MILLISECONDS); assertQuery("before:2009-09-29"); assertQuery("before:2009-09-30"); @@ -919,11 +907,11 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests { @Test public void byAfter() throws Exception { - clockStepMs = MILLISECONDS.convert(30, HOURS); + TestTimeUtil.resetWithClockStep(30, HOURS); TestRepository repo = createProject("repo"); Change change1 = insert(newChange(repo, null, null, null, null)); Change change2 = insert(newChange(repo, null, null, null, null)); - clockStepMs = 0; + TestTimeUtil.setClockStep(0, MILLISECONDS); assertQuery("after:2009-10-03"); assertQuery("after:\"2009-10-01 20:59:59 -0400\"", change2); @@ -1198,7 +1186,7 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests { @Test public void reviewedBy() throws Exception { - clockStepMs = MILLISECONDS.convert(2, MINUTES); + TestTimeUtil.resetWithClockStep(2, MINUTES); TestRepository repo = createProject("repo"); Change change1 = insert(newChange(repo, null, null, null, null)); Change change2 = insert(newChange(repo, null, null, null, null)); diff --git a/gerrit-server/src/test/java/com/google/gerrit/server/query/change/LuceneQueryChangesV14Test.java b/gerrit-server/src/test/java/com/google/gerrit/server/query/change/LuceneQueryChangesV14Test.java index 53a9805bf0..ed4e52072c 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/server/query/change/LuceneQueryChangesV14Test.java +++ b/gerrit-server/src/test/java/com/google/gerrit/server/query/change/LuceneQueryChangesV14Test.java @@ -15,7 +15,6 @@ package com.google.gerrit.server.query.change; import static com.google.common.truth.Truth.assertThat; -import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MINUTES; import com.google.gerrit.extensions.api.changes.ReviewInput; @@ -25,6 +24,7 @@ import com.google.gerrit.reviewdb.client.PatchSet; import com.google.gerrit.server.account.AuthRequest; import com.google.gerrit.testutil.InMemoryModule; import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo; +import com.google.gerrit.testutil.TestTimeUtil; import com.google.inject.Guice; import com.google.inject.Injector; @@ -87,7 +87,7 @@ public class LuceneQueryChangesV14Test extends LuceneQueryChangesTest { @Test public void isReviewed() throws Exception { - clockStepMs = MILLISECONDS.convert(2, MINUTES); + TestTimeUtil.resetWithClockStep(2, MINUTES); TestRepository repo = createProject("repo"); Change change1 = insert(newChange(repo, null, null, null, null)); Change change2 = insert(newChange(repo, null, null, null, null)); diff --git a/gerrit-server/src/test/java/com/google/gerrit/testutil/TestTimeUtil.java b/gerrit-server/src/test/java/com/google/gerrit/testutil/TestTimeUtil.java new file mode 100644 index 0000000000..4c71c573fc --- /dev/null +++ b/gerrit-server/src/test/java/com/google/gerrit/testutil/TestTimeUtil.java @@ -0,0 +1,75 @@ +// Copyright (C) 2015 The Android Open Source Project +// +// 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 com.google.gerrit.testutil; + +import static com.google.common.base.Preconditions.checkState; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + +import org.joda.time.DateTime; +import org.joda.time.DateTimeUtils; +import org.joda.time.DateTimeUtils.MillisProvider; +import org.joda.time.DateTimeZone; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +/** Static utility methods for dealing with dates and times in tests. */ +public class TestTimeUtil { + private static Long clockStepMs; + private static AtomicLong clockMs; + + /** + * Reset the clock to a known start point, then set the clock step. + *

+ * The clock is initially set to 2009/09/30 17:00:00 -0400. + * + * @param clockStep amount to increment clock by on each lookup. + * @param clockStepUnit time unit for {@code clockStep}. + */ + public static synchronized void resetWithClockStep( + long clockStep, TimeUnit clockStepUnit) { + // Set an arbitrary start point so tests are more repeatable. + clockMs = new AtomicLong( + new DateTime(2009, 9, 30, 17, 0, 0, DateTimeZone.forOffsetHours(-4)) + .getMillis()); + setClockStep(clockStep, clockStepUnit); + } + + /** + * Set the clock step used by {@link com.google.gerrit.common.TimeUtil}. + * + * @param clockStep amount to increment clock by on each lookup. + * @param clockStepUnit time unit for {@code clockStep}. + */ + public static synchronized void setClockStep( + long clockStep, TimeUnit clockStepUnit) { + checkState(clockMs != null, "call resetWithClockStep first"); + clockStepMs = MILLISECONDS.convert(clockStep, clockStepUnit); + DateTimeUtils.setCurrentMillisProvider(new MillisProvider() { + @Override + public long getMillis() { + return clockMs.getAndAdd(clockStepMs); + } + }); + } + + /** Reset the clock to use the actual system clock. */ + public static synchronized void useSystemTime() { + DateTimeUtils.setCurrentMillisSystem(); + } + + private TestTimeUtil() { + } +}