|
| 1 | +use crate::common::net::TurmoilConnector; |
| 2 | +use libsql::{params, Database, TransactionBehavior}; |
| 3 | + |
| 4 | +#[test] |
| 5 | +fn transaction_commit_and_rollback() { |
| 6 | + let mut sim = turmoil::Builder::new().build(); |
| 7 | + sim.host("primary", super::make_standalone_server); |
| 8 | + sim.client("client", async { |
| 9 | + let db = Database::open_remote_with_connector("http://primary:8080", "", TurmoilConnector)?; |
| 10 | + let conn = db.connect()?; |
| 11 | + |
| 12 | + // initialize tables |
| 13 | + let tx = conn.transaction().await?; |
| 14 | + tx.execute_batch(r#"create table t(x text);"#).await?; |
| 15 | + tx.commit().await?; |
| 16 | + |
| 17 | + // transaction with temporary data |
| 18 | + let tx = conn.transaction().await?; |
| 19 | + tx.execute("insert into t(x) values('hello');", ()).await?; |
| 20 | + |
| 21 | + let mut rows = tx |
| 22 | + .query("select * from t where x = ?", params!["hello"]) |
| 23 | + .await?; |
| 24 | + |
| 25 | + assert_eq!(rows.column_count(), 1); |
| 26 | + assert_eq!(rows.column_name(0), Some("x")); |
| 27 | + assert_eq!(rows.next()?.unwrap().get::<String>(0)?, "hello"); |
| 28 | + assert!(rows.next()?.is_none()); |
| 29 | + tx.rollback().await?; |
| 30 | + |
| 31 | + // confirm that temporary that was not committed |
| 32 | + let mut rows = conn |
| 33 | + .query("select * from t where x = ?", params!["hello"]) |
| 34 | + .await?; |
| 35 | + |
| 36 | + assert_eq!(rows.column_count(), 1); |
| 37 | + assert_eq!(rows.column_name(0), Some("x")); |
| 38 | + assert!(rows.next()?.is_none()); |
| 39 | + |
| 40 | + Ok(()) |
| 41 | + }); |
| 42 | + |
| 43 | + sim.run().unwrap(); |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn multiple_concurrent_transactions() { |
| 48 | + let mut sim = turmoil::Builder::new().build(); |
| 49 | + sim.host("primary", super::make_standalone_server); |
| 50 | + sim.client("client", async { |
| 51 | + let db = Database::open_remote_with_connector("http://primary:8080", "", TurmoilConnector)?; |
| 52 | + let conn = db.connect()?; |
| 53 | + conn.execute_batch(r#"create table t(x text);"#).await?; |
| 54 | + |
| 55 | + // open first transaction and alter data |
| 56 | + let tx1 = conn |
| 57 | + .transaction_with_behavior(TransactionBehavior::Deferred) |
| 58 | + .await?; |
| 59 | + tx1.execute("insert into t(x) values('hello');", ()).await?; |
| 60 | + |
| 61 | + // while first transaction is still open open another read-only transaction and try to read |
| 62 | + let tx2 = conn |
| 63 | + .transaction_with_behavior(TransactionBehavior::ReadOnly) |
| 64 | + .await?; |
| 65 | + let mut rows = tx2 |
| 66 | + .query("select * from t where x = ?", params!["hello"]) |
| 67 | + .await?; |
| 68 | + assert_eq!(rows.column_count(), 1); |
| 69 | + assert_eq!(rows.column_name(0), Some("x")); |
| 70 | + assert!(rows.next()?.is_none()); |
| 71 | + |
| 72 | + // commit first transaction - T2 should still read old data |
| 73 | + tx1.commit().await?; |
| 74 | + |
| 75 | + let mut rows = tx2 |
| 76 | + .query("select * from t where x = ?", params!["hello"]) |
| 77 | + .await?; |
| 78 | + assert_eq!(rows.column_count(), 1); |
| 79 | + assert_eq!(rows.column_name(0), Some("x")); |
| 80 | + assert!(rows.next()?.is_none()); |
| 81 | + tx2.commit().await?; |
| 82 | + |
| 83 | + // finally open new transaction - it now should read actual data |
| 84 | + let tx3 = conn |
| 85 | + .transaction_with_behavior(TransactionBehavior::ReadOnly) |
| 86 | + .await?; |
| 87 | + let mut rows = tx3 |
| 88 | + .query("select * from t where x = ?", params!["hello"]) |
| 89 | + .await?; |
| 90 | + assert_eq!(rows.column_count(), 1); |
| 91 | + assert_eq!(rows.column_name(0), Some("x")); |
| 92 | + assert_eq!(rows.next()?.unwrap().get::<String>(0)?, "hello"); |
| 93 | + assert!(rows.next()?.is_none()); |
| 94 | + |
| 95 | + Ok(()) |
| 96 | + }); |
| 97 | + sim.run().unwrap(); |
| 98 | +} |
0 commit comments