Fix flaky tests due to onDnsEvent
Since not every test checks onDnsEvent, a test might receive a
DnsEvent which should have been catched by the previous test.
Modify ExpectDnsEvent() to drop unexpected DnsEvents until the
target DnsEvent is found.
Bug: 159439402
Test: delayed the resolver to send DnsEvents, and ran
resolv_integration_test
Change-Id: Id06546fca9438f89aaa2af383b34b52b7e536e26
diff --git a/tests/resolv_integration_test.cpp b/tests/resolv_integration_test.cpp
index fddb96e..86ffb72 100644
--- a/tests/resolv_integration_test.cpp
+++ b/tests/resolv_integration_test.cpp
@@ -237,9 +237,13 @@
const DnsMetricsListener::DnsEvent expect = {
TEST_NETID, eventType, returnCode,
hostname, ipAddresses, static_cast<int32_t>(ipAddresses.size())};
- const auto dnsEvent = sDnsMetricsListener->popDnsEvent();
- ASSERT_TRUE(dnsEvent.has_value());
- EXPECT_EQ(dnsEvent.value(), expect);
+ do {
+ // Blocking call until timeout.
+ const auto dnsEvent = sDnsMetricsListener->popDnsEvent();
+ ASSERT_TRUE(dnsEvent.has_value()) << "Expected DnsEvent " << expect;
+ if (dnsEvent.value() == expect) break;
+ LOG(INFO) << "Skip unexpected DnsEvent: " << dnsEvent.value();
+ } while (true);
}
bool expectStatsFromGetResolverInfo(const std::vector<NameserverStats>& nameserversStats) {