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))
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)) )
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()
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()
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)
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)
def test_no_dependencies(): future = tasklets._MultiFuture(()) assert future.result() == ()
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")