Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df2490da authored by Mike Yu's avatar Mike Yu Committed by Automerger Merge Worker
Browse files

Test: Use condition_variable to wait for onNat64PrefixEvent am: 4f4f57a8

parents ca8ce0da 4f4f57a8
Loading
Loading
Loading
Loading
+15 −18
Original line number Diff line number Diff line
@@ -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())) {
    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;
    }
        }
        std::this_thread::sleep_for(kRetryIntervalMs);
    }

    // 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
+2 −7
Original line number Diff line number Diff line
@@ -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
+17 −14
Original line number Diff line number Diff line
@@ -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 &&
    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())) {
                    mNat64PrefixAddress.empty());
        })) {
        mUnexpectedNat64PrefixUpdates--;
        return true;
    }
        }
        std::this_thread::sleep_for(kRetryIntervalMs);
    }

    // Timeout.
    return false;
}