summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-28 15:40:57 +0200
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-03-28 15:40:57 +0200
commit4ac1dadfcd211640a4b52db240f6f72138bd8e45 (patch)
treec1470016041799a07ee8920577f38a1a62b18d21
parent58ec090017bbdca7ac06343d0d224648d5f7ac6f (diff)
Tested version of TimeoutManager.
-rw-r--r--src/main/java/com/orbekk/protobuf/TimeoutManager.java45
-rw-r--r--src/test/java/com/orbekk/protobuf/TimeoutManagerTest.java33
2 files changed, 61 insertions, 17 deletions
diff --git a/src/main/java/com/orbekk/protobuf/TimeoutManager.java b/src/main/java/com/orbekk/protobuf/TimeoutManager.java
index a19070b..48db879 100644
--- a/src/main/java/com/orbekk/protobuf/TimeoutManager.java
+++ b/src/main/java/com/orbekk/protobuf/TimeoutManager.java
@@ -4,6 +4,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.PriorityQueue;
import java.io.Closeable;
+import java.io.IOException;
public class TimeoutManager extends Thread {
private static final Logger logger =
@@ -11,7 +12,7 @@ public class TimeoutManager extends Thread {
private Environment environment;
private PriorityQueue<Entry> entries = new PriorityQueue<Entry>();
- private static class Entry implements Comparable<? extends Entry> {
+ private static class Entry implements Comparable<Entry> {
// May not be null.
public Long timeout;
public Closeable closeable;
@@ -21,6 +22,10 @@ public class TimeoutManager extends Thread {
this.closeable = closeable;
}
+ public void close() throws IOException {
+ closeable.close();
+ }
+
@Override public int compareTo(Entry other) {
return timeout.compareTo(other.timeout);
}
@@ -31,9 +36,9 @@ public class TimeoutManager extends Thread {
void sleep(long millis) throws InterruptedException;
}
- public static class DefaultEnvironment {
+ public static class DefaultEnvironment implements Environment {
@Override public long currentTimeMillis() {
- System.currentTimeMillis();
+ return System.currentTimeMillis();
}
@Override public void sleep(long millis) throws InterruptedException {
Thread.sleep(millis);
@@ -45,29 +50,35 @@ public class TimeoutManager extends Thread {
}
public TimeoutManager() {
- self(new DefaultTime());
+ this(new DefaultEnvironment());
}
- @Override public void run() {
- while (!Thread.interrupted()) {
- synchronized (this) {
- if (entries.isEmpty()) {
- environment.wait();
- } else {
- long sleepTime = entries.peek().timeout -
- environment.currentTimeMillis();
- if (sleepTime > 0) {
- environment.sleep(sleepTime);
- }
+ public synchronized void performWork() {
+ try {
+ if (entries.isEmpty()) {
+ wait();
+ } else {
+ long sleepTime = entries.peek().timeout -
+ environment.currentTimeMillis();
+ if (sleepTime > 0) {
+ wait(sleepTime);
}
- closeExpiredEntries();
}
+ closeExpiredEntries();
+ } catch (InterruptedException e) {
+ // Handled by outer loop.
+ }
+ }
+
+ @Override public void run() {
+ while (!Thread.interrupted()) {
+ performWork();
}
}
public synchronized void closeExpiredEntries() {
long currentTime = environment.currentTimeMillis();
- while (entries.peek().timeout <= currentTime) {
+ while (!entries.isEmpty() && entries.peek().timeout <= currentTime) {
try {
entries.poll().close();
} catch (IOException e) {
diff --git a/src/test/java/com/orbekk/protobuf/TimeoutManagerTest.java b/src/test/java/com/orbekk/protobuf/TimeoutManagerTest.java
new file mode 100644
index 0000000..9e4896a
--- /dev/null
+++ b/src/test/java/com/orbekk/protobuf/TimeoutManagerTest.java
@@ -0,0 +1,33 @@
+package com.orbekk.protobuf;
+
+import static org.mockito.Mockito.*;
+
+import java.io.Closeable;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.orbekk.protobuf.TimeoutManager.Environment;
+
+public class TimeoutManagerTest {
+ Closeable closeable = mock(Closeable.class);
+ Environment environment = mock(Environment.class);
+ TimeoutManager timeoutManager = new TimeoutManager(environment);
+
+ @Before public void setUp() {
+ }
+
+ @Test public void closesExpiredEntries() throws Exception {
+ when(environment.currentTimeMillis()).thenReturn(1000l);
+ timeoutManager.addEntry(1000l, closeable);
+ timeoutManager.performWork();
+ verify(closeable).close();
+ }
+
+ @Test public void doesNotCloseUnexpiredEntry() throws Exception {
+ when(environment.currentTimeMillis()).thenReturn(123456l);
+ timeoutManager.addEntry(123457l, closeable);
+ timeoutManager.performWork();
+ verify(closeable, never()).close();
+ }
+}