Skip to content

Commit a7e8d57

Browse files
author
Joshua Nelson
committed
Add test for rollbacks
The previous test was incorrect and would always succeed even if no rollback occurred.
1 parent fda0593 commit a7e8d57

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/simple_api/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ pub enum TransactionStatus {
13021302

13031303
type UserResult = Result<TransactionStatus, Box<dyn Error>>;
13041304

1305+
#[derive(Debug)]
13051306
enum CallBackError {
13061307
// the callback returned an error
13071308
ApplicationError(Box<dyn Error>),
@@ -1390,11 +1391,11 @@ extern "C" fn fn_callback(tptoken: u64, errstr: *mut ydb_buffer_t, tpfnparm: *mu
13901391
/// use yottadb::simple_api::{Key, tp_st};
13911392
///
13921393
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
1393-
/// let var = Key::variable("tpRollbackTest");
1394+
/// let var = Key::variable("^tpRollbackTest");
13941395
/// var.set_st(TpToken::default(), Vec::new(), "initial value")?;
13951396
/// let maybe_err = tp_st(TpToken::default(), Vec::new(), |tptoken| {
1396-
/// fallible_operation()?;
13971397
/// var.set_st(tptoken, Vec::new(), "new value")?;
1398+
/// fallible_operation()?;
13981399
/// Ok(TransactionStatus::Ok)
13991400
/// }, "BATCH", &[]);
14001401
/// let expected_val: &[_] = if maybe_err.is_ok() {
@@ -1457,7 +1458,7 @@ where
14571458
let mut err_buffer_t = Key::make_out_buffer_t(&mut err_buffer);
14581459

14591460
let mut locals: Vec<ConstYDBBuffer> = Vec::with_capacity(locals_to_reset.len());
1460-
for local in locals_to_reset.iter() {
1461+
for &local in locals_to_reset.iter() {
14611462
locals.push(
14621463
ydb_buffer_t {
14631464
buf_addr: local.as_ptr() as *const _ as *mut _,
@@ -2580,6 +2581,20 @@ pub(crate) mod tests {
25802581
}
25812582
}
25822583

2584+
#[test]
2585+
fn rollback() {
2586+
let key = Key::variable("^tpRollbackTest");
2587+
key.set_st(YDB_NOTTP, Vec::new(), "initial").unwrap();
2588+
let set_inner = |tptoken| {
2589+
key.set_st(tptoken, Vec::new(), "val")?;
2590+
Ok(TransactionStatus::Rollback)
2591+
};
2592+
let err = tp_st(YDB_NOTTP, Vec::new(), set_inner, "BATCH", &[]).unwrap_err();
2593+
let status = err.downcast::<YDBError>().unwrap().status;
2594+
assert_eq!(status, craw::YDB_TP_ROLLBACK);
2595+
assert_eq!(key.get_st(YDB_NOTTP, Vec::new()).unwrap(), b"initial");
2596+
}
2597+
25832598
#[test]
25842599
#[should_panic]
25852600
fn panic_in_cb() {

0 commit comments

Comments
 (0)