Permalink
Browse files

Add Clock interface around System.currentTimeMillis.

  • Loading branch information...
1 parent 43786f9 commit 30a496f69f5a89f7b98a17cebd6430ba27255463 @daithiocrualaoich daithiocrualaoich committed Nov 21, 2011
View
@@ -1,4 +1,4 @@
-Simplecache
+SimpleCache
===========
What is it?
@@ -110,6 +110,7 @@ How do I release it?
* Clone the `guardian.github.com` repository to your home directory.
* Update the version numbers in `simplecache` to the new release version number.
+ You can use the `update-version-number` script for this.
* Invoke `sbt` and run the `publish` goal. This will create the necessary
structure in the cloned `guardian.github.com` project.
@@ -22,11 +22,11 @@ public Object getValue() {
}
public boolean isExpired() {
- return absoluteExpiryTime <= System.currentTimeMillis();
+ return absoluteExpiryTime <= Clock.currentTimeMillis();
}
public long getInstantaneousSecondsToExpiryTime() {
- final long nowInMillis = System.currentTimeMillis();
+ final long nowInMillis = Clock.currentTimeMillis();
final long relativeExpiryTimeInMillis = absoluteExpiryTime - nowInMillis;
return TimeUnit.MILLISECONDS.toSeconds(relativeExpiryTimeInMillis);
@@ -5,7 +5,7 @@
public class CacheValueWithExpiryTimeFactory {
public CacheValueWithExpiryTime create(Object value, long duration, TimeUnit timeUnit) {
- return create(value, System.currentTimeMillis() + timeUnit.toMillis(duration));
+ return create(value, Clock.currentTimeMillis() + timeUnit.toMillis(duration));
}
public CacheValueWithExpiryTime create(Object value, long expirationTime) {
@@ -0,0 +1,27 @@
+package com.gu.cache.simplecache;
+
+import static com.gu.option.Option.none;
+import static com.gu.option.Option.some;
+import com.gu.option.Option;
+
+public class Clock {
+
+ private static Option<Long> freeze = none();
+
+ public static void freeze(long atTimeMillis) {
+ freeze = some(atTimeMillis);
+ }
+
+ public static void freeze() {
+ freeze(System.currentTimeMillis());
+ }
+
+ public static void unfreeze() {
+ freeze = none();
+ }
+
+ public static long currentTimeMillis() {
+ return freeze.getOrElse(System.currentTimeMillis());
+ }
+
+}
@@ -1,21 +1,24 @@
package com.gu.cache.simplecache;
-import static org.hamcrest.MatcherAssert.*;
-import static org.hamcrest.Matchers.*;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import static org.mockito.Mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.concurrent.TimeUnit;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+
@RunWith(MockitoJUnitRunner.class)
public class SoftReferenceSimpleCacheTest {
private SoftReferenceSimpleCache cache = new SoftReferenceSimpleCache();
-
- @Mock
- private CacheValueWithExpiryTimeFactory mockFactory;
+
+ @Before
+ public void setUp() {
+ Clock.unfreeze();
+ }
@Test
public void shouldPutGetAndRemove() throws Exception {
@@ -28,25 +31,22 @@ public void shouldPutGetAndRemove() throws Exception {
@Test
public void shouldGetCacheWithExpiryTime() throws Exception {
- CacheValueWithExpiryTime expectedValue = new CacheValueWithExpiryTime("value", Long.MAX_VALUE);
- when(mockFactory.create("value", 1, TimeUnit.DAYS)).thenReturn(expectedValue);
+ Clock.freeze();
- cache.setCacheValueWithExpiryFactory(mockFactory);
- cache.putWithExpiry("key", "value", 1, TimeUnit.DAYS);
-
- CacheValueWithExpiryTime actualValue = cache.getWithExpiry("key");
-
- assertThat(actualValue, is(expectedValue));
+ cache.putWithExpiry("key", "value", 1, TimeUnit.DAYS);
+
+ CacheValueWithExpiryTime actualValue = cache.getWithExpiry("key");
+
+ assertThat(actualValue.getValue(), is((Object)"value"));
+ assertThat(actualValue.getInstantaneousSecondsToExpiryTime(), is(TimeUnit.DAYS.toSeconds(1)));
}
@Test
public void shouldReturnNullIfTheRequestedCacheEntiryHasExpired() {
- CacheValueWithExpiryTime expectedValue = new CacheValueWithExpiryTime("value", System.currentTimeMillis() - 500);
- when(mockFactory.create("value", 1, TimeUnit.DAYS)).thenReturn(expectedValue);
+ cache.putWithExpiry("key", "value", 1, TimeUnit.MINUTES);
+
+ Clock.freeze(Clock.currentTimeMillis() + TimeUnit.HOURS.toMillis(1));
- cache.setCacheValueWithExpiryFactory(mockFactory);
- cache.putWithExpiry("key", "value", 1, TimeUnit.DAYS);
-
assertThat(cache.getWithExpiry("key"), is(nullValue()));
assertThat(cache.get("key"), is(nullValue()));
}
@@ -59,7 +59,7 @@ public void shouldPutAndGetEvenWhenTheValuesAreNotSerlizable() throws Exception
Object nonSerializable = new Object();
adaptor.putWithExpiry("key", nonSerializable, 1, TimeUnit.DAYS);
- assertThat(adaptor.get("key"), sameInstance((Object)nonSerializable));
+ assertThat(adaptor.get("key"), sameInstance(nonSerializable));
adaptor.remove("key");
assertThat(adaptor.get("key"), is(nullValue()));

0 comments on commit 30a496f

Please sign in to comment.