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 4cd11d16ea..815ea3f988 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 static org.apache.http.HttpStatus.SC_CONFLICT; import static org.apache.http.HttpStatus.SC_FORBIDDEN; @@ -58,6 +57,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; @@ -66,9 +66,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; @@ -81,7 +78,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 { @@ -114,20 +110,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 86e7ae185a..3f36a2df7e 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 @@ -17,7 +17,6 @@ package com.google.gerrit.acceptance.git; 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 java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import com.google.common.collect.ImmutableSet; @@ -33,12 +32,10 @@ import com.google.gerrit.extensions.common.ChangeInfo; import com.google.gerrit.extensions.common.EditInfo; import com.google.gerrit.extensions.common.LabelInfo; import com.google.gerrit.reviewdb.client.Change; +import com.google.gerrit.testutil.TestTimeUtil; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.transport.PushResult; -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; @@ -46,7 +43,6 @@ import org.junit.Test; import java.util.List; import java.util.Set; -import java.util.concurrent.atomic.AtomicLong; public abstract class AbstractPushForReview extends AbstractDaemonTest { protected enum Protocol { @@ -58,20 +54,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 c264779c6a..e3b05915d8 100644 --- a/gerrit-server/BUCK +++ b/gerrit-server/BUCK @@ -97,6 +97,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', @@ -173,7 +174,6 @@ java_test( '//gerrit-common:annotations', '//gerrit-server/src/main/prolog:common', '//lib/antlr:java_runtime', - '//lib/joda:joda-time', ], source_under_test = [':server'], ) @@ -194,7 +194,6 @@ java_test( '//lib:grappa', '//lib:guava', '//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 8030f9f967..cbd8ff55f2 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; @@ -51,6 +50,7 @@ import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.testutil.FakeAccountCache; 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; @@ -62,15 +62,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 { private static final TimeZone TZ = @@ -91,7 +87,6 @@ public class AbstractChangeNotesTest { private Injector injector; private String systemTimeZone; - private volatile long clockStepMs; @Inject private AllUsersNameProvider allUsers; @@ -151,21 +146,12 @@ public class AbstractChangeNotesTest { 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 3b053702e3..1d7a1ae598 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 @@ -71,6 +71,7 @@ import com.google.gerrit.testutil.DisabledReviewDb; 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; @@ -80,9 +81,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; @@ -96,7 +94,6 @@ import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; @Ignore @RunWith(ConfigSuite.class) @@ -144,7 +141,6 @@ public abstract class AbstractQueryChangesTest { protected ReviewDb db; protected Account.Id userId; protected CurrentUser user; - protected volatile long clockStepMs; private String systemTimeZone; @@ -200,21 +196,12 @@ public abstract class AbstractQueryChangesTest { @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); } @@ -791,7 +778,7 @@ public abstract class AbstractQueryChangesTest { @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(); @@ -816,7 +803,7 @@ public abstract class AbstractQueryChangesTest { @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); @@ -970,16 +957,17 @@ public abstract class AbstractQueryChangesTest { @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"); @@ -993,11 +981,11 @@ public abstract class AbstractQueryChangesTest { @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"); @@ -1013,11 +1001,11 @@ public abstract class AbstractQueryChangesTest { @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); @@ -1292,7 +1280,7 @@ public abstract class AbstractQueryChangesTest { @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 7e7899b50c..8b22ff594c 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; @@ -80,7 +80,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() { + } +}