Updates current tests to work with chain_monitor instead of zmq_sub

This commit is contained in:
Sergi Delgado Segura
2020-01-03 13:36:52 +01:00
parent 3889d30e31
commit 069e15fdba
3 changed files with 81 additions and 96 deletions

View File

@@ -1,22 +1,15 @@
import pytest
from uuid import uuid4
from threading import Thread
from queue import Queue, Empty
from cryptography.hazmat.primitives import serialization
from pisa.watcher import Watcher
from pisa.responder import Responder
from pisa.tools import bitcoin_cli
from test.pisa.unit.conftest import (
generate_block,
generate_blocks,
generate_dummy_appointment,
get_random_value_hex,
generate_keypair,
)
from test.pisa.unit.conftest import generate_blocks, generate_dummy_appointment, get_random_value_hex, generate_keypair
from pisa.chain_monitor import ChainMonitor
from pisa.conf import EXPIRY_DELTA, MAX_APPOINTMENTS
from common.tools import check_sha256_hex_format
from common.cryptographer import Cryptographer
@@ -35,8 +28,20 @@ sk_der = signing_key.private_bytes(
@pytest.fixture(scope="module")
def watcher(db_manager):
return Watcher(db_manager, sk_der)
def chain_monitor():
chain_monitor = ChainMonitor()
chain_monitor.monitor_chain()
return chain_monitor
@pytest.fixture(scope="module")
def watcher(db_manager, chain_monitor):
watcher = Watcher(db_manager, chain_monitor, sk_der)
chain_monitor.attach_watcher(watcher.block_queue, watcher.asleep)
chain_monitor.attach_responder(watcher.responder.block_queue, watcher.responder.asleep)
return watcher
@pytest.fixture(scope="module")
@@ -67,17 +72,18 @@ def create_appointments(n):
return appointments, locator_uuid_map, dispute_txs
def test_init(watcher):
assert type(watcher.appointments) is dict and len(watcher.appointments) == 0
assert type(watcher.locator_uuid_map) is dict and len(watcher.locator_uuid_map) == 0
def test_init(run_bitcoind, watcher):
assert isinstance(watcher.appointments, dict) and len(watcher.appointments) == 0
assert isinstance(watcher.locator_uuid_map, dict) and len(watcher.locator_uuid_map) == 0
assert isinstance(watcher.chain_monitor, ChainMonitor)
assert watcher.block_queue.empty()
assert watcher.asleep is True
assert watcher.max_appointments == MAX_APPOINTMENTS
assert watcher.zmq_subscriber is None
assert type(watcher.responder) is Responder
def test_add_appointment(run_bitcoind, watcher):
def test_add_appointment(watcher):
# The watcher automatically fires do_watch and do_subscribe on adding an appointment if it is asleep (initial state)
# Avoid this by setting the state to awake.
watcher.asleep = False
@@ -121,36 +127,18 @@ def test_add_too_many_appointments(watcher):
assert sig is None
def test_do_subscribe(watcher):
watcher.block_queue = Queue()
zmq_thread = Thread(target=watcher.do_subscribe)
zmq_thread.daemon = True
zmq_thread.start()
try:
generate_block()
block_hash = watcher.block_queue.get()
assert check_sha256_hex_format(block_hash)
except Empty:
assert False
def test_do_watch(watcher):
# We will wipe all the previous data and add 5 appointments
watcher.appointments, watcher.locator_uuid_map, dispute_txs = create_appointments(APPOINTMENTS)
watcher.chain_monitor.watcher_asleep = False
watch_thread = Thread(target=watcher.do_watch)
watch_thread.daemon = True
watch_thread.start()
Thread(target=watcher.do_watch, daemon=True).start()
# Broadcast the first two
for dispute_tx in dispute_txs[:2]:
bitcoin_cli().sendrawtransaction(dispute_tx)
# After leaving some time for the block to be mined and processed, the number of appointments should have reduced
# by two
# After generating enough blocks, the number of appointments should have reduced by two
generate_blocks(START_TIME_OFFSET + END_TIME_OFFSET)
assert len(watcher.appointments) == APPOINTMENTS - 2