diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b7f409b41..904e7482f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -122,7 +122,7 @@ jobs: - name: Run tests if: runner.os != 'Linux' - run: cargo nextest run --workspace --locked + run: cargo nextest run --workspace --locked --no-fail-fast - name: Test iOS text input on the host if: runner.os == 'macOS' @@ -130,7 +130,7 @@ jobs: - name: Run tests under Xvfb if: runner.os == 'Linux' - run: xvfb-run -a cargo nextest run --workspace --locked + run: xvfb-run -a cargo nextest run --workspace --locked --no-fail-fast macos-clippy: name: macOS arm64 / clippy diff --git a/crates/runtime/src/app/pull_requests_tests.rs b/crates/runtime/src/app/pull_requests_tests.rs index 06e390f79..5def3f14d 100644 --- a/crates/runtime/src/app/pull_requests_tests.rs +++ b/crates/runtime/src/app/pull_requests_tests.rs @@ -19,26 +19,13 @@ fn acked( command: Command, ) -> Result { state.dispatch_command(cx, id, command); - let deadline = std::time::Instant::now() + Duration::from_secs(5); - loop { - cx.run_until_parked(); - if let Some(result) = cx - .drain_outgoing() - .into_iter() - .find_map(|message| match message { - tcode_protocol::HostMessage::Ack { id: acked, result } if acked == id => { - Some(result) - } - _ => None, - }) - { - return result; + cx.run_until_sent(|message| match message { + tcode_protocol::HostMessage::Ack { id: acked, result } if *acked == id => { + Some(result.clone()) } - assert!( - std::time::Instant::now() < deadline, - "the command was answered" - ); - } + _ => None, + }) + .0 } struct HostReply { @@ -597,7 +584,7 @@ fn discovery_answers_local_branches_and_drops_a_linked_worktree_result_after_bra let _server = fixture.serve(move |exchange| { let sent: Value = serde_json::from_slice(&exchange.body).unwrap(); arrived.send(sent).unwrap(); - resume.recv_timeout(Duration::from_secs(5)).unwrap(); + resume.recv_timeout(HOST_DEADLINE).unwrap(); exchange.reply( 200, "", @@ -672,7 +659,7 @@ fn a_project_move_waits_for_discovery_to_leave_its_root() { let _server = fixture.serve(move |exchange| { if let Some((arrived, resume)) = held.lock().unwrap().take() { arrived.send(()).unwrap(); - resume.recv_timeout(Duration::from_secs(5)).unwrap(); + resume.recv_timeout(HOST_DEADLINE).unwrap(); } exchange.reply(200, "", br#"{"data":{"repository":{"h0":{"nodes":[]}}}}"#); }); @@ -1266,28 +1253,14 @@ fn reads_answer_only_a_linked_pull_request_and_a_synced_change_reads_it_fresh() read: tcode_protocol::PullRequestRead::Conversation, }), ); - let deadline = std::time::Instant::now() + Duration::from_secs(5); - loop { - cx.run_until_parked(); - if let Some(result) = - cx.drain_outgoing() - .into_iter() - .find_map(|message| match message { - tcode_protocol::HostMessage::QueryResult { id, result } - if id == next_id => - { - Some(result) - } - _ => None, - }) - { - return result; + let read_id = next_id; + cx.run_until_sent(|message| match message { + tcode_protocol::HostMessage::QueryResult { id, result } if *id == read_id => { + Some(result.clone()) } - assert!( - std::time::Instant::now() < deadline, - "the read was answered" - ); - } + _ => None, + }) + .0 }; sweep(&state, &mut cx); @@ -1634,28 +1607,17 @@ fn a_review_draft_is_its_accounts_and_an_unanswered_submission_waits_for_a_later let mut answer = |state: &TestEntity, cx: &mut TestAppContext, payload| { next_id += 1; state.deliver(cx, next_id, payload); - let deadline = std::time::Instant::now() + Duration::from_secs(5); - loop { - cx.run_until_parked(); - if let Some(result) = - cx.drain_outgoing() - .into_iter() - .find_map(|message| match message { - tcode_protocol::HostMessage::Ack { id, result } if id == next_id => { - Some(result.map(|response| json!(response))) - } - tcode_protocol::HostMessage::QueryResult { id, result } - if id == next_id => - { - Some(result.map(|response| json!(response))) - } - _ => None, - }) - { - return result; + let answer_id = next_id; + cx.run_until_sent(|message| match message { + tcode_protocol::HostMessage::Ack { id, result } if *id == answer_id => { + Some(result.clone().map(|response| json!(response))) } - assert!(std::time::Instant::now() < deadline, "it was answered"); - } + tcode_protocol::HostMessage::QueryResult { id, result } if *id == answer_id => { + Some(result.clone().map(|response| json!(response))) + } + _ => None, + }) + .0 }; let body = |body: &str| { tcode_protocol::ClientPayload::Command(Command::EditPullRequestReviewDraft { diff --git a/crates/runtime/src/app/test_support.rs b/crates/runtime/src/app/test_support.rs index 8be7e6d86..7c1b2b8ed 100644 --- a/crates/runtime/src/app/test_support.rs +++ b/crates/runtime/src/app/test_support.rs @@ -13,6 +13,11 @@ use tcode_protocol::{ClientMessage, ClientPayload, Command, HostMessage, decode_ )] pub(super) mod github_fixture; +/// How long a test waits on the host before giving up. It only bounds a hang: +/// a test that drives real git processes takes several seconds on a loaded +/// Windows runner. +pub(super) const HOST_DEADLINE: Duration = Duration::from_secs(30); + pub(super) struct TestStore(SessionStore); impl TestStore { @@ -89,13 +94,13 @@ impl TestAppContext { .as_ref() .and_then(Weak::upgrade) .expect("test state must outlive its context"); - let deadline = Instant::now() + Duration::from_secs(5); + let deadline = Instant::now() + HOST_DEADLINE; let mut store_flushed = false; loop { assert!( Instant::now() < deadline, - "test host failed to park within five seconds" + "test host failed to park within {HOST_DEADLINE:?}" ); let mut had_work = false; while let Ok(message) = self.mailbox_rx.try_recv() { @@ -135,7 +140,7 @@ impl TestAppContext { /// Park repeatedly until `done` observes the state a background chain /// must reach. pub(super) fn run_until(&mut self, mut done: impl FnMut(&TestClientState) -> bool) { - let deadline = Instant::now() + Duration::from_secs(5); + let deadline = Instant::now() + HOST_DEADLINE; loop { self.run_until_parked(); let state = self @@ -148,7 +153,31 @@ impl TestAppContext { } assert!( Instant::now() < deadline, - "test host condition not reached within five seconds" + "test host condition not reached within {HOST_DEADLINE:?}" + ); + std::thread::sleep(Duration::from_millis(1)); + } + } + + /// Park repeatedly until `pick` finds what the test waits for among the + /// messages the host sends; return it with every message drained on the + /// way. + pub(super) fn run_until_sent( + &mut self, + mut pick: impl FnMut(&HostMessage) -> Option, + ) -> (T, Vec) { + let deadline = Instant::now() + HOST_DEADLINE; + let mut sent = Vec::new(); + loop { + self.run_until_parked(); + let seen = sent.len(); + sent.extend(self.drain_outgoing()); + if let Some(found) = sent[seen..].iter().find_map(&mut pick) { + return (found, sent); + } + assert!( + Instant::now() < deadline, + "the host did not send what the test waits for within {HOST_DEADLINE:?}" ); std::thread::sleep(Duration::from_millis(1)); } @@ -163,14 +192,14 @@ impl TestAppContext { .as_ref() .and_then(Weak::upgrade) .expect("test state must outlive its context"); - let deadline = Instant::now() + Duration::from_secs(5); + let deadline = Instant::now() + HOST_DEADLINE; let message = loop { if let Ok(message) = self.mailbox_rx.try_recv() { break message; } assert!( Instant::now() < deadline, - "nothing reached the mailbox within five seconds" + "nothing reached the mailbox within {HOST_DEADLINE:?}" ); std::thread::sleep(Duration::from_millis(1)); }; diff --git a/crates/runtime/src/app/tests.rs b/crates/runtime/src/app/tests.rs index d2b1473f4..952df82b4 100644 --- a/crates/runtime/src/app/tests.rs +++ b/crates/runtime/src/app/tests.rs @@ -9044,18 +9044,13 @@ fn index_and_visit_changes_cross_the_wire_one_thread_at_a_time() { tcode_client::attachment::AttachmentPolicy::LOCAL, ) }); - cx.run_until_parked(); - let archived = cx - .drain_outgoing() - .into_iter() - .find_map(|message| match message { - HostMessage::QueryResult { - id: 3, - result: Ok(QueryResponse::ArchivedSessions(archived)), - } => Some(archived), - _ => None, - }) - .expect("archived list reply"); + let (archived, _) = cx.run_until_sent(|message| match message { + HostMessage::QueryResult { + id: 3, + result: Ok(QueryResponse::ArchivedSessions(archived)), + } => Some(archived.clone()), + _ => None, + }); assert_eq!(archived.revision, summary_revision); assert_eq!(archived.sessions[0].title, "Archived rename"); @@ -9976,21 +9971,11 @@ fn send_to_host( /// Run the host until it answered client message `id` and parked, and /// return every message it serialized since the last drain. fn answered(cx: &mut TestAppContext, id: u64) -> Vec { - let deadline = Instant::now() + Duration::from_secs(30); - let mut messages = Vec::new(); - loop { - cx.run_until_parked(); - messages.extend(cx.drain_outgoing()); - if messages.iter().any(|message| { - matches!(message, HostMessage::Ack { id: reply, .. } | HostMessage::QueryResult { id: reply, .. } if *reply == id) - }) { - return messages; - } - assert!( - Instant::now() < deadline, - "the host did not answer message {id} within thirty seconds" - ); - } + cx.run_until_sent(|message| { + matches!(message, HostMessage::Ack { id: reply, .. } | HostMessage::QueryResult { id: reply, .. } if *reply == id) + .then_some(()) + }) + .1 } /// Send `payload` through the host pipe as client message `id`, and return @@ -12215,17 +12200,10 @@ fn github_secret_command_persists_separately_and_settings_never_replicate_it() { email: None, }, ); - let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); - loop { - cx.run_until_parked(); - if cx.drain_outgoing().iter().any(|message| matches!(message, HostMessage::Ack { id: 644, result: Err(error) } if error.code == "settings_write_failed")) { - break; - } - assert!( - std::time::Instant::now() < deadline, - "secret write failure acknowledged" - ); - } + cx.run_until_sent(|message| { + matches!(message, HostMessage::Ack { id: 644, result: Err(error) } if error.code == "settings_write_failed") + .then_some(()) + }); assert_eq!( std::fs::read_to_string(store.root().join("secrets.json")).unwrap(), unreadable