def create_app(*, debug=False, threads=4): """Return an instance of the Flask application. Args: debug (bool): a flag to activate the debug mode for the app (default: False). threads (int): number of threads to use Return: an instance of the Flask application. """ app = Flask(__name__) CORS(app, headers=( 'x-requested-with', 'content-type', 'accept', 'origin', 'authorization', 'x-csrftoken', 'withcredentials', 'cache-control', 'cookie', 'session-id', ), supports_credentials=True) app.debug = debug app.config['bigchain_pool'] = utils.pool(Bigchain, size=threads) add_routes(app) return app
def create_app(*, debug=False, threads=1, bigchaindb_factory=None): """Return an instance of the Flask application. Args: debug (bool): a flag to activate the debug mode for the app (default: False). threads (int): number of threads to use Return: an instance of the Flask application. """ if not bigchaindb_factory: bigchaindb_factory = Bigchain app = Flask(__name__) app.wsgi_app = StripContentTypeMiddleware(app.wsgi_app) CORS(app) app.debug = debug app.config['bigchain_pool'] = utils.pool(bigchaindb_factory, size=threads) add_routes(app) return app
def create_app(*, debug=False, threads=1, bigchaindb_factory=None): """Return an instance of the Flask application. Args: debug (bool): a flag to activate the debug mode for the app (default: False). threads (int): number of threads to use Return: an instance of the Flask application. """ if not bigchaindb_factory: bigchaindb_factory = BigchainDB app = Flask(__name__) app.wsgi_app = StripContentTypeMiddleware(app.wsgi_app) CORS(app) app.debug = debug app.config['bigchain_pool'] = utils.pool(bigchaindb_factory, size=threads) add_routes(app) return app
def test_empty_pool_is_populated_with_instances(mock_queue): from bigchaindb import utils pool = utils.pool(lambda: 'hello', 4) assert len(mock_queue.items) == 0 with pool() as instance: assert instance == 'hello' assert len(mock_queue.items) == 1 with pool() as instance: assert instance == 'hello' assert len(mock_queue.items) == 2 with pool() as instance: assert instance == 'hello' assert len(mock_queue.items) == 3 with pool() as instance: assert instance == 'hello' assert len(mock_queue.items) == 4 with pool() as instance: assert instance == 'hello' assert len(mock_queue.items) == 4
def test_pool_raises_empty_exception_when_timeout(mock_queue): from bigchaindb import utils pool = utils.pool(lambda: 'hello', 1, timeout=1) assert len(mock_queue.items) == 0 with pool() as instance: assert instance == 'hello' assert len(mock_queue.items) == 1 # take the only resource available assert pool().__enter__() == 'hello' with pytest.raises(queue.Empty): with pool() as instance: assert instance == 'hello'
def create_app(*, debug=False, threads=4): """Return an instance of the Flask application. Args: debug (bool): a flag to activate the debug mode for the app (default: False). threads (int): number of threads to use Return: an instance of the Flask application. """ app = Flask(__name__) app.debug = debug app.config['bigchain_pool'] = utils.pool(Bigchain, size=threads) add_routes(app) return app
def test_pool_blocks_if_no_instances_available(mock_queue): from bigchaindb import utils pool = utils.pool(lambda: 'hello', 4) assert len(mock_queue.items) == 0 # We need to manually trigger the `__enter__` method so the context # manager will "hang" and not return the resource to the pool assert pool().__enter__() == 'hello' assert len(mock_queue.items) == 0 assert pool().__enter__() == 'hello' assert len(mock_queue.items) == 0 assert pool().__enter__() == 'hello' assert len(mock_queue.items) == 0 # We need to keep a reference of the last context manager so we can # manually release the resource last = pool() assert last.__enter__() == 'hello' assert len(mock_queue.items) == 0 # This would block using `queue.Queue` but since we mocked it it will # just raise a IndexError because it's trying to pop from an empty list. with pytest.raises(IndexError): assert pool().__enter__() == 'hello' assert len(mock_queue.items) == 0 # Release the last resource last.__exit__(None, None, None) assert len(mock_queue.items) == 1 assert pool().__enter__() == 'hello' assert len(mock_queue.items) == 0