예제 #1
0
async def run_test_async(app, func):
    """Run a test against the application.

    func is a function that accepts an app url as a parameter and returns a result.

    func can be synchronous or asynchronous.  If it is synchronous, it will be run
    in a thread, so asynchronous is preferred.
    """
    handler = LogErrorHandler()
    app.log.addHandler(handler)

    env_patch = TestEnv()
    env_patch.start()

    # The entry URL for browser tests is different in notebook >= 6.0,
    # since that uses a local HTML file to point the user at the app.
    if hasattr(app, 'browser_open_file'):
        url = urljoin('file:', pathname2url(app.browser_open_file))
    else:
        url = app.display_url

    # Allow a synchronous function to be passed in.
    if inspect.iscoroutinefunction(func):
        test = func(url)
    else:
        app.log.info('Using thread pool executor to run test')
        loop = asyncio.get_event_loop()
        executor = ThreadPoolExecutor()
        task = loop.run_in_executor(executor, func, url)
        test = asyncio.wait([task])

    try:
        await test
    except Exception as e:
        app.log.critical("Caught exception during the test:")
        app.log.error(str(e))

    app.log.info("Test Complete")

    result = 0
    if handler.errored:
        result = 1
        app.log.critical('Exiting with 1 due to errors')
    else:
        app.log.info('Exiting normally')

    app.log.info('Stopping server...')
    try:
        app.http_server.stop()
        app.io_loop.stop()
        env_patch.stop()
    except Exception as e:
        app.log.error(str(e))
        result = 1
    finally:
        time.sleep(2)
        os._exit(result)
예제 #2
0
def run_test(app, func):
    """Run a test against the application.

    func is a function that accepts an app url as a parameter and returns a result.
    """
    handler = LogErrorHandler()

    env_patch = TestEnv()
    env_patch.start()

    def finished(future):
        try:
            result = future.result()
        except Exception as e:
            app.log.error(str(e))
        app.log.info('Stopping server...')
        app.stop()
        if handler.errored:
            app.log.critical('Exiting with 1 due to errors')
            result = 1
        elif result != 0:
            app.log.critical('Exiting with %s due to errors' % result)
        else:
            app.log.info('Exiting normally')
            result = 0

        try:
            app.http_server.stop()
            app.io_loop.stop()
            env_patch.stop()
            os._exit(result)
        except Exception as e:
            self.log.error(str(e))
            if 'Stream is closed' in str(e):
                os._exit(result)
            os._exit(1)

    # The entry URL for browser tests is different in notebook >= 6.0,
    # since that uses a local HTML file to point the user at the app.
    if hasattr(app, 'browser_open_file'):
        url = urljoin('file:', pathname2url(app.browser_open_file))
    else:
        url = app.display_url

    app.log.addHandler(handler)
    pool = ThreadPoolExecutor()
    future = pool.submit(func, url)
    IOLoop.current().add_future(future, finished)