planetmint/tests/test_events.py
Jürgen Eckel 0b0c954d34
331 refactor a certain module gets a specific driver type flask sync driver abci server async driver first we stick to the current tarantool driver (#337)
* created ABCI_RPC class to seperate RPC interaction from the other ABCI interactions
* renamed validation.py to validator.py
* simplified planetmint/__init__.py
* moved methods used by testing to tests/utils.py
* making planetmint/__init__.py lean
* moved ProcessGroup object to tests as it is only used there
* reintegrated disabled tests


Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
2023-02-27 16:48:31 +01:00

72 lines
2.0 KiB
Python

# Copyright © 2020 Interplanetary Database Association e.V.,
# Planetmint and IPDB software contributors.
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
# Code is Apache-2.0 and docs are CC-BY-4.0
import pytest
from planetmint.ipc.events import EventTypes, Event, POISON_PILL
from planetmint.ipc.exchange import Exchange
def test_event_handler():
# create and event
event_data = {"msg": "some data"}
event = Event(EventTypes.BLOCK_VALID, event_data)
# create the events pub sub
exchange = Exchange()
sub0 = exchange.get_subscriber_queue(EventTypes.BLOCK_VALID)
sub1 = exchange.get_subscriber_queue(EventTypes.BLOCK_VALID | EventTypes.BLOCK_INVALID)
# Subscribe to all events
sub2 = exchange.get_subscriber_queue()
sub3 = exchange.get_subscriber_queue(EventTypes.BLOCK_INVALID)
# push and event to the queue
exchange.dispatch(event)
# get the event from the queue
event_sub0 = sub0.get()
event_sub1 = sub1.get()
event_sub2 = sub2.get()
assert event_sub0.type == event.type
assert event_sub0.data == event.data
assert event_sub1.type == event.type
assert event_sub1.data == event.data
assert event_sub2.type == event.type
assert event_sub2.data == event.data
assert sub3.qsize() == 0
def test_event_handler_raises_when_called_after_start():
exchange = Exchange()
publisher_queue = exchange.get_publisher_queue()
publisher_queue.put(POISON_PILL)
exchange.run()
with pytest.raises(RuntimeError):
exchange.get_subscriber_queue()
def test_exchange_stops_with_poison_pill():
# create and event
event_data = {"msg": "some data"}
event = Event(EventTypes.BLOCK_VALID, event_data)
# create the events pub sub
exchange = Exchange()
publisher_queue = exchange.get_publisher_queue()
# push and event to the queue
publisher_queue.put(event)
publisher_queue.put(POISON_PILL)
exchange.run()
assert publisher_queue.qsize() == 0