コード例 #1
0
    def test_get_with_wait_that_times_out(self):
        mocked_results_empty = {
            'children': {},
            'data': None,
            'path': '/services/staging/uswest2/memcache',
            'stat': None
        }

        # Mock up a ServiceRegistry object and use the side_effect setting
        # of MagicMock to return empty results twice before returning valid
        # results with a real child entry on the third try. This allows us to
        # fake the 'waiting' for Zookeeper to respond.
        mocked_sr = mock.MagicMock()
        mocked_sr.add_callback.return_value = True
        mocked_sr.get.side_effect = [
            mocked_results_empty, mocked_results_empty, mocked_results_empty,
            mocked_results_empty
        ]
        utils._service_registry = mocked_sr

        # Create a simple callback method
        def callme(data):
            data

        # Simulate a a basic get with no callback, and pass in a wait value of
        # zero. This means we will only iterate once calling the get() method
        # before failing. This tests the fail case, but doesn't take any real
        # time during our tests.
        utils.get('/foo', callback=callme, wait=0.1)
        mocked_sr.get.assert_has_calls(
            [mock.call('/foo'),
             mock.call('/foo', callback=callme)])
コード例 #2
0
    def test_get_with_callback(self):
        mocked_results = {
            'children': {
                u'staging-mc1-uswest2-i-2dfa181e:123': {
                    u'created': u'2012-12-22 19:49:54',
                    u'pid': 11167,
                    u'zone': u'us-west-2b'
                }
            },
            'data': None,
            'path': '/services/staging/uswest2/memcache',
            'stat': None
        }

        # Mock up a ServiceRegistry object so we can track the calls
        mocked_sr = mock.MagicMock()
        mocked_sr.get.return_value = mocked_results
        utils._service_registry = mocked_sr

        # Create a simple callback method
        def callme(data):
            self.assertEquals(data['path'], mocked_results['path'])

        # Simulate a a basic get with no callback, and no wait.
        utils.get('/foo', callback=callme)
        mocked_sr.get.assert_called_with('/foo', callback=callme)
コード例 #3
0
    def test_get_with_callback_and_wait(self):
        mocked_results_empty = {
            'children': {},
            'data': None,
            'path': '/services/staging/uswest2/memcache',
            'stat': None
        }

        mocked_results = {
            'children': {
                u'staging-mc1-uswest2-i-2dfa181e:123': {
                    u'created': u'2012-12-22 19:49:54',
                    u'pid': 11167,
                    u'zone': u'us-west-2b'
                }
            },
            'data': None,
            'path': '/services/staging/uswest2/memcache',
            'stat': None
        }

        # Mock up a ServiceRegistry object and use the side_effect setting
        # of MagicMock to return empty results twice before returning valid
        # results with a real child entry on the third try. This allows us to
        # fake the 'waiting' for Zookeeper to respond.
        mocked_sr = mock.MagicMock()
        mocked_sr.add_callback.return_value = True
        mocked_sr.get.side_effect = [
            mocked_results_empty, mocked_results_empty, mocked_results
        ]
        utils._service_registry = mocked_sr

        # Create a simple callback method
        def callme(data):
            data

        # Simulate a a basic get with no callback, and no wait.
        utils.get('/foo', callback=callme, wait=1)
        mocked_sr.get.assert_called_with('/foo')
        mocked_sr.add_callback.assert_called_with('/foo', callback=callme)