Пример #1
0
    def _read_topology(self):
        """Read the /topology node and return an InternalTopology object.

        This object should be used with read-only semantics. For changing the
        topology, check out the _retry_topology_change() method.

        Note that this method name is underlined to mean "protected", not
        "private", since the only purpose of this method is to be used by
        subclasses.
        """
        topology = InternalTopology()
        try:
            content, stat = yield self._client.get("/topology")
            topology.parse(content)
        except NoNodeException:
            pass
        returnValue(topology)
Пример #2
0
    def test_change_non_empty_topology(self):
        """
        Attempting to change a pre-existing topology should modify
        it accordingly.
        """
        test_topology = InternalTopology()
        test_topology.add_machine("m-0")
        topology_dump = test_topology.dump()
        yield self.client.create("/topology", topology_dump)

        def change_topology(topology):
            topology.add_machine("m-1")
        yield self.base._retry_topology_change(change_topology)

        content, stat = yield self.client.get("/topology")
        topology = self.parse_topology(content)
        self.assertTrue(topology.has_machine("m-0"))
        self.assertTrue(topology.has_machine("m-1"))
Пример #3
0
    def test_watch_stops_on_early_closed_connection(self):
        """Verify watches stops when the connection is closed early.

        _watch_topology chains from an exists_and_watch to a
        get_and_watch. This test ensures that this chaining will fail
        gracefully if the connection is closed before this chaining
        can occur.
        """
        # Use a separate client connection for watching so it can be
        # disconnected.
        watch_client = ZookeeperClient(get_test_zookeeper_address())
        yield watch_client.connect()
        watch_base = StateBase(watch_client)

        calls = []

        @inlineCallbacks
        def watcher(old_topology, new_topology):
            calls.append((old_topology, new_topology))

        # Create the topology.
        topology = InternalTopology()
        topology.add_machine("m-0")
        yield self.set_topology(topology)
        
        # Now disconnect the client.
        watch_client.close()
        self.assertFalse(watch_client.connected)
        self.assertTrue(self.client.connected)

        # Start watching.
        yield watch_base._watch_topology(watcher)

        # Change the topology, this will trigger the watch.
        topology.add_machine("m-1")
        yield self.set_topology(topology)

        # Give a chance for something bad to happen.
        yield self.poke_zk()

        # Ensure the watcher was never called, because its client was
        # disconnected.
        self.assertEquals(len(calls), 0)
Пример #4
0
    def test_stop_watch(self):
        """
        A watch that fires a `StopWatcher` exception will end the
        watch."""
        wait_callback = [Deferred() for i in range(5)]
        calls = []

        def watcher(old_topology, new_topology):
            calls.append((old_topology, new_topology))
            wait_callback[len(calls)-1].callback(True)
            if len(calls) == 2:
                raise StopWatcher()

        # Start watching.
        self.base._watch_topology(watcher)

        # Create the topology.
        topology = InternalTopology()
        topology.add_machine("m-0")
        yield self.set_topology(topology)

        # Hold off until callback is started.
        yield wait_callback[0]

        # Change the topology again.
        topology.add_machine("m-1")
        yield self.set_topology(topology)

        yield wait_callback[1]
        self.assertEqual(len(calls), 2)

        # Change the topology again, we shouldn't see this.
        topology.add_machine("m-2")
        yield self.set_topology(topology)

        # Give a chance for something bad to happen.
        yield self.poke_zk()

        # Ensure we still have a single call.
        self.assertEquals(len(calls), 2)
Пример #5
0
 def change_content_function(content, stat):
     topology = InternalTopology()
     if content:
         topology.parse(content)
     change_topology_function(topology)
     return topology.dump()
Пример #6
0
 def setUp(self):
     self.topology = InternalTopology()
Пример #7
0
 def get_topology(self):
     """Read /topology and return InternalTopology instance with it."""
     content, stat = yield self.client.get("/topology")
     topology = InternalTopology()
     topology.parse(content)
     returnValue(topology)
Пример #8
0
 def parse_topology(self, content):
     topology = InternalTopology()
     topology.parse(content)
     return topology