Skip to content

Commit

Permalink
Add an example of using async
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed Aug 18, 2024
1 parent 6d57bee commit 29e0420
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ default-features = false

[dev-dependencies]
temporary = "0.6"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
41 changes: 30 additions & 11 deletions tests/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,40 @@ fn open_with_flags() {
}
}

#[tokio::test]
async fn open_thread_safe_async() {
use std::sync::Arc;

use tokio::task::spawn_blocking as spawn;

let connection = Arc::new(ok!(ok!(
spawn(|| Connection::open_thread_safe(":memory:")).await
)));

{
let connection = connection.clone();
ok!(ok!(spawn(move || connection.execute("SELECT 1")).await));
}

{
let connection = connection.clone();
ok!(ok!(spawn(move || connection.execute("SELECT 1")).await));
}
}

#[test]
fn open_thread_safe() {
fn open_thread_safe_sync() {
use std::sync::Arc;
use std::thread;
use std::thread::spawn;

let connection = ok!(Connection::open_thread_safe(":memory:"));
let connection = Arc::new(connection);
let connection = Arc::new(ok!(Connection::open_thread_safe(":memory:")));

let mut threads = Vec::new();
for _ in 0..5 {
let connection_ = connection.clone();
let thread = thread::spawn(move || {
ok!(connection_.execute("SELECT 1"));
});
threads.push(thread);
let connection = connection.clone();
threads.push(spawn(move || {
ok!(connection.execute("SELECT 1"));
}));
}
for thread in threads {
ok!(thread.join());
Expand Down Expand Up @@ -80,7 +99,7 @@ fn iterate() {

#[test]
fn set_busy_handler() {
use std::thread;
use std::thread::spawn;
use temporary::Directory;

let directory = ok!(Directory::new("sqlite"));
Expand All @@ -90,7 +109,7 @@ fn set_busy_handler() {
let guards = (0..10)
.map(|_| {
let path = path.to_path_buf();
thread::spawn(move || {
spawn(move || {
let mut connection = ok!(sqlite::open(&path));
ok!(connection.set_busy_handler(|_| true));
let query = "INSERT INTO users VALUES (?, ?, ?, ?, ?)";
Expand Down

0 comments on commit 29e0420

Please sign in to comment.