summaryrefslogtreecommitdiff
path: root/kazoo/tests/test_party.py
blob: 2089d54785148970e0920c0cc5863e7ea6e8943f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import uuid

from kazoo.testing import KazooTestCase


class KazooPartyTests(KazooTestCase):
    def setUp(self):
        super(KazooPartyTests, self).setUp()
        self.path = "/" + uuid.uuid4().hex

    def test_party(self):
        parties = [self.client.Party(self.path, "p%s" % i)
                   for i in range(5)]

        one_party = parties[0]

        assert list(one_party) == []
        assert len(one_party) == 0

        participants = set()
        for party in parties:
            party.join()
            participants.add(party.data.decode('utf-8'))

            assert set(party) == participants
            assert len(party) == len(participants)

        for party in parties:
            party.leave()
            participants.remove(party.data.decode('utf-8'))

            assert set(party) == participants
            assert len(party) == len(participants)

    def test_party_reuse_node(self):
        party = self.client.Party(self.path, "p1")
        self.client.ensure_path(self.path)
        self.client.create(party.create_path)
        party.join()
        assert party.participating is True
        party.leave()
        assert party.participating is False
        assert len(party) == 0

    def test_party_vanishing_node(self):
        party = self.client.Party(self.path, "p1")
        party.join()
        assert party.participating is True
        self.client.delete(party.create_path)
        party.leave()
        assert party.participating is False
        assert len(party) == 0


class KazooShallowPartyTests(KazooTestCase):
    def setUp(self):
        super(KazooShallowPartyTests, self).setUp()
        self.path = "/" + uuid.uuid4().hex

    def test_party(self):
        parties = [self.client.ShallowParty(self.path, "p%s" % i)
                   for i in range(5)]

        one_party = parties[0]

        assert list(one_party) == []
        assert len(one_party) == 0

        participants = set()
        for party in parties:
            party.join()
            participants.add(party.data.decode('utf-8'))

            assert set(party) == participants
            assert len(party) == len(participants)

        for party in parties:
            party.leave()
            participants.remove(party.data.decode('utf-8'))

            assert set(party) == participants
            assert len(party) == len(participants)