[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [metrics-web/release] Fix two issues found using metrics-test.



commit 5d98d4806e01169f226bace3fc095298844d4427
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date:   Mon Sep 16 16:29:10 2019 +0200

    Fix two issues found using metrics-test.
    
     - Make unit test locale-independent, similar to Onionoo's 860228c.
    
     - Replace deprecated method Class.newInstance().
---
 .../java/org/torproject/metrics/stats/hidserv/Aggregator.java  |  5 +++--
 .../metrics/stats/hidserv/ComputedNetworkFractions.java        |  5 +++--
 .../org/torproject/metrics/stats/hidserv/DocumentStore.java    |  6 ++++--
 .../metrics/stats/hidserv/ExtrapolatedHidServStats.java        | 10 ++++++----
 .../java/org/torproject/metrics/stats/hidserv/Simulate.java    |  9 +++++----
 5 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/torproject/metrics/stats/hidserv/Aggregator.java b/src/main/java/org/torproject/metrics/stats/hidserv/Aggregator.java
index 36e7967..12a89a3 100644
--- a/src/main/java/org/torproject/metrics/stats/hidserv/Aggregator.java
+++ b/src/main/java/org/torproject/metrics/stats/hidserv/Aggregator.java
@@ -13,6 +13,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.SortedMap;
@@ -176,8 +177,8 @@ public class Aggregator {
         /* Put together all aggregated values in a single line. */
         String date = e.getKey();
         int numStats = weightedValues.size();
-        sb.append(String.format("%s,%s,%.0f,%.0f,%.0f,%.8f,%d%n", date,
-            type, weightedMean, weightedMedian, weightedInterquartileMean,
+        sb.append(String.format(Locale.US, "%s,%s,%.0f,%.0f,%.0f,%.8f,%d%n",
+            date, type, weightedMean, weightedMedian, weightedInterquartileMean,
             sumFraction, numStats));
       }
     }
diff --git a/src/main/java/org/torproject/metrics/stats/hidserv/ComputedNetworkFractions.java b/src/main/java/org/torproject/metrics/stats/hidserv/ComputedNetworkFractions.java
index d5fd279..835ac6f 100644
--- a/src/main/java/org/torproject/metrics/stats/hidserv/ComputedNetworkFractions.java
+++ b/src/main/java/org/torproject/metrics/stats/hidserv/ComputedNetworkFractions.java
@@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
 
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 
 /** Computed fraction of hidden-service activity that a single relay is
@@ -116,9 +117,9 @@ public class ComputedNetworkFractions implements Document {
         validAfterDate);
     String second = validAfterHour
         + (this.fractionRendRelayedCells == 0.0 ? ","
-            : String.format(",%f", this.fractionRendRelayedCells))
+            : String.format(Locale.US, ",%f", this.fractionRendRelayedCells))
         + (this.fractionDirOnionsSeen == 0.0 ? ","
-            : String.format(",%f", this.fractionDirOnionsSeen));
+            : String.format(Locale.US, ",%f", this.fractionDirOnionsSeen));
     return new String[] { first, second };
   }
 
diff --git a/src/main/java/org/torproject/metrics/stats/hidserv/DocumentStore.java b/src/main/java/org/torproject/metrics/stats/hidserv/DocumentStore.java
index f3163e1..9061aa3 100644
--- a/src/main/java/org/torproject/metrics/stats/hidserv/DocumentStore.java
+++ b/src/main/java/org/torproject/metrics/stats/hidserv/DocumentStore.java
@@ -13,6 +13,7 @@ import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.LineNumberReader;
+import java.lang.reflect.InvocationTargetException;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -136,7 +137,7 @@ public class DocumentStore<T extends Document> {
             && !formattedString0.startsWith(prefix)) {
           /* Skip line not starting with prefix. */
         } else {
-          T document = this.clazz.newInstance();
+          T document = this.clazz.getDeclaredConstructor().newInstance();
           if (!document.parse(new String[] { formattedString0,
               line.substring(1) })) {
             log.warn("Unable to read line {} from {}. Not retrieving any "
@@ -151,7 +152,8 @@ public class DocumentStore<T extends Document> {
       log.warn("Unable to read {}. Not retrieving any previously stored "
           + "documents.", documentFile.getAbsolutePath(), e);
       return null;
-    } catch (InstantiationException | IllegalAccessException e) {
+    } catch (InstantiationException | IllegalAccessException
+        | NoSuchMethodException | InvocationTargetException e) {
       log.warn("Unable to read {}. Cannot instantiate document object.",
           documentFile.getAbsolutePath(), e);
       return null;
diff --git a/src/main/java/org/torproject/metrics/stats/hidserv/ExtrapolatedHidServStats.java b/src/main/java/org/torproject/metrics/stats/hidserv/ExtrapolatedHidServStats.java
index ec09630..eb66c7a 100644
--- a/src/main/java/org/torproject/metrics/stats/hidserv/ExtrapolatedHidServStats.java
+++ b/src/main/java/org/torproject/metrics/stats/hidserv/ExtrapolatedHidServStats.java
@@ -6,6 +6,8 @@ package org.torproject.metrics.stats.hidserv;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Locale;
+
 /** Extrapolated network totals of hidden-service statistics reported by a
  * single relay.  Extrapolated values are based on reported statistics and
  * computed network fractions in the statistics interval. */
@@ -116,11 +118,11 @@ public class ExtrapolatedHidServStats implements Document {
         DateTimeHelper.ISO_DATE_FORMAT);
     String second = this.fingerprint
         + (this.fractionRendRelayedCells == 0.0 ? ",,"
-        : String.format(",%.0f,%f", this.extrapolatedRendRelayedCells,
-        this.fractionRendRelayedCells))
+        : String.format(Locale.US, ",%.0f,%f",
+        this.extrapolatedRendRelayedCells, this.fractionRendRelayedCells))
         + (this.fractionDirOnionsSeen == 0.0 ? ",,"
-        : String.format(",%.0f,%f", this.extrapolatedDirOnionsSeen,
-        this.fractionDirOnionsSeen));
+        : String.format(Locale.US, ",%.0f,%f",
+        this.extrapolatedDirOnionsSeen, this.fractionDirOnionsSeen));
     return new String[] { first, second };
   }
 
diff --git a/src/main/java/org/torproject/metrics/stats/hidserv/Simulate.java b/src/main/java/org/torproject/metrics/stats/hidserv/Simulate.java
index ac8a82e..445d931 100644
--- a/src/main/java/org/torproject/metrics/stats/hidserv/Simulate.java
+++ b/src/main/java/org/torproject/metrics/stats/hidserv/Simulate.java
@@ -13,6 +13,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Random;
 import java.util.SortedMap;
@@ -196,8 +197,8 @@ public class Simulate {
           totalInterquartileProbability += extrapolation[2];
         }
       }
-      sb.append(String.format("%d,%.2f,%.0f,%.0f,%.0f%n", run, fraction,
-          totalValues / totalProbability, weightedMedian,
+      sb.append(String.format(Locale.US, "%d,%.2f,%.0f,%.0f,%.0f%n", run,
+          fraction, totalValues / totalProbability, weightedMedian,
           totalInterquartileValues / totalInterquartileProbability));
     }
     return sb.toString();
@@ -351,8 +352,8 @@ public class Simulate {
           totalInterquartileProbability += extrapolation[2];
         }
       }
-      sb.append(String.format("%d,%.2f,%.0f,%.0f,%.0f%n", run, fraction,
-          totalValues / totalProbability, weightedMedian,
+      sb.append(String.format(Locale.US, "%d,%.2f,%.0f,%.0f,%.0f%n", run,
+          fraction, totalValues / totalProbability, weightedMedian,
           totalInterquartileValues / totalInterquartileProbability));
     }
     return sb.toString();



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits