toasty 0.9.0

An async ORM for Rust supporting SQL and NoSQL databases
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Deterministic tests for the pool's background sweep and recycle
//! behavior.
//!
//! Each test runs on paused tokio time against a fault-injecting
//! `Driver` that wraps a file-backed SQLite. The wrapper lets the
//! test arm a queue of "next N pings fail" / "next N execs fail"
//! tokens; everything else passes through to SQLite so the engine
//! can run real schema pushes and inserts. No real-time deadlines.

use std::{
    borrow::Cow,
    sync::{
        Arc,
        atomic::{AtomicU32, Ordering},
    },
    time::Duration,
};

use async_trait::async_trait;
use tempfile::TempDir;
use toasty_core::{
    Result, Schema,
    driver::{Capability, ConnectContext, Connection, Driver, ExecResponse, Operation},
    schema::{
        db::{AppliedMigration, Migration},
        diff,
    },
};

#[derive(Debug, toasty::Model)]
struct User {
    #[key]
    #[auto]
    id: u64,
    name: String,
    age: i64,
}

#[derive(Debug, Default)]
struct MockState {
    pings: AtomicU32,
    ping_fail_tokens: AtomicU32,
    exec_fail_tokens: AtomicU32,
    connects: AtomicU32,
}

#[derive(Debug)]
struct MockDriver {
    inner: toasty_driver_sqlite::Sqlite,
    state: Arc<MockState>,
    // Keeps the on-disk SQLite file alive for the lifetime of the
    // driver — the tempdir self-deletes on drop.
    _tempdir: TempDir,
}

impl MockDriver {
    fn new() -> Self {
        let tempdir = TempDir::new().expect("create tempdir");
        let path = tempdir.path().join("pool_sweep.db");
        Self {
            inner: toasty_driver_sqlite::Sqlite::open(&path),
            state: Arc::new(MockState::default()),
            _tempdir: tempdir,
        }
    }

    fn state(&self) -> Arc<MockState> {
        self.state.clone()
    }
}

#[async_trait]
impl Driver for MockDriver {
    fn url(&self) -> Cow<'_, str> {
        self.inner.url()
    }

    fn capability(&self) -> &'static Capability {
        self.inner.capability()
    }

    async fn connect(&self, cx: &ConnectContext) -> Result<Box<dyn Connection>> {
        let inner = self.inner.connect(cx).await?;
        self.state.connects.fetch_add(1, Ordering::Relaxed);
        Ok(Box::new(MockConnection {
            inner,
            state: self.state.clone(),
            valid: true,
        }))
    }

    fn generate_migration(&self, diff: &diff::Schema<'_>) -> Migration {
        self.inner.generate_migration(diff)
    }

    async fn reset_db(&self) -> Result<()> {
        self.inner.reset_db().await
    }
}

#[derive(Debug)]
struct MockConnection {
    inner: Box<dyn Connection>,
    state: Arc<MockState>,
    valid: bool,
}

#[async_trait]
impl Connection for MockConnection {
    async fn exec(&mut self, schema: &Arc<Schema>, op: Operation) -> Result<ExecResponse> {
        if self.state.exec_fail_tokens.load(Ordering::Relaxed) > 0 {
            self.state.exec_fail_tokens.fetch_sub(1, Ordering::Relaxed);
            self.valid = false;
            return Err(toasty_core::Error::connection_lost(std::io::Error::other(
                "mock exec failure",
            )));
        }
        self.inner.exec(schema, op).await
    }

    fn is_valid(&self) -> bool {
        self.valid && self.inner.is_valid()
    }

    async fn ping(&mut self) -> Result<()> {
        self.state.pings.fetch_add(1, Ordering::Relaxed);
        if self.state.ping_fail_tokens.load(Ordering::Relaxed) > 0 {
            self.state.ping_fail_tokens.fetch_sub(1, Ordering::Relaxed);
            self.valid = false;
            return Err(toasty_core::Error::connection_lost(std::io::Error::other(
                "mock ping failure",
            )));
        }
        self.inner.ping().await
    }

    async fn push_schema(&mut self, schema: &Schema) -> Result<()> {
        self.inner.push_schema(schema).await
    }

    async fn applied_migrations(&mut self) -> Result<Vec<AppliedMigration>> {
        self.inner.applied_migrations().await
    }

    async fn apply_migration(&mut self, id: u64, name: &str, migration: &Migration) -> Result<()> {
        self.inner.apply_migration(id, name, migration).await
    }
}

/// Build a `Db` with the given pool configuration. Returns the `Db`
/// and a handle to the mock's fault-injection / counter state.
async fn build_db(
    max_pool_size: usize,
    health_check_interval: Option<Duration>,
    pre_ping: bool,
) -> (toasty::Db, Arc<MockState>) {
    let driver = MockDriver::new();
    let state = driver.state();

    let db = toasty::Db::builder()
        .models(toasty::models!(User))
        .max_pool_size(max_pool_size)
        .pool_health_check_interval(health_check_interval)
        .pool_pre_ping(pre_ping)
        .build(driver)
        .await
        .unwrap();
    db.push_schema().await.unwrap();
    (db, state)
}

/// Passive recovery: with the sweep disabled, a user-observed
/// `connection_lost` must still drain the dead slot so the next
/// call opens a fresh connection. Exercises the `Manager::recycle`
/// path in isolation.
#[tokio::test(start_paused = true)]
async fn pool_recovers_after_connection_lost() {
    let (mut db, state) = build_db(1, None, false).await;

    toasty::create!(User {
        name: "alice",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    state.exec_fail_tokens.store(1, Ordering::Relaxed);

    let err = toasty::create!(User {
        name: "bob",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap_err();
    assert!(
        err.is_connection_lost(),
        "expected connection_lost, got {err}"
    );

    toasty::create!(User {
        name: "carol",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();
}

/// Periodic sweep evicts a silently-broken idle connection without
/// needing a user query to trip it.
#[tokio::test(start_paused = true)]
async fn sweep_evicts_dead_idle_connection() {
    let (mut db, state) = build_db(1, Some(Duration::from_millis(50)), false).await;

    // Force the pool to open its one connection.
    toasty::create!(User {
        name: "alice",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();
    assert_eq!(db.pool().status().size, 1);

    state.ping_fail_tokens.store(1, Ordering::Relaxed);

    // Advance past the first sweep tick.
    tokio::time::sleep(Duration::from_millis(100)).await;

    assert_eq!(
        db.pool().status().size,
        0,
        "sweep did not evict the dead idle connection",
    );

    // Next user query opens a fresh connection.
    toasty::create!(User {
        name: "bob",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();
}

/// One user query observing `connection_lost` must trigger an eager
/// sweep that pings every remaining idle connection. Otherwise each
/// queued fault would surface as a separate user-query failure.
#[tokio::test(start_paused = true)]
async fn eager_escalation_after_observed_loss() {
    // 60-second interval so the periodic tick cannot fire during the
    // test — only the notify-driven escalation path can drain the
    // queued faults here.
    let (mut db, state) = build_db(3, Some(Duration::from_secs(60)), false).await;

    let c1 = db.connection().await.unwrap();
    let c2 = db.connection().await.unwrap();
    let c3 = db.connection().await.unwrap();
    drop((c1, c2, c3));
    assert_eq!(db.pool().status().size, 3);

    // One fault for the user query, two for the sweep's escalation
    // pings over the remaining idle connections.
    state.exec_fail_tokens.store(1, Ordering::Relaxed);
    state.ping_fail_tokens.store(2, Ordering::Relaxed);

    let err = toasty::create!(User {
        name: "alice",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap_err();
    assert!(
        err.is_connection_lost(),
        "expected connection_lost, got {err}"
    );

    // Yield long enough on the virtual clock for the sweep to
    // observe the wake() and run its escalate.
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Sweep must have pinged both remaining idles. With a 60s
    // periodic interval, nothing else explains this — only the
    // notify-driven escalation path can have fired.
    assert_eq!(state.pings.load(Ordering::Relaxed), 2);
    // Both queued ping faults were consumed by the escalation pass.
    assert_eq!(state.ping_fail_tokens.load(Ordering::Relaxed), 0);

    // And the system recovers: the next queries succeed against
    // fresh connections (would surface connection_lost if the bad
    // idles were still around).
    toasty::create!(User {
        name: "bob",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();
    toasty::create!(User {
        name: "carol",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();
}

/// A failing periodic ping self-wakes via `ConnectionTask::respond`,
/// which queues a sweep-notify permit before the same iteration
/// calls `escalate()`. The escalate snapshot includes that bump, so
/// on the next loop pass the queued permit must be deduped against
/// `last_serviced`. Without the dedup, every periodic-detected
/// failure produces a second escalate that re-pings every surviving
/// idle connection.
#[tokio::test(start_paused = true)]
async fn periodic_failure_does_not_redundantly_escalate() {
    let (db, state) = build_db(3, Some(Duration::from_secs(1)), false).await;

    let c1 = db.connection().await.unwrap();
    let c2 = db.connection().await.unwrap();
    let c3 = db.connection().await.unwrap();
    drop((c1, c2, c3));
    assert_eq!(db.pool().status().size, 3);

    state.ping_fail_tokens.store(1, Ordering::Relaxed);

    // Past the first tick (1s), well before the second (2s).
    tokio::time::sleep(Duration::from_millis(1_500)).await;

    // 1 failing periodic ping + 2 healthy escalate pings = 3. Without
    // dedup, the queued notify permit would drive a second escalate
    // over the two remaining idles, raising the count to 5.
    assert_eq!(
        state.pings.load(Ordering::Relaxed),
        3,
        "expected one escalation pass (1 fail + 2 healthy); redundant escalate would have raised this to 5",
    );
}

/// Pre-ping catches a silently-broken idle connection on checkout
/// and the caller never sees the failure. Sweep is disabled so the
/// only path that can drain the dead slot before the user's query
/// goes out is `Manager::recycle`'s active ping.
#[tokio::test(start_paused = true)]
async fn pre_ping_evicts_silently_broken_idle_connection() {
    let (mut db, state) = build_db(1, None, true).await;

    toasty::create!(User {
        name: "alice",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();
    assert_eq!(db.pool().status().size, 1);

    let pings_before = state.pings.load(Ordering::Relaxed);
    state.ping_fail_tokens.store(1, Ordering::Relaxed);

    // Recycle pings, the ping fails, deadpool drops the slot and
    // opens a fresh one before the user's exec runs.
    toasty::create!(User {
        name: "bob",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    assert_eq!(state.pings.load(Ordering::Relaxed) - pings_before, 1);
    assert_eq!(state.ping_fail_tokens.load(Ordering::Relaxed), 0);
    assert_eq!(db.pool().status().size, 1);
}

/// Pre-ping issues a round-trip on every checkout. The first query
/// after build may or may not recycle (depending on whether the
/// build-time probe left a slot warm), so we measure pings against a
/// snapshot taken once the pool is steady-state.
#[tokio::test(start_paused = true)]
async fn pre_ping_runs_on_every_checkout() {
    let (mut db, state) = build_db(1, None, true).await;

    // Warm the pool past any setup-driven acquires.
    toasty::create!(User {
        name: "alice",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    let pings_before = state.pings.load(Ordering::Relaxed);

    // Two more checkouts — each must recycle the lone idle slot and
    // therefore fire exactly one pre-ping.
    toasty::create!(User {
        name: "bob",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();
    toasty::create!(User {
        name: "carol",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    assert_eq!(state.pings.load(Ordering::Relaxed) - pings_before, 2);
}

/// Lifetime cap: a connection older than `pool_max_connection_lifetime`
/// is evicted by `recycle` and replaced with a fresh one. Uses real
/// time because deadpool's `Metrics::age` reads `std::time::Instant`,
/// which `tokio::time::pause` does not advance.
#[tokio::test]
async fn lifetime_cap_evicts_aged_connection() {
    let driver = MockDriver::new();
    let state = driver.state();

    let mut db = toasty::Db::builder()
        .models(toasty::models!(User))
        .max_pool_size(1)
        .pool_health_check_interval(None)
        .pool_max_connection_lifetime(Some(Duration::from_millis(50)))
        .build(driver)
        .await
        .unwrap();
    db.push_schema().await.unwrap();

    toasty::create!(User {
        name: "alice",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    let connects_before = state.connects.load(Ordering::Relaxed);

    // Real-time wait past the lifetime cap.
    tokio::time::sleep(Duration::from_millis(150)).await;

    toasty::create!(User {
        name: "bob",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    assert_eq!(
        state.connects.load(Ordering::Relaxed) - connects_before,
        1,
        "recycle did not replace the over-lifetime connection",
    );
    assert_eq!(db.pool().status().size, 1);
}

/// Idle-time cap: a connection that has been sitting unused for longer
/// than `pool_max_connection_idle_time` is evicted by `recycle` and
/// replaced on the next checkout.
#[tokio::test]
async fn idle_time_cap_evicts_long_idle_connection() {
    let driver = MockDriver::new();
    let state = driver.state();

    let mut db = toasty::Db::builder()
        .models(toasty::models!(User))
        .max_pool_size(1)
        .pool_health_check_interval(None)
        .pool_max_connection_idle_time(Some(Duration::from_millis(50)))
        .build(driver)
        .await
        .unwrap();
    db.push_schema().await.unwrap();

    toasty::create!(User {
        name: "alice",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    let connects_before = state.connects.load(Ordering::Relaxed);

    // Real-time wait past the idle cap.
    tokio::time::sleep(Duration::from_millis(150)).await;

    toasty::create!(User {
        name: "bob",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    assert_eq!(
        state.connects.load(Ordering::Relaxed) - connects_before,
        1,
        "recycle did not replace the over-idle connection",
    );
    assert_eq!(db.pool().status().size, 1);
}

/// A connection within both caps is reused — `recycle` must not evict
/// a healthy young connection.
#[tokio::test]
async fn lifetime_and_idle_caps_do_not_evict_fresh_connection() {
    let driver = MockDriver::new();
    let state = driver.state();

    let mut db = toasty::Db::builder()
        .models(toasty::models!(User))
        .max_pool_size(1)
        .pool_health_check_interval(None)
        .pool_max_connection_lifetime(Some(Duration::from_secs(60)))
        .pool_max_connection_idle_time(Some(Duration::from_secs(60)))
        .build(driver)
        .await
        .unwrap();
    db.push_schema().await.unwrap();

    toasty::create!(User {
        name: "alice",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    let connects_before = state.connects.load(Ordering::Relaxed);

    toasty::create!(User {
        name: "bob",
        age: 30
    })
    .exec(&mut db)
    .await
    .unwrap();

    assert_eq!(
        state.connects.load(Ordering::Relaxed),
        connects_before,
        "recycle replaced a connection within both caps",
    );
}