def test_broadcast_events(self): """Test that broadcast_events works with a single subscriber to the block_commit event type and that the subscriber does not receive events until it is enabled. """ mock_service = Mock() event_broadcaster = EventBroadcaster(mock_service) block = create_block() event_broadcaster.chain_update(block, []) mock_service.send.assert_not_called() event_broadcaster.add_subscriber("test_conn_id", [create_block_commit_subscription()], []) event_broadcaster.chain_update(block, []) mock_service.send.assert_not_called() event_broadcaster.enable_subscriber("test_conn_id") event_broadcaster.chain_update(block, []) event_list = events_pb2.EventList( events=BlockEventExtractor(block).extract( [create_block_commit_subscription()])).SerializeToString() mock_service.send.assert_called_with( validator_pb2.Message.CLIENT_EVENTS, event_list, connection_id="test_conn_id")
def test_broadcast_events(self): """Test that broadcast_events works with a single subscriber to the sawtooth/block-commit event type and that the subscriber does not receive events until it is enabled. """ mock_service = Mock() mock_block_store = Mock() mock_receipt_store = Mock() event_broadcaster = EventBroadcaster(mock_service, mock_block_store, mock_receipt_store) block = create_block() event_broadcaster.chain_update(block, []) mock_service.send.assert_not_called() event_broadcaster.add_subscriber( "test_conn_id", [create_block_commit_subscription()], []) event_broadcaster.chain_update(block, []) mock_service.send.assert_not_called() event_broadcaster.enable_subscriber("test_conn_id") event_broadcaster.chain_update(block, []) event_list = events_pb2.EventList( events=BlockEventExtractor(block).extract( [create_block_commit_subscription()])).SerializeToString() mock_service.send.assert_called_with( validator_pb2.Message.CLIENT_EVENTS, event_list, connection_id="test_conn_id", one_way=True)
def test_add_remove_subscriber(self): """Test adding and removing a subscriber.""" mock_service = Mock() mock_block_store = Mock() mock_receipt_store = Mock() event_broadcaster = EventBroadcaster(mock_service, mock_block_store, mock_receipt_store) subscriptions = [ EventSubscription( event_type="test_event", filters=[ FILTER_FACTORY.create(key="test", match_string="test") ], ) ] event_broadcaster.add_subscriber("test_conn_id", subscriptions, []) self.assertTrue( "test_conn_id" in event_broadcaster._subscribers.keys()) self.assertEqual( event_broadcaster._subscribers["test_conn_id"].subscriptions, subscriptions) event_broadcaster.remove_subscriber("test_conn_id") self.assertTrue( "test_conn_id" not in event_broadcaster._subscribers.keys())
def test_catchup_subscriber(self): """Test that catch subscriber handles the case of: - no blocks (i.e. the genesis block has not been produced or received - a block that has some receipts exists and sends results """ mock_service = Mock() mock_block_store = MagicMock() mock_block_store.chain_head = None mock_block_store.get_predecessor_iter.return_value = [] mock_receipt_store = Mock() event_broadcaster = EventBroadcaster(mock_service, mock_block_store, mock_receipt_store) event_broadcaster.add_subscriber( "test_conn_id", [create_block_commit_subscription()], []) event_broadcaster.catchup_subscriber("test_conn_id") mock_service.send.assert_not_called() block = create_block() mock_block_store.chain_head = block mock_block_store.get_predecessor_iter.return_value = [block] mock_block_store.__getitem__.return_value = block event_broadcaster.catchup_subscriber("test_conn_id") event_list = events_pb2.EventList( events=BlockEventExtractor(block).extract( [create_block_commit_subscription()])).SerializeToString() mock_service.send.assert_called_with( validator_pb2.Message.CLIENT_EVENTS, event_list, connection_id="test_conn_id", one_way=True)