<commit_before>import sys
import threading
import time
import uuid
import unittest

from nose import SkipTest
from nose.tools import eq_
from nose.tools import raises

from kazoo.testing import KazooTestCase
from kazoo.exceptions import BadArgumentsError
from kazoo.exceptions import ConfigurationError
from kazoo.exceptions import InvalidACLError
from kazoo.exceptions import NoNodeError
from kazoo.exceptions import NoAuthError
from kazoo.exceptions import ConnectionLoss

if sys.version_info > (3, ):  # pragma: nocover
    def u(s):
        return s
else:  # pragma: nocover
    def u(s):
        return unicode(s, "unicode_escape")


class TestClientTransitions(KazooTestCase):
    def test_connection_and_disconnection(self):
        from kazoo.client import KazooState
        states = []
        rc = threading.Event()

        @self.client.add_listener
        def listener(state):
            states.append(state)
            if state == KazooState.CONNECTED:
                rc.set()

        self.client.stop()
        eq_(states, [KazooState.LOST])
        states.pop()

        self.client.start()
        rc.wait(2)
        eq_(states, [KazooState.CONNECTED])
        rc.clear()
        states.pop()
        self.expire_session()
        rc.wait(2)<commit_msg>Issue #42: added tests showing misbehaving session id `stickiness`<commit_after>import atexit
import os
import sys
import threading
import time
import uuid
import unittest

from nose import SkipTest
from nose.tools import eq_
from nose.tools import raises

from kazoo.testing import KazooTestCase
from kazoo.exceptions import BadArgumentsError
from kazoo.exceptions import ConfigurationError
from kazoo.exceptions import InvalidACLError
from kazoo.exceptions import NoNodeError
from kazoo.exceptions import NoAuthError
from kazoo.exceptions import ConnectionLoss

if sys.version_info > (3, ):  # pragma: nocover
    def u(s):
        return s
else:  # pragma: nocover
    def u(s):
        return unicode(s, "unicode_escape")


class TestSessions(unittest.TestCase):

    def setUp(self):
        from kazoo.client import KazooClient
        from kazoo.protocol.states import KazooState
        from kazoo.testing.common import ZookeeperCluster
        ZK_HOME = os.environ.get("ZOOKEEPER_PATH")
        self.cluster = ZookeeperCluster(ZK_HOME, size=1, port_offset=21000)
        self.cluster.start()
        atexit.register(lambda cluster: self.cluster.terminate(), self.cluster)
        self.client = KazooClient(self.cluster[0].address, max_retries=3)
        self.ev = threading.Event()

        def back(state):
            if state == KazooState.CONNECTED:
                self.ev.set()
        self.client.start()
        self.path = self.client.create(uuid.uuid4().hex)
        self.client.add_listener(back)

    def test_restarted_server(self):
        self.cluster.stop()
        self.cluster.start()
        self.ev.wait(5)
        raise SkipTest('Patch missing')
        eq_(self.ev.is_set(), True)
        self.assertTrue(self.client.retry(self.client.exists, self.path))

    def test_terminated_server(self):
        self.cluster.reset()
        self.cluster.start()
        self.ev.wait(5)
        raise SkipTest('Patch missing')
        eq_(self.ev.is_set(), True)
        self.assertFalse(self.client.retry(self.client.exists, self.path))

    def tearDown(self):
        self.ev.clear()
        self.client.stop()
        self.cluster.stop()


class TestClientTransitions(KazooTestCase):
    def test_connection_and_disconnection(self):
        from kazoo.client import KazooState
        states = []
        rc = threading.Event()

        @self.client.add_listener
        def listener(state):
            states.append(state)
            if state == KazooState.CONNECTED:
                rc.set()

        self.client.stop()
        eq_(states, [KazooState.LOST])
        states.pop()

        self.client.start()
        rc.wait(2)
        eq_(states, [KazooState.CONNECTED])
        rc.clear()
        states.pop()
        self.expire_session()
        rc.wait(2)
{'fraction_non_alphanumeric': 0.09401919763721388, 'fraction_numerical': 0.004676347526458282, 'mean_word_length': 2.7114155251141554, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '5738995', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1219, 'n_words': 278}