[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [onionoo/master] Use reflection and overloading in order to facilitate testing and
commit db7377faa0b5398fe88e6dd83fec61956b992ce5
Author: iwakeh <iwakeh@xxxxxxxxxxxxxx>
Date: Wed May 17 13:26:37 2017 +0000
Use reflection and overloading in order to facilitate testing and
to avoid the use of TimeFactory and Time classes. This also makes
it more obvious where time is changed for making the tests work.
---
.../torproject/onionoo/docs/BandwidthStatus.java | 12 ++++----
.../org/torproject/onionoo/docs/ClientsStatus.java | 4 +--
.../org/torproject/onionoo/docs/DocumentStore.java | 10 +------
.../org/torproject/onionoo/docs/WeightsStatus.java | 4 +--
.../org/torproject/onionoo/server/NodeIndexer.java | 18 +++++++-----
.../onionoo/server/PerformanceMetrics.java | 9 +-----
.../torproject/onionoo/server/ResourceServlet.java | 19 ++++++-------
.../onionoo/updater/NodeDetailsStatusUpdater.java | 3 +-
.../onionoo/updater/RdnsLookupRequest.java | 6 ++--
.../onionoo/updater/RdnsLookupWorker.java | 2 +-
.../onionoo/updater/ReverseDomainNameResolver.java | 10 +------
.../java/org/torproject/onionoo/util/Time.java | 18 ------------
.../org/torproject/onionoo/util/TimeFactory.java | 32 ---------------------
.../onionoo/writer/BandwidthDocumentWriter.java | 3 +-
.../onionoo/writer/ClientsDocumentWriter.java | 3 +-
.../onionoo/writer/UptimeDocumentWriter.java | 3 +-
.../onionoo/writer/WeightsDocumentWriter.java | 3 +-
.../onionoo/docs/BandwidthStatusTest.java | 16 +++--------
.../onionoo/server/ResourceServletTest.java | 26 ++++++++---------
.../org/torproject/onionoo/util/DummyTime.java | 17 -----------
.../onionoo/writer/UptimeDocumentWriterTest.java | 33 +++++++++++++++-------
21 files changed, 80 insertions(+), 171 deletions(-)
diff --git a/src/main/java/org/torproject/onionoo/docs/BandwidthStatus.java b/src/main/java/org/torproject/onionoo/docs/BandwidthStatus.java
index 911af2a..b58e57c 100644
--- a/src/main/java/org/torproject/onionoo/docs/BandwidthStatus.java
+++ b/src/main/java/org/torproject/onionoo/docs/BandwidthStatus.java
@@ -4,7 +4,6 @@
package org.torproject.onionoo.docs;
import org.torproject.descriptor.BandwidthHistory;
-import org.torproject.onionoo.util.TimeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -110,18 +109,21 @@ public class BandwidthStatus extends Document {
}
public void compressHistory() {
- this.compressHistory(this.writeHistory);
- this.compressHistory(this.readHistory);
+ this.compressHistory(System.currentTimeMillis());
}
- private void compressHistory(SortedMap<Long, long[]> history) {
+ public void compressHistory(long now) {
+ this.compressHistory(this.writeHistory, now);
+ this.compressHistory(this.readHistory, now);
+ }
+
+ private void compressHistory(SortedMap<Long, long[]> history, long now) {
SortedMap<Long, long[]> uncompressedHistory = new TreeMap<>(history);
history.clear();
long lastStartMillis = 0L;
long lastEndMillis = 0L;
long lastBandwidth = 0L;
String lastMonthString = "1970-01";
- long now = TimeFactory.getTime().currentTimeMillis();
for (long[] v : uncompressedHistory.values()) {
long startMillis = v[0];
long endMillis = v[1];
diff --git a/src/main/java/org/torproject/onionoo/docs/ClientsStatus.java b/src/main/java/org/torproject/onionoo/docs/ClientsStatus.java
index cfc424e..45a4ddf 100644
--- a/src/main/java/org/torproject/onionoo/docs/ClientsStatus.java
+++ b/src/main/java/org/torproject/onionoo/docs/ClientsStatus.java
@@ -3,8 +3,6 @@
package org.torproject.onionoo.docs;
-import org.torproject.onionoo.util.TimeFactory;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -76,7 +74,7 @@ public class ClientsStatus extends Document {
history.clear();
ClientsHistory lastResponses = null;
String lastMonthString = "1970-01";
- long now = TimeFactory.getTime().currentTimeMillis();
+ long now = System.currentTimeMillis();
for (ClientsHistory responses : uncompressedHistory) {
long intervalLengthMillis;
if (now - responses.getEndMillis()
diff --git a/src/main/java/org/torproject/onionoo/docs/DocumentStore.java b/src/main/java/org/torproject/onionoo/docs/DocumentStore.java
index 39d6271..34bc8ef 100644
--- a/src/main/java/org/torproject/onionoo/docs/DocumentStore.java
+++ b/src/main/java/org/torproject/onionoo/docs/DocumentStore.java
@@ -4,8 +4,6 @@
package org.torproject.onionoo.docs;
import org.torproject.onionoo.util.FormattingUtils;
-import org.torproject.onionoo.util.Time;
-import org.torproject.onionoo.util.TimeFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@@ -52,12 +50,6 @@ public class DocumentStore {
this.outDir = outDir;
}
- private Time time;
-
- public DocumentStore() {
- this.time = TimeFactory.getTime();
- }
-
private long listOperations = 0L;
private long listedFiles = 0L;
@@ -814,7 +806,7 @@ public class DocumentStore {
return;
}
UpdateStatus updateStatus = new UpdateStatus();
- updateStatus.setUpdatedMillis(this.time.currentTimeMillis());
+ updateStatus.setUpdatedMillis(System.currentTimeMillis());
this.store(updateStatus);
}
diff --git a/src/main/java/org/torproject/onionoo/docs/WeightsStatus.java b/src/main/java/org/torproject/onionoo/docs/WeightsStatus.java
index 3257283..4c9c899 100644
--- a/src/main/java/org/torproject/onionoo/docs/WeightsStatus.java
+++ b/src/main/java/org/torproject/onionoo/docs/WeightsStatus.java
@@ -3,8 +3,6 @@
package org.torproject.onionoo.docs;
-import org.torproject.onionoo.util.TimeFactory;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -137,7 +135,7 @@ public class WeightsStatus extends Document {
double[] lastWeights = null;
String lastMonthString = "1970-01";
int lastMissingValues = -1;
- long now = TimeFactory.getTime().currentTimeMillis();
+ long now = System.currentTimeMillis();
for (Map.Entry<long[], double[]> e : uncompressedHistory.entrySet()) {
long startMillis = e.getKey()[0];
long endMillis = e.getKey()[1];
diff --git a/src/main/java/org/torproject/onionoo/server/NodeIndexer.java b/src/main/java/org/torproject/onionoo/server/NodeIndexer.java
index d380aaa..f6b9510 100644
--- a/src/main/java/org/torproject/onionoo/server/NodeIndexer.java
+++ b/src/main/java/org/torproject/onionoo/server/NodeIndexer.java
@@ -7,8 +7,6 @@ import org.torproject.onionoo.docs.DocumentStore;
import org.torproject.onionoo.docs.DocumentStoreFactory;
import org.torproject.onionoo.docs.SummaryDocument;
import org.torproject.onionoo.docs.UpdateStatus;
-import org.torproject.onionoo.util.Time;
-import org.torproject.onionoo.util.TimeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -125,6 +123,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
indexerThread.interrupt();
}
+ private long specialTime = -1L;
+
private void indexNodeStatuses() {
long updateStatusMillis = -1L;
DocumentStore documentStore = DocumentStoreFactory.getDocumentStore();
@@ -173,7 +173,7 @@ public class NodeIndexer implements ServletContextListener, Runnable {
currentBridges.add(node);
}
}
- Time time = TimeFactory.getTime();
+
/* This variable can go away once all Onionoo services had their
* hourly updater write effective families to summary documents at
* least once. Remove this code after September 8, 2015. */
@@ -220,7 +220,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
if (entry.getEffectiveFamily() != null) {
newRelaysByFamily.put(fingerprint, entry.getEffectiveFamily());
}
- int daysSinceFirstSeen = (int) ((time.currentTimeMillis()
+ int daysSinceFirstSeen = (int) ((
+ (specialTime < 0 ? System.currentTimeMillis() : specialTime)
- entry.getFirstSeenMillis()) / ONE_DAY);
if (!newRelaysByFirstSeenDays.containsKey(daysSinceFirstSeen)) {
newRelaysByFirstSeenDays.put(daysSinceFirstSeen,
@@ -229,7 +230,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
newRelaysByFirstSeenDays.get(daysSinceFirstSeen).add(fingerprint);
newRelaysByFirstSeenDays.get(daysSinceFirstSeen).add(
hashedFingerprint);
- int daysSinceLastSeen = (int) ((time.currentTimeMillis()
+ int daysSinceLastSeen = (int) ((
+ (specialTime < 0 ? System.currentTimeMillis() : specialTime)
- entry.getLastSeenMillis()) / ONE_DAY);
if (!newRelaysByLastSeenDays.containsKey(daysSinceLastSeen)) {
newRelaysByLastSeenDays.put(daysSinceLastSeen,
@@ -277,7 +279,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
newBridgesByFlag.get(flagLowerCase).add(
hashedHashedFingerprint);
}
- int daysSinceFirstSeen = (int) ((time.currentTimeMillis()
+ int daysSinceFirstSeen = (int) ((
+ (specialTime < 0 ? System.currentTimeMillis() : specialTime)
- entry.getFirstSeenMillis()) / ONE_DAY);
if (!newBridgesByFirstSeenDays.containsKey(daysSinceFirstSeen)) {
newBridgesByFirstSeenDays.put(daysSinceFirstSeen,
@@ -287,7 +290,8 @@ public class NodeIndexer implements ServletContextListener, Runnable {
hashedFingerprint);
newBridgesByFirstSeenDays.get(daysSinceFirstSeen).add(
hashedHashedFingerprint);
- int daysSinceLastSeen = (int) ((time.currentTimeMillis()
+ int daysSinceLastSeen = (int) ((
+ (specialTime < 0 ? System.currentTimeMillis() : specialTime)
- entry.getLastSeenMillis()) / ONE_DAY);
if (!newBridgesByLastSeenDays.containsKey(daysSinceLastSeen)) {
newBridgesByLastSeenDays.put(daysSinceLastSeen,
diff --git a/src/main/java/org/torproject/onionoo/server/PerformanceMetrics.java b/src/main/java/org/torproject/onionoo/server/PerformanceMetrics.java
index 1f52397..935915c 100644
--- a/src/main/java/org/torproject/onionoo/server/PerformanceMetrics.java
+++ b/src/main/java/org/torproject/onionoo/server/PerformanceMetrics.java
@@ -4,8 +4,6 @@
package org.torproject.onionoo.server;
import org.torproject.onionoo.docs.DateTimeHelper;
-import org.torproject.onionoo.util.Time;
-import org.torproject.onionoo.util.TimeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -21,8 +19,6 @@ public class PerformanceMetrics {
private static final Object lock = new Object();
- private static Time time;
-
private static long lastLoggedMillis = -1L;
private static final long LOG_INTERVAL_SECONDS = 60L * 60L;
@@ -62,11 +58,8 @@ public class PerformanceMetrics {
int bridgeDocumentsWritten, int charsWritten,
long writtenResponseMillis) {
synchronized (lock) {
- if (time == null) {
- time = TimeFactory.getTime();
- }
if (lastLoggedMillis < 0L) {
- lastLoggedMillis = time.currentTimeMillis();
+ lastLoggedMillis = System.currentTimeMillis();
} else if (receivedRequestMillis - lastLoggedMillis
> LOG_INTERVAL_MILLIS) {
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(
diff --git a/src/main/java/org/torproject/onionoo/server/ResourceServlet.java b/src/main/java/org/torproject/onionoo/server/ResourceServlet.java
index c5fd949..869574c 100644
--- a/src/main/java/org/torproject/onionoo/server/ResourceServlet.java
+++ b/src/main/java/org/torproject/onionoo/server/ResourceServlet.java
@@ -3,9 +3,6 @@
package org.torproject.onionoo.server;
-import org.torproject.onionoo.util.Time;
-import org.torproject.onionoo.util.TimeFactory;
-
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
@@ -81,12 +78,18 @@ public class ResourceServlet extends HttpServlet {
private static Pattern ipv6AddressPattern =
Pattern.compile(ipv6AddressPatternString);
+ public void doGet(HttpServletRequestWrapper request,
+ HttpServletResponseWrapper response) throws IOException {
+ doGet(request, response, System.currentTimeMillis());
+ }
+
/** Handles the HTTP GET request in the wrapped <code>request</code> by
* writing an HTTP GET response to the likewise <code>response</code>,
* both of which are wrapped to facilitate testing. */
@SuppressWarnings("checkstyle:variabledeclarationusagedistance")
public void doGet(HttpServletRequestWrapper request,
- HttpServletResponseWrapper response) throws IOException {
+ HttpServletResponseWrapper response, long receivedRequestMillis)
+ throws IOException {
if (this.maintenanceMode) {
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
@@ -99,10 +102,6 @@ public class ResourceServlet extends HttpServlet {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
-
- Time time = TimeFactory.getTime();
- long receivedRequestMillis = time.currentTimeMillis();
-
String uri = request.getRequestURI();
if (uri.startsWith("/onionoo/")) {
uri = uri.substring("/onionoo".length());
@@ -323,7 +322,7 @@ public class ResourceServlet extends HttpServlet {
rh.setFamily(family);
}
rh.handleRequest();
- long parsedRequestMillis = time.currentTimeMillis();
+ long parsedRequestMillis = System.currentTimeMillis();
ResponseBuilder rb = new ResponseBuilder();
rb.setResourceType(resourceType);
@@ -364,7 +363,7 @@ public class ResourceServlet extends HttpServlet {
int relayDocumentsWritten = rh.getOrderedRelays().size();
int bridgeDocumentsWritten = rh.getOrderedBridges().size();
int charsWritten = rb.getCharsWritten();
- long writtenResponseMillis = time.currentTimeMillis();
+ long writtenResponseMillis = System.currentTimeMillis();
PerformanceMetrics.logStatistics(receivedRequestMillis, resourceType,
parameterMap.keySet(), parsedRequestMillis, relayDocumentsWritten,
bridgeDocumentsWritten, charsWritten, writtenResponseMillis);
diff --git a/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java b/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java
index 802fb9d..8172cb7 100644
--- a/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java
+++ b/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java
@@ -16,7 +16,6 @@ import org.torproject.onionoo.docs.DocumentStore;
import org.torproject.onionoo.docs.DocumentStoreFactory;
import org.torproject.onionoo.docs.NodeStatus;
import org.torproject.onionoo.util.FormattingUtils;
-import org.torproject.onionoo.util.TimeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -104,7 +103,7 @@ public class NodeDetailsStatusUpdater implements DescriptorListener,
this.reverseDomainNameResolver = reverseDomainNameResolver;
this.lookupService = lookupService;
this.documentStore = DocumentStoreFactory.getDocumentStore();
- this.now = TimeFactory.getTime().currentTimeMillis();
+ this.now = System.currentTimeMillis();
this.registerDescriptorListeners();
}
diff --git a/src/main/java/org/torproject/onionoo/updater/RdnsLookupRequest.java b/src/main/java/org/torproject/onionoo/updater/RdnsLookupRequest.java
index 3f2f561..959468a 100644
--- a/src/main/java/org/torproject/onionoo/updater/RdnsLookupRequest.java
+++ b/src/main/java/org/torproject/onionoo/updater/RdnsLookupRequest.java
@@ -30,8 +30,7 @@ class RdnsLookupRequest extends Thread {
@Override
public void run() {
- this.lookupStartedMillis =
- this.reverseDomainNameResolver.time.currentTimeMillis();
+ this.lookupStartedMillis = System.currentTimeMillis();
try {
String result = InetAddress.getByName(this.address).getHostName();
synchronized (this) {
@@ -40,8 +39,7 @@ class RdnsLookupRequest extends Thread {
} catch (UnknownHostException e) {
/* We'll try again the next time. */
}
- this.lookupCompletedMillis =
- this.reverseDomainNameResolver.time.currentTimeMillis();
+ this.lookupCompletedMillis = System.currentTimeMillis();
this.parent.interrupt();
}
diff --git a/src/main/java/org/torproject/onionoo/updater/RdnsLookupWorker.java b/src/main/java/org/torproject/onionoo/updater/RdnsLookupWorker.java
index e490075..72494ed 100644
--- a/src/main/java/org/torproject/onionoo/updater/RdnsLookupWorker.java
+++ b/src/main/java/org/torproject/onionoo/updater/RdnsLookupWorker.java
@@ -13,7 +13,7 @@ class RdnsLookupWorker extends Thread {
@Override
public void run() {
- while (this.reverseDomainNameResolver.time.currentTimeMillis()
+ while (System.currentTimeMillis()
- ReverseDomainNameResolver.RDNS_LOOKUP_MAX_DURATION_MILLIS
<= this.reverseDomainNameResolver.startedRdnsLookups) {
String rdnsLookupJob = null;
diff --git a/src/main/java/org/torproject/onionoo/updater/ReverseDomainNameResolver.java b/src/main/java/org/torproject/onionoo/updater/ReverseDomainNameResolver.java
index 04222bf..366d5ce 100644
--- a/src/main/java/org/torproject/onionoo/updater/ReverseDomainNameResolver.java
+++ b/src/main/java/org/torproject/onionoo/updater/ReverseDomainNameResolver.java
@@ -4,8 +4,6 @@
package org.torproject.onionoo.updater;
import org.torproject.onionoo.util.FormattingUtils;
-import org.torproject.onionoo.util.Time;
-import org.torproject.onionoo.util.TimeFactory;
import java.util.ArrayList;
import java.util.Collections;
@@ -17,12 +15,6 @@ import java.util.Set;
public class ReverseDomainNameResolver {
- Time time;
-
- public ReverseDomainNameResolver() {
- this.time = TimeFactory.getTime();
- }
-
static final long RDNS_LOOKUP_MAX_REQUEST_MILLIS = 10L * 1000L;
static final long RDNS_LOOKUP_MAX_DURATION_MILLIS = 5L * 60L * 1000L;
@@ -51,7 +43,7 @@ public class ReverseDomainNameResolver {
/** Starts reverse domain name lookups in one or more background
* threads and returns immediately. */
public void startReverseDomainNameLookups() {
- this.startedRdnsLookups = this.time.currentTimeMillis();
+ this.startedRdnsLookups = System.currentTimeMillis();
this.rdnsLookupJobs = new HashSet<>();
for (Map.Entry<String, Long> e :
this.addressLastLookupTimes.entrySet()) {
diff --git a/src/main/java/org/torproject/onionoo/util/Time.java b/src/main/java/org/torproject/onionoo/util/Time.java
deleted file mode 100644
index f06ceb6..0000000
--- a/src/main/java/org/torproject/onionoo/util/Time.java
+++ /dev/null
@@ -1,18 +0,0 @@
-/* Copyright 2013--2017 The Tor Project
- * See LICENSE for licensing information */
-
-package org.torproject.onionoo.util;
-
-/*
- * Wrapper for System.currentTimeMillis() that can be replaced with a
- * custom time source for testing.
- *
- * @deprecated Try to use other time setting methods for testing.
- */
-public class Time {
-
- public long currentTimeMillis() {
- return System.currentTimeMillis();
- }
-}
-
diff --git a/src/main/java/org/torproject/onionoo/util/TimeFactory.java b/src/main/java/org/torproject/onionoo/util/TimeFactory.java
deleted file mode 100644
index 68dcc98..0000000
--- a/src/main/java/org/torproject/onionoo/util/TimeFactory.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/* Copyright 2014--2017 The Tor Project
- * See LICENSE for licensing information */
-
-package org.torproject.onionoo.util;
-
-public class TimeFactory {
-
- private static Time timeInstance;
-
- /** Sets a custom singleton time instance that will be returned by
- * {@link #getTime} rather than creating an instance upon first
- * invocation.
- *
- * @deprecated Try to use other time setting methods for testing.
- */
- public static void setTime(Time time) {
- timeInstance = time;
- }
-
- /** Returns the singleton node indexer instance that gets created upon
- * first invocation of this method.
- *
- * @deprecated Try to use other time setting methods for testing.
- */
- public static Time getTime() {
- if (timeInstance == null) {
- timeInstance = new Time();
- }
- return timeInstance;
- }
-}
-
diff --git a/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java
index a809aba..d4b20d7 100644
--- a/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java
+++ b/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java
@@ -10,7 +10,6 @@ import org.torproject.onionoo.docs.DocumentStore;
import org.torproject.onionoo.docs.DocumentStoreFactory;
import org.torproject.onionoo.docs.GraphHistory;
import org.torproject.onionoo.docs.UpdateStatus;
-import org.torproject.onionoo.util.TimeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -33,7 +32,7 @@ public class BandwidthDocumentWriter implements DocumentWriter {
public BandwidthDocumentWriter() {
this.documentStore = DocumentStoreFactory.getDocumentStore();
- this.now = TimeFactory.getTime().currentTimeMillis();
+ this.now = System.currentTimeMillis();
}
@Override
diff --git a/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java
index fb262e9..599ecff 100644
--- a/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java
+++ b/src/main/java/org/torproject/onionoo/writer/ClientsDocumentWriter.java
@@ -12,7 +12,6 @@ import org.torproject.onionoo.docs.DocumentStore;
import org.torproject.onionoo.docs.DocumentStoreFactory;
import org.torproject.onionoo.docs.UpdateStatus;
import org.torproject.onionoo.util.FormattingUtils;
-import org.torproject.onionoo.util.TimeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -59,7 +58,7 @@ public class ClientsDocumentWriter implements DocumentWriter {
public ClientsDocumentWriter() {
this.documentStore = DocumentStoreFactory.getDocumentStore();
- this.now = TimeFactory.getTime().currentTimeMillis();
+ this.now = System.currentTimeMillis();
}
private int writtenDocuments = 0;
diff --git a/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java
index 0154604..8910e3a 100644
--- a/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java
+++ b/src/main/java/org/torproject/onionoo/writer/UptimeDocumentWriter.java
@@ -12,7 +12,6 @@ import org.torproject.onionoo.docs.UptimeDocument;
import org.torproject.onionoo.docs.UptimeHistory;
import org.torproject.onionoo.docs.UptimeStatus;
import org.torproject.onionoo.util.FormattingUtils;
-import org.torproject.onionoo.util.TimeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -37,7 +36,7 @@ public class UptimeDocumentWriter implements DocumentWriter {
public UptimeDocumentWriter() {
this.documentStore = DocumentStoreFactory.getDocumentStore();
- this.now = TimeFactory.getTime().currentTimeMillis();
+ this.now = System.currentTimeMillis();
}
@Override
diff --git a/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java
index 5b45b78..a4568f5 100644
--- a/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java
+++ b/src/main/java/org/torproject/onionoo/writer/WeightsDocumentWriter.java
@@ -10,7 +10,6 @@ import org.torproject.onionoo.docs.GraphHistory;
import org.torproject.onionoo.docs.UpdateStatus;
import org.torproject.onionoo.docs.WeightsDocument;
import org.torproject.onionoo.docs.WeightsStatus;
-import org.torproject.onionoo.util.TimeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -33,7 +32,7 @@ public class WeightsDocumentWriter implements DocumentWriter {
public WeightsDocumentWriter() {
this.documentStore = DocumentStoreFactory.getDocumentStore();
- this.now = TimeFactory.getTime().currentTimeMillis();
+ this.now = System.currentTimeMillis();
}
@Override
diff --git a/src/test/java/org/torproject/onionoo/docs/BandwidthStatusTest.java b/src/test/java/org/torproject/onionoo/docs/BandwidthStatusTest.java
index 6fcd05b..c74b1ec 100644
--- a/src/test/java/org/torproject/onionoo/docs/BandwidthStatusTest.java
+++ b/src/test/java/org/torproject/onionoo/docs/BandwidthStatusTest.java
@@ -8,10 +8,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.torproject.descriptor.BandwidthHistory;
-import org.torproject.onionoo.util.DummyTime;
-import org.torproject.onionoo.util.TimeFactory;
-import org.junit.Before;
import org.junit.Test;
import java.util.SortedMap;
@@ -19,14 +16,9 @@ import java.util.TreeMap;
public class BandwidthStatusTest {
- private long currentTimeMillis = DateTimeHelper.parse(
+ private static final long TEST_TIME = DateTimeHelper.parse(
"2014-08-01 02:22:22");
- @Before
- public void createDummyTime() {
- TimeFactory.setTime(new DummyTime(this.currentTimeMillis));
- }
-
@Test()
public void testEmptyStatusNotDirty() {
BandwidthStatus bandwidthStatus = new BandwidthStatus();
@@ -123,7 +115,7 @@ public class BandwidthStatusTest {
"w 2014-08-01 00:07:22 2014-08-01 00:22:22 30720\n"
+ "w 2014-08-01 00:22:22 2014-08-01 00:37:22 4096\n";
bandwidthStatus.setFromDocumentString(existingLines);
- bandwidthStatus.compressHistory();
+ bandwidthStatus.compressHistory(TEST_TIME);
assertEquals("Two recent intervals should not be compressed.",
existingLines, bandwidthStatus.toDocumentString());
}
@@ -134,7 +126,7 @@ public class BandwidthStatusTest {
bandwidthStatus.setFromDocumentString(
"w 2013-08-01 00:07:22 2013-08-01 00:22:22 30720\n"
+ "w 2013-08-01 00:22:22 2013-08-01 00:37:22 4096\n");
- bandwidthStatus.compressHistory();
+ bandwidthStatus.compressHistory(TEST_TIME);
assertEquals("Two old intervals should be compressed into one.",
"w 2013-08-01 00:07:22 2013-08-01 00:37:22 34816\n",
bandwidthStatus.toDocumentString());
@@ -147,7 +139,7 @@ public class BandwidthStatusTest {
"w 2013-07-31 23:52:22 2013-08-01 00:07:22 4096\n"
+ "w 2013-08-01 00:07:22 2013-08-01 00:22:22 30720\n";
bandwidthStatus.setFromDocumentString(statusLines);
- bandwidthStatus.compressHistory();
+ bandwidthStatus.compressHistory(TEST_TIME);
assertEquals("Two old intervals should not be merged over month end.",
statusLines, bandwidthStatus.toDocumentString());
}
diff --git a/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java b/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java
index a28c66b..04c5d7d 100644
--- a/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java
+++ b/src/test/java/org/torproject/onionoo/server/ResourceServletTest.java
@@ -6,14 +6,12 @@ package org.torproject.onionoo.server;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
import org.torproject.onionoo.docs.DateTimeHelper;
import org.torproject.onionoo.docs.DocumentStoreFactory;
import org.torproject.onionoo.docs.DummyDocumentStore;
import org.torproject.onionoo.docs.UpdateStatus;
-import org.torproject.onionoo.util.DummyTime;
-import org.torproject.onionoo.util.Time;
-import org.torproject.onionoo.util.TimeFactory;
import com.google.gson.Gson;
@@ -23,6 +21,7 @@ import org.junit.Test;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@@ -41,8 +40,7 @@ public class ResourceServletTest {
private SortedMap<String, org.torproject.onionoo.docs.SummaryDocument>
bridges;
- private long currentTimeMillis = DateTimeHelper.parse(
- "2013-04-24 12:22:22");
+ private static long TEST_TIME = DateTimeHelper.parse("2013-04-24 12:22:22");
private class TestingHttpServletRequestWrapper
extends HttpServletRequestWrapper {
@@ -211,7 +209,6 @@ public class ResourceServletTest {
private void runTest(String request) {
try {
- this.createDummyTime();
this.createDummyDocumentStore();
this.createNodeIndexer();
this.makeRequest(request);
@@ -221,15 +218,10 @@ public class ResourceServletTest {
}
}
- private void createDummyTime() {
- Time dummyTime = new DummyTime(this.currentTimeMillis);
- TimeFactory.setTime(dummyTime);
- }
-
private void createDummyDocumentStore() {
DummyDocumentStore documentStore = new DummyDocumentStore();
UpdateStatus updateStatus = new UpdateStatus();
- updateStatus.setUpdatedMillis(this.currentTimeMillis);
+ updateStatus.setUpdatedMillis(TEST_TIME);
documentStore.addDocument(updateStatus, null);
for (Map.Entry<String, org.torproject.onionoo.docs.SummaryDocument> e :
this.relays.entrySet()) {
@@ -244,6 +236,14 @@ public class ResourceServletTest {
private void createNodeIndexer() {
NodeIndexer newNodeIndexer = new NodeIndexer();
+ try {
+ Field specialTimeField = newNodeIndexer.getClass()
+ .getDeclaredField("specialTime");
+ specialTimeField.setAccessible(true);
+ specialTimeField.set(newNodeIndexer, TEST_TIME);
+ } catch (Exception ex) {
+ fail("Cannot manipulate test-time. Failing all.");
+ }
newNodeIndexer.startIndexing();
NodeIndexerFactory.setNodeIndexer(newNodeIndexer);
}
@@ -257,7 +257,7 @@ public class ResourceServletTest {
this.request = new TestingHttpServletRequestWrapper(path, queryString,
parameterMap);
this.response = new TestingHttpServletResponseWrapper();
- rs.doGet(this.request, this.response);
+ rs.doGet(this.request, this.response, TEST_TIME);
}
private void parseResponse() {
diff --git a/src/test/java/org/torproject/onionoo/util/DummyTime.java b/src/test/java/org/torproject/onionoo/util/DummyTime.java
deleted file mode 100644
index 54b419d..0000000
--- a/src/test/java/org/torproject/onionoo/util/DummyTime.java
+++ /dev/null
@@ -1,17 +0,0 @@
-/* Copyright 2014--2017 The Tor Project
- * See LICENSE for licensing information */
-
-package org.torproject.onionoo.util;
-
-public class DummyTime extends Time {
- private long currentTimeMillis;
-
- public DummyTime(long currentTimeMillis) {
- this.currentTimeMillis = currentTimeMillis;
- }
-
- public long currentTimeMillis() {
- return this.currentTimeMillis;
- }
-}
-
diff --git a/src/test/java/org/torproject/onionoo/writer/UptimeDocumentWriterTest.java b/src/test/java/org/torproject/onionoo/writer/UptimeDocumentWriterTest.java
index 0c19a04..b49853d 100644
--- a/src/test/java/org/torproject/onionoo/writer/UptimeDocumentWriterTest.java
+++ b/src/test/java/org/torproject/onionoo/writer/UptimeDocumentWriterTest.java
@@ -5,6 +5,7 @@ package org.torproject.onionoo.writer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import org.torproject.onionoo.docs.DateTimeHelper;
import org.torproject.onionoo.docs.DocumentStoreFactory;
@@ -14,12 +15,11 @@ import org.torproject.onionoo.docs.UptimeDocument;
import org.torproject.onionoo.docs.UptimeStatus;
import org.torproject.onionoo.updater.DescriptorSourceFactory;
import org.torproject.onionoo.updater.DummyDescriptorSource;
-import org.torproject.onionoo.util.DummyTime;
-import org.torproject.onionoo.util.TimeFactory;
import org.junit.Before;
import org.junit.Test;
+import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
@@ -28,14 +28,6 @@ public class UptimeDocumentWriterTest {
private static final long TEST_TIME = DateTimeHelper.parse(
"2014-03-23 12:00:00");
- private DummyTime dummyTime;
-
- @Before
- public void createDummyTime() {
- this.dummyTime = new DummyTime(TEST_TIME);
- TimeFactory.setTime(this.dummyTime);
- }
-
private DummyDescriptorSource descriptorSource;
@Before
@@ -52,9 +44,21 @@ public class UptimeDocumentWriterTest {
DocumentStoreFactory.setDocumentStore(this.documentStore);
}
+ private void setTime(UptimeDocumentWriter udw) {
+ try {
+ Field nowField = udw.getClass()
+ .getDeclaredField("now");
+ nowField.setAccessible(true);
+ nowField.set(udw, TEST_TIME);
+ } catch (Exception ex) {
+ fail("Cannot manipulate test-time. Failing all.");
+ }
+ }
+
@Test
public void testNoStatuses() {
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
writer.writeDocuments();
assertEquals("Without providing any data, nothing should be written "
+ "to disk.", 0,
@@ -127,6 +131,7 @@ public class UptimeDocumentWriterTest {
this.addStatusOneWeekSample("r 2014-03-23-11 1\n",
"r 2014-03-23-11 1\n");
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
DescriptorSourceFactory.getDescriptorSource().readDescriptors();
writer.writeDocuments();
assertEquals("Should write exactly one document.", 1,
@@ -142,6 +147,7 @@ public class UptimeDocumentWriterTest {
this.addStatusOneWeekSample("r 2014-03-23-10 2\n",
"r 2014-03-23-10 2\n");
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
DescriptorSourceFactory.getDescriptorSource().readDescriptors();
writer.writeDocuments();
assertEquals("Should write exactly one document.", 1,
@@ -157,6 +163,7 @@ public class UptimeDocumentWriterTest {
this.addStatusOneWeekSample("r 2014-03-23-09 1\nr 2014-03-23-11 1\n",
"r 2014-03-23-09 1\nr 2014-03-23-11 1\n");
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
DescriptorSourceFactory.getDescriptorSource().readDescriptors();
writer.writeDocuments();
assertEquals("Should write exactly one document.", 1,
@@ -172,6 +179,7 @@ public class UptimeDocumentWriterTest {
this.addStatusOneWeekSample("r 2014-03-23-09 3\n",
"r 2014-03-23-09 1\nr 2014-03-23-11 1\n");
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
DescriptorSourceFactory.getDescriptorSource().readDescriptors();
writer.writeDocuments();
assertEquals("Should write exactly one document.", 1,
@@ -188,6 +196,7 @@ public class UptimeDocumentWriterTest {
this.addStatusOneWeekSample("r 2014-03-23-09 3\n",
"r 2014-03-23-09 2\n");
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
DescriptorSourceFactory.getDescriptorSource().readDescriptors();
writer.writeDocuments();
assertEquals("Should write exactly one document.", 1,
@@ -204,6 +213,7 @@ public class UptimeDocumentWriterTest {
this.addStatusOneWeekSample("r 2014-03-16-12 168\n",
"r 2014-03-16-12 168\n");
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
DescriptorSourceFactory.getDescriptorSource().readDescriptors();
writer.writeDocuments();
assertEquals("Should write exactly one document.", 1,
@@ -219,6 +229,7 @@ public class UptimeDocumentWriterTest {
this.addStatusOneWeekSample("r 2014-03-16-11 169\n",
"r 2014-03-16-11 169\n");
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
DescriptorSourceFactory.getDescriptorSource().readDescriptors();
writer.writeDocuments();
assertEquals("Should write exactly one document.", 1,
@@ -236,6 +247,7 @@ public class UptimeDocumentWriterTest {
this.addStatusOneWeekSample("r 2014-03-16-08 8\n",
"r 2014-03-16-11 5\n");
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
DescriptorSourceFactory.getDescriptorSource().readDescriptors();
writer.writeDocuments();
assertEquals("Should write exactly one document.", 1,
@@ -251,6 +263,7 @@ public class UptimeDocumentWriterTest {
this.addStatusOneWeekSample("r 2014-03-16-08 8\n",
"r 2014-03-16-10 1\nr 2014-03-16-12 1\n");
UptimeDocumentWriter writer = new UptimeDocumentWriter();
+ setTime(writer);
DescriptorSourceFactory.getDescriptorSource().readDescriptors();
writer.writeDocuments();
assertEquals("Should write exactly one document.", 1,
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits