use symforge::live_index::index_lifecycle::embedded::{EmbedRefusal, EmbeddedSourceFactory};
use symforge::live_index::index_lifecycle::registry::ProjectKey;
#[test]
fn one_handle_close_and_drop_coalesce() {
let registration = EmbeddedSourceFactory::new();
let key = ProjectKey::new("embedded");
let handle = registration.open(key.clone()).expect("first open succeeds");
assert!(handle.is_open());
assert_eq!(registration.open_count(), 1);
assert_eq!(
registration
.open(key.clone())
.expect_err("a second handle to one source must be refused"),
EmbedRefusal::SourceAlreadyOpen {
held_by: handle.identity()
}
);
let other = registration
.open(ProjectKey::new("other"))
.expect("a different source opens");
assert_eq!(registration.open_count(), 2);
assert_ne!(other.identity(), handle.identity());
let receipt = handle.close().expect("an open handle closes");
assert_eq!(receipt.identity(), handle.identity());
assert!(
receipt.performed_shutdown(),
"the first close did not perform the shutdown"
);
assert!(
!receipt.was_final_owner(),
"one source still open, so this was not the final owner"
);
assert!(!handle.is_open());
assert_eq!(registration.open_count(), 1);
assert_eq!(
handle.close().expect_err("a closed handle refuses"),
EmbedRefusal::AlreadyClosed
);
drop(handle);
let reopened = registration
.open(key)
.expect("the closed source can be reopened");
assert_eq!(registration.open_count(), 2);
drop(reopened);
assert_eq!(
registration.open_count(),
1,
"a handle dropped without closing leaked its source"
);
assert!(!registration.has_shut_down());
let final_receipt = other.close().expect("the last handle closes");
assert!(
final_receipt.was_final_owner(),
"the last close was not reported as the final owner"
);
assert!(registration.has_shut_down());
assert_eq!(registration.open_count(), 0);
}
#[test]
fn closing_from_within_a_finalizer_refuses_instead_of_waiting_on_itself() {
let registration = EmbeddedSourceFactory::new();
let handle = registration
.open(ProjectKey::new("self-wait"))
.expect("opens");
let refusal = handle.finalize(|| {
handle
.close()
.expect_err("close must refuse in a finalizer")
});
assert_eq!(refusal, EmbedRefusal::WouldSelfWait);
assert!(handle.is_open(), "a refused close closed the source anyway");
let receipt = handle.close().expect("close succeeds outside a finalizer");
assert!(receipt.performed_shutdown());
}
#[test]
fn a_panicking_finalizer_does_not_wedge_later_closes() {
let registration = EmbeddedSourceFactory::new();
let handle = registration
.open(ProjectKey::new("panicking"))
.expect("opens");
let panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
handle.finalize(|| panic!("finalizer blew up"));
}));
assert!(panicked.is_err(), "the finalizer was expected to panic");
let receipt = handle
.close()
.expect("a close after a panicking finalizer must still work");
assert!(receipt.performed_shutdown());
assert!(registration.has_shut_down());
}
#[test]
fn a_reopened_source_is_a_new_identity() {
let registration = EmbeddedSourceFactory::new();
let key = ProjectKey::new("reopened");
let first = registration.open(key.clone()).expect("opens");
let first_identity = first.identity();
first.close().expect("closes");
let second = registration.open(key).expect("reopens");
assert_ne!(
second.identity(),
first_identity,
"a reopened source reused its predecessor's identity"
);
}
#[test]
fn a_finalizer_refuses_only_its_own_source_and_shutdown_does_not_latch() {
let factory = EmbeddedSourceFactory::new();
let a = factory.open(ProjectKey::new("a")).expect("open a");
let b = factory.open(ProjectKey::new("b")).expect("open b");
let refusal = a.finalize(|| a.close().expect_err("closing self from its own finalizer"));
assert_eq!(refusal, EmbedRefusal::WouldSelfWait);
assert!(a.is_open(), "a refused close still closed the source");
let receipt = a.finalize(|| {
b.close()
.expect("closing another source is not a self-wait")
});
assert!(receipt.performed_shutdown());
assert!(!b.is_open());
assert_eq!(factory.open_count(), 1);
let receipt = a.close().expect("a closes outside its finalizer");
assert!(receipt.was_final_owner());
assert_eq!(factory.open_count(), 0);
assert!(factory.has_shut_down());
let c = factory
.open(ProjectKey::new("c"))
.expect("open after shutdown");
assert!(!factory.has_shut_down());
assert_eq!(factory.open_count(), 1);
drop(c);
assert!(factory.has_shut_down());
}