Skip to content

Commit fa7977e

Browse files
committed
fix: add a one loop tolerance for no progress
1 parent f81c69e commit fa7977e

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

examples/loop_yield_data_state.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ fn main() {
2222
}
2323

2424
async fn common_code() -> Result<(), Box<dyn std::error::Error>> {
25+
// Allows for one iteration where we see no progress but next loop should go
26+
// into first branch
27+
let mut seen_no_progress = false;
28+
2529
let client = reqwest::Client::new();
2630
let mut state = DataState::None;
2731

@@ -35,22 +39,22 @@ async fn common_code() -> Result<(), Box<dyn std::error::Error>> {
3539
assert_eq!(status_code, &200);
3640
break;
3741
} else {
38-
let is_able_to_make_progress = state
39-
.get(|| {
40-
let req = client.get("http://httpbin.org/get");
41-
let response_handler = |resp: reqwest::Result<reqwest::Response>| async {
42-
resp.map(|resp| resp.status())
43-
.context("Request failed, got an error back")
44-
};
45-
let ui_notify = || {
46-
println!(
47-
"Request Completed, this is where you would wake up your UI thread"
48-
);
49-
};
50-
Awaiting(fetch_plus(req, response_handler, ui_notify))
51-
})
52-
.is_able_to_make_progress();
53-
assert!(is_able_to_make_progress);
42+
let outcome = state.get(|| {
43+
let req = client.get("http://httpbin.org/get");
44+
let response_handler = |resp: reqwest::Result<reqwest::Response>| async {
45+
resp.map(|resp| resp.status())
46+
.context("Request failed, got an error back")
47+
};
48+
let ui_notify = || {
49+
println!("Request Completed, this is where you would wake up your UI thread");
50+
};
51+
Awaiting(fetch_plus(req, response_handler, ui_notify))
52+
});
53+
assert!(!seen_no_progress);
54+
if outcome.is_unable_to_make_progress() {
55+
// We should never get into this branch again
56+
seen_no_progress = true;
57+
}
5458
reqwest_cross::yield_now().await;
5559
}
5660
}

0 commit comments

Comments
 (0)