Add a testing method to set the clock step used by TimeUtil

Change-Id: Ia6e642474c97e5217a6c5de01db41c8d24ba5d51
This commit is contained in:
Dave Borowitz 2015-12-07 16:16:10 -05:00 committed by David Pursehouse
parent 1ced04653e
commit bd5fdc3d29
8 changed files with 106 additions and 96 deletions

View File

@ -17,7 +17,6 @@ package com.google.gerrit.acceptance.edit;
import static com.google.common.collect.Iterables.getOnlyElement; import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.http.HttpStatus.SC_CONFLICT; import static org.apache.http.HttpStatus.SC_CONFLICT;
import static org.apache.http.HttpStatus.SC_FORBIDDEN; 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.git.ProjectConfig;
import com.google.gerrit.server.project.InvalidChangeOperationException; import com.google.gerrit.server.project.InvalidChangeOperationException;
import com.google.gerrit.server.project.Util; import com.google.gerrit.server.project.Util;
import com.google.gerrit.testutil.TestTimeUtil;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gwtorm.server.SchemaFactory; import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject; 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.ObjectId;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.RefUpdate; 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.After;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
@ -81,7 +78,6 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
public class ChangeEditIT extends AbstractDaemonTest { public class ChangeEditIT extends AbstractDaemonTest {
@ -114,20 +110,12 @@ public class ChangeEditIT extends AbstractDaemonTest {
@BeforeClass @BeforeClass
public static void setTimeForTesting() { public static void setTimeForTesting() {
final long clockStepMs = MILLISECONDS.convert(1, SECONDS); TestTimeUtil.resetWithClockStep(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);
}
});
} }
@AfterClass @AfterClass
public static void restoreTime() { public static void restoreTime() {
DateTimeUtils.setCurrentMillisSystem(); TestTimeUtil.useSystemTime();
} }
@Before @Before

View File

@ -17,7 +17,6 @@ package com.google.gerrit.acceptance.git;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume; import static com.google.common.truth.TruthJUnit.assume;
import static com.google.gerrit.acceptance.GitUtil.pushHead; import static com.google.gerrit.acceptance.GitUtil.pushHead;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import com.google.common.collect.ImmutableSet; 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.EditInfo;
import com.google.gerrit.extensions.common.LabelInfo; import com.google.gerrit.extensions.common.LabelInfo;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.testutil.TestTimeUtil;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.transport.PushResult; 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.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
@ -46,7 +43,6 @@ import org.junit.Test;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
public abstract class AbstractPushForReview extends AbstractDaemonTest { public abstract class AbstractPushForReview extends AbstractDaemonTest {
protected enum Protocol { protected enum Protocol {
@ -58,20 +54,12 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
@BeforeClass @BeforeClass
public static void setTimeForTesting() { public static void setTimeForTesting() {
final long clockStepMs = MILLISECONDS.convert(1, SECONDS); TestTimeUtil.resetWithClockStep(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);
}
});
} }
@AfterClass @AfterClass
public static void restoreTime() { public static void restoreTime() {
DateTimeUtils.setCurrentMillisSystem(); TestTimeUtil.useSystemTime();
} }
@Before @Before

View File

@ -15,7 +15,6 @@
package com.google.gerrit.acceptance.rest.change; package com.google.gerrit.acceptance.rest.change;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import com.google.gerrit.acceptance.AbstractDaemonTest; 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.ChangeInfo;
import com.google.gerrit.extensions.common.ChangeMessageInfo; import com.google.gerrit.extensions.common.ChangeMessageInfo;
import com.google.gerrit.testutil.ConfigSuite; 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.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.util.Iterator; import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;
@RunWith(ConfigSuite.class) @RunWith(ConfigSuite.class)
public class ChangeMessagesIT extends AbstractDaemonTest { public class ChangeMessagesIT extends AbstractDaemonTest {
private String systemTimeZone; private String systemTimeZone;
private volatile long clockStepMs;
@Before @Before
public void setTimeForTesting() { public void setTimeForTesting() {
systemTimeZone = System.setProperty("user.timezone", "US/Eastern"); systemTimeZone = System.setProperty("user.timezone", "US/Eastern");
clockStepMs = MILLISECONDS.convert(1, SECONDS); TestTimeUtil.resetWithClockStep(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);
}
});
} }
@After @After
public void resetTime() { public void resetTime() {
DateTimeUtils.setCurrentMillisSystem(); TestTimeUtil.useSystemTime();
System.setProperty("user.timezone", systemTimeZone); System.setProperty("user.timezone", systemTimeZone);
} }

View File

@ -97,6 +97,7 @@ TESTUTIL_DEPS = [
'//lib/guice:guice-servlet', '//lib/guice:guice-servlet',
'//lib/jgit:jgit', '//lib/jgit:jgit',
'//lib/jgit:junit', '//lib/jgit:junit',
'//lib/joda:joda-time',
'//lib/log:api', '//lib/log:api',
'//lib/log:impl_log4j', '//lib/log:impl_log4j',
'//lib/log:log4j', '//lib/log:log4j',
@ -173,7 +174,6 @@ java_test(
'//gerrit-common:annotations', '//gerrit-common:annotations',
'//gerrit-server/src/main/prolog:common', '//gerrit-server/src/main/prolog:common',
'//lib/antlr:java_runtime', '//lib/antlr:java_runtime',
'//lib/joda:joda-time',
], ],
source_under_test = [':server'], source_under_test = [':server'],
) )
@ -194,7 +194,6 @@ java_test(
'//lib:grappa', '//lib:grappa',
'//lib:guava', '//lib:guava',
'//lib/guice:guice-assistedinject', '//lib/guice:guice-assistedinject',
'//lib/joda:joda-time',
'//lib/prolog:runtime', '//lib/prolog:runtime',
], ],
source_under_test = [':server'], source_under_test = [':server'],

View File

@ -15,7 +15,6 @@
package com.google.gerrit.server.notedb; package com.google.gerrit.server.notedb;
import static com.google.inject.Scopes.SINGLETON; import static com.google.inject.Scopes.SINGLETON;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import com.google.common.collect.ImmutableList; 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.FakeAccountCache;
import com.google.gerrit.testutil.InMemoryRepositoryManager; import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gerrit.testutil.TestChanges; import com.google.gerrit.testutil.TestChanges;
import com.google.gerrit.testutil.TestTimeUtil;
import com.google.gwtorm.client.KeyUtil; import com.google.gwtorm.client.KeyUtil;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.StandardKeyEncoder; 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.internal.storage.dfs.InMemoryRepository;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent; 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.After;
import org.junit.Before; import org.junit.Before;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicLong;
public class AbstractChangeNotesTest { public class AbstractChangeNotesTest {
private static final TimeZone TZ = private static final TimeZone TZ =
@ -91,7 +87,6 @@ public class AbstractChangeNotesTest {
private Injector injector; private Injector injector;
private String systemTimeZone; private String systemTimeZone;
private volatile long clockStepMs;
@Inject private AllUsersNameProvider allUsers; @Inject private AllUsersNameProvider allUsers;
@ -151,21 +146,12 @@ public class AbstractChangeNotesTest {
private void setTimeForTesting() { private void setTimeForTesting() {
systemTimeZone = System.setProperty("user.timezone", "US/Eastern"); systemTimeZone = System.setProperty("user.timezone", "US/Eastern");
clockStepMs = MILLISECONDS.convert(1, SECONDS); TestTimeUtil.resetWithClockStep(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);
}
});
} }
@After @After
public void resetTime() { public void resetTime() {
DateTimeUtils.setCurrentMillisSystem(); TestTimeUtil.useSystemTime();
System.setProperty("user.timezone", systemTimeZone); System.setProperty("user.timezone", systemTimeZone);
} }

View File

@ -71,6 +71,7 @@ import com.google.gerrit.testutil.DisabledReviewDb;
import com.google.gerrit.testutil.InMemoryDatabase; import com.google.gerrit.testutil.InMemoryDatabase;
import com.google.gerrit.testutil.InMemoryRepositoryManager; import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo; import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo;
import com.google.gerrit.testutil.TestTimeUtil;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Provider; 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.Config;
import org.eclipse.jgit.lib.ObjectInserter; import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.revwalk.RevCommit; 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.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore; import org.junit.Ignore;
@ -96,7 +94,6 @@ import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
@Ignore @Ignore
@RunWith(ConfigSuite.class) @RunWith(ConfigSuite.class)
@ -144,7 +141,6 @@ public abstract class AbstractQueryChangesTest {
protected ReviewDb db; protected ReviewDb db;
protected Account.Id userId; protected Account.Id userId;
protected CurrentUser user; protected CurrentUser user;
protected volatile long clockStepMs;
private String systemTimeZone; private String systemTimeZone;
@ -200,21 +196,12 @@ public abstract class AbstractQueryChangesTest {
@Before @Before
public void setTimeForTesting() { public void setTimeForTesting() {
systemTimeZone = System.setProperty("user.timezone", "US/Eastern"); systemTimeZone = System.setProperty("user.timezone", "US/Eastern");
clockStepMs = 1; TestTimeUtil.resetWithClockStep(1, MILLISECONDS);
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);
}
});
} }
@After @After
public void resetTime() { public void resetTime() {
DateTimeUtils.setCurrentMillisSystem(); TestTimeUtil.useSystemTime();
System.setProperty("user.timezone", systemTimeZone); System.setProperty("user.timezone", systemTimeZone);
} }
@ -791,7 +778,7 @@ public abstract class AbstractQueryChangesTest {
@Test @Test
public void updateOrder() throws Exception { public void updateOrder() throws Exception {
clockStepMs = MILLISECONDS.convert(2, MINUTES); TestTimeUtil.resetWithClockStep(2, MINUTES);
TestRepository<Repo> repo = createProject("repo"); TestRepository<Repo> repo = createProject("repo");
List<ChangeInserter> inserters = Lists.newArrayList(); List<ChangeInserter> inserters = Lists.newArrayList();
List<Change> changes = Lists.newArrayList(); List<Change> changes = Lists.newArrayList();
@ -816,7 +803,7 @@ public abstract class AbstractQueryChangesTest {
@Test @Test
public void updatedOrderWithMinuteResolution() throws Exception { public void updatedOrderWithMinuteResolution() throws Exception {
clockStepMs = MILLISECONDS.convert(2, MINUTES); TestTimeUtil.resetWithClockStep(2, MINUTES);
TestRepository<Repo> repo = createProject("repo"); TestRepository<Repo> repo = createProject("repo");
ChangeInserter ins1 = newChange(repo, null, null, null, null); ChangeInserter ins1 = newChange(repo, null, null, null, null);
Change change1 = insert(ins1); Change change1 = insert(ins1);
@ -970,16 +957,17 @@ public abstract class AbstractQueryChangesTest {
@Test @Test
public void byAge() throws Exception { public void byAge() throws Exception {
long thirtyHours = MILLISECONDS.convert(30, HOURS); long thirtyHoursInMs = MILLISECONDS.convert(30, HOURS);
clockStepMs = thirtyHours; TestTimeUtil.resetWithClockStep(thirtyHoursInMs, MILLISECONDS);
TestRepository<Repo> repo = createProject("repo"); TestRepository<Repo> repo = createProject("repo");
Change change1 = insert(newChange(repo, null, null, null, null)); Change change1 = insert(newChange(repo, null, null, null, null));
Change change2 = 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(); long now = TimeUtil.nowMs();
assertThat(lastUpdatedMs(change2) - lastUpdatedMs(change1)) assertThat(lastUpdatedMs(change2) - lastUpdatedMs(change1))
.isEqualTo(thirtyHours); .isEqualTo(thirtyHoursInMs);
assertThat(now - lastUpdatedMs(change2)).isEqualTo(thirtyHours); assertThat(now - lastUpdatedMs(change2)).isEqualTo(thirtyHoursInMs);
assertThat(TimeUtil.nowMs()).isEqualTo(now); assertThat(TimeUtil.nowMs()).isEqualTo(now);
assertQuery("-age:1d"); assertQuery("-age:1d");
@ -993,11 +981,11 @@ public abstract class AbstractQueryChangesTest {
@Test @Test
public void byBefore() throws Exception { public void byBefore() throws Exception {
clockStepMs = MILLISECONDS.convert(30, HOURS); TestTimeUtil.resetWithClockStep(30, HOURS);
TestRepository<Repo> repo = createProject("repo"); TestRepository<Repo> repo = createProject("repo");
Change change1 = insert(newChange(repo, null, null, null, null)); Change change1 = insert(newChange(repo, null, null, null, null));
Change change2 = 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-29");
assertQuery("before:2009-09-30"); assertQuery("before:2009-09-30");
@ -1013,11 +1001,11 @@ public abstract class AbstractQueryChangesTest {
@Test @Test
public void byAfter() throws Exception { public void byAfter() throws Exception {
clockStepMs = MILLISECONDS.convert(30, HOURS); TestTimeUtil.resetWithClockStep(30, HOURS);
TestRepository<Repo> repo = createProject("repo"); TestRepository<Repo> repo = createProject("repo");
Change change1 = insert(newChange(repo, null, null, null, null)); Change change1 = insert(newChange(repo, null, null, null, null));
Change change2 = 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-03");
assertQuery("after:\"2009-10-01 20:59:59 -0400\"", change2); assertQuery("after:\"2009-10-01 20:59:59 -0400\"", change2);
@ -1292,7 +1280,7 @@ public abstract class AbstractQueryChangesTest {
@Test @Test
public void reviewedBy() throws Exception { public void reviewedBy() throws Exception {
clockStepMs = MILLISECONDS.convert(2, MINUTES); TestTimeUtil.resetWithClockStep(2, MINUTES);
TestRepository<Repo> repo = createProject("repo"); TestRepository<Repo> repo = createProject("repo");
Change change1 = insert(newChange(repo, null, null, null, null)); Change change1 = insert(newChange(repo, null, null, null, null));
Change change2 = insert(newChange(repo, null, null, null, null)); Change change2 = insert(newChange(repo, null, null, null, null));

View File

@ -15,7 +15,6 @@
package com.google.gerrit.server.query.change; package com.google.gerrit.server.query.change;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.MINUTES;
import com.google.gerrit.extensions.api.changes.ReviewInput; 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.server.account.AuthRequest;
import com.google.gerrit.testutil.InMemoryModule; import com.google.gerrit.testutil.InMemoryModule;
import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo; import com.google.gerrit.testutil.InMemoryRepositoryManager.Repo;
import com.google.gerrit.testutil.TestTimeUtil;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
@ -80,7 +80,7 @@ public class LuceneQueryChangesV14Test extends LuceneQueryChangesTest {
@Test @Test
public void isReviewed() throws Exception { public void isReviewed() throws Exception {
clockStepMs = MILLISECONDS.convert(2, MINUTES); TestTimeUtil.resetWithClockStep(2, MINUTES);
TestRepository<Repo> repo = createProject("repo"); TestRepository<Repo> repo = createProject("repo");
Change change1 = insert(newChange(repo, null, null, null, null)); Change change1 = insert(newChange(repo, null, null, null, null));
Change change2 = insert(newChange(repo, null, null, null, null)); Change change2 = insert(newChange(repo, null, null, null, null));

View File

@ -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.
* <p>
* 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() {
}
}