示例#1
0
    def test_get_notifier(self):
        method = self.method = config.Method(FakeConfig(), 'label',
                                             [('module', 'FakeClass'),
                                              ('method', 'instance_method'),
                                              ('metric', 'FakeMetric')])

        self.assertIsInstance(method.notifier, FakeNotifier)
示例#2
0
    def test_get_no_app(self):
        method = self.method = config.Method(None, 'label',
                                             [('module', 'FakeClass'),
                                              ('method', 'instance_method'),
                                              ('metric', 'FakeMetric')])

        self.assertEqual(method.app, None)
示例#3
0
    def test_get_metric(self):
        method = self.method = config.Method(None, 'label',
                                             [('module', 'FakeClass'),
                                              ('method', 'instance_method'),
                                              ('metric', 'FakeMetric')])

        self.assertIsInstance(method.metric, FakeMetric)
示例#4
0
    def test_init(self):
        # We have to grab these here, because we can't look for them
        # after they've been monkey-patched.
        original = FakeClass.__dict__['instance_method']
        original_method = FakeClass.instance_method

        method = self.method = config.Method('config', 'label',
                                             [('module', 'FakeClass'),
                                              ('method', 'instance_method'),
                                              ('metric', 'FakeMetric'),
                                              ('notifier', 'notifier'),
                                              ('app_helper', 'FakeHelper'),
                                              ('app', 'fake_helper'),
                                              ('foo', 'bar')])
        self.assertEqual(method.config, 'config')
        self.assertEqual(method.label, 'label')
        self.assertEqual(method._app_cache, None)
        self.assertEqual(method._metric_cache, None)
        self.assertEqual(method.additional, {'foo': 'bar'})
        self.assertEqual(method._module, 'FakeClass')
        self.assertEqual(method._method, 'instance_method')
        self.assertEqual(method._metric, 'FakeMetric')
        self.assertEqual(method._notifier, 'notifier')
        self.assertEqual(method._app_helper, 'FakeHelper')
        self.assertEqual(method._app, 'fake_helper')
        self.assertEqual(method._method_cls, FakeClass)
        self.assertTrue(inspect.isfunction(method._method_wrapper))
        self.assertEqual(method._method_orig, original)
        self.assertEqual(method._method_wrapper.tach_descriptor, method)
        self.assertEqual(method._method_wrapper.tach_function, original_method)
示例#5
0
    def test_get_app(self):
        method = self.method = config.Method(None, 'label',
                                             [('module', 'FakeClass'),
                                              ('method', 'instance_method'),
                                              ('metric', 'FakeMetric'),
                                              ('app_helper', 'FakeHelper'),
                                              ('app', 'fake_helper')])

        self.assertEqual(method.app, FakeHelper.fake_helper)
示例#6
0
    def test_additional_config(self):
        method = self.method = config.Method(None, 'label',
                                             [('module', 'FakeClass'),
                                              ('method', 'instance_method'),
                                              ('metric', 'FakeMetric'),
                                              ('foo', 'bar')])

        self.assertEqual(method['foo'], 'bar')
        with self.assertRaises(KeyError):
            _foo = method['bar']
示例#7
0
 def test_get_transaction_id(self):
     method = self.method = config.Method(FakeConfig(), 'label',
                                          [('module', 'FakeClass'),
                                           ('method', 'instance_method'),
                                           ('metric', 'FakeMetric')])
     self.assertTrue('_bump_transaction_id' in method.metric.__dict__)
     method.metric._bump_transaction_id = True
     self.assertEquals(method.notifier.transaction_id, 1)
     instance = FakeClass()
     instance.instance_method()
     self.assertEquals(method.notifier.transaction_id, 2)
示例#8
0
    def test_init_function(self):
        method = self.method = config.Method(None, 'label',
                                             [('module', 'fake_module'),
                                              ('method', 'function'),
                                              ('metric', 'FakeMetric')])

        self.assertEqual(method._method_cls, fake_module)
        self.assertTrue(inspect.isfunction(method._method_wrapper))

        method.detach()
        self.assertEqual(method._method_orig, fake_module.function)
示例#9
0
    def test_init_static(self):
        method = self.method = config.Method(None, 'label',
                                             [('module', 'FakeClass'),
                                              ('method', 'static_method'),
                                              ('metric', 'FakeMetric')])

        self.assertEqual(method._method_cls, FakeClass)
        self.assertIsInstance(method._method_wrapper, staticmethod)

        method.detach()
        self.assertEqual(method._method_orig,
                         FakeClass.__dict__['static_method'])
示例#10
0
    def test_wrapper_basic(self):
        method = self.method = config.Method(FakeConfig(), 'label',
                                             [('module', 'fake_module'),
                                              ('method', 'function'),
                                              ('metric', 'FakeMetric')])

        # We know that method._method_wrapper is the wrapper function,
        # so we can call it with impunity
        result = method._method_wrapper(1, 2, 3, a=4, b=5, c=6)

        self.assertEqual(
            result,
            ('function', dict(args=(1, 2, 3), kwargs=dict(a=4, b=5, c=6))))
        self.assertEqual(method.notifier.sent_msgs,
                         ["default/'started/ended'/'label'"])
示例#11
0
 def test_missing_metric(self):
     regexp = 'Missing configuration options for label: metric'
     with self.assertRaisesRegexp(Exception, regexp):
         self.method = config.Method(None, 'label',
                                     [('module', 'FakeClass'),
                                      ('method', 'instance_method')])