Exemplo n.º 1
0
    def clear(self):
        """Reset the internal flag to ``False``.

        Calls to `.wait` will block until `.set` is called.
        """
        if self._future.done():
            self._future = Future()
Exemplo n.º 2
0
    def test_moment(self):
        calls = []

        @gen.coroutine
        def f(name, yieldable):
            for i in range(5):
                calls.append(name)
                yield yieldable

        # First, confirm the behavior without moment: each coroutine
        # monopolizes the event loop until it finishes.
        immediate = Future()
        immediate.set_result(None)
        yield [f('a', immediate), f('b', immediate)]
        self.assertEqual(''.join(calls), 'aaaaabbbbb')

        # With moment, they take turns.
        calls = []
        yield [f('a', gen.moment), f('b', gen.moment)]
        self.assertEqual(''.join(calls), 'ababababab')
        self.finished = True

        calls = []
        yield [f('a', gen.moment), f('b', immediate)]
        self.assertEqual(''.join(calls), 'abbbbbaaaa')
Exemplo n.º 3
0
    def test_iterator(self):
        futures = [Future(), Future(), Future(), Future()]

        self.finish_coroutines(0, futures)

        g = gen.WaitIterator(*futures)

        i = 0
        while not g.done():
            try:
                r = yield g.next()
            except ZeroDivisionError:
                self.assertIs(g.current_future, futures[0],
                              'exception future invalid')
            else:
                if i == 0:
                    self.assertEqual(r, 24, 'iterator value incorrect')
                    self.assertEqual(g.current_index, 2, 'wrong index')
                elif i == 2:
                    self.assertEqual(r, 42, 'iterator value incorrect')
                    self.assertEqual(g.current_index, 1, 'wrong index')
                elif i == 3:
                    self.assertEqual(r, 84, 'iterator value incorrect')
                    self.assertEqual(g.current_index, 3, 'wrong index')
            i += 1
Exemplo n.º 4
0
 def get_app(self):
     self.close_future = Future()
     return Application([
         ('/echo', EchoHandler, dict(close_future=self.close_future)),
         ('/non_ws', NonWebSocketHandler),
         ('/header', HeaderHandler, dict(close_future=self.close_future)),
         ('/header_echo', HeaderEchoHandler,
          dict(close_future=self.close_future)),
         ('/close_reason', CloseReasonHandler,
          dict(close_future=self.close_future)),
         ('/error_in_on_message', ErrorInOnMessageHandler,
          dict(close_future=self.close_future)),
         ('/async_prepare', AsyncPrepareHandler,
          dict(close_future=self.close_future)),
         ('/path_args/(.*)', PathArgsHandler,
          dict(close_future=self.close_future)),
         ('/coroutine', CoroutineOnMessageHandler,
          dict(close_future=self.close_future)),
         ('/render', RenderMessageHandler,
          dict(close_future=self.close_future)),
     ],
                        template_loader=DictLoader({
                            'message.html':
                            '<b>{{ message }}</b>',
                        }))
Exemplo n.º 5
0
 def test_already_resolved(self):
     future = Future()
     future.set_result('asdf')
     result = yield gen.with_timeout(datetime.timedelta(seconds=3600),
                                     future,
                                     io_loop=self.io_loop)
     self.assertEqual(result, 'asdf')
Exemplo n.º 6
0
 def test_completes_before_timeout(self):
     future = Future()
     self.io_loop.add_timeout(datetime.timedelta(seconds=0.1),
                              lambda: future.set_result('asdf'))
     result = yield gen.with_timeout(datetime.timedelta(seconds=3600),
                                     future,
                                     io_loop=self.io_loop)
     self.assertEqual(result, 'asdf')
Exemplo n.º 7
0
 def test_fails_before_timeout(self):
     future = Future()
     self.io_loop.add_timeout(
         datetime.timedelta(seconds=0.1),
         lambda: future.set_exception(ZeroDivisionError()))
     with self.assertRaises(ZeroDivisionError):
         yield gen.with_timeout(datetime.timedelta(seconds=3600),
                                future,
                                io_loop=self.io_loop)
Exemplo n.º 8
0
    def __init__(self, addrinfo, io_loop, connect):
        self.io_loop = io_loop
        self.connect = connect

        self.future = Future()
        self.timeout = None
        self.last_error = None
        self.remaining = len(addrinfo)
        self.primary_addrs, self.secondary_addrs = self.split(addrinfo)
Exemplo n.º 9
0
def multi_future(children, quiet_exceptions=()):
    """Wait for multiple asynchronous futures in parallel.

    This function is similar to `multi`, but does not support
    `YieldPoints <YieldPoint>`.

    .. versionadded:: 4.0

    .. versionchanged:: 4.2
       If multiple ``Futures`` fail, any exceptions after the first (which is
       raised) will be logged. Added the ``quiet_exceptions``
       argument to suppress this logging for selected exception types.

    .. deprecated:: 4.3
       Use `multi` instead.
    """
    if isinstance(children, dict):
        keys = list(children.keys())
        children = children.values()
    else:
        keys = None
    children = list(map(convert_yielded, children))
    assert all(is_future(i) for i in children)
    unfinished_children = set(children)

    future = Future()
    if not children:
        future.set_result({} if keys is not None else [])

    def callback(f):
        unfinished_children.remove(f)
        if not unfinished_children:
            result_list = []
            for f in children:
                try:
                    result_list.append(f.result())
                except Exception as e:
                    if future.done():
                        if not isinstance(e, quiet_exceptions):
                            app_log.error("Multiple exceptions in yield list",
                                          exc_info=True)
                    else:
                        future.set_exc_info(sys.exc_info())
            if not future.done():
                if keys is not None:
                    future.set_result(dict(zip(keys, result_list)))
                else:
                    future.set_result(result_list)

    listening = set()
    for f in children:
        if f not in listening:
            listening.add(f)
            f.add_done_callback(callback)
    return future
Exemplo n.º 10
0
 def capitalize(self, request_data, callback=None):
     logging.info("capitalize")
     self.request_data = request_data
     self.stream = IOStream(socket.socket(), io_loop=self.io_loop)
     self.stream.connect(('127.0.0.1', self.port),
                         callback=self.handle_connect)
     self.future = Future()
     if callback is not None:
         self.future.add_done_callback(
             stack_context.wrap(lambda future: callback(future.result())))
     return self.future
Exemplo n.º 11
0
    def get(self, timeout=None):
        """Remove and return an item from the queue.

        Returns a Future which resolves once an item is available, or raises
        `tornado.gen.TimeoutError` after a timeout.
        """
        future = Future()
        try:
            future.set_result(self.get_nowait())
        except QueueEmpty:
            self._getters.append(future)
            _set_timeout(future, timeout)
        return future
Exemplo n.º 12
0
    def _test_parse_input(self, method, asynchronous=False):
        if asynchronous:
            target = "salt.client.LocalClient.pub_async"
            pub_ret = Future()
            pub_ret.set_result({"jid": "123456789", "minions": ["m1"]})
        else:
            target = "salt.client.LocalClient.pub"
            pub_ret = {"jid": "123456789", "minions": ["m1"]}

        with patch(target, return_value=pub_ret) as pub_mock:
            with patch(
                "salt.client.LocalClient.get_cli_event_returns",
                return_value=[{"m1": {"ret": ["test.arg"]}}],
            ):
                with patch(
                    "salt.client.LocalClient.get_iter_returns",
                    return_value=[{"m1": {"ret": True}}],
                ):
                    ret = getattr(self.client, method)(
                        "*",
                        "test.arg",
                        arg=[
                            "a",
                            5,
                            "yaml_arg={qux: Qux}",
                            "another_yaml={bax: 12345}",
                        ],
                        jid="123456789",
                    )

                    # iterate generator if needed
                    if asynchronous:
                        pass
                    else:
                        ret = list(ret)

                    # main test here is that yaml_arg is getting deserialized properly
                    parsed_args = [
                        "a",
                        5,
                        {
                            "yaml_arg": {"qux": "Qux"},
                            "another_yaml": {"bax": 12345},
                            "__kwarg__": True,
                        },
                    ]
                    self.assertTrue(
                        any(parsed_args in call[0] for call in pub_mock.call_args_list)
                    )
Exemplo n.º 13
0
 def setUp(self):
     try:
         super(TestIOStreamStartTLS, self).setUp()
         self.listener, self.port = bind_unused_port()
         self.server_stream = None
         self.server_accepted = Future()
         netutil.add_accept_handler(self.listener, self.accept)
         self.client_stream = IOStream(socket.socket())
         self.io_loop.add_future(self.client_stream.connect(
             ('127.0.0.1', self.port)), self.stop)
         self.wait()
         self.io_loop.add_future(self.server_accepted, self.stop)
         self.wait()
     except Exception as e:
         print(e)
         raise
Exemplo n.º 14
0
    def wait_for_exit(self, raise_error=True):
        """Returns a `.Future` which resolves when the process exits.

        Usage::

            ret = yield proc.wait_for_exit()

        This is a coroutine-friendly alternative to `set_exit_callback`
        (and a replacement for the blocking `subprocess.Popen.wait`).

        By default, raises `subprocess.CalledProcessError` if the process
        has a non-zero exit status. Use ``wait_for_exit(raise_error=False)``
        to suppress this behavior and return the exit status without raising.

        .. versionadded:: 4.2
        """
        future = Future()

        def callback(ret):
            if ret != 0 and raise_error:
                # Unfortunately we don't have the original args any more.
                future.set_exception(CalledProcessError(ret, None))
            else:
                future.set_result(ret)

        self.set_exit_callback(callback)
        return future
Exemplo n.º 15
0
def Task(func, *args, **kwargs):
    """Adapts a callback-based asynchronous function for use in coroutines.

    Takes a function (and optional additional arguments) and runs it with
    those arguments plus a ``callback`` keyword argument.  The argument passed
    to the callback is returned as the result of the yield expression.

    .. versionchanged:: 4.0
       ``gen.Task`` is now a function that returns a `.Future`, instead of
       a subclass of `YieldPoint`.  It still behaves the same way when
       yielded.
    """
    future = Future()

    def handle_exception(typ, value, tb):
        if future.done():
            return False
        future.set_exc_info((typ, value, tb))
        return True

    def set_result(result):
        if future.done():
            return
        future.set_result(result)
    with stack_context.ExceptionStackContext(handle_exception):
        func(*args, callback=_argument_adapter(set_result), **kwargs)
    return future
Exemplo n.º 16
0
 def get_app(self):
     self.close_future = Future()
     return Application([
         ('/echo', EchoHandler,
          dict(close_future=self.close_future,
               compression_options=self.get_server_compression_options())),
     ])
Exemplo n.º 17
0
 def __init__(self, stream, is_client, params=None, context=None):
     """
     :arg stream: an `.IOStream`
     :arg bool is_client: client or server
     :arg params: a `.HTTP1ConnectionParameters` instance or ``None``
     :arg context: an opaque application-defined object that can be accessed
         as ``connection.context``.
     """
     self.is_client = is_client
     self.stream = stream
     if params is None:
         params = HTTP1ConnectionParameters()
     self.params = params
     self.context = context
     self.no_keep_alive = params.no_keep_alive
     # The body limits can be altered by the delegate, so save them
     # here instead of just referencing self.params later.
     self._max_body_size = (self.params.max_body_size
                            or self.stream.max_buffer_size)
     self._body_timeout = self.params.body_timeout
     # _write_finished is set to True when finish() has been called,
     # i.e. there will be no more data sent.  Data may still be in the
     # stream's write buffer.
     self._write_finished = False
     # True when we have read the entire incoming body.
     self._read_finished = False
     # _finish_future resolves when all data has been written and flushed
     # to the IOStream.
     self._finish_future = Future()
     # If true, the connection should be closed after this request
     # (after the response has been written in the server side,
     # and after it has been read in the client)
     self._disconnect_on_finish = False
     self._clear_callbacks()
     # Save the start lines after we read or write them; they
     # affect later processing (e.g. 304 responses and HEAD methods
     # have content-length but no bodies)
     self._request_start_line = None
     self._response_start_line = None
     self._request_headers = None
     # True if we are writing output with chunked encoding.
     self._chunking_output = None
     # While reading a body with a content-length, this is the
     # amount left to read.
     self._expected_content_remaining = None
     # A Future for our outgoing writes, returned by IOStream.write.
     self._pending_write = None
Exemplo n.º 18
0
def sleep(duration):
    """Return a `.Future` that resolves after the given number of seconds.

    When used with ``yield`` in a coroutine, this is a non-blocking
    analogue to `time.sleep` (which should not be used in coroutines
    because it is blocking)::

        yield gen.sleep(0.5)

    Note that calling this function on its own does nothing; you must
    wait on the `.Future` it returns (usually by yielding it).

    .. versionadded:: 4.1
    """
    f = Future()
    IOLoop.current().call_later(duration, lambda: f.set_result(None))
    return f
Exemplo n.º 19
0
    def get_app(self):
        class PingHandler(TestWebSocketHandler):
            def on_ping(self, data):
                self.write_message("got ping")

        self.close_future = Future()
        return Application([
            ('/', PingHandler, dict(close_future=self.close_future)),
        ])
Exemplo n.º 20
0
    def wait(self, timeout=None):
        """Wait for `.notify`.

        Returns a `.Future` that resolves ``True`` if the condition is notified,
        or ``False`` after a timeout.
        """
        waiter = Future()
        self._waiters.append(waiter)
        if timeout:

            def on_timeout():
                waiter.set_result(False)
                self._garbage_collect()

            io_loop = ioloop.IOLoop.current()
            timeout_handle = io_loop.add_timeout(timeout, on_timeout)
            waiter.add_done_callback(
                lambda _: io_loop.remove_timeout(timeout_handle))
        return waiter
Exemplo n.º 21
0
def maybe_future(x):
    """Converts ``x`` into a `.Future`.

    If ``x`` is already a `.Future`, it is simply returned; otherwise
    it is wrapped in a new `.Future`.  This is suitable for use as
    ``result = yield gen.maybe_future(f())`` when you don't know whether
    ``f()`` returns a `.Future` or not.

    .. deprecated:: 4.3
       This function only handles ``Futures``, not other yieldable objects.
       Instead of `maybe_future`, check for the non-future result types
       you expect (often just ``None``), and ``yield`` anything unknown.
    """
    if is_future(x):
        return x
    else:
        fut = Future()
        fut.set_result(x)
        return fut
Exemplo n.º 22
0
    def write(self, chunk, callback=None):
        """Implements `.HTTPConnection.write`.

        For backwards compatibility is is allowed but deprecated to
        skip `write_headers` and instead call `write()` with a
        pre-encoded header block.
        """
        future = None
        if self.stream.closed():
            future = self._write_future = Future()
            self._write_future.set_exception(iostream.StreamClosedError())
            self._write_future.exception()
        else:
            if callback is not None:
                self._write_callback = stack_context.wrap(callback)
            else:
                future = self._write_future = Future()
            self._pending_write = self.stream.write(self._format_chunk(chunk))
            self._pending_write.add_done_callback(self._on_write_complete)
        return future
Exemplo n.º 23
0
    def _(d):
        f = Future()

        def errback(failure):
            try:
                failure.raiseException()
                # Should never happen, but just in case
                raise Exception("errback called without error")
            except:
                f.set_exc_info(sys.exc_info())
        d.addCallbacks(f.set_result, errback)
        return f
Exemplo n.º 24
0
class ManualCapClient(BaseCapClient):
    def capitalize(self, request_data, callback=None):
        logging.info("capitalize")
        self.request_data = request_data
        self.stream = IOStream(socket.socket(), io_loop=self.io_loop)
        self.stream.connect(('127.0.0.1', self.port),
                            callback=self.handle_connect)
        self.future = Future()
        if callback is not None:
            self.future.add_done_callback(
                stack_context.wrap(lambda future: callback(future.result())))
        return self.future

    def handle_connect(self):
        logging.info("handle_connect")
        self.stream.write(utf8(self.request_data + "\n"))
        self.stream.read_until(b'\n', callback=self.handle_read)

    def handle_read(self, data):
        logging.info("handle_read")
        self.stream.close()
        try:
            self.future.set_result(self.process_response(data))
        except CapError as e:
            self.future.set_exception(e)
Exemplo n.º 25
0
    def test_wait_for_handshake_already_waiting_error(self):
        test = self
        handshake_future = Future()

        class TestServer(TCPServer):
            def handle_stream(self, stream, address):
                stream.wait_for_handshake(self.handshake_done)
                test.assertRaises(RuntimeError, stream.wait_for_handshake)

            def handshake_done(self):
                handshake_future.set_result(None)

        yield self.connect_to_server(TestServer)
        yield handshake_future
Exemplo n.º 26
0
 def _create_stream(self,
                    max_buffer_size,
                    af,
                    addr,
                    source_ip=None,
                    source_port=None):
     # Always connect in plaintext; we'll convert to ssl if necessary
     # after one connection has completed.
     source_port_bind = source_port if isinstance(source_port, int) else 0
     source_ip_bind = source_ip
     if source_port_bind and not source_ip:
         # User required a specific port, but did not specify
         # a certain source IP, will bind to the default loopback.
         source_ip_bind = '::1' if af == socket.AF_INET6 else '127.0.0.1'
         # Trying to use the same address family as the requested af socket:
         # - 127.0.0.1 for IPv4
         # - ::1 for IPv6
     socket_obj = socket.socket(af)
     set_close_exec(socket_obj.fileno())
     if source_port_bind or source_ip_bind:
         # If the user requires binding also to a specific IP/port.
         try:
             socket_obj.bind((source_ip_bind, source_port_bind))
         except socket.error:
             socket_obj.close()
             # Fail loudly if unable to use the IP/port.
             raise
     try:
         stream = IOStream(socket_obj,
                           io_loop=self.io_loop,
                           max_buffer_size=max_buffer_size)
     except socket.error as e:
         fu = Future()
         fu.set_exception(e)
         return fu
     else:
         return stream.connect(addr)
Exemplo n.º 27
0
    def test_iterator_async_await(self):
        # Recreate the previous test with py35 syntax. It's a little clunky
        # because of the way the previous test handles an exception on
        # a single iteration.
        futures = [Future(), Future(), Future(), Future()]
        self.finish_coroutines(0, futures)
        self.finished = False

        namespace = exec_test(
            globals(), locals(), """
        async def f():
            i = 0
            g = gen.WaitIterator(*futures)
            try:
                async for r in g:
                    if i == 0:
                        self.assertEqual(r, 24, 'iterator value incorrect')
                        self.assertEqual(g.current_index, 2, 'wrong index')
                    else:
                        raise Exception("expected exception on iteration 1")
                    i += 1
            except ZeroDivisionError:
                i += 1
            async for r in g:
                if i == 2:
                    self.assertEqual(r, 42, 'iterator value incorrect')
                    self.assertEqual(g.current_index, 1, 'wrong index')
                elif i == 3:
                    self.assertEqual(r, 84, 'iterator value incorrect')
                    self.assertEqual(g.current_index, 3, 'wrong index')
                else:
                    raise Exception("didn't expect iteration %d" % i)
                i += 1
            self.finished = True
        """)
        yield namespace['f']()
        self.assertTrue(self.finished)
Exemplo n.º 28
0
    def put(self, item, timeout=None):
        """Put an item into the queue, perhaps waiting until there is room.

        Returns a Future, which raises `tornado.gen.TimeoutError` after a
        timeout.
        """
        try:
            self.put_nowait(item)
        except QueueFull:
            future = Future()
            self._putters.append((item, future))
            _set_timeout(future, timeout)
            return future
        else:
            return gen._null_future
Exemplo n.º 29
0
    def test_wait_for_handshake_future(self):
        test = self
        handshake_future = Future()

        class TestServer(TCPServer):
            def handle_stream(self, stream, address):
                test.assertIsNone(stream.socket.cipher())
                test.io_loop.spawn_callback(self.handle_connection, stream)

            @gen.coroutine
            def handle_connection(self, stream):
                yield stream.wait_for_handshake()
                handshake_future.set_result(None)

        yield self.connect_to_server(TestServer)
        yield handshake_future
Exemplo n.º 30
0
    def test_wait_for_handshake_already_connected(self):
        handshake_future = Future()

        class TestServer(TCPServer):
            def handle_stream(self, stream, address):
                self.stream = stream
                stream.wait_for_handshake(self.handshake_done)

            def handshake_done(self):
                self.stream.wait_for_handshake(self.handshake2_done)

            def handshake2_done(self):
                handshake_future.set_result(None)

        yield self.connect_to_server(TestServer)
        yield handshake_future