Beispiel #1
0
class TestZookeeperWatcher(TestCase):
    def __init__(self, name, hosts):
        self.hosts = hosts
        self.file_path = '/test_path'
        self.file_data = 'test_data'
        self.pointer_path = '/test_pointer'
        self.pointer_data = self.file_path
        TestCase.__init__(self, name)

    def setUp(self):
        self.zoo_client = KazooClient(hosts=self.hosts)
        self.zoo_client.start()
        # prepare data
        self.zoo_client.ensure_path(self.file_path)
        self.zoo_client.set(self.file_path, self.file_data.encode('utf-8'))
        self.zoo_client.ensure_path(self.pointer_path)
        self.zoo_client.set(self.pointer_path,
                            self.pointer_data.encode('utf-8'))

        self.zoo_watcher = ZookeeperWatcher(hosts=self.hosts,
                                            filepath=self.file_path,
                                            pointer=False,
                                            ensure=False,
                                            valid_init=True)

    def test_ping(self):
        self.assertTrue(self.zoo_watcher.ping())

    def test_get_file_contents(self):
        pointer_zoo_watcher = ZookeeperWatcher(hosts=self.hosts,
                                               filepath=self.pointer_path,
                                               pointer=True,
                                               ensure=False,
                                               valid_init=True)

        self.assertEqual(self.zoo_watcher.get_file_contents(), self.file_data)
        self.assertEqual(pointer_zoo_watcher.get_file_contents(),
                         self.file_data)
        self.assertEqual(pointer_zoo_watcher.get_file_contents(True),
                         self.pointer_data)

        pointer_zoo_watcher.close()

    def tearDown(self):
        self.zoo_watcher.close()

        self.zoo_client.ensure_path(self.file_path)
        self.zoo_client.delete(self.file_path)
        self.zoo_client.ensure_path(self.pointer_path)
        self.zoo_client.delete(self.pointer_path)
        self.zoo_client.stop()
        self.zoo_client.close()
Beispiel #2
0
    def test_get_file_contents(self):
        pointer_zoo_watcher = ZookeeperWatcher(hosts=self.hosts,
                                               filepath=self.pointer_path,
                                               pointer=True,
                                               ensure=False,
                                               valid_init=True)

        self.assertEqual(self.zoo_watcher.get_file_contents(), self.file_data)
        self.assertEqual(pointer_zoo_watcher.get_file_contents(),
                         self.file_data)
        self.assertEqual(pointer_zoo_watcher.get_file_contents(True),
                         self.pointer_data)

        pointer_zoo_watcher.close()
Beispiel #3
0
        zoo_watcher = ZookeeperWatcher(hosts,
                                       file,
                                       valid_handler=valid_file,
                                       config_handler=change_file,
                                       error_handler=error_file,
                                       pointer=True,
                                       ensure=True,
                                       valid_init=valid)
else:
    if poll:
        zoo_watcher = ZookeeperWatcher(hosts, file, ensure=True)
    elif event:
        zoo_watcher = ZookeeperWatcher(hosts,
                                       file,
                                       valid_handler=valid_file,
                                       config_handler=change_file,
                                       error_handler=error_file,
                                       valid_init=valid,
                                       ensure=True)

print "Use a keyboard interrupt to shut down the process."
try:
    while True:
        if poll:
            print "Valid File?", zoo_watcher.is_valid()
            print "Contents:", zoo_watcher.get_file_contents()
        sleep(sleep_time)
except:
    pass
zoo_watcher.close()
Beispiel #4
0
# You can use any or all of these, polling + handlers, some handlers, etc
if pointer:
    if poll:
        zoo_watcher = ZookeeperWatcher(hosts, file, ensure=True,pointer=True)
    elif event:
        zoo_watcher = ZookeeperWatcher(hosts, file,
                                       valid_handler=valid_file,
                                       config_handler=change_file,
                                       error_handler=error_file,
                                       pointer=True, ensure=True, valid_init=valid)
else:
    if poll:
        zoo_watcher = ZookeeperWatcher(hosts, file, ensure=True)
    elif event:
        zoo_watcher = ZookeeperWatcher(hosts, file,
                                       valid_handler=valid_file,
                                       config_handler=change_file,
                                       error_handler=error_file,
                                       valid_init=valid, ensure=True)

print "Use a keyboard interrupt to shut down the process."
try:
    while True:
        if poll:
            print "Valid File?", zoo_watcher.is_valid()
            print "Contents:", zoo_watcher.get_file_contents()
        sleep(sleep_time)
except:
    pass
zoo_watcher.close()