From 4b38ffab15a7a414a88fbd106b818f614cb1a5f9 Mon Sep 17 00:00:00 2001 From: Tri Vo Date: Thu, 28 Jul 2022 17:08:58 -0700 Subject: [PATCH] trusty: metrics: Drain events before each test Also add tighter checks to make sure we didn't drop events. Bug: 240617890 Test: libtrusty_metrics_test Change-Id: I0029d91ad0ff67eb97913b2316efca627b118616 --- trusty/metrics/metrics_test.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/trusty/metrics/metrics_test.cpp b/trusty/metrics/metrics_test.cpp index 989795087..0c6db7fc7 100644 --- a/trusty/metrics/metrics_test.cpp +++ b/trusty/metrics/metrics_test.cpp @@ -61,6 +61,18 @@ class TrustyMetricsTest : public TrustyMetrics, public ::testing::Test { virtual void SetUp() override { auto ret = Open(); ASSERT_TRUE(ret.ok()) << ret.error(); + + /* Drain events (if any) and reset state */ + DrainEvents(); + crashed_app_.clear(); + event_drop_count_ = 0; + } + + void DrainEvents() { + while (WaitForEvent(1000 /* 1 second timeout */).ok()) { + auto ret = HandleEvent(); + ASSERT_TRUE(ret.ok()) << ret.error(); + } } void WaitForAndHandleEvent() { @@ -79,6 +91,9 @@ TEST_F(TrustyMetricsTest, Crash) { TriggerCrash(); WaitForAndHandleEvent(); + /* Check that no event was dropped. */ + ASSERT_EQ(event_drop_count_, 0); + /* Check that correct TA crashed. */ ASSERT_EQ(crashed_app_, "36f5b435-5bd3-4526-8b76-200e3a7e79f3:crasher"); } @@ -110,6 +125,9 @@ TEST_F(TrustyMetricsTest, PollSet) { auto ret = HandleEvent(); ASSERT_TRUE(ret.ok()) << ret.error(); + /* Check that no event was dropped. */ + ASSERT_EQ(event_drop_count_, 0); + /* Check that correct TA crashed. */ ASSERT_EQ(crashed_app_, "36f5b435-5bd3-4526-8b76-200e3a7e79f3:crasher"); }