mockgres 0.0.22

An in-memory database that replicates a reasonable subset of Postgres functionality to make unit tests that rely on a database to run.
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
mod common;

use tokio::time::{Duration, Instant, sleep};
use tokio_postgres::error::SqlState;

#[tokio::test(flavor = "multi_thread")]
async fn for_update_skip_locked_splits_between_sessions() {
    let ctx = common::start().await;
    ctx.client
        .execute("create table jobs(id int primary key, ready bool)", &[])
        .await
        .expect("create jobs");
    ctx.client
        .execute(
            "insert into jobs values (1, true), (2, true), (3, true), (4, true)",
            &[],
        )
        .await
        .expect("seed jobs");
    let client_b = ctx.new_client().await;

    ctx.client.execute("begin", &[]).await.expect("begin a");
    client_b.execute("begin", &[]).await.expect("begin b");

    let rows_a = fetch_ids(
        &ctx.client,
        "select id from jobs where ready limit 2 for update skip locked",
    )
    .await;
    let rows_b = fetch_ids(
        &client_b,
        "select id from jobs where ready limit 2 for update skip locked",
    )
    .await;

    assert_eq!(rows_a.len(), 2, "session A should lock first two rows");
    assert_eq!(rows_b.len(), 2, "session B should get remaining rows");
    for id in &rows_a {
        assert!(
            !rows_b.contains(id),
            "row {id} should not be visible to session B"
        );
    }

    ctx.client
        .execute("rollback", &[])
        .await
        .expect("rollback a");
    client_b.execute("rollback", &[]).await.expect("rollback b");
    let _ = ctx.shutdown.send(());
}

#[tokio::test(flavor = "multi_thread")]
async fn for_update_skip_locked_respects_ordering() {
    let ctx = common::start().await;
    ctx.client
        .execute("create table jobs(id int primary key, ready bool)", &[])
        .await
        .expect("create jobs");
    ctx.client
        .execute(
            "insert into jobs values (1, true), (2, true), (3, true), (4, true)",
            &[],
        )
        .await
        .expect("seed jobs");
    let client_b = ctx.new_client().await;

    ctx.client.execute("begin", &[]).await.expect("begin a");
    client_b.execute("begin", &[]).await.expect("begin b");

    let rows_a = fetch_ids(
        &ctx.client,
        "select id from jobs where ready order by id limit 2 for update skip locked",
    )
    .await;
    let rows_b = fetch_ids(
        &client_b,
        "select id from jobs where ready order by id limit 2 for update skip locked",
    )
    .await;

    assert_eq!(rows_a, vec![1, 2], "session A should lock first rows by id");
    assert_eq!(
        rows_b,
        vec![3, 4],
        "session B should see remaining ordered rows"
    );

    ctx.client
        .execute("rollback", &[])
        .await
        .expect("rollback a");
    client_b.execute("rollback", &[]).await.expect("rollback b");
    let _ = ctx.shutdown.send(());
}

#[tokio::test(flavor = "multi_thread")]
async fn update_conflicts_with_locked_row() {
    let ctx = common::start().await;
    ctx.client
        .execute(
            "create table accounts(id int primary key, balance int)",
            &[],
        )
        .await
        .expect("create accounts");
    ctx.client
        .execute("insert into accounts values (1, 10)", &[])
        .await
        .expect("seed accounts");
    let blocker = ctx.new_client().await;

    ctx.client
        .execute("begin", &[])
        .await
        .expect("begin locker");
    ctx.client
        .query_one("select id from accounts where id = 1 for update", &[])
        .await
        .expect("lock row");

    let err = blocker
        .execute("update accounts set balance = 5 where id = 1", &[])
        .await
        .expect_err("update should fail while locked");
    common::assert_db_error_contains(&err, "lock");

    ctx.client
        .execute("rollback", &[])
        .await
        .expect("rollback locker");
    let _ = ctx.shutdown.send(());
}

#[tokio::test(flavor = "multi_thread")]
async fn for_update_nowait_errors_immediately() {
    let ctx = common::start().await;
    ctx.client
        .execute("create table locks(id int primary key)", &[])
        .await
        .expect("create locks");
    ctx.client
        .execute("insert into locks values (1)", &[])
        .await
        .expect("seed locks");

    let other = ctx.new_client().await;

    ctx.client
        .execute("begin", &[])
        .await
        .expect("begin locker");
    ctx.client
        .query("select id from locks for update", &[])
        .await
        .expect("lock row");

    let err = other
        .query("select id from locks for update nowait", &[])
        .await
        .expect_err("NOWAIT should error");
    assert_eq!(
        err.code(),
        Some(&SqlState::LOCK_NOT_AVAILABLE),
        "expected 55P03"
    );

    ctx.client
        .execute("rollback", &[])
        .await
        .expect("rollback locker");
    let _ = ctx.shutdown.send(());
}

#[tokio::test(flavor = "multi_thread")]
async fn for_update_honors_lock_timeout() {
    let ctx = common::start().await;
    ctx.client
        .execute("create table t_lock_timeout(id int primary key)", &[])
        .await
        .expect("create table");
    ctx.client
        .execute("insert into t_lock_timeout values (1)", &[])
        .await
        .expect("seed table");

    let blocked_client = ctx.new_client().await;

    ctx.client
        .execute("begin", &[])
        .await
        .expect("begin lock holder");
    ctx.client
        .query("select id from t_lock_timeout for update", &[])
        .await
        .expect("lock row");

    blocked_client
        .execute("begin", &[])
        .await
        .expect("begin waiter");
    blocked_client
        .execute("set lock_timeout = '50ms'", &[])
        .await
        .expect("set lock_timeout");

    let started = Instant::now();
    let err = blocked_client
        .query("select id from t_lock_timeout for update", &[])
        .await
        .expect_err("waiter should time out");
    assert_eq!(
        err.code(),
        Some(&SqlState::LOCK_NOT_AVAILABLE),
        "expected 55P03"
    );
    let message = err
        .as_db_error()
        .expect("expected db error")
        .message()
        .to_ascii_lowercase();
    assert!(
        message.contains("lock timeout"),
        "unexpected error message: {message}"
    );
    assert!(
        started.elapsed() < Duration::from_millis(500),
        "lock timeout should fail quickly"
    );

    blocked_client
        .execute("rollback", &[])
        .await
        .expect("rollback waiter");
    ctx.client
        .execute("rollback", &[])
        .await
        .expect("rollback holder");
    let _ = ctx.shutdown.send(());
}

#[tokio::test(flavor = "multi_thread")]
async fn for_update_blocks_until_row_is_free() {
    let ctx = common::start().await;
    ctx.client
        .execute("create table blocking(id int primary key)", &[])
        .await
        .expect("create blocking");
    ctx.client
        .execute("insert into blocking values (1)", &[])
        .await
        .expect("seed blocking");
    let blocked_client = ctx.new_client().await;

    ctx.client
        .execute("begin", &[])
        .await
        .expect("begin holder");
    ctx.client
        .query("select id from blocking for update", &[])
        .await
        .expect("lock row");

    let runner = tokio::spawn(async move {
        blocked_client
            .query("select id from blocking for update", &[])
            .await
    });

    sleep(Duration::from_millis(50)).await;
    assert!(
        !runner.is_finished(),
        "second FOR UPDATE should block while lock held"
    );

    ctx.client
        .execute("commit", &[])
        .await
        .expect("commit holder");

    let rows = runner
        .await
        .expect("join spawned query")
        .expect("blocking select succeeds");
    assert_eq!(rows.len(), 1, "second reader runs after commit");

    let _ = ctx.shutdown.send(());
}

#[tokio::test(flavor = "multi_thread")]
async fn locks_release_after_rollback() {
    let ctx = common::start().await;
    ctx.client
        .execute("create table msgs(id int primary key)", &[])
        .await
        .expect("create msgs");
    ctx.client
        .execute("insert into msgs values (1)", &[])
        .await
        .expect("seed msgs");
    let client_b = ctx.new_client().await;

    ctx.client.execute("begin", &[]).await.expect("begin a");
    ctx.client
        .query("select id from msgs for update", &[])
        .await
        .expect("lock row");

    let rows_b = fetch_ids(&client_b, "select id from msgs for update skip locked").await;
    assert!(
        rows_b.is_empty(),
        "row should be locked until transaction ends"
    );

    ctx.client
        .execute("rollback", &[])
        .await
        .expect("rollback a");

    let rows_after = fetch_ids(&client_b, "select id from msgs for update").await;
    assert_eq!(
        rows_after,
        vec![1],
        "lock should be released after rollback"
    );
    let _ = ctx.shutdown.send(());
}

#[tokio::test(flavor = "multi_thread")]
async fn join_with_for_update_not_supported() {
    let ctx = common::start().await;
    ctx.client
        .execute("create table parents(id int primary key)", &[])
        .await
        .expect("create parents");
    ctx.client
        .execute(
            "create table children(id int primary key, parent_id int references parents(id))",
            &[],
        )
        .await
        .expect("create children");
    ctx.client
        .execute("insert into parents values (1)", &[])
        .await
        .expect("insert parent");
    ctx.client
        .execute("insert into children values (1, 1)", &[])
        .await
        .expect("insert child");

    let err = ctx
        .client
        .query("select parents.id from parents, children for update", &[])
        .await
        .expect_err("joins with FOR UPDATE should be rejected");
    common::assert_db_error_contains(&err, "FOR UPDATE is only supported for single-table SELECT");

    let _ = ctx.shutdown.send(());
}

#[tokio::test(flavor = "multi_thread")]
async fn repeat_select_returns_same_rows_for_owner() {
    let ctx = common::start().await;
    ctx.client
        .execute("create table queue(id int primary key)", &[])
        .await
        .expect("create queue");
    ctx.client
        .execute("insert into queue values (1), (2)", &[])
        .await
        .expect("seed queue");
    ctx.client.execute("begin", &[]).await.expect("begin queue");

    let first = fetch_ids(
        &ctx.client,
        "select id from queue order by id for update skip locked",
    )
    .await;
    let second = fetch_ids(
        &ctx.client,
        "select id from queue order by id for update skip locked",
    )
    .await;
    assert_eq!(first, second, "same session should see locked rows");

    ctx.client
        .execute("rollback", &[])
        .await
        .expect("rollback queue txn");
    let _ = ctx.shutdown.send(());
}

#[tokio::test(flavor = "multi_thread")]
async fn statement_locks_are_released_after_batch_failure() {
    let ctx = common::start().await;
    ctx.client
        .execute("create table batch_lock_fail(id int primary key)", &[])
        .await
        .expect("create table");
    ctx.client
        .execute("insert into batch_lock_fail values (1)", &[])
        .await
        .expect("seed row");
    let client_b = ctx.new_client().await;

    let _ = ctx
        .client
        .simple_query(
            "select id from batch_lock_fail for update skip locked; \
             insert into batch_lock_fail values ('bad')",
        )
        .await
        .expect_err("second statement should fail");

    let rows_b = fetch_ids(
        &client_b,
        "select id from batch_lock_fail for update skip locked",
    )
    .await;
    assert_eq!(
        rows_b,
        vec![1],
        "lock from first statement should be released even when a later batch statement fails"
    );

    let _ = ctx.shutdown.send(());
}

async fn fetch_ids(client: &tokio_postgres::Client, sql: &str) -> Vec<i32> {
    client
        .query(sql, &[])
        .await
        .expect("query rows")
        .iter()
        .map(|row| row.get(0))
        .collect()
}