sz-orm-core 1.0.0

Core ORM engine: Model trait, ActiveRecord, QueryBuilder, Pool, Transaction, migration, and SQL dialect abstraction
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! Stress 测试套件
//!
//! 针对连接池、事务、查询的高并发和长时间运行进行压力测试
//! 目标:发现资源泄漏、状态不一致、死锁、活锁等问题
//!
//! 不变量:
//! - 任意时刻 active_count <= max_size(active_count = idle + borrowed)
//! - close_all 后所有 release 的连接被关闭,active_count 减少
//! - acquire_timeout 后必须返回 PoolError::Timeout
//! - 高频 acquire/release 下最终 active_count == idle_count(无借出)
//!
//! 注意:PoolStatus.active 是"池中总连接数"(idle + borrowed),
//!       PoolStatus.idle 是"空闲连接数",
//!       借出连接数 = active - idle

mod common;

use common::{MockConnectionFactory, Rng};
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use sz_orm_core::TransactOptions;
use sz_orm_core::Transaction;
use sz_orm_core::{Pool, PoolConfigBuilder, PoolError};
use tokio::sync::Mutex;

/// 辅助:构建一个连接池(使用 leak 获得 'static 引用,供 tokio::spawn 使用)
fn make_pool(max_size: u32, acquire_timeout_secs: u64) -> &'static Pool {
    let db = Arc::new(Mutex::new(common::InMemoryDb::new()));
    let factory = Arc::new(MockConnectionFactory::new(db));
    let config = PoolConfigBuilder::new()
        .max_size(max_size)
        .min_idle(0)
        .acquire_timeout(acquire_timeout_secs)
        .build()
        .unwrap();
    let pool = Pool::new(config, factory).unwrap();
    Box::leak(Box::new(pool))
}

/// 辅助:断言没有借出连接(active == idle)
async fn assert_no_borrowed(pool: &Pool, msg: &str) {
    let status = pool.status().await;
    assert_eq!(
        status.active, status.idle,
        "{}: active={}, idle={}",
        msg, status.active, status.idle
    );
}

/// Stress 1:高并发 acquire/release 循环
/// 验证:active_count 始终 <= max_size,最终无借出;无连接泄漏
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stress_pool_concurrent_acquire_release() {
    let pool = make_pool(20, 10);
    let total_ops = Arc::new(AtomicU64::new(0));
    let max_active_observed = Arc::new(AtomicU32::new(0));

    let mut handles = Vec::new();
    for _ in 0..8 {
        let total_ops = total_ops.clone();
        let max_active = max_active_observed.clone();
        handles.push(tokio::spawn(async move {
            for _ in 0..100 {
                let conn = match pool.acquire().await {
                    Ok(c) => c,
                    Err(_) => continue,
                };
                let status = pool.status().await;
                max_active.fetch_max(status.active, Ordering::Relaxed);
                tokio::task::yield_now().await;
                pool.release(conn).await;
                total_ops.fetch_add(1, Ordering::Relaxed);
            }
        }));
    }

    for h in handles {
        h.await.unwrap();
    }

    assert!(
        total_ops.load(Ordering::Relaxed) > 0,
        "should complete some ops"
    );
    assert!(
        max_active_observed.load(Ordering::Relaxed) <= 20,
        "active_count must never exceed max_size; observed {}",
        max_active_observed.load(Ordering::Relaxed)
    );
    assert_no_borrowed(pool, "after all ops").await;
}

/// Stress 2:max_size 限制测试
/// 验证:当所有连接被借出时,新 acquire 会超时
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn stress_pool_max_size_enforcement() {
    let pool = make_pool(5, 1);
    // 借出所有连接
    let mut held_conns = Vec::new();
    for _ in 0..5 {
        held_conns.push(pool.acquire().await.unwrap());
    }
    let status = pool.status().await;
    assert_eq!(status.active, 5);
    assert_eq!(status.idle, 0);

    // 新 acquire 必然超时
    let start = Instant::now();
    let result = pool.acquire().await;
    let elapsed = start.elapsed();
    assert!(result.is_err(), "should timeout when pool exhausted");
    assert!(
        matches!(result, Err(PoolError::Timeout)),
        "expected Timeout error, got {:?}",
        result.as_ref().err()
    );
    assert!(
        elapsed >= Duration::from_millis(900),
        "should wait at least ~1s before timeout, got {:?}",
        elapsed
    );
    assert!(
        elapsed < Duration::from_millis(2000),
        "should not wait too long, got {:?}",
        elapsed
    );

    // 释放一个连接
    pool.release(held_conns.pop().unwrap()).await;
    // 现在 acquire 应该立即成功
    let start = Instant::now();
    let conn = pool.acquire().await;
    let elapsed = start.elapsed();
    assert!(conn.is_ok(), "acquire should succeed after release");
    assert!(
        elapsed < Duration::from_millis(200),
        "acquire should be fast after release, got {:?}",
        elapsed
    );

    // 清理
    if let Ok(c) = conn {
        pool.release(c).await;
    }
    for c in held_conns {
        pool.release(c).await;
    }
    assert_no_borrowed(pool, "after cleanup").await;
}

/// Stress 3:突发大量并发任务
/// 验证:在突发负载下不会出现死锁或状态错乱
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
async fn stress_pool_burst_load() {
    let pool = make_pool(10, 5);
    let success_count = Arc::new(AtomicU64::new(0));
    let timeout_count = Arc::new(AtomicU64::new(0));

    let mut handles = Vec::new();
    for _ in 0..100 {
        let sc = success_count.clone();
        let tc = timeout_count.clone();
        handles.push(tokio::spawn(async move {
            // 持有连接 30ms 模拟工作
            match pool.acquire().await {
                Ok(conn) => {
                    tokio::time::sleep(Duration::from_millis(30)).await;
                    pool.release(conn).await;
                    sc.fetch_add(1, Ordering::Relaxed);
                }
                Err(PoolError::Timeout) => {
                    tc.fetch_add(1, Ordering::Relaxed);
                }
                Err(e) => panic!("unexpected error: {:?}", e),
            }
        }));
    }

    for h in handles {
        h.await.unwrap();
    }

    let total = success_count.load(Ordering::Relaxed) + timeout_count.load(Ordering::Relaxed);
    assert_eq!(total, 100, "all tasks must complete");
    // 10 个连接,每个 30ms,5 秒内可以完成 ~1600 个操作
    // 100 个任务应该都能成功
    assert!(
        success_count.load(Ordering::Relaxed) >= 80,
        "at least 80 should succeed, got {}",
        success_count.load(Ordering::Relaxed)
    );

    assert_no_borrowed(pool, "after burst load").await;
}

/// Stress 4:长时间运行事务
/// 验证:长事务期间其他连接仍可正常工作
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stress_long_transaction() {
    let pool = make_pool(5, 5);

    // 启动一个长事务
    let conn = pool.acquire().await.unwrap();
    let mut tx = Transaction::new(conn.into_inner(), TransactOptions::default());
    tx.execute("INSERT INTO t VALUES (1)").await.unwrap();
    tx.execute("INSERT INTO t VALUES (2)").await.unwrap();

    // 长事务期间,其他连接应该能正常工作
    let success = Arc::new(AtomicU64::new(0));
    let mut handles = Vec::new();
    for _ in 0..4 {
        let s = success.clone();
        handles.push(tokio::spawn(async move {
            if let Ok(c) = pool.acquire().await {
                tokio::time::sleep(Duration::from_millis(10)).await;
                pool.release(c).await;
                s.fetch_add(1, Ordering::Relaxed);
            }
        }));
    }

    for h in handles {
        h.await.unwrap();
    }
    assert!(
        success.load(Ordering::Relaxed) >= 3,
        "other tasks should succeed during long tx"
    );

    // 提交长事务
    tx.commit().await.unwrap();
    // Transaction 持有的 conn 不会自动归还到池中
    // 需要手动取出并归还(这里通过 drop 释放,连接不会回到池中)
    drop(tx);

    let status = pool.status().await;
    // 长事务的连接未归还,所以 active - idle == 1
    assert_eq!(status.active - status.idle, 1, "long-tx conn not returned");
}

/// Stress 5:混合工作负载(短查询 + 长事务 + 写操作)
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stress_mixed_workload() {
    let pool = make_pool(15, 5);
    let completed = Arc::new(AtomicU64::new(0));

    let mut handles = Vec::new();
    for i in 0..60 {
        let completed = completed.clone();
        handles.push(tokio::spawn(async move {
            match i % 3 {
                0 => {
                    // 短查询
                    if let Ok(conn) = pool.acquire().await {
                        tokio::task::yield_now().await;
                        pool.release(conn).await;
                    }
                }
                1 => {
                    // 长事务
                    if let Ok(conn) = pool.acquire().await {
                        let mut tx =
                            Transaction::new(conn.into_inner(), TransactOptions::default());
                        let _ = tx.execute("INSERT").await;
                        let _ = tx.commit().await;
                        drop(tx);
                    }
                }
                _ => {
                    // 写操作
                    if let Ok(conn) = pool.acquire().await {
                        drop(conn);
                    }
                }
            }
            completed.fetch_add(1, Ordering::Relaxed);
        }));
    }

    for h in handles {
        h.await.unwrap();
    }

    assert_eq!(completed.load(Ordering::Relaxed), 60);
    // 注意:Transaction drop 后连接不会自动归还,但 i%3==1 的分支连接通过 tx 持有
    // tx.commit 后 tx 仍持有 conn,drop(tx) 后 conn 被 drop(不归还池)
    // 所以 active - idle 可能 > 0
    let status = pool.status().await;
    assert!(status.active <= 15, "active must not exceed max_size");
}

/// Stress 6:连接池耗尽后恢复
/// 验证:池耗尽 → 超时 → 释放连接 → 新 acquire 成功
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn stress_pool_exhaustion_recovery() {
    let pool = make_pool(3, 1);

    // 借出所有
    let mut held = Vec::new();
    for _ in 0..3 {
        held.push(pool.acquire().await.unwrap());
    }

    // 验证 acquire 会超时
    let result = tokio::time::timeout(Duration::from_secs(2), pool.acquire()).await;
    assert!(result.is_ok(), "acquire should complete (with timeout)");
    assert!(result.unwrap().is_err(), "should be Timeout error");

    // 释放一个
    pool.release(held.pop().unwrap()).await;

    // 现在 acquire 应该立即成功
    let start = Instant::now();
    let conn = pool.acquire().await;
    let elapsed = start.elapsed();
    assert!(conn.is_ok(), "acquire should succeed after release");
    assert!(
        elapsed < Duration::from_millis(200),
        "acquire should be fast after release, got {:?}",
        elapsed
    );

    // 清理
    if let Ok(c) = conn {
        pool.release(c).await;
    }
    for c in held {
        pool.release(c).await;
    }
    assert_no_borrowed(pool, "after recovery").await;
}

/// Stress 7:高频 acquire/release 切换
/// 验证:在极高频率下状态仍然一致
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stress_high_frequency_acquire_release() {
    let pool = make_pool(8, 5);
    let iterations = Arc::new(AtomicU64::new(0));

    let mut handles = Vec::new();
    for _ in 0..4 {
        let iters = iterations.clone();
        handles.push(tokio::spawn(async move {
            for _ in 0..500 {
                if let Ok(conn) = pool.acquire().await {
                    pool.release(conn).await;
                    iters.fetch_add(1, Ordering::Relaxed);
                }
            }
        }));
    }

    for h in handles {
        h.await.unwrap();
    }

    assert!(
        iterations.load(Ordering::Relaxed) > 0,
        "should complete some iterations"
    );
    assert_no_borrowed(pool, "after high frequency").await;
}

/// Stress 8:close_all 后所有 release 的连接被关闭
/// 验证:close_all + release 后 active_count 减少到 0
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn stress_close_all_drops_released() {
    let pool = make_pool(10, 5);

    // 借出 5 个连接
    let mut held = Vec::new();
    for _ in 0..5 {
        held.push(pool.acquire().await.unwrap());
    }
    let status = pool.status().await;
    assert_eq!(status.active, 5);
    assert_eq!(status.idle, 0);

    // close_all(只关闭 idle,不关闭已借出的)
    pool.close_all().await;
    let status = pool.status().await;
    assert_eq!(status.idle, 0);
    assert_eq!(
        status.active, 5,
        "close_all should not affect borrowed conns"
    );

    // 归还连接:池已关闭,应直接关闭连接并减少 active_count
    for conn in held {
        pool.release(conn).await;
    }
    let status = pool.status().await;
    assert_eq!(
        status.idle, 0,
        "released conns should be closed, not returned to idle"
    );
    assert_eq!(
        status.active, 0,
        "active should be 0 after release on closed pool"
    );
}

/// Stress 9:长时间运行,验证状态一致性
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stress_long_running_steady_state() {
    let pool = make_pool(10, 5);
    let total = Arc::new(AtomicU64::new(0));

    // 持续运行 1 秒
    let deadline = Instant::now() + Duration::from_secs(1);
    let mut handles = Vec::new();
    for _ in 0..4 {
        let total = total.clone();
        handles.push(tokio::spawn(async move {
            while Instant::now() < deadline {
                if let Ok(conn) = pool.acquire().await {
                    tokio::task::yield_now().await;
                    pool.release(conn).await;
                    total.fetch_add(1, Ordering::Relaxed);
                }
            }
        }));
    }

    for h in handles {
        h.await.unwrap();
    }

    let ops = total.load(Ordering::Relaxed);
    assert!(ops > 100, "should complete many ops in 1s, got {}", ops);

    assert_no_borrowed(pool, "after steady state").await;
}

/// Stress 10:并发 reap_idle
/// 验证:并发 reap_idle 不会导致状态错乱
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stress_concurrent_reap_idle() {
    let pool = make_pool(20, 5);

    // 创建一些空闲连接
    let mut conns = Vec::new();
    for _ in 0..10 {
        conns.push(pool.acquire().await.unwrap());
    }
    for c in conns.drain(..) {
        pool.release(c).await;
    }

    // 并发 reap_idle
    let mut handles = Vec::new();
    for _ in 0..2 {
        handles.push(tokio::spawn(async move {
            for _ in 0..5 {
                pool.reap_idle().await;
                tokio::task::yield_now().await;
            }
        }));
    }
    // 同时有任务在 acquire/release
    for _ in 0..2 {
        handles.push(tokio::spawn(async move {
            for _ in 0..50 {
                if let Ok(conn) = pool.acquire().await {
                    pool.release(conn).await;
                }
            }
        }));
    }

    for h in handles {
        h.await.unwrap();
    }

    assert_no_borrowed(pool, "after concurrent reap").await;
    let status = pool.status().await;
    assert!(status.idle <= 20);
}

/// Stress 11:使用 FaultyConnectionFactory 测试故障下的压力行为
/// 验证:工厂故障时不会导致状态不一致
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stress_with_factory_faults() {
    use common::FaultyConnectionFactory;
    use sz_orm_core::ConnectionFactory;
    let db = Arc::new(Mutex::new(common::InMemoryDb::new()));
    let factory: Arc<dyn ConnectionFactory> = Arc::new(FaultyConnectionFactory::new(db, 5));
    let config = PoolConfigBuilder::new()
        .max_size(10)
        .min_idle(0)
        .acquire_timeout(5)
        .build()
        .unwrap();
    let pool: &'static Pool = &*Box::leak(Box::new(Pool::new(config, factory).unwrap()));

    let success = Arc::new(AtomicU64::new(0));
    let failure = Arc::new(AtomicU64::new(0));

    let mut handles = Vec::new();
    for _ in 0..10 {
        let s = success.clone();
        let f = failure.clone();
        handles.push(tokio::spawn(async move {
            for _ in 0..10 {
                match pool.acquire().await {
                    Ok(conn) => {
                        pool.release(conn).await;
                        s.fetch_add(1, Ordering::Relaxed);
                    }
                    Err(_) => {
                        f.fetch_add(1, Ordering::Relaxed);
                    }
                }
            }
        }));
    }

    for h in handles {
        h.await.unwrap();
    }

    let total = success.load(Ordering::Relaxed) + failure.load(Ordering::Relaxed);
    assert_eq!(total, 100, "all ops must complete");

    assert_no_borrowed(pool, "after factory faults").await;
}

/// Stress 12:随机延迟下的压力测试
/// 验证:在随机持有时间下,池状态始终一致
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stress_random_hold_times() {
    let pool = make_pool(12, 5);
    let mut rng = Rng::new(42);
    let total = Arc::new(AtomicU64::new(0));

    let mut delays = Vec::new();
    for _ in 0..150 {
        delays.push(rng.next_usize(20));
    }
    let delays = Arc::new(Mutex::new(delays));

    let mut handles = Vec::new();
    for _ in 0..4 {
        let total = total.clone();
        let delays = delays.clone();
        handles.push(tokio::spawn(async move {
            for _ in 0..30 {
                let delay_ms = {
                    let mut d = delays.lock().await;
                    if d.is_empty() {
                        5
                    } else {
                        d.pop().unwrap() as u64
                    }
                };
                if let Ok(conn) = pool.acquire().await {
                    tokio::time::sleep(Duration::from_millis(delay_ms)).await;
                    pool.release(conn).await;
                    total.fetch_add(1, Ordering::Relaxed);
                }
            }
        }));
    }

    for h in handles {
        h.await.unwrap();
    }

    assert!(
        total.load(Ordering::Relaxed) > 0,
        "should complete some ops"
    );
    assert_no_borrowed(pool, "after random hold times").await;
    let status = pool.status().await;
    assert!(status.idle <= 12);
}