Exemple #1
0
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}
Exemple #2
0
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
Exemple #3
0
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]
Exemple #4
0
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]
Exemple #5
0
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]
Exemple #6
0
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"]]
Exemple #7
0
    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)
Exemple #8
0
    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)
Exemple #9
0
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)))
Exemple #10
0
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)))
Exemple #11
0
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)))
Exemple #12
0
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']]
Exemple #13
0
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']]
Exemple #14
0
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"]]
Exemple #15
0
    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)
Exemple #16
0
    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)
Exemple #17
0
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
Exemple #18
0
 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
Exemple #19
0
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
Exemple #20
0
 def mset(self, *args, **kwargs):
     return Promise.all([self.set(k, v) for k, v in dict(*args, **kwargs)
                         .iteritems()]).then(lambda x: None)
Exemple #21
0
 def mget(self, keys, *args):
     args = list_or_args(keys, args)
     return Promise.all([self.get(arg) for arg in args])
Exemple #22
0
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
Exemple #23
0
 def mset(self, *args, **kwargs):
     return Promise.all([self.set(k, v) for k, v in dict(*args, **kwargs)
                         .iteritems()]).then(lambda x: None)
Exemple #24
0
 def mget(self, keys, *args):
     args = list_or_args(keys, args)
     return Promise.all([self.get(arg) for arg in args])
Exemple #25
0
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