コード例 #1
0
    def testHandleBatchSingle(self, mockEngine, addMetricMock):
        # Create mocks
        metric_storer.gCustomMetrics = {}
        metricMock = MagicMock()

        def addMetricSideEffect(*_args, **_kwargs):
            metric_storer.gCustomMetrics["test.metric"] = [
                metricMock, datetime.datetime.utcnow()
            ]

        addMetricMock.side_effect = addMetricSideEffect
        modelSwapperMock = MagicMock()
        metricStreamerMock = MagicMock()
        body = '{"protocol": "plain", "data": ["test.metric 4.0 1386792175"]}'
        message = MagicMock()
        message.body = body
        # Call the function under test
        metric_storer._handleBatch(mockEngine, [message], [],
                                   metricStreamerMock, modelSwapperMock)
        # Check the results
        addMetricMock.assert_called_once_with(mockEngine, "test.metric")
        metricStreamerMock.streamMetricData.assert_called_once()
        data, _uid, modelSwapper = metricStreamerMock.streamMetricData.call_args[
            0]
        self.assertIs(modelSwapper, modelSwapperMock)
        self.assertEqual(len(data), 1)
        self.assertEqual(len(data[0]), 2)
        self.assertEqual(repr(data[0][0]),
                         "datetime.datetime(2013, 12, 11, 20, 2, 55)")
        self.assertAlmostEqual(data[0][1], 4.0)
コード例 #2
0
    def testHandleBatchSingle(self, mockEngine, addMetricMock):
        # Create mocks
        metric_storer.gCustomMetrics = {}
        metricMock = MagicMock()

        def addMetricSideEffect(*_args, **_kwargs):
            metric_storer.gCustomMetrics["test.metric"] = [metricMock, datetime.datetime.utcnow()]

        addMetricMock.side_effect = addMetricSideEffect
        modelSwapperMock = MagicMock()
        metricStreamerMock = MagicMock()
        body = '{"protocol": "plain", "data": ["test.metric 4.0 1386792175"]}'
        message = MagicMock()
        message.body = body
        # Call the function under test
        metric_storer._handleBatch(mockEngine, [message], [], metricStreamerMock, modelSwapperMock)
        # Check the results
        addMetricMock.assert_called_once_with(mockEngine, "test.metric")
        metricStreamerMock.streamMetricData.assert_called_once()
        data, _uid, modelSwapper = metricStreamerMock.streamMetricData.call_args[0]
        self.assertIs(modelSwapper, modelSwapperMock)
        self.assertEqual(len(data), 1)
        self.assertEqual(len(data[0]), 2)
        self.assertEqual(repr(data[0][0]), "datetime.datetime(2013, 12, 11, 20, 2, 55)")
        self.assertAlmostEqual(data[0][1], 4.0)
コード例 #3
0
 def testHandleDataInvalidBody(self, mockEngine, loggingMock):
     """Make sure _handleData doesn't throw an exception for invalid data."""
     # Call the function under test
     body = '{"protocol": "plain", "data": ["test.metric 4.0 12:30PM"]}'
     message = MagicMock()
     message.body = body
     metric_storer._handleBatch(mockEngine, [message], [], MagicMock(), MagicMock())
     # Check the results
     self.assertTrue(loggingMock.warn.called)
コード例 #4
0
 def testHandleDataInvalidProtocol(self, mockEngine, loggingMock):
     """Ensure _handleData doesn't throw an exception for unknown protocol."""
     # Call the function under test
     body = '{"protocol": "unknown_protocol",' ' "data": ["test.metric 4.0 1386792175"]}'
     message = MagicMock()
     message.body = body
     metric_storer._handleBatch(mockEngine, [message], [], MagicMock(), MagicMock())
     # Check the results
     self.assertTrue(loggingMock.warn.called)
コード例 #5
0
 def testHandleDataInvalidBody(self, mockEngine, loggingMock):
     """Make sure _handleData doesn't throw an exception for invalid data."""
     # Call the function under test
     body = '{"protocol": "plain", "data": ["test.metric 4.0 12:30PM"]}'
     message = MagicMock()
     message.body = body
     metric_storer._handleBatch(mockEngine, [message], [], MagicMock(),
                                MagicMock())
     # Check the results
     self.assertTrue(loggingMock.warn.called)
コード例 #6
0
 def testHandleDataInvalidProtocol(self, mockEngine, loggingMock):
     """Ensure _handleData doesn't throw an exception for unknown protocol."""
     # Call the function under test
     body = ('{"protocol": "unknown_protocol",'
             ' "data": ["test.metric 4.0 1386792175"]}')
     message = MagicMock()
     message.body = body
     metric_storer._handleBatch(mockEngine, [message], [], MagicMock(),
                                MagicMock())
     # Check the results
     self.assertTrue(loggingMock.warn.called)