From f55e37b0fc831f8f3d4b64371f1bf0c9d5846343 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Mon, 30 Mar 2020 16:19:04 +0200 Subject: [PATCH] Adds unit test for watcher.get_appointment_summary --- teos/watcher.py | 9 ++++++--- test/teos/unit/test_watcher.py | 13 ++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/teos/watcher.py b/teos/watcher.py index 757ffc0..24fec8b 100644 --- a/teos/watcher.py +++ b/teos/watcher.py @@ -39,8 +39,8 @@ class Watcher: expiry_delta (:obj:`int`): the additional time the ``Watcher`` will keep an expired appointment around. Attributes: - appointments (:obj:`dict`): a dictionary containing a simplification of the appointments (:obj:`Appointment - ` instances) accepted by the tower (``locator`` and ``end_time``). + appointments (:obj:`dict`): a dictionary containing a summary of the appointments (:obj:`Appointment + ` instances) accepted by the tower (``locator``, ``end_time``, and ``size``). It's populated trough ``add_appointment``. locator_uuid_map (:obj:`dict`): a ``locator:uuid`` map used to allow the :obj:`Watcher` to deal with several appointments with the same ``locator``. @@ -71,6 +71,10 @@ class Watcher: self.signing_key = Cryptographer.load_private_key_der(sk_der) def awake(self): + """ + Starts a new thread to monitor the blockchain for channel breaches. + """ + watcher_thread = Thread(target=self.do_watch, daemon=True) watcher_thread.start() @@ -88,7 +92,6 @@ class Watcher: :obj:`dict` or :obj:`None`: a dictionary with the appointment summary, or None if the appointment is not found. """ - return self.appointments.get(uuid) def add_appointment(self, appointment, user_pk): diff --git a/test/teos/unit/test_watcher.py b/test/teos/unit/test_watcher.py index 7645c80..e61a178 100644 --- a/test/teos/unit/test_watcher.py +++ b/test/teos/unit/test_watcher.py @@ -112,6 +112,17 @@ def test_init(run_bitcoind, watcher): assert isinstance(watcher.signing_key, PrivateKey) +def test_get_appointment_summary(watcher): + # get_appointment_summary returns an appointment summary if found, else None. + random_uuid = get_random_value_hex(16) + appointment_summary = {"locator": get_random_value_hex(16), "end_time": 10, "size": 200} + watcher.appointments[random_uuid] = appointment_summary + assert watcher.get_appointment_summary(random_uuid) == appointment_summary + + # Requesting a non-existing appointment + assert watcher.get_appointment_summary(get_random_value_hex(16)) is None + + def test_add_appointment(watcher): # We should be able to add appointments up to the limit for _ in range(10): @@ -186,7 +197,7 @@ def test_do_watch(watcher, temp_db_manager): watcher.appointments = {} for uuid, appointment in appointments.items(): - watcher.appointments[uuid] = {"locator": appointment.locator, "end_time": appointment.end_time} + watcher.appointments[uuid] = {"locator": appointment.locator, "end_time": appointment.end_time, "size": 200} watcher.db_manager.store_watcher_appointment(uuid, appointment.to_json()) watcher.db_manager.create_append_locator_map(appointment.locator, uuid)