commit 25a68337b17c36b23bc2eb0013ce825622dd2524 Author: Karsten Loesing karsten.loesing@gmx.net Date: Mon Jan 9 17:19:04 2017 +0100
Exclude bandwidth history values from the future.
Fixes #21047. --- CHANGELOG.md | 1 + .../torproject/onionoo/docs/BandwidthDocument.java | 10 ++- .../onionoo/writer/BandwidthDocumentWriter.java | 3 + .../writer/BandwidthDocumentWriterTest.java | 88 ++++++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md index c8ab3e1..6d01ec0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ process. - Accept the same characters in qualified search terms as in their parameter equivalents. + - Exclude bandwidth history values from the future.
* Minor changes - Include XZ binaries in release binaries. diff --git a/src/main/java/org/torproject/onionoo/docs/BandwidthDocument.java b/src/main/java/org/torproject/onionoo/docs/BandwidthDocument.java index 1990734..575b8a0 100644 --- a/src/main/java/org/torproject/onionoo/docs/BandwidthDocument.java +++ b/src/main/java/org/torproject/onionoo/docs/BandwidthDocument.java @@ -15,18 +15,24 @@ public class BandwidthDocument extends Document { this.fingerprint = fingerprint; }
- @SuppressWarnings("unused") private Map<String, GraphHistory> write_history;
public void setWriteHistory(Map<String, GraphHistory> writeHistory) { this.write_history = writeHistory; }
- @SuppressWarnings("unused") + public Map<String, GraphHistory> getWriteHistory() { + return this.write_history; + } + private Map<String, GraphHistory> read_history;
public void setReadHistory(Map<String, GraphHistory> readHistory) { this.read_history = readHistory; } + + public Map<String, GraphHistory> getReadHistory() { + return this.read_history; + } }
diff --git a/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java b/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java index 33b943e..25bca5e 100644 --- a/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java +++ b/src/main/java/org/torproject/onionoo/writer/BandwidthDocumentWriter.java @@ -112,6 +112,9 @@ public class BandwidthDocumentWriter implements DocumentWriter { continue; } long startMillis = v[0]; + if (startMillis > this.now) { + break; + } if (endMillis - startMillis > dataPointInterval) { /* This history interval is too long for this graph's data point * interval. Maybe the next graph will contain it, but not this diff --git a/src/test/java/org/torproject/onionoo/writer/BandwidthDocumentWriterTest.java b/src/test/java/org/torproject/onionoo/writer/BandwidthDocumentWriterTest.java new file mode 100644 index 0000000..925f943 --- /dev/null +++ b/src/test/java/org/torproject/onionoo/writer/BandwidthDocumentWriterTest.java @@ -0,0 +1,88 @@ +/* Copyright 2017 The Tor Project + * See LICENSE for licensing information */ + +package org.torproject.onionoo.writer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.torproject.onionoo.docs.BandwidthDocument; +import org.torproject.onionoo.docs.BandwidthStatus; +import org.torproject.onionoo.docs.DateTimeHelper; +import org.torproject.onionoo.docs.DocumentStoreFactory; +import org.torproject.onionoo.docs.DummyDocumentStore; +import org.torproject.onionoo.docs.GraphHistory; +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; + +public class BandwidthDocumentWriterTest { + + private static final long TEST_TIME = DateTimeHelper.parse( + "2017-01-09 12:00:00"); + + private DummyTime dummyTime; + + @Before + public void createDummyTime() { + this.dummyTime = new DummyTime(TEST_TIME); + TimeFactory.setTime(this.dummyTime); + } + + private DummyDescriptorSource descriptorSource; + + @Before + public void createDummyDescriptorSource() { + this.descriptorSource = new DummyDescriptorSource(); + DescriptorSourceFactory.setDescriptorSource(this.descriptorSource); + } + + private DummyDocumentStore documentStore; + + @Before + public void createDummyDocumentStore() { + this.documentStore = new DummyDocumentStore(); + DocumentStoreFactory.setDocumentStore(this.documentStore); + } + + @Test + public void testIgnore2019() { + BandwidthStatus status = new BandwidthStatus(); + String documentString = + "r 2017-01-08 08:29:33 2017-01-08 12:29:33 144272636928\n" + + "r 2017-01-08 12:29:33 2017-01-08 16:29:33 144407647232\n" + + "r 2017-01-08 16:29:33 2017-01-08 20:29:33 154355623936\n" + + "r 2017-01-08 20:29:33 2017-01-09 00:29:33 149633244160\n" + + "r 2019-08-06 05:31:45 2019-08-06 09:31:45 0\n" + + "r 2019-08-06 09:31:45 2019-08-06 13:31:45 0\n" + + "r 2019-08-06 13:31:45 2019-08-06 17:31:45 0\n" + + "r 2019-08-06 17:31:45 2019-08-06 21:31:45 0\n" + + "r 2019-08-06 21:31:45 2019-08-07 01:31:45 0\n" + + "r 2019-08-07 01:31:45 2019-08-07 05:31:45 0\n"; + status.setFromDocumentString(documentString); + String ibibUNC0Fingerprint = "7C0AA4E3B73E407E9F5FEB1912F8BE26D8AA124D"; + this.documentStore.addDocument(status, ibibUNC0Fingerprint); + BandwidthDocumentWriter writer = new BandwidthDocumentWriter(); + DescriptorSourceFactory.getDescriptorSource().readDescriptors(); + writer.writeDocuments(); + assertEquals(1, this.documentStore.getPerformedStoreOperations()); + BandwidthDocument document = this.documentStore.getDocument( + BandwidthDocument.class, ibibUNC0Fingerprint); + assertEquals(1, document.getReadHistory().size()); + assertTrue(document.getReadHistory().containsKey("1_month")); + GraphHistory history = document.getReadHistory().get("1_month"); + assertEquals(DateTimeHelper.parse("2017-01-08 14:00:00"), + history.getFirst()); + assertEquals(DateTimeHelper.parse("2017-01-09 02:00:00"), + history.getLast()); + assertEquals(DateTimeHelper.FOUR_HOURS / DateTimeHelper.ONE_SECOND, + (int) history.getInterval()); + assertEquals(4, (int) history.getCount()); + assertEquals(4, history.getValues().size()); + } +} +
tor-commits@lists.torproject.org