Merge "AStatsEvent_overwriteTimestamp"

This commit is contained in:
TreeHugger Robot 2020-02-14 23:11:48 +00:00 committed by Android (Google) Code Review
commit ea37af2c62
3 changed files with 27 additions and 0 deletions

View file

@ -152,6 +152,7 @@ void AStatsEvent_addBoolAnnotation(AStatsEvent* event, uint8_t annotationId, boo
void AStatsEvent_addInt32Annotation(AStatsEvent* event, uint8_t annotationId, int32_t value);
// Internal/test APIs. Should not be exposed outside of the APEX.
void AStatsEvent_overwriteTimestamp(AStatsEvent* event, uint64_t timestampNs);
uint32_t AStatsEvent_getAtomId(AStatsEvent* event);
// Size is an output parameter.
uint8_t* AStatsEvent_getBuffer(AStatsEvent* event, size_t* size);

View file

@ -115,6 +115,14 @@ void AStatsEvent_setAtomId(AStatsEvent* event, uint32_t atomId) {
event->numElements++;
}
// Overwrites the timestamp populated in AStatsEvent_obtain with a custom
// timestamp. Should only be called from test code.
void AStatsEvent_overwriteTimestamp(AStatsEvent* event, uint64_t timestampNs) {
memcpy(&event->buf[POS_TIMESTAMP + sizeof(uint8_t)], &timestampNs, sizeof(timestampNs));
// Do not increment numElements because we already accounted for the timestamp
// within AStatsEvent_obtain.
}
// Side-effect: modifies event->errors if the buffer would overflow
static bool overflows(AStatsEvent* event, size_t size) {
if (event->size + size > MAX_EVENT_PAYLOAD) {

View file

@ -318,3 +318,21 @@ TEST(StatsEventTest, TestOverflowError) {
AStatsEvent_release(event);
}
TEST(StatsEventTest, TestOverwriteTimestamp) {
uint32_t atomId = 100;
int64_t expectedTimestamp = 0x123456789;
AStatsEvent* event = AStatsEvent_obtain();
AStatsEvent_setAtomId(event, atomId);
AStatsEvent_overwriteTimestamp(event, expectedTimestamp);
AStatsEvent_build(event);
uint8_t* buffer = AStatsEvent_getBuffer(event, NULL);
// Make sure that the timestamp is being overwritten.
checkMetadata(&buffer, /*numElements=*/0, /*startTime=*/expectedTimestamp,
/*endTime=*/expectedTimestamp, atomId);
EXPECT_EQ(AStatsEvent_getErrors(event), 0);
AStatsEvent_release(event);
}