use std::thread;
use pi_atom::Atom;
use pi_db::{
utils::{CreateTableOptions, KVDBEvent},
KVDBTableType,
};
#[test]
fn test_create_table_options_preserve_exact_payloads() {
assert!(matches!(
CreateTableOptions::Empty.clone(),
CreateTableOptions::Empty
));
let log = CreateTableOptions::LogOrdTab(17, 23, 31);
match log.clone() {
CreateTableOptions::LogOrdTab(log_file_limit, block_limit, load_buf_len) => {
assert_eq!(log_file_limit, 17);
assert_eq!(block_limit, 23);
assert_eq!(load_buf_len, 31);
}
other => panic!("LogOrdTab clone changed variant: {other:?}"),
}
let btree = CreateTableOptions::BtreeOrdTab(65_536, true);
match btree.clone() {
CreateTableOptions::BtreeOrdTab(cache_size, enable_compact) => {
assert_eq!(cache_size, 65_536);
assert!(enable_compact);
}
other => panic!("BtreeOrdTab clone changed variant: {other:?}"),
}
}
#[test]
fn test_event_predicates_are_mutually_exclusive() {
let report = KVDBEvent::<u64>::ReportTrInfo;
assert!(report.is_report_transaction_info());
assert!(!report.is_commit_failed());
assert!(!report.is_confirm_commited());
let failed = KVDBEvent::CommitFailed(
Atom::from("source-failed"),
Atom::from("table-failed"),
KVDBTableType::LogOrdTab,
101_u64,
201_u64,
);
assert!(!failed.is_report_transaction_info());
assert!(failed.is_commit_failed());
assert!(!failed.is_confirm_commited());
let confirmed = KVDBEvent::ConfirmCommited(
Atom::from("source-confirmed"),
Atom::from("table-confirmed"),
KVDBTableType::BtreeOrdTab,
102_u64,
202_u64,
);
assert!(!confirmed.is_report_transaction_info());
assert!(!confirmed.is_commit_failed());
assert!(confirmed.is_confirm_commited());
}
#[test]
fn test_event_payload_order_clone_and_thread_move() {
let event = KVDBEvent::ConfirmCommited(
Atom::from("payload-source"),
Atom::from("payload-table"),
KVDBTableType::MemOrdTab,
0x11_u64,
0x22_u64,
);
let cloned = event.clone();
let returned = thread::spawn(move || {
assert!(cloned.is_confirm_commited());
cloned
})
.join()
.expect("moving a concrete KVDBEvent across an OS thread must not panic");
match returned {
KVDBEvent::ConfirmCommited(source, table, table_type, transaction_uid, commit_uid) => {
assert_eq!(source.as_str(), "payload-source");
assert_eq!(table.as_str(), "payload-table");
assert_eq!(table_type, KVDBTableType::MemOrdTab);
assert_eq!(transaction_uid, 0x11);
assert_eq!(commit_uid, 0x22);
}
other => panic!("event clone/thread move changed variant: {other:?}"),
}
assert!(event.is_confirm_commited());
match event {
KVDBEvent::ConfirmCommited(source, table, _, transaction_uid, commit_uid) => {
assert_eq!(source.as_str(), "payload-source");
assert_eq!(table.as_str(), "payload-table");
assert_eq!((transaction_uid, commit_uid), (0x11, 0x22));
}
other => panic!("original event changed variant: {other:?}"),
}
}