Exemple #1
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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #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_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 #11
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 #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 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 #14
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 #15
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 #16
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 #17
0
 def mget(self, keys, *args):
     args = list_or_args(keys, args)
     return Promise.all([self.get(arg) for arg in args])
Exemple #18
0
 def mget(self, keys, *args):
     args = list_or_args(keys, args)
     return Promise.all([self.get(arg) for arg in args])