Skip to main content

wlan_power_manager_testing/
testing.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use async_trait::async_trait;
6use fidl_fuchsia_power_system as fsystem;
7use fuchsia_sync::Mutex;
8use std::sync::Arc;
9use wlan_power_manager::{PowerManager, WakeLease};
10
11pub struct TestPowerManager {
12    pub calls: Arc<Mutex<Vec<String>>>,
13    pub active_leases: Arc<Mutex<Vec<(String, fsystem::LeaseToken)>>>,
14}
15
16impl TestPowerManager {
17    pub fn new() -> Self {
18        Self { calls: Arc::new(Mutex::new(vec![])), active_leases: Arc::new(Mutex::new(vec![])) }
19    }
20
21    pub fn is_lease_dropped(&self, name: &str) -> bool {
22        let leases = self.active_leases.lock();
23        if let Some((_, token)) = leases.iter().rev().find(|(n, _)| n == name) {
24            #[allow(clippy::match_like_matches_macro)]
25            match token
26                .wait_one(zx::Signals::EVENTPAIR_PEER_CLOSED, zx::MonotonicInstant::INFINITE_PAST)
27            {
28                zx::WaitResult::TimedOut(_) => false,
29                _ => true,
30            }
31        } else {
32            true
33        }
34    }
35}
36
37#[async_trait]
38impl PowerManager for TestPowerManager {
39    async fn take_wake_lease(&self, name: &str) -> Option<WakeLease> {
40        self.calls.lock().push(name.to_string());
41        let (local_token, token) = fsystem::LeaseToken::create();
42        self.active_leases.lock().push((name.to_string(), local_token));
43        Some(WakeLease::from_token_for_test(token))
44    }
45}