Exemplo n.º 1
0
 def test_submit_request_context(self):
     test_value = random.randint(1, 101)
     executor = Executor(self.app)
     with self.app.test_request_context(''):
         request.test_value = test_value
         future = executor.submit(request_context_test_value)
     assert future.result() == test_value
Exemplo n.º 2
0
 def test_submit_app_context(self):
     test_value = random.randint(1, 101)
     self.app.config['TEST_VALUE'] = test_value
     executor = Executor(self.app)
     with self.app.test_request_context(''):
         future = executor.submit(app_context_test_value)
     assert future.result() == test_value
Exemplo n.º 3
0
 def test_propagate_exception_callback(self):
     self.app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
     executor = Executor(self.app)
     with self.assertRaises(RuntimeError):
         with self.app.test_request_context('/'):
             future = executor.submit(fail)
             assert propagate_exceptions_callback in future._done_callbacks
             concurrent.futures.wait([future])
             propagate_exceptions_callback(future)
Exemplo n.º 4
0
    def test_default_done_callback(self):
        executor = Executor(self.app)

        def callback(future):
            setattr(future, 'test', 'test')

        executor.add_default_done_callback(callback)
        with self.app.test_request_context('/'):
            future = executor.submit(fib, 5)
            concurrent.futures.wait([future])
            assert hasattr(future, 'test')
Exemplo n.º 5
0
 def test_future_proxy(self):
     app = Flask(__name__)
     executor = Executor(app)
     with app.test_request_context(''):
         future = executor.submit(pow, 2, 4)
     # Test if we're returning a subclass of Future
     assert isinstance(future, concurrent.futures.Future)
     assert isinstance(future, FutureProxy)
     concurrent.futures.wait([future])
     # test standard Future methods and attributes
     assert future._state == concurrent.futures._base.FINISHED
     assert future.done()
     assert future.exception(timeout=0) is None
Exemplo n.º 6
0
    def test_add_done_callback(self):
        """Exceptions thrown in callbacks can't be easily caught and make it hard
        to test for callback failure. To combat this, a global variable is used to
        store the value of an exception and test for its existence.
        """
        app = Flask(__name__)
        executor = Executor(app)
        global exception
        exception = None
        with app.test_request_context(''):
            future = executor.submit(time.sleep, 0.5)

            def callback(future):
                global exception
                try:
                    executor.submit(time.sleep, 0)
                except RuntimeError as e:
                    exception = e

            future.add_done_callback(callback)
        concurrent.futures.wait([future])
        assert exception is None
Exemplo n.º 7
0
 def test_submit(self):
     self.app = Flask(__name__)
     executor = Executor(self.app)
     with self.app.test_request_context(''):
         future = executor.submit(fib, 5)
     assert future.result() == fib(5)
Exemplo n.º 8
0
 def test_named_executor_submit(self):
     name = 'custom'
     custom_executor = Executor(self.app, name=name)
     with self.app.test_request_context(''):
         future = custom_executor.submit(fib, 5)
     assert future.result() == fib(5)