def test_greedy_ownership_of_state(self):
        path = '%s/unittest-greedy-state' % self.sandbox
        data = {'unittest': 'data'}
        eph1 = EphemeralNode(zk=self.zk, path=path, data=data)
        waituntil(eph1.data, None, 5)

        # Try disabling the node. If the node gets recreated automatically in
        # some way (by some rogue daemon), then we should destroy it.
        def path_exists_in_zk():
            if self.zk.exists(path):
                return True

            return False

        eph1.stop()
        self.assertEquals(None, self.zk.exists(path))
        self.zk.create(path)
        waituntil(path_exists_in_zk, True, 5)
        self.assertEquals(None, self.zk.exists(path))
    def test_init(self):
        path = '%s/unittest-init' % self.sandbox
        data = {'unittest': 'data'}
        eph1 = EphemeralNode(zk=self.zk, path=path, data=data)
        waituntil(eph1.data, None, 5)

        # The EphemeralNode object DOES immediately register itself in
        # zookeeper, so we should be able to pull that data from Zookeeper
        # right away.
        (data, stat) = self.zk.get(path)
        self.assertNotEquals(None, stat)
        self.assertTrue('"unittest":"data"' in data)
    def test_greedy_ownership_of_data(self):
        path = '%s/unittest-greedy-data' % self.sandbox
        data = {'unittest': 'data'}
        eph1 = EphemeralNode(zk=self.zk, path=path, data=data)
        waituntil(eph1.data, None, 5)

        # Lets intentionally change the data in Zookeeper directly,
        # the EphemeralNode should immediately re-set the data.
        current_stat = eph1.get()
        self.zk.set(path, value='bogus')
        waituntil(eph1._watcher.get, current_stat, 5)
        (data, stat) = self.zk.get(path)
        self.assertTrue('"unittest":"data"' in data)
        self.assertFalse('bogus' in data)

        # Now lets intentionally delete the node and see it get re-run
        current_stat = eph1.get()
        self.zk.delete(path)
        waituntil(eph1._watcher.get, current_stat, 5)
        (data, stat) = self.zk.get(path)
        self.assertTrue('"unittest":"data"' in data)
        self.assertTrue(self.zk.exists(path))