def test_auto_coercion(): p = Promise.all([1, 2, Promise.resolved(3)]) assert p.is_resolved assert p.value == [1, 2, 3] p = Promise.all({1: 1, 2: 2, 3: Promise.resolved(3)}) assert p.is_resolved assert p.value == {1: 1, 2: 2, 3: 3}
def test_rejected_promise(): err = RuntimeError('So fail') p = Promise.rejected(err) assert not p.is_resolved assert not p.is_pending assert p.is_rejected assert p.reason == err
def test_promise_then(): p = Promise.resolved([1, 2, 3]) def on_success(value): return value + [4] p2 = p.then(success=on_success) assert p2.value == [1, 2, 3, 4]
def test_failure_callbacks(): results = [] p = Promise() assert p.is_pending p.done(on_failure=results.append) assert results == [] p.reject(42) assert results == [42] p = Promise.rejected(23) p.done(on_failure=results.append) assert results == [42, 23]
def test_success_callbacks(): results = [] p = Promise() assert p.is_pending p.done(results.append) assert results == [] p.resolve(42) assert results == [42] p = Promise.resolved(23) p.done(results.append) assert results == [42, 23]
def test_batch_promise_all(cluster): with cluster.map() as client: client.set("1", "a") client.set("2", "b") client.set("3", "c") client.set("4", "d") client.hset("a", "b", "XXX") with cluster.map() as client: rv = Promise.all([client.mget("1", "2"), client.hget("a", "b"), client.mget("3", "4")]) assert rv.value == [["a", "b"], "XXX", ["c", "d"]]
def execute_command(self, *args): promises = {} hosts = self._target_hosts if hosts == 'all': hosts = self.connection_pool.cluster.hosts.keys() elif hosts is None: raise RuntimeError('Fanout client was not targeted to hosts.') for host_id in hosts: buf = self._get_command_buffer(host_id, args[0]) promises[host_id] = buf.enqueue_command(args[0], args[1:]) return Promise.all(promises)
def test_reconnect(cluster): with cluster.map() as client: for x in xrange(10): client.set(str(x), str(x)) with cluster.all() as client: client.config_set("timeout", 1) time.sleep(2) with cluster.map() as client: rv = Promise.all([client.get(str(x)) for x in xrange(10)]) assert rv.value == list(map(str, xrange(10)))
def test_reconnect(cluster): with cluster.map() as client: for x in range(10): client.set(text_type(x), text_type(x)) with cluster.all() as client: client.config_set("timeout", 1) time.sleep(2) with cluster.map() as client: rv = Promise.all([client.get(text_type(x)) for x in range(10)]) assert rv.value == list(map(text_type, range(10)))
def test_reconnect(cluster): with cluster.map() as client: for x in xrange(10): client.set(str(x), str(x)) with cluster.all() as client: client.config_set('timeout', 1) time.sleep(2) with cluster.map() as client: rv = Promise.all([client.get(str(x)) for x in xrange(10)]) assert rv.value == list(map(str, xrange(10)))
def test_batch_promise_all(cluster): with cluster.map() as client: client.set('1', 'a') client.set('2', 'b') client.set('3', 'c') client.set('4', 'd') client.hset('a', 'b', 'XXX') with cluster.map() as client: rv = Promise.all([ client.mget('1', '2'), client.hget('a', 'b'), client.mget('3', '4'), ]) assert rv.value == [['a', 'b'], 'XXX', ['c', 'd']]
def test_batch_promise_all(cluster): with cluster.map() as client: client.set("1", "a") client.set("2", "b") client.set("3", "c") client.set("4", "d") client.hset("a", "b", "XXX") with cluster.map() as client: rv = Promise.all([ client.mget("1", "2"), client.hget("a", "b"), client.mget("3", "4"), ]) assert rv.value == [["a", "b"], "XXX", ["c", "d"]]
def execute_command(self, *args, **options): promises = {} hosts = self._target_hosts if hosts == 'all': hosts = self.connection_pool.cluster.hosts.keys() elif hosts is None: raise RuntimeError('Fanout client was not targeted to hosts.') for host_id in hosts: buf = self._get_command_buffer(host_id, args[0]) promise = buf.enqueue_command(args[0], args[1:], options) if self.__resolve_singular_result and len(hosts) == 1: return promise promises[host_id] = promise return Promise.all(promises)
def merge_batch(command_name, arg_promise_tuples): batch_command, list_response = AUTO_BATCH_COMMANDS[command_name] if len(arg_promise_tuples) == 1: args, promise = arg_promise_tuples[0] return command_name, args, promise promise = Promise() @promise.done def on_success(value): if list_response: for item, (_, promise) in izip(value, arg_promise_tuples): promise.resolve(item) else: for _, promise in arg_promise_tuples: promise.resolve(value) args = [] for individual_args, _ in arg_promise_tuples: args.extend(individual_args) return batch_command, args, promise
def enqueue_command(self, command_name, args): """Enqueue a new command into this pipeline.""" assert_open(self) promise = Promise() self.commands.append((command_name, args, promise)) return promise
def test_resolved_promise(): p = Promise.resolved(42) assert p.is_resolved assert not p.is_pending assert not p.is_rejected assert p.value == 42
def mset(self, *args, **kwargs): return Promise.all([self.set(k, v) for k, v in dict(*args, **kwargs) .iteritems()]).then(lambda x: None)
def mget(self, keys, *args): args = list_or_args(keys, args) return Promise.all([self.get(arg) for arg in args])
def test_promise_all(): p = Promise.all([]) assert p.is_resolved assert p.value == [] p = Promise.all({}) assert p.is_resolved assert p.value == {} p = Promise.all([ Promise.resolved(1), Promise.resolved(2), Promise.resolved(3), ]) assert p.is_resolved assert p.value == [1, 2, 3] p = Promise.all({ "key1": Promise.resolved(1), "key2": Promise.resolved(2), "key3": Promise.resolved(3), }) assert p.is_resolved assert p.value == {"key1": 1, "key2": 2, "key3": 3} p = Promise.all([ Promise.resolved(1), Promise.rejected(2), Promise.resolved(3), ]) assert p.is_rejected assert p.reason == 2
def test_promise_all(): p = Promise.all([]) assert p.is_resolved assert p.value == [] p = Promise.all({}) assert p.is_resolved assert p.value == {} p = Promise.all([ Promise.resolved(1), Promise.resolved(2), Promise.resolved(3), ]) assert p.is_resolved assert p.value == [1, 2, 3] p = Promise.all({ 'key1': Promise.resolved(1), 'key2': Promise.resolved(2), 'key3': Promise.resolved(3), }) assert p.is_resolved assert p.value == {'key1': 1, 'key2': 2, 'key3': 3} p = Promise.all([ Promise.resolved(1), Promise.rejected(2), Promise.resolved(3), ]) assert p.is_rejected assert p.reason == 2