Skip to content

Commit 792402e

Browse files
committed
sdk: Expose WeakClient::strong_count
... allowing you to see if the client is alive without the overhead of constructng a ref to the client.
1 parent 53e3a7c commit 792402e

File tree

1 file changed

+13
-0
lines changed
  • crates/matrix-sdk/src/client

1 file changed

+13
-0
lines changed

crates/matrix-sdk/src/client/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,13 @@ impl WeakClient {
21622162
pub fn get(&self) -> Option<Client> {
21632163
self.client.upgrade().map(|inner| Client { inner })
21642164
}
2165+
2166+
/// Gets the number of strong (`Arc`) pointers still pointing to this
2167+
/// client.
2168+
#[allow(dead_code)]
2169+
pub fn strong_count(&self) -> usize {
2170+
self.client.strong_count()
2171+
}
21652172
}
21662173

21672174
// The http mocking library is not supported for wasm32
@@ -2510,7 +2517,12 @@ pub(crate) mod tests {
25102517
#[async_test]
25112518
async fn test_client_no_cycle_with_event_cache() {
25122519
let client = logged_in_client(None).await;
2520+
2521+
// Wait for the init tasks to die.
2522+
tokio::time::sleep(Duration::from_secs(1)).await;
2523+
25132524
let weak_client = WeakClient::from_client(&client);
2525+
assert_eq!(weak_client.strong_count(), 1);
25142526

25152527
{
25162528
let room_id = room_id!("!room:example.org");
@@ -2533,6 +2545,7 @@ pub(crate) mod tests {
25332545
tokio::time::sleep(Duration::from_secs(1)).await;
25342546

25352547
// The weak client must be the last reference to the client now.
2548+
assert_eq!(weak_client.strong_count(), 0);
25362549
let client = weak_client.get();
25372550
assert!(
25382551
client.is_none(),

0 commit comments

Comments
 (0)