mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 13:49:53 +01:00
Use milliseconds for timing.
This commit is contained in:
@@ -11,11 +11,6 @@ public interface Clock {
|
|||||||
*/
|
*/
|
||||||
long currentTimeMillis();
|
long currentTimeMillis();
|
||||||
|
|
||||||
/**
|
|
||||||
* @see System#nanoTime()
|
|
||||||
*/
|
|
||||||
long nanoTime();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see Thread#sleep(long)
|
* @see Thread#sleep(long)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -16,11 +16,6 @@ public class ArrayClock implements Clock {
|
|||||||
return times[index++];
|
return times[index++];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public long nanoTime() {
|
|
||||||
return times[index++] * 1_000_000;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sleep(long milliseconds) throws InterruptedException {
|
public void sleep(long milliseconds) throws InterruptedException {
|
||||||
Thread.sleep(milliseconds);
|
Thread.sleep(milliseconds);
|
||||||
|
|||||||
@@ -17,11 +17,6 @@ public class SettableClock implements Clock {
|
|||||||
return time.get();
|
return time.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public long nanoTime() {
|
|
||||||
return time.get() * 1_000_000;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sleep(long milliseconds) throws InterruptedException {
|
public void sleep(long milliseconds) throws InterruptedException {
|
||||||
Thread.sleep(milliseconds);
|
Thread.sleep(milliseconds);
|
||||||
|
|||||||
@@ -13,28 +13,28 @@ class TimeoutInputStream extends InputStream {
|
|||||||
|
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
private final long timeoutNs;
|
private final long timeoutMs;
|
||||||
private final CloseListener listener;
|
private final CloseListener listener;
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
@GuardedBy("lock")
|
@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) {
|
CloseListener listener) {
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
this.in = in;
|
this.in = in;
|
||||||
this.timeoutNs = timeoutNs;
|
this.timeoutMs = timeoutMs;
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
readStartedNs = clock.nanoTime();
|
readStartedMs = clock.currentTimeMillis();
|
||||||
}
|
}
|
||||||
int input = in.read();
|
int input = in.read();
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
readStartedNs = -1;
|
readStartedMs = -1;
|
||||||
}
|
}
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
@@ -47,11 +47,11 @@ class TimeoutInputStream extends InputStream {
|
|||||||
@Override
|
@Override
|
||||||
public int read(byte[] b, int off, int len) throws IOException {
|
public int read(byte[] b, int off, int len) throws IOException {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
readStartedNs = clock.nanoTime();
|
readStartedMs = clock.currentTimeMillis();
|
||||||
}
|
}
|
||||||
int read = in.read(b, off, len);
|
int read = in.read(b, off, len);
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
readStartedNs = -1;
|
readStartedMs = -1;
|
||||||
}
|
}
|
||||||
return read;
|
return read;
|
||||||
}
|
}
|
||||||
@@ -92,8 +92,8 @@ class TimeoutInputStream extends InputStream {
|
|||||||
|
|
||||||
boolean hasTimedOut() {
|
boolean hasTimedOut() {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
return readStartedNs != -1 &&
|
return readStartedMs != -1 &&
|
||||||
clock.nanoTime() - readStartedNs > timeoutNs;
|
clock.currentTimeMillis() - readStartedMs > timeoutMs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class TimeoutMonitorImpl implements TimeoutMonitor {
|
|||||||
public InputStream createTimeoutInputStream(InputStream in,
|
public InputStream createTimeoutInputStream(InputStream in,
|
||||||
long timeoutMs) {
|
long timeoutMs) {
|
||||||
TimeoutInputStream stream = new TimeoutInputStream(clock, in,
|
TimeoutInputStream stream = new TimeoutInputStream(clock, in,
|
||||||
timeoutMs * 1_000_000, this::removeStream);
|
timeoutMs, this::removeStream);
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (streams.isEmpty()) {
|
if (streams.isEmpty()) {
|
||||||
task = scheduler.scheduleWithFixedDelay(this::checkTimeouts,
|
task = scheduler.scheduleWithFixedDelay(this::checkTimeouts,
|
||||||
|
|||||||
@@ -12,11 +12,6 @@ public class SystemClock implements Clock {
|
|||||||
return System.currentTimeMillis();
|
return System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public long nanoTime() {
|
|
||||||
return System.nanoTime();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sleep(long milliseconds) throws InterruptedException {
|
public void sleep(long milliseconds) throws InterruptedException {
|
||||||
Thread.sleep(milliseconds);
|
Thread.sleep(milliseconds);
|
||||||
|
|||||||
@@ -2420,11 +2420,6 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
|
|||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public long nanoTime() {
|
|
||||||
return time * 1_000_000;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sleep(long milliseconds) throws InterruptedException {
|
public void sleep(long milliseconds) throws InterruptedException {
|
||||||
Thread.sleep(milliseconds);
|
Thread.sleep(milliseconds);
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class TimeoutInputStreamTest extends BrambleTestCase {
|
|||||||
in = new UnresponsiveInputStream();
|
in = new UnresponsiveInputStream();
|
||||||
listenerCalled = new AtomicBoolean(false);
|
listenerCalled = new AtomicBoolean(false);
|
||||||
stream = new TimeoutInputStream(new SettableClock(time), in,
|
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);
|
readReturned = new CountDownLatch(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user