Skip to content

Commit 75aba1d

Browse files
committed
clippy: declare victory over clippy
So as to not use sync mutexes across await points, we have to use an async mutex, BUT it can't be immediately called in a wiremock responder, so we need to shoe-horn a bit: create a new tokio runtime, which can only be called from another running thread, etc. It's a bit ugly, but I couldn't find another mechanism to block the responder from returning a Response immediately; happy to change that anytime if there's a simpler way.
1 parent c5e8cb7 commit 75aba1d

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

crates/matrix-sdk/tests/integration/send_queue.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
sync::{Arc, Mutex},
3-
time::Duration,
4-
};
1+
use std::{sync::Arc, time::Duration};
52

63
use assert_matches2::{assert_let, assert_matches};
74
use futures_util::FutureExt as _;
@@ -16,7 +13,7 @@ use ruma::{
1613
room_id, EventId,
1714
};
1815
use serde_json::json;
19-
use tokio::time::timeout;
16+
use tokio::{sync::Mutex, time::timeout};
2017
use wiremock::{
2118
matchers::{header, method, path_regex},
2219
Mock, Request, ResponseTemplate,
@@ -152,7 +149,7 @@ async fn test_smoke() {
152149
let event_id = event_id!("$1");
153150

154151
let lock = Arc::new(Mutex::new(()));
155-
let lock_guard = lock.lock().unwrap();
152+
let lock_guard = lock.lock().await;
156153

157154
let mock_lock = lock.clone();
158155

@@ -162,8 +159,15 @@ async fn test_smoke() {
162159
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*"))
163160
.and(header("authorization", "Bearer 1234"))
164161
.respond_with(move |_req: &Request| {
165-
// Wait for the signal.
166-
drop(mock_lock.lock().unwrap());
162+
// Wait for the signal from the main thread that we can process this query.
163+
let mock_lock = mock_lock.clone();
164+
std::thread::spawn(move || {
165+
tokio::runtime::Runtime::new().unwrap().block_on(async {
166+
drop(mock_lock.lock().await);
167+
});
168+
})
169+
.join()
170+
.unwrap();
167171

168172
ResponseTemplate::new(200).set_body_json(json!({
169173
"event_id": "$1",
@@ -236,7 +240,7 @@ async fn test_error() {
236240
assert!(watch.is_empty());
237241

238242
let lock = Arc::new(Mutex::new(()));
239-
let lock_guard = lock.lock().unwrap();
243+
let lock_guard = lock.lock().await;
240244

241245
let mock_lock = lock.clone();
242246

@@ -246,8 +250,15 @@ async fn test_error() {
246250
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*"))
247251
.and(header("authorization", "Bearer 1234"))
248252
.respond_with(move |_req: &Request| {
249-
// Wait for the signal.
250-
drop(mock_lock.lock().unwrap());
253+
// Wait for the signal from the main thread that we can process this query.
254+
let mock_lock = mock_lock.clone();
255+
std::thread::spawn(move || {
256+
tokio::runtime::Runtime::new().unwrap().block_on(async {
257+
drop(mock_lock.lock().await);
258+
});
259+
})
260+
.join()
261+
.unwrap();
251262

252263
ResponseTemplate::new(500)
253264
})
@@ -394,7 +405,7 @@ async fn test_reenabling_queue() {
394405

395406
mock_encryption_state(&server, false).await;
396407

397-
let num_request = Mutex::new(1);
408+
let num_request = std::sync::Mutex::new(1);
398409
Mock::given(method("PUT"))
399410
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*"))
400411
.and(header("authorization", "Bearer 1234"))
@@ -455,19 +466,26 @@ async fn test_cancellation() {
455466
assert!(watch.is_empty());
456467

457468
let lock = Arc::new(Mutex::new(()));
458-
let lock_guard = lock.lock().unwrap();
469+
let lock_guard = lock.lock().await;
459470

460471
let mock_lock = lock.clone();
461472

462473
mock_encryption_state(&server, false).await;
463474

464-
let num_request = Mutex::new(1);
475+
let num_request = std::sync::Mutex::new(1);
465476
Mock::given(method("PUT"))
466477
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*"))
467478
.and(header("authorization", "Bearer 1234"))
468479
.respond_with(move |_req: &Request| {
469-
// Wait for the signal.
470-
drop(mock_lock.lock().unwrap());
480+
// Wait for the signal from the main thread that we can process this query.
481+
let mock_lock = mock_lock.clone();
482+
std::thread::spawn(move || {
483+
tokio::runtime::Runtime::new().unwrap().block_on(async {
484+
drop(mock_lock.lock().await);
485+
});
486+
})
487+
.join()
488+
.unwrap();
471489

472490
let mut num_request = num_request.lock().unwrap();
473491

0 commit comments

Comments
 (0)