GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still and

Commit 6c8fc408 authored by juga  's avatar juga
Browse files

fix: tests: Add relaylist test

Tests don't pass in this commit, they're fixed in the next commits.
parent e989477b
......@@ -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)
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)
......@@ -75,7 +75,7 @@ setup(
# 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'],
......@@ -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.
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"):
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment