|
From: <be...@us...> - 2010-11-21 16:06:37
|
Revision: 380
http://objectlabkit.svn.sourceforge.net/objectlabkit/?rev=380&view=rev
Author: benoitx
Date: 2010-11-21 16:06:31 +0000 (Sun, 21 Nov 2010)
Log Message:
-----------
Added a stop method
unit tests to use the time provider mechanism.
Modified Paths:
--------------
trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractReadOnlyExpiringCollection.java
trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMap.java
trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSet.java
trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapTest.java
trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSetTest.java
Modified: trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractReadOnlyExpiringCollection.java
===================================================================
--- trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractReadOnlyExpiringCollection.java 2010-11-21 15:45:38 UTC (rev 379)
+++ trunk/utils/src/main/java/net/objectlab/kit/collections/AbstractReadOnlyExpiringCollection.java 2010-11-21 16:06:31 UTC (rev 380)
@@ -48,7 +48,7 @@
}
protected boolean hasExpired() {
- return timeProvider.getCurrentTimeMillis() - lastLoadingTime > expiryTimeoutMilliseconds;
+ return lastLoadingTime == 0 || timeProvider.getCurrentTimeMillis() - lastLoadingTime > expiryTimeoutMilliseconds;
}
public void start() {
Modified: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMap.java
===================================================================
--- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMap.java 2010-11-21 15:45:38 UTC (rev 379)
+++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMap.java 2010-11-21 16:06:31 UTC (rev 380)
@@ -23,6 +23,7 @@
setReloadOnExpiry(builder.isReloadOnExpiry());
setLoadOnFirstAccess(builder.isLoadOnFirstAccess());
setReloadWhenExpired(builder.isReloadWhenExpired());
+ setTimeProvider(builder.getTimeProvider());
start();
}
Modified: trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSet.java
===================================================================
--- trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSet.java 2010-11-21 15:45:38 UTC (rev 379)
+++ trunk/utils/src/main/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSet.java 2010-11-21 16:06:31 UTC (rev 380)
@@ -23,6 +23,7 @@
setReloadOnExpiry(builder.isReloadOnExpiry());
setLoadOnFirstAccess(builder.isLoadOnFirstAccess());
setReloadWhenExpired(builder.isReloadWhenExpired());
+ setTimeProvider(builder.getTimeProvider());
start();
}
Modified: trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapTest.java
===================================================================
--- trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapTest.java 2010-11-21 15:45:38 UTC (rev 379)
+++ trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashMapTest.java 2010-11-21 16:06:31 UTC (rev 380)
@@ -15,13 +15,15 @@
* @author xhensevalb
*
*/
-public class ReadOnlyExpiringHashMapTest implements MapLoader<String, Integer> {
+public class ReadOnlyExpiringHashMapTest implements MapLoader<String, Integer>, TimeProvider {
private int reloadCount;
+ private long time;
@Before
public void reset() {
reloadCount = 0;
+ time = System.currentTimeMillis();
}
@Test
@@ -31,6 +33,7 @@
builder.loadOnFirstAccess(true);
builder.reloadOnExpiry(false);
builder.reloadWhenExpired(false);
+ builder.timeProvider(this);
builder.id("Greetings");
final ReadOnlyExpiringMap<String, Integer> ims = new ReadOnlyExpiringHashMap<String, Integer>(builder);
@@ -44,11 +47,10 @@
assertTrue("Correct key", ims.containsKey("Hello"));
assertNull("diff key", ims.get("Hi"));
assertEquals(Integer.valueOf(2), ims.get("Yo"));
- try {
- Thread.sleep(101);
- } catch (final InterruptedException e) {
- }
+
+ time += 101; // simulate 101 ms
+
// second call
assertFalse(ims.isEmpty());
assertEquals(2, ims.size());
@@ -57,11 +59,9 @@
assertTrue("Correct key", ims.containsKey("Hello"));
assertNull("diff key", ims.get("Hi"));
assertEquals(Integer.valueOf(2), ims.get("Yo"));
- try {
- Thread.sleep(901);
- } catch (final InterruptedException e) {
- }
+ time += 901; // simulate 901 ms
+
// should be gone
assertTrue(ims.isEmpty());
assertEquals(0, ims.size());
@@ -79,6 +79,7 @@
builder.loadOnFirstAccess(true);
builder.reloadOnExpiry(false);
builder.reloadWhenExpired(true);
+ builder.timeProvider(this);
builder.id("Greetings");
final ReadOnlyExpiringMap<String, Integer> ims = new ReadOnlyExpiringHashMap<String, Integer>(builder);
@@ -92,11 +93,9 @@
assertTrue("Correct key", ims.containsKey("Hello"));
assertNull("diff key", ims.get("Hi"));
assertEquals(Integer.valueOf(2), ims.get("Yo"));
- try {
- Thread.sleep(101);
- } catch (final InterruptedException e) {
- }
+ time += 101; // simulate 101 ms
+
// second call
assertFalse(ims.isEmpty());
assertEquals(2, ims.size());
@@ -105,11 +104,9 @@
assertTrue("Correct key", ims.containsKey("Hello"));
assertNull("diff key", ims.get("Hi"));
assertEquals(Integer.valueOf(2), ims.get("Yo"));
- try {
- Thread.sleep(901);
- } catch (final InterruptedException e) {
- }
+ time += 901; // simulate 901 ms
+
assertEquals("2) Should not call load until called", 1, reloadCount);
// should be gone
@@ -129,6 +126,7 @@
builder.loadOnFirstAccess(false);
builder.reloadOnExpiry(false);
builder.reloadWhenExpired(true);
+ builder.timeProvider(this);
builder.id("Greetings");
final ReadOnlyExpiringMap<String, Integer> ims = new ReadOnlyExpiringHashMap<String, Integer>(builder);
@@ -142,11 +140,9 @@
assertTrue("Correct key", ims.containsKey("Hello"));
assertNull("diff key", ims.get("Hi"));
assertEquals(Integer.valueOf(2), ims.get("Yo"));
- try {
- Thread.sleep(101);
- } catch (final InterruptedException e) {
- }
+ time += 101; // simulate 101 ms
+
// second call
assertFalse(ims.isEmpty());
assertEquals(2, ims.size());
@@ -155,11 +151,9 @@
assertTrue("Correct key", ims.containsKey("Hello"));
assertNull("diff key", ims.get("Hi"));
assertEquals(Integer.valueOf(2), ims.get("Yo"));
- try {
- Thread.sleep(901);
- } catch (final InterruptedException e) {
- }
+ time += 901; // simulate 901 ms
+
assertEquals("2) Should not call load until called", 1, reloadCount);
// should be gone
@@ -179,6 +173,7 @@
builder.loadOnFirstAccess(false);
builder.reloadOnExpiry(true);
builder.reloadWhenExpired(false);
+ builder.timeProvider(this);
builder.id("Greetings");
final ReadOnlyExpiringMap<String, Integer> ims = new ReadOnlyExpiringHashMap<String, Integer>(builder);
@@ -193,6 +188,8 @@
assertNull("diff key", ims.get("Hi"));
assertEquals(Integer.valueOf(2), ims.get("Yo"));
+ time += 101; // simulate 101 ms
+
// second call
assertFalse(ims.isEmpty());
assertEquals(2, ims.size());
@@ -201,8 +198,11 @@
assertTrue("Correct key", ims.containsKey("Hello"));
assertNull("diff key", ims.get("Hi"));
assertEquals(Integer.valueOf(2), ims.get("Yo"));
+
+ time += 901; // simulate 901 ms
+
try {
- Thread.sleep(901);
+ Thread.sleep(1001);
} catch (final InterruptedException e) {
}
@@ -225,4 +225,8 @@
reloadCount++;
}
+ public long getCurrentTimeMillis() {
+ return time;
+ }
+
}
Modified: trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSetTest.java
===================================================================
--- trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSetTest.java 2010-11-21 15:45:38 UTC (rev 379)
+++ trunk/utils/src/test/java/net/objectlab/kit/collections/ReadOnlyExpiringHashSetTest.java 2010-11-21 16:06:31 UTC (rev 380)
@@ -14,13 +14,15 @@
* @author xhensevalb
*
*/
-public class ReadOnlyExpiringHashSetTest implements SetLoader<String> {
+public class ReadOnlyExpiringHashSetTest implements SetLoader<String>, TimeProvider {
private int reloadCount;
+ private long time;
@Before
public void reset() {
reloadCount = 0;
+ time = System.currentTimeMillis();
}
@Test
@@ -30,10 +32,10 @@
builder.loadOnFirstAccess(true);
builder.reloadOnExpiry(false);
builder.reloadWhenExpired(false);
+ builder.timeProvider(this);
builder.id("Greetings");
final ReadOnlyExpiringSet<String> ims = new ReadOnlyExpiringHashSet<String>(builder);
-
assertEquals("Should not call load until called", 0, reloadCount);
assertFalse(ims.isEmpty());
@@ -41,22 +43,18 @@
assertEquals(1, reloadCount);
assertFalse("diff key", ims.contains("Hi"));
assertTrue("Correct key", ims.contains("Hello"));
- try {
- Thread.sleep(101);
- } catch (final InterruptedException e) {
- }
+ time += 100; // simulate 100 ms
+
// second call
assertFalse(ims.isEmpty());
assertEquals(1, ims.size());
assertEquals(1, reloadCount);
assertFalse("diff key", ims.contains("Hi"));
assertTrue("Correct key", ims.contains("Hello"));
- try {
- Thread.sleep(901);
- } catch (final InterruptedException e) {
- }
+ time += 901;
+
// should be gone
assertTrue(ims.isEmpty());
assertEquals(0, ims.size());
@@ -72,6 +70,7 @@
builder.loadOnFirstAccess(true);
builder.reloadOnExpiry(false);
builder.reloadWhenExpired(true);
+ builder.timeProvider(this);
builder.id("Greetings");
final ReadOnlyExpiringSet<String> ims = new ReadOnlyExpiringHashSet<String>(builder);
@@ -83,10 +82,7 @@
assertEquals(1, reloadCount);
assertFalse("diff key", ims.contains("Hi"));
assertTrue("Correct key", ims.contains("Hello"));
- try {
- Thread.sleep(101);
- } catch (final InterruptedException e) {
- }
+ time += 100; // simulate 100 ms
// second call
assertFalse(ims.isEmpty());
@@ -94,10 +90,7 @@
assertEquals(1, reloadCount);
assertFalse("diff key", ims.contains("Hi"));
assertTrue("Correct key", ims.contains("Hello"));
- try {
- Thread.sleep(901);
- } catch (final InterruptedException e) {
- }
+ time += 901; // simulate 901 ms
assertEquals("Should NOT have reloaded until called!", 1, reloadCount);
@@ -116,6 +109,7 @@
builder.loadOnFirstAccess(false);
builder.reloadOnExpiry(false);
builder.reloadWhenExpired(true);
+ builder.timeProvider(this);
builder.id("Greetings");
final ReadOnlyExpiringSet<String> ims = new ReadOnlyExpiringHashSet<String>(builder);
@@ -123,14 +117,12 @@
assertEquals("Should call load immediately", 1, reloadCount);
assertFalse(ims.isEmpty());
+ assertEquals(1, reloadCount);
assertEquals(1, ims.size());
assertEquals(1, reloadCount);
assertFalse("diff key", ims.contains("Hi"));
assertTrue("Correct key", ims.contains("Hello"));
- try {
- Thread.sleep(101);
- } catch (final InterruptedException e) {
- }
+ time += 100; // simulate 100 ms
// second call
assertFalse(ims.isEmpty());
@@ -138,10 +130,7 @@
assertEquals(1, reloadCount);
assertFalse("diff key", ims.contains("Hi"));
assertTrue("Correct key", ims.contains("Hello"));
- try {
- Thread.sleep(901);
- } catch (final InterruptedException e) {
- }
+ time += 901; // simulate 901 ms
assertEquals("Should NOT have reloaded until called!", 1, reloadCount);
@@ -160,6 +149,7 @@
builder.loadOnFirstAccess(false);
builder.reloadOnExpiry(true);
builder.reloadWhenExpired(false); // but does not matter
+ builder.timeProvider(this);
builder.id("Greetings");
final ReadOnlyExpiringSet<String> ims = new ReadOnlyExpiringHashSet<String>(builder);
@@ -171,20 +161,20 @@
assertEquals(1, reloadCount);
assertFalse("diff key", ims.contains("Hi"));
assertTrue("Correct key", ims.contains("Hello"));
- // try {
- // Thread.sleep(101);
- // } catch (final InterruptedException e) {
- // }
+ time += 101;
+
// second call
assertFalse(ims.isEmpty());
assertEquals(1, ims.size());
assertEquals(1, reloadCount);
assertFalse("diff key", ims.contains("Hi"));
assertTrue("Correct key", ims.contains("Hello"));
+ time += 901; // simulate 901 ms
+
try {
- Thread.sleep(901);
- } catch (final InterruptedException e) {
+ Thread.sleep(1000); // ensure that the timer can catch up.
+ } catch (InterruptedException e) {
}
assertEquals("Should have reloaded until called!", 2, reloadCount);
@@ -202,4 +192,8 @@
builder.add("Hello");
reloadCount++;
}
+
+ public long getCurrentTimeMillis() {
+ return time;
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|