blob: 6080b58ee3f5669995b167ebfe4500d06d402398 [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <gtest/gtest.h>
#include "src/local_aggregation_1_1/aggregation_procedures/aggregation_procedure.h"
#include "src/local_aggregation_1_1/aggregation_procedures/testing/test_aggregation_procedure.h"
#include "src/local_aggregation_1_1/testing/test_registry.cb.h"
#include "src/logger/project_context_factory.h"
namespace cobalt::local_aggregation {
class SumAndCountAggregationProcedureTest : public testing::TestAggregationProcedure {};
TEST_F(SumAndCountAggregationProcedureTest, UpdateAggregateWorks) {
uint32_t metric_id = kIntegerMetricMetricId;
uint32_t report_index = kIntegerMetricFleetwideMeansReportReportIndex;
std::unique_ptr<AggregationProcedure> procedure = GetProcedureFor(metric_id, report_index);
ReportAggregate aggregate;
const uint32_t kNumEventCodes = 100;
ASSERT_GE(GetReportDef(metric_id, report_index).event_vector_buffer_max(), kNumEventCodes);
const int64_t kValue = 42;
const uint32_t kHourId = 1;
AddIntegerEvents(kNumEventCodes, kValue, kHourId, procedure.get(), &aggregate);
ASSERT_EQ(aggregate.hourly().by_hour_id().count(kHourId), 1u);
ASSERT_EQ(aggregate.hourly().by_hour_id().at(kHourId).by_event_code_size(), kNumEventCodes);
for (uint32_t i = 0; i < kNumEventCodes; ++i) {
ASSERT_EQ(
aggregate.hourly().by_hour_id().at(kHourId).by_event_code(i).data().sum_and_count().count(),
i + 1);
ASSERT_EQ(
aggregate.hourly().by_hour_id().at(kHourId).by_event_code(i).data().sum_and_count().sum(),
(i + 1) * kValue);
}
}
TEST_F(SumAndCountAggregationProcedureTest, GenerateObservationWorks) {
uint32_t metric_id = kIntegerMetricMetricId;
uint32_t report_index = kIntegerMetricFleetwideMeansReportReportIndex;
std::unique_ptr<AggregationProcedure> procedure = GetProcedureFor(metric_id, report_index);
ReportAggregate aggregate;
const uint32_t kNumEventCodes = 10;
ASSERT_GE(GetReportDef(metric_id, report_index).event_vector_buffer_max(), kNumEventCodes);
const int64_t kValue = 42;
const uint32_t kEndHourId = 11;
for (int hour_id = 1; hour_id <= kEndHourId; hour_id += 2) {
AddIntegerEvents(kNumEventCodes, kValue, hour_id, procedure.get(), &aggregate);
}
util::TimeInfo info;
info.hour_id = kEndHourId;
lib::statusor::StatusOr<std::unique_ptr<Observation>> observation_or =
GenerateObservation(info, procedure.get(), &aggregate);
ASSERT_TRUE(observation_or.ok());
std::unique_ptr<Observation> observation = observation_or.ConsumeValueOrDie();
// Should only generate for kEndHourId
ASSERT_TRUE(observation);
ASSERT_EQ(observation->sum_and_count().sums_and_counts_size(), kNumEventCodes);
for (const SumAndCountObservation_SumAndCount& sum_and_count :
observation->sum_and_count().sums_and_counts()) {
ASSERT_EQ(sum_and_count.event_codes(0), sum_and_count.count());
ASSERT_EQ(sum_and_count.event_codes(0) * kValue, sum_and_count.sum());
}
ASSERT_EQ(aggregate.hourly().by_hour_id_size(), 0);
}
} // namespace cobalt::local_aggregation