Commit c80d3196 authored by akwizgran's avatar akwizgran

Use milliseconds for timing.

parent c4273d22
Pipeline #4370 passed with stage
in 9 minutes and 13 seconds
......@@ -11,11 +11,6 @@ public interface Clock {
*/
long currentTimeMillis();
/**
* @see System#nanoTime()
*/
long nanoTime();
/**
* @see Thread#sleep(long)
*/
......
......@@ -16,11 +16,6 @@ public class ArrayClock implements Clock {
return times[index++];
}
@Override
public long nanoTime() {
return times[index++] * 1_000_000;
}
@Override
public void sleep(long milliseconds) throws InterruptedException {
Thread.sleep(milliseconds);
......
......@@ -17,11 +17,6 @@ public class SettableClock implements Clock {
return time.get();
}
@Override
public long nanoTime() {
return time.get() * 1_000_000;
}
@Override
public void sleep(long milliseconds) throws InterruptedException {
Thread.sleep(milliseconds);
......
......@@ -13,28 +13,28 @@ class TimeoutInputStream extends InputStream {
private final Clock clock;
private final InputStream in;
private final long timeoutNs;
private final long timeoutMs;
private final CloseListener listener;
private final Object lock = new Object();
@GuardedBy("lock")
private long readStartedNs = -1;
private long readStartedMs = -1;
TimeoutInputStream(Clock clock, InputStream in, long timeoutNs,
TimeoutInputStream(Clock clock, InputStream in, long timeoutMs,
CloseListener listener) {
this.clock = clock;
this.in = in;
this.timeoutNs = timeoutNs;
this.timeoutMs = timeoutMs;
this.listener = listener;
}
@Override
public int read() throws IOException {
synchronized (lock) {
readStartedNs = clock.nanoTime();
readStartedMs = clock.currentTimeMillis();
}
int input = in.read();
synchronized (lock) {
readStartedNs = -1;
readStartedMs = -1;
}
return input;
}
......@@ -47,11 +47,11 @@ class TimeoutInputStream extends InputStream {
@Override
public int read(byte[] b, int off, int len) throws IOException {
synchronized (lock) {
readStartedNs = clock.nanoTime();
readStartedMs = clock.currentTimeMillis();
}
int read = in.read(b, off, len);
synchronized (lock) {
readStartedNs = -1;
readStartedMs = -1;
}
return read;
}
......@@ -92,8 +92,8 @@ class TimeoutInputStream extends InputStream {
boolean hasTimedOut() {
synchronized (lock) {
return readStartedNs != -1 &&
clock.nanoTime() - readStartedNs > timeoutNs;
return readStartedMs != -1 &&
clock.currentTimeMillis() - readStartedMs > timeoutMs;
}
}
......
......@@ -52,7 +52,7 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
public InputStream createTimeoutInputStream(InputStream in,
long timeoutMs) {
TimeoutInputStream stream = new TimeoutInputStream(clock, in,
timeoutMs * 1_000_000, this::removeStream);
timeoutMs, this::removeStream);
synchronized (lock) {
if (streams.isEmpty()) {
task = scheduler.scheduleWithFixedDelay(this::checkTimeouts,
......
......@@ -12,11 +12,6 @@ public class SystemClock implements Clock {
return System.currentTimeMillis();
}
@Override
public long nanoTime() {
return System.nanoTime();
}
@Override
public void sleep(long milliseconds) throws InterruptedException {
Thread.sleep(milliseconds);
......
......@@ -2420,11 +2420,6 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
return time;
}
@Override
public long nanoTime() {
return time * 1_000_000;
}
@Override
public void sleep(long milliseconds) throws InterruptedException {
Thread.sleep(milliseconds);
......
......@@ -36,7 +36,7 @@ public class TimeoutInputStreamTest extends BrambleTestCase {
in = new UnresponsiveInputStream();
listenerCalled = new AtomicBoolean(false);
stream = new TimeoutInputStream(new SettableClock(time), in,
TIMEOUT_MS * 1_000_000, stream -> listenerCalled.set(true));
TIMEOUT_MS, stream -> listenerCalled.set(true));
readReturned = new CountDownLatch(1);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment