def test_merge_last_updated_none(self):
        observer1 = ValueObserverAggregator()
        observer2 = ValueObserverAggregator()

        mmsc_checkpoint1 = MinMaxSumCountAggregator._TYPE(3, 150, 101, 3)
        mmsc_checkpoint2 = MinMaxSumCountAggregator._TYPE(1, 33, 44, 2)

        checkpoint1 = ValueObserverAggregator._TYPE(*(mmsc_checkpoint1 +
                                                      (23, )))

        checkpoint2 = ValueObserverAggregator._TYPE(*(mmsc_checkpoint2 +
                                                      (27, )))

        observer1.mmsc.checkpoint = mmsc_checkpoint1
        observer2.mmsc.checkpoint = mmsc_checkpoint2

        observer1.last_update_timestamp = 0
        observer2.last_update_timestamp = 100

        observer1.checkpoint = checkpoint1
        observer2.checkpoint = checkpoint2

        observer1.merge(observer2)

        self.assertEqual(
            observer1.checkpoint,
            (
                min(checkpoint1.min, checkpoint2.min),
                max(checkpoint1.max, checkpoint2.max),
                checkpoint1.sum + checkpoint2.sum,
                checkpoint1.count + checkpoint2.count,
                checkpoint2.last,
            ),
        )
        self.assertEqual(observer1.last_update_timestamp, 100)
    def test_merge_with_empty(self):
        observer1 = ValueObserverAggregator()
        observer2 = ValueObserverAggregator()

        mmsc_checkpoint1 = MinMaxSumCountAggregator._TYPE(3, 150, 101, 3)
        checkpoint1 = ValueObserverAggregator._TYPE(*(mmsc_checkpoint1 +
                                                      (23, )))

        observer1.mmsc.checkpoint = mmsc_checkpoint1
        observer1.checkpoint = checkpoint1
        observer1.last_update_timestamp = 100

        observer1.merge(observer2)

        self.assertEqual(observer1.checkpoint, checkpoint1)
    def test_export_value_observer(self):
        client = mock.Mock()

        with mock.patch(
                "opentelemetry.exporter.cloud_monitoring.time_ns",
                lambda: NANOS_PER_SECOND,
        ):
            exporter = CloudMonitoringMetricsExporter(
                project_id=self.project_id, client=client)

        exporter.project_name = self.project_name

        client.create_metric_descriptor.return_value = MetricDescriptor(
            **{
                "name": None,
                "type": "custom.googleapis.com/OpenTelemetry/name",
                "display_name": "name",
                "description": "description",
                "labels": [],
                "metric_kind": "GAUGE",
                "value_type": "INT64",
            })

        aggregator = ValueObserverAggregator()
        aggregator.checkpoint = aggregator._TYPE(1, 2, 3, 4, 5)
        aggregator.last_update_timestamp = (WRITE_INTERVAL +
                                            1) * NANOS_PER_SECOND
        exporter.export([
            ExportRecord(
                MockMetric(meter=mock_meter()),
                (),
                aggregator,
                Resource.create_empty(),
            )
        ])

        series = TimeSeries()
        series.metric_kind = MetricDescriptor.MetricKind.GAUGE
        series.metric.type = "custom.googleapis.com/OpenTelemetry/name"
        point = series.points.add()
        point.value.int64_value = 5
        point.interval.end_time.seconds = WRITE_INTERVAL + 1
        point.interval.end_time.nanos = 0
        point.interval.start_time.seconds = WRITE_INTERVAL + 1
        point.interval.start_time.nanos = 0
        client.create_time_series.assert_has_calls(
            [mock.call(self.project_name, [series])])