fix tests

This commit is contained in:
Rodolphe Marques 2017-04-03 14:29:31 +02:00 committed by vrde
parent 96daa98699
commit 83a7cffc3f
No known key found for this signature in database
GPG Key ID: 6581C7C39B3D397D
3 changed files with 24 additions and 19 deletions

View File

@ -23,9 +23,11 @@ logger_results = logging.getLogger('pipeline.election.results')
class Election: class Election:
"""Election class.""" """Election class."""
def __init__(self, events_queue): def __init__(self, events_queue=None):
self.bigchain = Bigchain() self.bigchain = Bigchain()
self.event_handler = EventHandler(events_queue) self.event_handler = None
if events_queue:
self.event_handler = EventHandler(events_queue)
def check_for_quorum(self, next_vote): def check_for_quorum(self, next_vote):
""" """
@ -71,19 +73,20 @@ class Election:
return invalid_block return invalid_block
def handle_block_events(self, result, block_id): def handle_block_events(self, result, block_id):
if result['status'] == self.bigchain.BLOCK_UNDECIDED: if self.event_handler:
return if result['status'] == self.bigchain.BLOCK_UNDECIDED:
elif result['status'] == self.bigchain.BLOCK_INVALID: return
event_type = EventTypes.BLOCK_INVALID elif result['status'] == self.bigchain.BLOCK_INVALID:
elif result['status'] == self.bigchain.BLOCK_VALID: event_type = EventTypes.BLOCK_INVALID
event_type = EventTypes.BLOCK_VALID elif result['status'] == self.bigchain.BLOCK_VALID:
event_type = EventTypes.BLOCK_VALID
event = Event(event_type, {'block_id': block_id}) event = Event(event_type, {'block_id': block_id})
self.event_handler.put_event(event) self.event_handler.put_event(event)
def create_pipeline(events_queue): def create_pipeline(events_queue=None):
election = Election(events_queue) election = Election(events_queue=events_queue)
election_pipeline = Pipeline([ election_pipeline = Pipeline([
Node(election.check_for_quorum), Node(election.check_for_quorum),
@ -98,8 +101,8 @@ def get_changefeed():
return backend.get_changefeed(connection, 'votes', ChangeFeed.INSERT) return backend.get_changefeed(connection, 'votes', ChangeFeed.INSERT)
def start(events_queue): def start(events_queue=None):
pipeline = create_pipeline(events_queue) pipeline = create_pipeline(events_queue=events_queue)
pipeline.setup(indata=get_changefeed()) pipeline.setup(indata=get_changefeed())
pipeline.start() pipeline.start()
return pipeline return pipeline

View File

@ -45,7 +45,7 @@ def start():
stale.start() stale.start()
logger.info('Starting election') logger.info('Starting election')
election.start(events_queue) election.start(events_queue=events_queue)
# start the web api # start the web api
app_server = server.create_server(bigchaindb.config['server']) app_server = server.create_server(bigchaindb.config['server'])

View File

@ -1,6 +1,6 @@
from unittest.mock import patch from unittest.mock import patch
from multiprocessing import Process from multiprocessing import Process, Queue
from bigchaindb.pipelines import vote, block, election, stale from bigchaindb.pipelines import vote, block, election, stale
@ -9,14 +9,16 @@ from bigchaindb.pipelines import vote, block, election, stale
@patch.object(block, 'start') @patch.object(block, 'start')
@patch.object(vote, 'start') @patch.object(vote, 'start')
@patch.object(Process, 'start') @patch.object(Process, 'start')
def test_processes_start(mock_vote, mock_block, mock_election, mock_stale, def test_processes_start(mock_process, mock_vote, mock_block, mock_election,
mock_process): mock_stale):
from bigchaindb import processes from bigchaindb import processes
processes.start() processes.start()
mock_vote.assert_called_with() mock_vote.assert_called_with()
mock_block.assert_called_with() mock_block.assert_called_with()
mock_election.assert_called_with()
mock_stale.assert_called_with() mock_stale.assert_called_with()
mock_process.assert_called_with() mock_process.assert_called_with()
assert mock_election.call_count == 1
# the events queue is declared inside processes.start()
assert type(mock_election.call_args[1]['events_queue']) == type(Queue())