Beispiel #1
0
def test_event_handler():
    from bigchaindb.events import EventTypes, Event, Exchange

    # 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
Beispiel #2
0
def start(args):
    # Exchange object for event stream api
    logger.info('Starting BigchainDB')
    exchange = Exchange()
    # start the web api
    app_server = server.create_server(settings=bigchaindb.config['server'],
                                      log_config=bigchaindb.config['log'],
                                      bigchaindb_factory=BigchainDB)
    p_webapi = Process(name='bigchaindb_webapi',
                       target=app_server.run,
                       daemon=True)
    p_webapi.start()

    logger.info(BANNER.format(bigchaindb.config['server']['bind']))

    # start websocket server
    p_websocket_server = Process(name='bigchaindb_ws',
                                 target=websocket_server.start,
                                 daemon=True,
                                 args=(exchange.get_subscriber_queue(
                                     EventTypes.BLOCK_VALID), ))
    p_websocket_server.start()

    p_exchange = Process(name='bigchaindb_exchange',
                         target=exchange.run,
                         daemon=True)
    p_exchange.start()

    # We need to import this after spawning the web server
    # because import ABCIServer will monkeypatch all sockets
    # for gevent.
    from abci.server import ABCIServer

    setproctitle.setproctitle('bigchaindb')

    # Start the ABCIServer
    abci = ABCI(TmVersion(bigchaindb.config['tendermint']['version']))
    if args.experimental_parallel_validation:
        app = ABCIServer(app=ParallelValidationApp(
            abci=abci.types,
            events_queue=exchange.get_publisher_queue(),
        ))
    else:
        app = ABCIServer(app=App(
            abci=abci.types,
            events_queue=exchange.get_publisher_queue(),
        ))
    app.run()
Beispiel #3
0
def start():
    # Exchange object for event stream api
    logger.info('Starting BigchainDB')
    exchange = Exchange()
    # start the web api
    app_server = server.create_server(settings=bigchaindb.config['server'],
                                      log_config=bigchaindb.config['log'],
                                      bigchaindb_factory=BigchainDB)
    p_webapi = Process(name='bigchaindb_webapi',
                       target=app_server.run,
                       daemon=True)
    p_webapi.start()

    # start message
    logger.info(BANNER.format(bigchaindb.config['server']['bind']))

    # start websocket server
    p_websocket_server = Process(name='bigchaindb_ws',
                                 target=websocket_server.start,
                                 daemon=True,
                                 args=(exchange.get_subscriber_queue(
                                     EventTypes.BLOCK_VALID), ))
    p_websocket_server.start()

    # connect to tendermint event stream
    p_websocket_client = Process(name='bigchaindb_ws_to_tendermint',
                                 target=event_stream.start,
                                 daemon=True,
                                 args=(exchange.get_publisher_queue(), ))
    p_websocket_client.start()

    p_exchange = Process(name='bigchaindb_exchange',
                         target=exchange.run,
                         daemon=True)
    p_exchange.start()

    # We need to import this after spawning the web server
    # because import ABCIServer will monkeypatch all sockets
    # for gevent.
    from abci import ABCIServer

    setproctitle.setproctitle('bigchaindb')

    # Start the ABCIServer
    app = ABCIServer(app=App())
    app.run()
Beispiel #4
0
def test_event_handler():
    from bigchaindb.events import EventTypes, Event, Exchange

    # 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
Beispiel #5
0
def test_exchange_stops_with_poison_pill():
    from bigchaindb.events import EventTypes, Event, Exchange, 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
Beispiel #6
0
def test_exchange_stops_with_poison_pill():
    from bigchaindb.events import EventTypes, Event, Exchange, 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
Beispiel #7
0
def start():
    # Exchange object for event stream api
    logger.info('Starting BigchainDB')
    exchange = Exchange()
    # start the web api
    app_server = server.create_server(
        settings=bigchaindb.config['server'],
        log_config=bigchaindb.config['log'],
        bigchaindb_factory=BigchainDB)
    p_webapi = Process(name='bigchaindb_webapi', target=app_server.run, daemon=True)
    p_webapi.start()

    # start message
    logger.info(BANNER.format(bigchaindb.config['server']['bind']))

    # start websocket server
    p_websocket_server = Process(name='bigchaindb_ws',
                                 target=websocket_server.start,
                                 daemon=True,
                                 args=(exchange.get_subscriber_queue(EventTypes.BLOCK_VALID),))
    p_websocket_server.start()

    # connect to tendermint event stream
    p_websocket_client = Process(name='bigchaindb_ws_to_tendermint',
                                 target=event_stream.start,
                                 daemon=True,
                                 args=(exchange.get_publisher_queue(),))
    p_websocket_client.start()

    p_exchange = Process(name='bigchaindb_exchange', target=exchange.run, daemon=True)
    p_exchange.start()

    # We need to import this after spawning the web server
    # because import ABCIServer will monkeypatch all sockets
    # for gevent.
    from abci import ABCIServer

    setproctitle.setproctitle('bigchaindb')

    # Start the ABCIServer
    app = ABCIServer(app=App())
    app.run()
Beispiel #8
0
def start():
    logger.info('Initializing BigchainDB...')

    # Create a Exchange object.
    # The events queue needs to be initialized once and shared between
    # processes. This seems the best way to do it
    # At this point only the election processs and the event consumer require
    # this queue.
    exchange = Exchange()

    # start the processes
    logger.info('Starting block')
    block.start()

    logger.info('Starting voter')
    vote.start()

    logger.info('Starting stale transaction monitor')
    stale.start()

    logger.info('Starting election')
    election.start(events_queue=exchange.get_publisher_queue())

    # start the web api
    app_server = server.create_server(settings=bigchaindb.config['server'],
                                      log_config=bigchaindb.config['log'])
    p_webapi = mp.Process(name='webapi', target=app_server.run)
    p_webapi.start()

    logger.info('WebSocket server started')
    p_websocket_server = mp.Process(name='ws',
                                    target=websocket_server.start,
                                    args=(exchange.get_subscriber_queue(EventTypes.BLOCK_VALID),))
    p_websocket_server.start()

    # start message
    logger.info(BANNER.format(bigchaindb.config['server']['bind']))

    start_events_plugins(exchange)

    exchange.run()
Beispiel #9
0
def test_event_handler_raises_when_called_after_start():
    from bigchaindb.events import Exchange, POISON_PILL

    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_handle_block_events():
    from bigchaindb.events import Exchange, EventTypes

    exchange = Exchange()
    events_queue = exchange.get_publisher_queue()
    e = election.Election(events_queue=events_queue)
    block_id = 'a' * 64

    assert events_queue.qsize() == 0

    # no event should be emitted in case a block is undecided
    e.handle_block_events({'status': Bigchain.BLOCK_UNDECIDED}, block_id)
    assert events_queue.qsize() == 0

    # put an invalid block event in the queue
    e.handle_block_events({'status': Bigchain.BLOCK_INVALID}, block_id)
    event = events_queue.get()
    assert event.type == EventTypes.BLOCK_INVALID

    # put a valid block event in the queue
    e.handle_block_events({'status': Bigchain.BLOCK_VALID}, block_id)
    event = events_queue.get()
    assert event.type == EventTypes.BLOCK_VALID
Beispiel #11
0
def test_event_handler_raises_when_called_after_start():
    from bigchaindb.events import Exchange, POISON_PILL

    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_start_events_plugins(mock_process, monkeypatch):
    class MockPlugin:
        def __init__(self, event_types):
            self.event_types = event_types

        def run(self, queue):
            pass

    monkeypatch.setattr(
        'bigchaindb.config_utils.load_events_plugins',
        lambda names: [('one', MockPlugin(1)), ('two', MockPlugin(2))])

    from bigchaindb import processes
    from bigchaindb.events import Exchange

    exchange = Exchange()
    processes.start_events_plugins(exchange)
    assert len(exchange.queues) == 2
Beispiel #13
0
def start():
    logger.info('Initializing BigchainDB...')

    # Create a Exchange object.
    # The events queue needs to be initialized once and shared between
    # processes. This seems the best way to do it
    # At this point only the election processs and the event consumer require
    # this queue.
    exchange = Exchange()

    # start the processes
    logger.info('Starting block')
    block.start()

    logger.info('Starting voter')
    vote.start()

    logger.info('Starting stale transaction monitor')
    stale.start()

    logger.info('Starting election')
    election.start(events_queue=exchange.get_publisher_queue())

    # start the web api
    app_server = server.create_server(settings=bigchaindb.config['server'],
                                      log_config=bigchaindb.config['log'])
    p_webapi = mp.Process(name='webapi', target=app_server.run)
    p_webapi.start()

    logger.info('WebSocket server started')
    p_websocket_server = mp.Process(name='ws',
                                    target=websocket_server.start,
                                    args=(exchange.get_subscriber_queue(
                                        EventTypes.BLOCK_VALID), ))
    p_websocket_server.start()

    # start message
    logger.info(BANNER.format(bigchaindb.config['server']['bind']))

    start_events_plugins(exchange)

    exchange.run()