Esempio n. 1
0
    def test_shutdown_result(self):
        """
        `MeterProvider.shutdown` provides a way to let the caller know if it
        succeeded or failed.

        `MeterProvider.shutdown` is implemented by at least invoking
        ``shutdown`` on all registered `SDKMetricReader`s and `ConsoleMetricExporter`s.
        """

        meter_provider = MeterProvider()

        meter_provider.register_metric_reader(
            Mock(**{"shutdown.return_value": True}))
        meter_provider.register_metric_exporter(
            Mock(**{"shutdown.return_value": True}))

        self.assertTrue(meter_provider.shutdown())

        meter_provider = MeterProvider()

        meter_provider.register_metric_reader(
            Mock(**{"shutdown.return_value": True}))
        meter_provider.register_metric_exporter(
            Mock(**{"shutdown.return_value": False}))

        self.assertFalse(meter_provider.shutdown())
Esempio n. 2
0
    def test_shutdown(self):

        mock_metric_reader_0 = MagicMock(
            **{
                "shutdown.return_value": False,
                "__str__.return_value": "mock_metric_reader_0",
            })
        mock_metric_reader_1 = Mock(**{"shutdown.return_value": True})

        meter_provider = MeterProvider(
            metric_readers=[mock_metric_reader_0, mock_metric_reader_1])

        with self.assertLogs(level=WARNING) as log:
            self.assertFalse(meter_provider.shutdown())
            self.assertEqual(
                log.records[0].getMessage(),
                "MetricReader mock_metric_reader_0 failed to shutdown",
            )
        mock_metric_reader_0.shutdown.assert_called_once()
        mock_metric_reader_1.shutdown.assert_called_once()

        mock_metric_reader_0 = Mock(**{"shutdown.return_value": True})
        mock_metric_reader_1 = Mock(**{"shutdown.return_value": True})

        meter_provider = MeterProvider(
            metric_readers=[mock_metric_reader_0, mock_metric_reader_1])

        self.assertTrue(meter_provider.shutdown())
        mock_metric_reader_0.shutdown.assert_called_once()
        mock_metric_reader_1.shutdown.assert_called_once()
Esempio n. 3
0
    def test_shutdown_subsequent_calls(self):
        """
        No subsequent attempts to get a `Meter` are allowed after calling
        `MeterProvider.shutdown`
        """

        meter_provider = MeterProvider()

        with self.assertRaises(AssertionError):
            with self.assertLogs(level=WARNING):
                meter_provider.shutdown()

        with self.assertLogs(level=WARNING):
            meter_provider.shutdown()