def test_rejoin(self):
        handler = TestHandler(1)

        service = LeaderElectedService(self.client, "test", ("127.0.0.1", 80),
                                       handler)
        service.join()

        assert_that(handler. await (5), is_(True))
        assert_that(handler.value, is_(1))

        # Expected number depends on how many times expire_session
        # interrupted our session.
        handler.expected = None
        handler.calls = 0
        count = [0]

        def session_watcher(state):
            if state == kazoo.protocol.states.KazooState.CONNECTED:
                count[0] += 1

        self.client.add_listener(session_watcher)

        self.expire_session()
        self.client.retry(self.client.get, "/")

        self.client.remove_listener(session_watcher)

        # Should have received 2x number of calls as CONNECTED events:
        # on_left and a following on_joined.
        handler.expected = count[0] * 2

        assert_that(handler. await (5), is_(True))
        assert_that(handler.value, is_(1))
    def test_rejoin(self):
        handler = TestHandler(1)

        service = LeaderElectedService(self.client, "test", ("127.0.0.1", 80), handler)
        service.join()

        assert_that(handler.await(5), is_(True))
        assert_that(handler.value, is_(1))

        # Expected number depends on how many times expire_session
        # interrupted our session.
        handler.expected = None
        handler.calls = 0
        count = [0]

        def session_watcher(state):
            if state == kazoo.protocol.states.KazooState.CONNECTED:
                count[0] += 1

        self.client.add_listener(session_watcher)

        self.expire_session()
        self.client.retry(self.client.get, "/")

        self.client.remove_listener(session_watcher)

        # Should have received 2x number of calls as CONNECTED events:
        # on_left and a following on_joined.
        handler.expected = count[0] * 2

        assert_that(handler.await(5), is_(True))
        assert_that(handler.value, is_(1))
    def test_join(self):
        handler = TestHandler(1)

        service_name = "test"
        service = LeaderElectedService(self.client, service_name, ("127.0.0.1", 80), handler)
        service_path = service.join()

        assert_that(handler.await(5), is_(True))
        assert_that(handler.value, is_(1))
        assert_that(service_path, starts_with("/services/{0}/node-".format(service_name)))
    def test_join(self):
        handler = TestHandler(1)

        service_name = "test"
        service = LeaderElectedService(self.client, service_name,
                                       ("127.0.0.1", 80), handler)
        service_path = service.join()

        assert_that(handler. await (5), is_(True))
        assert_that(handler.value, is_(1))
        assert_that(service_path,
                    starts_with("/services/{0}/node-".format(service_name)))
    def test_service_node(self):
        handler = TestHandler(1)

        service = LeaderElectedService(self.client, "test", ("127.0.0.1", 80), handler)
        service.join()

        assert_that(handler.await(5), is_(True))
        assert_that(handler.value, is_(1))

        children = self.client.get_children("/services/test")
        assert_that(children, has_len(1))

        child, _ = self.client.get("/services/test/%s" % children[0])

        assert_that(parse_address(child), is_(("127.0.0.1", 80)))
    def test_service_node(self):
        handler = TestHandler(1)

        service = LeaderElectedService(self.client, "test", ("127.0.0.1", 80),
                                       handler)
        service.join()

        assert_that(handler. await (5), is_(True))
        assert_that(handler.value, is_(1))

        children = self.client.get_children("/services/test")
        assert_that(children, has_len(1))

        child, _ = self.client.get("/services/test/%s" % children[0])

        assert_that(parse_address(child), is_(("127.0.0.1", 80)))
    def test_single_concurrent_member(self):
        handler_a = TestHandler(1)
        handler_b = TestHandler(1)

        service_name = "test"
        service_a = LeaderElectedService(self.client, service_name, ("127.0.0.1", 80), handler_a)
        service_path_a = service_a.join()

        # Just A
        assert_that(handler_a.await(5), is_(True))
        assert_that(handler_a.value, is_(1))
        assert_that(service_path_a, starts_with("/services/{0}/node-".format(service_name)))

        service_b = LeaderElectedService(self.client, service_name, ("127.0.0.1", 8080), handler_b)
        service_path_b = service_b.join()

        # B is still waiting to get membership (timeout after 1 second)
        assert_that(handler_b.await(1), is_(False))
        assert_that(handler_b.value, is_(0))
        assert_that(service_path_b, starts_with("/services/{0}/node-".format(service_name)))

        assert_that(service_path_a, not equal_to_ignoring_case(service_path_b))

        handler_a.calls = 0
        service_a.leave()

        # A left
        assert_that(handler_a.await(5), is_(True))
        assert_that(handler_a.value, is_(0))

        # B should have joined since A is gone
        assert_that(handler_b.await(5), is_(True))
        assert_that(handler_b.value, is_(1))
    def test_single_concurrent_member(self):
        handler_a = TestHandler(1)
        handler_b = TestHandler(1)

        service_name = "test"
        service_a = LeaderElectedService(self.client, service_name,
                                         ("127.0.0.1", 80), handler_a)
        service_path_a = service_a.join()

        # Just A
        assert_that(handler_a. await (5), is_(True))
        assert_that(handler_a.value, is_(1))
        assert_that(service_path_a,
                    starts_with("/services/{0}/node-".format(service_name)))

        service_b = LeaderElectedService(self.client, service_name,
                                         ("127.0.0.1", 8080), handler_b)
        service_path_b = service_b.join()

        # B is still waiting to get membership (timeout after 1 second)
        assert_that(handler_b. await (1), is_(False))
        assert_that(handler_b.value, is_(0))
        assert_that(service_path_b,
                    starts_with("/services/{0}/node-".format(service_name)))

        assert_that(service_path_a, not equal_to_ignoring_case(service_path_b))

        handler_a.calls = 0
        service_a.leave()

        # A left
        assert_that(handler_a. await (5), is_(True))
        assert_that(handler_a.value, is_(0))

        # B should have joined since A is gone
        assert_that(handler_b. await (5), is_(True))
        assert_that(handler_b.value, is_(1))