<commit_before>import unittest

from kazoo.client import KazooClient

from pykit import threadutil
from pykit import utdocker
from pykit import ututil
from pykit import zkutil

dd = ututil.dd

zk_tag = 'daocloud.io/zookeeper:3.4.10'
zk_name = 'zk_test'


class TestAcid(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        utdocker.pull_image(zk_tag)

    def setUp(self):

        utdocker.create_network()
        utdocker.start_container(
            zk_name,
            zk_tag,
            env={
                "ZOO_MY_ID": 1,
                "ZOO_SERVERS": "server.1=0.0.0.0:2888:3888",
            },
            port_bindings={2181: 2181}
        )

        self.zk = KazooClient('127.0.0.1:2181')
        self.zk.start()

        dd('start zk-test in docker')

        self.path = 'a'
        self.zk.create(self.path, '1')

    def tearDown(self):

        self.zk.stop()
        utdocker.remove_container(zk_name)

    def test_cas(self):

        for curr, set_val in zkutil.cas_loop(self.zk, self.path):
            set_val(curr + 2)

        final_val, zstat = self.zk.get(self.path)
        dd(final_val, zstat)
        self.assertEqual('3', final_val)

    def test_cas_create_zk(self):

        for first_arg in (
                '127.0.0.1:2181',
                {'hosts': '127.0.0.1:2181'},
                zkutil.ZKConf(hosts='127.0.0.1:2181'),
        ):
            self.zk.set(self.path, '1')

            for curr, set_val in zkutil.cas_loop(first_arg, self.path):
                set_val(curr + 2)

            final_val, zstat = self.zk.get(self.path)
            dd(final_val, zstat)
            self.assertEqual('3', final_val)

    def test_cas_non_json(self):

        for curr, set_val in zkutil.cas_loop(self.zk, self.path, json=False):
            self.assertIsInstance(curr, str)
            set_val(curr + "a")

        final_val, zstat = self.zk.get(self.path)
        dd(final_val, zstat)
        self.assertEqual('1a', final_val)

    def test_cas_concurrent(self):

        def _update():
            for ii in range(10):
                for curr, set_val in zkutil.cas_loop('127.0.0.1:2181', self.path):
                    set_val(curr + 1)

        ths = [threadutil.start_daemon(_update)
               for _ in range(5)]

        for th in ths:
            th.join()

        final_val, zstat = self.zk.get(self.path)
        dd(final_val, zstat)

        self.assertEqual('51', final_val)
<commit_msg>zkacid: cas_loop(): test break<commit_after>import unittest

from kazoo.client import KazooClient

from pykit import threadutil
from pykit import utdocker
from pykit import ututil
from pykit import zkutil

dd = ututil.dd

zk_tag = 'daocloud.io/zookeeper:3.4.10'
zk_name = 'zk_test'


class TestAcid(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        utdocker.pull_image(zk_tag)

    def setUp(self):

        utdocker.create_network()
        utdocker.start_container(
            zk_name,
            zk_tag,
            env={
                "ZOO_MY_ID": 1,
                "ZOO_SERVERS": "server.1=0.0.0.0:2888:3888",
            },
            port_bindings={2181: 2181}
        )

        self.zk = KazooClient('127.0.0.1:2181')
        self.zk.start()

        dd('start zk-test in docker')

        self.path = 'a'
        self.zk.create(self.path, '1')

    def tearDown(self):

        self.zk.stop()
        utdocker.remove_container(zk_name)

    def test_cas(self):

        for curr, set_val in zkutil.cas_loop(self.zk, self.path):
            set_val(curr + 2)

        final_val, zstat = self.zk.get(self.path)
        dd(final_val, zstat)
        self.assertEqual('3', final_val)

    def test_cas_abort(self):

        for curr, set_val in zkutil.cas_loop(self.zk, self.path):
            set_val(curr + 2)
            break

        final_val, zstat = self.zk.get(self.path)
        dd(final_val, zstat)
        self.assertEqual('1', final_val, 'a break statement cancel set_val()')

    def test_cas_create_zk(self):

        for first_arg in (
                '127.0.0.1:2181',
                {'hosts': '127.0.0.1:2181'},
                zkutil.ZKConf(hosts='127.0.0.1:2181'),
        ):
            self.zk.set(self.path, '1')

            for curr, set_val in zkutil.cas_loop(first_arg, self.path):
                set_val(curr + 2)

            final_val, zstat = self.zk.get(self.path)
            dd(final_val, zstat)
            self.assertEqual('3', final_val)

    def test_cas_non_json(self):

        for curr, set_val in zkutil.cas_loop(self.zk, self.path, json=False):
            self.assertIsInstance(curr, str)
            set_val(curr + "a")

        final_val, zstat = self.zk.get(self.path)
        dd(final_val, zstat)
        self.assertEqual('1a', final_val)

    def test_cas_concurrent(self):

        def _update():
            for ii in range(10):
                for curr, set_val in zkutil.cas_loop('127.0.0.1:2181', self.path):
                    set_val(curr + 1)

        ths = [threadutil.start_daemon(_update)
               for _ in range(5)]

        for th in ths:
            th.join()

        final_val, zstat = self.zk.get(self.path)
        dd(final_val, zstat)

        self.assertEqual('51', final_val)

{'fraction_non_alphanumeric': 0.12014470677837015, 'fraction_numerical': 0.03427265803503427, 'mean_word_length': 1.9038142620232172, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4790479', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1799, 'n_words': 342}