示例#1
0
    def test_nested():
        dependencies = [tasklets.Future() for _ in range(3)]
        future = tasklets._MultiFuture((dependencies[0], dependencies[1:]))
        for i, dependency in enumerate(dependencies):
            dependency.set_result(i)

        assert future.result() == (0, (1, 2))
示例#2
0
 def test___repr__():
     this, that = (tasklets.Future("this"), tasklets.Future("that"))
     future = tasklets._MultiFuture((this, that))
     assert repr(future) == (
         "_MultiFuture(Future('this') <{}>,"
         " Future('that') <{}>) <{}>".format(id(this), id(that), id(future))
     )
示例#3
0
 def test_cancel():
     dependencies = (tasklets.Future(), tasklets.Future())
     future = tasklets._MultiFuture(dependencies)
     future.cancel()
     assert dependencies[0].cancelled()
     assert dependencies[1].cancelled()
     with pytest.raises(exceptions.Cancelled):
         future.result()
示例#4
0
 def test_error():
     dependencies = (tasklets.Future(), tasklets.Future())
     future = tasklets._MultiFuture(dependencies)
     error = Exception("Spurious error.")
     dependencies[0].set_exception(error)
     dependencies[1].set_result("two")
     assert future.exception() is error
     with pytest.raises(Exception):
         future.result()
示例#5
0
    def add(self, keys):
        """Add incomplete keys to batch to allocate.

        Args:
            keys (list(datastore.key)): Allocate ids for these keys.

        Returns:
            tasklets.Future: A future for the eventual keys completed with
                allocated ids.
        """
        futures = []
        for key in keys:
            future = tasklets.Future(info="AllocateIds({})".format(key))
            futures.append(future)
            self.keys.append(key)

        self.futures.extend(futures)
        return tasklets._MultiFuture(futures)
示例#6
0
def allocate(keys, options):
    """Allocate ids for incomplete keys.

    Args:
        key (key.Key): The incomplete key.
        options (_options.Options): The options for the request.

    Returns:
        tasklets.Future: A future for the key completed with the allocated id.
    """
    futures = []
    while keys:
        batch = _batch.get_batch(_AllocateIdsBatch, options)
        room_left = batch.room_left()
        batch_keys = keys[:room_left]
        futures.extend(batch.add(batch_keys))
        keys = keys[room_left:]

    return tasklets._MultiFuture(futures)
示例#7
0
 def test_no_dependencies():
     future = tasklets._MultiFuture(())
     assert future.result() == ()
示例#8
0
 def test_success():
     dependencies = (tasklets.Future(), tasklets.Future())
     future = tasklets._MultiFuture(dependencies)
     dependencies[0].set_result("one")
     dependencies[1].set_result("two")
     assert future.result() == ("one", "two")