def test_unsubscribe_any_other_exception(self, zk):
        zk_obj = zk.return_value
        zk_obj.delete_topic_partitions.side_effect = ZookeeperError("Boom!")

        unsubscriber = ZookeeperUnsubscriber(zk_obj)
        with pytest.raises(ZookeeperError):
            unsubscriber.unsubscribe_topics(
                'some_group',
                ['topic1'],
                [0, 1, 2],
                self.topics_partitions,
            )
    def test_unsubscribe_no_node_error(self, zk):
        zk_obj = zk.return_value
        zk_obj.delete_topic_partitions.side_effect = NoNodeError("Boom!")

        unsubscriber = ZookeeperUnsubscriber(zk_obj)
        unsubscriber.unsubscribe_topics(
            'some_group',
            ['topic1'],
            [0, 1, 2],
            self.topics_partitions,
        )

        assert zk_obj.delete_topic_partitions.called
    def test_unsubscribe_wipe_default_partitions(self, zk):
        zk_obj = zk.return_value
        zk_obj.get_my_subscribed_partitions.return_value = []

        unsubscriber = ZookeeperUnsubscriber(zk_obj)
        unsubscriber.unsubscribe_topics(
            'some_group',
            ['topic1'],
            None,
            self.topics_partitions,
        )

        assert zk_obj.delete_topic_partitions.call_count == 0
        assert zk_obj.delete_topic.call_args_list == [
            mock.call('some_group', 'topic1'),
        ]
    def test_unsubscribe_some_topics_disregard_partitions(self, zk):
        zk_obj = zk.return_value
        zk_obj.get_my_subscribed_partitions.return_value = []

        unsubscriber = ZookeeperUnsubscriber(zk_obj)
        unsubscriber.unsubscribe_topics(
            'some_group',
            ['topic1', 'topic3'],
            [0, 1],
            self.topics_partitions,
        )

        assert zk_obj.delete_topic_partitions.call_count == 0
        assert sorted(zk_obj.delete_topic.call_args_list) == sorted(
            [
                mock.call('some_group', 'topic1'),
                mock.call('some_group', 'topic3'),
            ],
        )
    def test_unsubscribe_wipe_all_partitions(self, zk):
        zk_obj = zk.return_value
        zk_obj.get_my_subscribed_partitions.return_value = []

        unsubscriber = ZookeeperUnsubscriber(zk_obj)
        unsubscriber.unsubscribe_topics(
            'some_group',
            ['topic1'],
            [0, 1, 2],
            self.topics_partitions,
        )

        calls = [
            mock.call(
                'some_group',
                'topic1',
                [0, 1, 2],
            ),
        ]

        assert zk_obj.delete_topic_partitions.call_args_list == calls
        assert zk_obj.delete_topic.call_args_list == [
            mock.call('some_group', 'topic1'),
        ]
    def test_unsubscribe_some_partitions_left(self, zk):
        zk_obj = zk.return_value
        zk_obj.get_my_subscribed_partitions.return_value = [3]

        unsubscriber = ZookeeperUnsubscriber(zk_obj)
        unsubscriber.unsubscribe_topics(
            'some_group',
            ['topic1'],
            [0, 1, 2],
            self.topics_partitions,
        )

        calls = [
            mock.call(
                'some_group',
                'topic1',
                [0, 1, 2],
            ),
        ]

        assert zk_obj.delete_topic_partitions.call_args_list == calls
        # Delete topic should not be called because the group is still
        # subscribed to some topic partitions
        assert not zk_obj.delete_topic.called