mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-05 05:50:04 +08:00
* chore(clippy): scope crate lint policy Keep panic-on-error lints at application target roots and scope signal dependencies to Linux. * docs(agents): define Rust backend architecture boundaries Document which layer owns Rust capabilities, fallible inputs, background work, and crate validation. * refactor(draft): type async publication conflicts Return typed conflicts for async Draft edits. Release the lock and edit permit before converting the error, and exercise overlapping updates. * refactor(logging): use typed logger errors Expose crate-owned logger errors and preserve the underlying error sources. * refactor(subscription): extract response decoding Keep HTTP and persistence in the app. Decode response metadata and framing in the crate, and fall back when the server filename is invalid. * refactor(profile): extract profile models and index rules Move profile models and index rules into the crate, preserving serialization and returning a typed missing-profile error. * refactor(profile): separate subscription refresh and scheduling policy Separate download and scheduling policy from the subscription use case while retaining the existing write path. * refactor(subscription): decode deep links in the subscription crate * refactor(profile): extract selected node recovery rules * docs(agents): define dependencies between capability crates * refactor(task): move retry primitives into the task crate * refactor(backup): extract backup ZIP writing * refactor(subscription): keep profile writes outside proxy fallback * fix(profile): preserve delays for future update timestamps * fix(subscription): preserve filenames from response headers and URL paths * fix(subscription): separate deep link parameters from nested URLs * refactor(profile): look up the refresh target once * refactor(subscription): pass the proxy directly to each download * refactor(profile): build companion options in one place * refactor(profile): remove companions from the deleted profile * style(profile): keep profile fields together * refactor(profile): name index operations after what they change * refactor(profile): call the core reload directly * refactor(backup): move archive validation to the restore change
184 lines
5.7 KiB
Rust
184 lines
5.7 KiB
Rust
use std::{
|
|
pin::pin,
|
|
sync::{
|
|
Arc,
|
|
atomic::{AtomicBool, Ordering},
|
|
},
|
|
task::{Context, Poll, Wake, Waker},
|
|
};
|
|
|
|
use clash_verge_draft::{Draft, DraftBusy, DraftConflict, DraftTransaction};
|
|
use tokio::sync::{Notify, oneshot};
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
struct Config {
|
|
enabled: bool,
|
|
port: u16,
|
|
}
|
|
|
|
#[test]
|
|
fn edits_can_be_committed_or_discarded() {
|
|
let draft = Draft::new(Config {
|
|
enabled: false,
|
|
port: 7897,
|
|
});
|
|
|
|
draft.edit_draft(|config| config.enabled = true);
|
|
assert!(!draft.data_arc().enabled);
|
|
assert!(draft.latest_arc().enabled);
|
|
draft.apply();
|
|
assert!(draft.data_arc().enabled);
|
|
|
|
draft.edit_draft(|config| config.port = 9090);
|
|
draft.discard();
|
|
assert_eq!(draft.latest_arc().port, 7897);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn async_updates_are_serialized() -> Result<(), Box<dyn std::error::Error>> {
|
|
let draft = Arc::new(Draft::new(0_u8));
|
|
let mut updates = Vec::new();
|
|
|
|
for _ in 0..16 {
|
|
let draft = Arc::clone(&draft);
|
|
updates.push(tokio::spawn(async move {
|
|
draft
|
|
.with_data_modify(|value| async move {
|
|
tokio::task::yield_now().await;
|
|
Ok::<_, DraftConflict>((value + 1, ()))
|
|
})
|
|
.await
|
|
}));
|
|
}
|
|
for update in updates {
|
|
update.await??;
|
|
}
|
|
|
|
assert_eq!(**draft.data_arc(), 16);
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn async_update_rejects_a_changed_committed_value() -> Result<(), Box<dyn std::error::Error>> {
|
|
let draft = Arc::new(Draft::new(Config::default()));
|
|
let started = Arc::new(Notify::new());
|
|
let finish = Arc::new(Notify::new());
|
|
let update = {
|
|
let draft = Arc::clone(&draft);
|
|
let started = Arc::clone(&started);
|
|
let finish = Arc::clone(&finish);
|
|
tokio::spawn(async move {
|
|
draft
|
|
.with_data_modify(|mut config| async move {
|
|
started.notify_one();
|
|
finish.notified().await;
|
|
config.enabled = true;
|
|
Ok::<_, DraftConflict>((config, ()))
|
|
})
|
|
.await
|
|
})
|
|
};
|
|
|
|
started.notified().await;
|
|
draft.edit_draft(|config| config.port = 7897);
|
|
draft.apply();
|
|
finish.notify_one();
|
|
|
|
assert_eq!(update.await?, Err(DraftConflict));
|
|
assert!(!draft.data_arc().enabled);
|
|
assert_eq!(draft.data_arc().port, 7897);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn parked_async_update_is_woken_after_optimistic_conflict() {
|
|
#[derive(Default)]
|
|
struct WakeFlag(AtomicBool);
|
|
|
|
impl Wake for WakeFlag {
|
|
fn wake(self: Arc<Self>) {
|
|
self.0.store(true, Ordering::Relaxed);
|
|
}
|
|
}
|
|
|
|
let draft = Draft::new(1);
|
|
let (finish, finished) = oneshot::channel();
|
|
let mut owner = pin!(draft.with_data_modify(|value| async move {
|
|
finished.await.expect("owner completion signal");
|
|
Ok::<_, DraftConflict>((value + 10, ()))
|
|
}));
|
|
let mut owner_context = Context::from_waker(Waker::noop());
|
|
assert!(owner.as_mut().poll(&mut owner_context).is_pending());
|
|
|
|
let wake_flag = Arc::new(WakeFlag::default());
|
|
let waker = Waker::from(Arc::clone(&wake_flag));
|
|
let mut waiter_context = Context::from_waker(&waker);
|
|
let mut waiter = pin!(draft.with_data_modify(|value| async move { Ok::<_, DraftConflict>((value + 1, value)) }));
|
|
|
|
// The fast retry wakes once before the waiter parks on Notify.
|
|
assert!(waiter.as_mut().poll(&mut waiter_context).is_pending());
|
|
assert!(wake_flag.0.swap(false, Ordering::Relaxed), "fast retry did not wake");
|
|
assert!(waiter.as_mut().poll(&mut waiter_context).is_pending());
|
|
assert!(!wake_flag.0.load(Ordering::Relaxed), "waiter did not park");
|
|
|
|
draft.edit_draft(|value| *value = 7);
|
|
draft.apply();
|
|
assert!(finish.send(()).is_ok());
|
|
assert!(matches!(owner.as_mut().poll(&mut owner_context), Poll::Ready(Err(_))));
|
|
assert_eq!(**draft.data_arc(), 7);
|
|
assert!(wake_flag.0.load(Ordering::Relaxed), "parked waiter was not notified");
|
|
assert!(matches!(waiter.as_mut().poll(&mut waiter_context), Poll::Ready(Ok(7))));
|
|
assert_eq!(**draft.data_arc(), 8);
|
|
}
|
|
|
|
#[test]
|
|
fn transaction_commits_every_layer() -> Result<(), DraftBusy> {
|
|
let first = Draft::new(1_u8);
|
|
let second = Draft::new(String::from("a"));
|
|
let transaction = DraftTransaction::begin(vec![&first, &second])?;
|
|
|
|
first.edit_draft(|value| *value = 2);
|
|
second.edit_draft(|value| *value = String::from("b"));
|
|
transaction.commit();
|
|
|
|
assert_eq!(**first.data_arc(), 2);
|
|
assert_eq!(**second.data_arc(), "b");
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn dropped_transaction_rolls_every_layer_back() -> Result<(), DraftBusy> {
|
|
let first = Draft::new(1_u8);
|
|
let second = Draft::new(String::from("a"));
|
|
|
|
{
|
|
let _transaction = DraftTransaction::begin(vec![&first, &second])?;
|
|
first.edit_draft(|value| *value = 2);
|
|
second.edit_draft(|value| *value = String::from("b"));
|
|
}
|
|
|
|
assert_eq!(**first.latest_arc(), 1);
|
|
assert_eq!(**second.latest_arc(), "a");
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn transaction_claims_are_exclusive_and_release_partial_claims() -> Result<(), DraftBusy> {
|
|
let first = Draft::new(1_u8);
|
|
let second = Draft::new(2_u16);
|
|
let held = DraftTransaction::begin(vec![&second])?;
|
|
|
|
let busy = DraftTransaction::begin(vec![&first, &second])
|
|
.err()
|
|
.expect("transaction claimed an already-held layer");
|
|
let layer = std::any::type_name::<u16>();
|
|
assert_eq!(busy.layer, layer);
|
|
assert!(busy.to_string().contains(layer));
|
|
let available = DraftTransaction::begin(vec![&first])?;
|
|
|
|
drop(available);
|
|
drop(held);
|
|
assert!(DraftTransaction::begin(vec![&second]).is_ok());
|
|
Ok(())
|
|
}
|