[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [sbws/master] fix: tests: Add relaylist test
commit 6c8fc408ac3e648d3da1da1bb60ebb6026f9dd4e
Author: juga0 <juga@xxxxxxxxxx>
Date: Sun Mar 8 10:17:21 2020 +0000
fix: tests: Add relaylist test
Tests don't pass in this commit, they're fixed in the next commits.
---
sbws/util/timestamp.py | 2 +-
setup.py | 2 +-
tests/unit/lib/test_relaylist.py | 51 +++++++++++++++++++++++++++++++++++++++-
3 files changed, 52 insertions(+), 3 deletions(-)
diff --git a/sbws/util/timestamp.py b/sbws/util/timestamp.py
index 075e82b..2b056f2 100644
--- a/sbws/util/timestamp.py
+++ b/sbws/util/timestamp.py
@@ -86,7 +86,7 @@ def is_old(timestamp, measurements_period=MEASUREMENTS_PERIOD):
# This will raise an exception if the string is not correctly
# formatted.
timestamp = isostr_to_dt_obj(timestamp)
- else:
+ elif isinstance(timestamp, int) or isinstance(timestamp, float):
# This will raise an exception if the type is not int or float or
# is not actually a timestamp
timestamp = unixts_to_dt_obj(timestamp)
diff --git a/setup.py b/setup.py
index f567327..f328d7a 100755
--- a/setup.py
+++ b/setup.py
@@ -75,7 +75,7 @@ setup(
extras_require={
# vulture: find unused code
'dev': ['flake8', 'vulture'],
- 'test': ['tox', 'pytest', 'coverage'],
+ 'test': ['tox', 'pytest', 'coverage', 'freezegun'],
# recommonmark: to make sphinx render markdown
'doc': ['sphinx', 'recommonmark', 'pylint'],
},
diff --git a/tests/unit/lib/test_relaylist.py b/tests/unit/lib/test_relaylist.py
index 3dec382..9c3b4d6 100644
--- a/tests/unit/lib/test_relaylist.py
+++ b/tests/unit/lib/test_relaylist.py
@@ -7,7 +7,7 @@ from datetime import datetime, timedelta
# freezegun is able to mock any datetime object, it also allows comparations.
from freezegun import freeze_time
-from sbws.lib.relaylist import remove_old_consensus_timestamps
+from sbws.lib.relaylist import RelayList, remove_old_consensus_timestamps
def test_remove_old_consensus_timestamps():
@@ -21,3 +21,52 @@ def test_remove_old_consensus_timestamps():
)
assert len(new_timestamps) == len(timestamps) - 1
assert days_ago not in new_timestamps
+
+
+def test_init_relays(
+ args, conf, controller, controller_1h_later, controller_5days_later
+):
+ """
+ Test `init_relays` when creating the RelayList the first time and when a
+ new consensus is received.
+ Test that the number of consesus timesamps and relays is correct.
+ """
+ # There is no need to mock datetime to update the consensus, since the
+ # actual date will be always later.
+ # But it's needed to have the correct list of timestamps both for RelayList
+ # and Relay.
+ with freeze_time("2020-02-29 10:00:00"):
+ relay_list = RelayList(args, conf, controller=controller)
+ assert len(relay_list._consensus_timestamps) == 1
+ # The actual number of relays in the consensus
+ assert len(relay_list._relays) == 6433
+ fps = {r.fingerprint for r in relay_list._relays}
+
+ # One hour later there is a new consensus
+ relay_list._controller = controller_1h_later
+ with freeze_time("2020-02-29 11:00:00"):
+ # Call relays update the list of relays.
+ relay_list.relays
+ assert len(relay_list._consensus_timestamps) == 2
+ # Check that the number of relays is now the previous one plus the relays
+ # that are in the new consensus that there were not in the previous one.
+ fps_1h_later = {r.fingerprint for r in relay_list._relays}
+ added_fps = fps_1h_later.difference(fps)
+ assert 6505 == 6433 + len(added_fps)
+
+ # Five days later plus 1 second.
+ # The first consensus timestamp will get removed.
+ relay_list._controller = controller_5days_later
+ with freeze_time("2020-03-05 10:00:01"):
+ relay_list.relays
+ assert len(relay_list._consensus_timestamps) == 2
+ fps_5days_later = {r.fingerprint for r in relay_list._relays}
+ # The number of added relays will be the number of relays in this
+ # consensus that were not in the other 2 conensuses
+ added_fps = fps_5days_later.difference(fps_1h_later)
+ # The number of removed relays that are in this consensus, plus the added
+ # ones that were not in the first consensus (because it has been removed).
+ removed_fps = fps.difference(fps_5days_later)
+ # The number of relays will be the number of relays in the cosensus plus
+ # the added ones minus the removed ones.
+ assert 6925 == 6505 + len(added_fps) - len(removed_fps)
\ No newline at end of file
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits