use std::sync::Arc;
use std::sync::atomic::{AtomicI64, Ordering};
use crate::context::builder::test_support::{SwappingSession, named_agent, session};
use crate::context::{ContextDelta, InvocationContext};
fn generation_of(v: &str) -> u64 {
let (_, tag) = v
.split_once('-')
.unwrap_or_else(|| panic!("{v:?} has no generation tag"));
tag.parse()
.unwrap_or_else(|e| panic!("{v:?}: generation {tag:?} is not a number: {e}"))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_identity_is_race_free_across_concurrent_readers() {
let ic = InvocationContext::builder()
.session(session("app-alice", "alice", "sid-alice"))
.build_invocation();
let ctx = ic.clone().into_context();
let derived = vec![
ic.clone(),
ctx.as_invocation().clone(),
ic.with_delta(ContextDelta {
agent: Some(named_agent("a")),
..Default::default()
}),
ctx.with_delta(ContextDelta {
function_call_id: Some("fc".into()),
..Default::default()
})
.as_invocation()
.clone(),
ctx.with_agent_cancel().0.as_invocation().clone(),
];
let mut tasks = Vec::new();
for c in derived {
for _ in 0..4 {
let c = c.clone();
tasks.push(tokio::spawn(async move {
for _ in 0..200 {
assert_eq!(
(c.user_id(), c.app_name(), c.session_id()),
("alice", "app-alice", "sid-alice")
);
tokio::task::yield_now().await;
}
}));
}
}
for t in tasks {
t.await.unwrap();
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_identity_against_a_session_that_changes_under_it() {
const READERS: i64 = 64;
const ROUNDS: i64 = 50;
let sess = Arc::new(SwappingSession::default());
let ctx = InvocationContext::builder()
.session(sess.clone())
.build_invocation()
.into_context();
let spanned = Arc::new(AtomicI64::new(0));
let mut readers = Vec::new();
for i in 0..READERS {
let (sess, ctx, spanned) = (sess.clone(), ctx.clone(), spanned.clone());
readers.push(tokio::spawn(async move {
for _ in 0..ROUNDS {
let before = sess.now();
let got = [
("UserID", "user", ctx.user_id()),
("AppName", "app", ctx.app_name()),
("SessionID", "sid", ctx.session_id()),
];
let after = sess.now();
for (name, want, v) in got {
let (prefix, _) = v
.split_once('-')
.unwrap_or_else(|| panic!("reader {i}: {name} = {v:?}, want prefix {want:?}"));
assert_eq!(prefix, want, "reader {i}: {name} = {v:?}");
let generation = generation_of(v);
assert!(
generation > before && generation <= after,
"reader {i}: {name} = {v:?}, generation {generation} is outside ({before},{after}]"
);
}
if after > before {
spanned.fetch_add(1, Ordering::SeqCst);
}
tokio::task::yield_now().await;
}
}));
}
for r in readers {
r.await.unwrap();
}
assert_eq!(
spanned.load(Ordering::SeqCst),
READERS * ROUNDS,
"the fixture stopped advancing on every accessor call"
);
}
#[test]
fn test_identity_is_not_cached() {
let sess = Arc::new(SwappingSession::default());
let ctx = InvocationContext::builder()
.session(sess.clone())
.build_invocation()
.into_context();
let read = || {
[
ctx.user_id().to_string(),
ctx.app_name().to_string(),
ctx.session_id().to_string(),
]
};
let first = read();
sess.swap();
let second = read();
assert_ne!(first, second, "the identity is being cached");
for (name, a, b) in [
("UserID", &first[0], &second[0]),
("AppName", &first[1], &second[1]),
("SessionID", &first[2], &second[2]),
] {
assert!(
generation_of(b) > generation_of(a),
"{name}: {a:?} then {b:?}"
);
}
}