diff options
author | Mike Yu <yumike@google.com> | 2023-11-10 03:08:14 +0000 |
---|---|---|
committer | Mike Yu <yumike@google.com> | 2023-11-20 11:45:42 +0000 |
commit | 4f4f57a842138b76534743789b7914303f0d63b9 (patch) | |
tree | 45e15f4ebe6c46787dad8d7ce906670c0da8e6e4 | |
parent | 18f4d9eb091b6d7632ec3f58e40cb7e95fe6ba79 (diff) | |
download | DnsResolver-4f4f57a842138b76534743789b7914303f0d63b9.tar.gz |
Test: Use condition_variable to wait for onNat64PrefixEvent
This CL improves the test running time.
PrefixDiscoveryBypassTls: 360ms -> 307ms
SetAndClearNat64Prefix: 232ms -> 200ms
Bug: 310108475
Test: resolv_integration_test passed
Change-Id: Ia427e45d4045ad2ce17ed52dd31250adc5ba6ce5
-rw-r--r-- | tests/dns_metrics_listener/dns_metrics_listener.cpp | 33 | ||||
-rw-r--r-- | tests/dns_metrics_listener/dns_metrics_listener.h | 9 | ||||
-rw-r--r-- | tests/unsolicited_listener/unsolicited_event_listener.cpp | 31 |
3 files changed, 34 insertions, 39 deletions
diff --git a/tests/dns_metrics_listener/dns_metrics_listener.cpp b/tests/dns_metrics_listener/dns_metrics_listener.cpp index e6cdef88..2ce99966 100644 --- a/tests/dns_metrics_listener/dns_metrics_listener.cpp +++ b/tests/dns_metrics_listener/dns_metrics_listener.cpp @@ -21,9 +21,7 @@ #include <android-base/chrono_utils.h> #include <android-base/format.h> -namespace android { -namespace net { -namespace metrics { +namespace android::net::metrics { using android::base::ScopedLockAssertion; using std::chrono::milliseconds; @@ -49,6 +47,7 @@ std::ostream& operator<<(std::ostream& os, const DnsMetricsListener::DnsEvent& d std::lock_guard lock(mMutex); mUnexpectedNat64PrefixUpdates++; if (netId == mNetId) mNat64Prefix = added ? prefixString : ""; + mCv.notify_all(); return ::ndk::ScopedAStatus::ok(); } @@ -60,7 +59,7 @@ std::ostream& operator<<(std::ostream& os, const DnsMetricsListener::DnsEvent& d // keep updating the server to have latest validation status. mValidationRecords.insert_or_assign({netId, ipAddress}, validated); } - mCv.notify_one(); + mCv.notify_all(); return ::ndk::ScopedAStatus::ok(); } @@ -78,18 +77,18 @@ std::ostream& operator<<(std::ostream& os, const DnsMetricsListener::DnsEvent& d } bool DnsMetricsListener::waitForNat64Prefix(ExpectNat64PrefixStatus status, milliseconds timeout) { - android::base::Timer t; - while (t.duration() < timeout) { - { - std::lock_guard lock(mMutex); - if ((status == EXPECT_FOUND && !mNat64Prefix.empty()) || - (status == EXPECT_NOT_FOUND && mNat64Prefix.empty())) { - mUnexpectedNat64PrefixUpdates--; - return true; - } - } - std::this_thread::sleep_for(kRetryIntervalMs); + std::unique_lock lock(mMutex); + ScopedLockAssertion assume_lock(mMutex); + + if (mCv.wait_for(lock, timeout, [&]() REQUIRES(mMutex) { + return (status == EXPECT_FOUND && !mNat64Prefix.empty()) || + (status == EXPECT_NOT_FOUND && mNat64Prefix.empty()); + })) { + mUnexpectedNat64PrefixUpdates--; + return true; } + + // Timeout. return false; } @@ -129,6 +128,4 @@ std::optional<DnsMetricsListener::DnsEvent> DnsMetricsListener::popDnsEvent() { return ret; } -} // namespace metrics -} // namespace net -} // namespace android +} // namespace android::net::metrics diff --git a/tests/dns_metrics_listener/dns_metrics_listener.h b/tests/dns_metrics_listener/dns_metrics_listener.h index ff714664..e34662b2 100644 --- a/tests/dns_metrics_listener/dns_metrics_listener.h +++ b/tests/dns_metrics_listener/dns_metrics_listener.h @@ -30,11 +30,8 @@ enum ExpectNat64PrefixStatus : bool { EXPECT_NOT_FOUND, }; -namespace android { -namespace net { -namespace metrics { +namespace android::net::metrics { -// TODO: Perhaps use condition variable but continually polling. // TODO: Perhaps create a queue to monitor the event changes. That improves the unit test which can // verify the event count, the event change order, and so on. class DnsMetricsListener : public BaseMetricsListener { @@ -131,6 +128,4 @@ class DnsMetricsListener : public BaseMetricsListener { std::condition_variable mCv; }; -} // namespace metrics -} // namespace net -} // namespace android +} // namespace android::net::metrics diff --git a/tests/unsolicited_listener/unsolicited_event_listener.cpp b/tests/unsolicited_listener/unsolicited_event_listener.cpp index 367df972..bae42754 100644 --- a/tests/unsolicited_listener/unsolicited_event_listener.cpp +++ b/tests/unsolicited_listener/unsolicited_event_listener.cpp @@ -51,6 +51,7 @@ constexpr milliseconds kRetryIntervalMs{20}; ? event.prefixAddress : ""; } + mCv.notify_all(); return ::ndk::ScopedAStatus::ok(); } @@ -62,7 +63,7 @@ constexpr milliseconds kRetryIntervalMs{20}; mValidationRecords.insert_or_assign({event.netId, event.ipAddress, event.protocol}, event.validation); } - mCv.notify_one(); + mCv.notify_all(); return ::ndk::ScopedAStatus::ok(); } @@ -84,20 +85,22 @@ bool UnsolicitedEventListener::findAndRemoveValidationRecord(const ServerKey& ke } bool UnsolicitedEventListener::waitForNat64Prefix(int operation, const milliseconds& timeout) { - android::base::Timer t; - while (t.duration() < timeout) { - { - std::lock_guard lock(mMutex); - if ((operation == IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_ADDED && - !mNat64PrefixAddress.empty()) || - (operation == IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_REMOVED && - mNat64PrefixAddress.empty())) { - mUnexpectedNat64PrefixUpdates--; - return true; - } - } - std::this_thread::sleep_for(kRetryIntervalMs); + const auto now = std::chrono::steady_clock::now(); + + std::unique_lock lock(mMutex); + ScopedLockAssertion assume_lock(mMutex); + + if (mCv.wait_for(lock, timeout, [&]() REQUIRES(mMutex) { + return (operation == IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_ADDED && + !mNat64PrefixAddress.empty()) || + (operation == IDnsResolverUnsolicitedEventListener::PREFIX_OPERATION_REMOVED && + mNat64PrefixAddress.empty()); + })) { + mUnexpectedNat64PrefixUpdates--; + return true; } + + // Timeout. return false; } |