bsql 0.27.0

Safe SQL for Rust — if it compiles, the SQL is correct
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Integration tests: LISTEN/NOTIFY via Listener.
//!
//! Requires a running PostgreSQL.
//! Set BSQL_DATABASE_URL=postgres://bsql:bsql@localhost/bsql_test

use bsql::{BsqlError, Listener};
use std::sync::atomic::{AtomicU64, Ordering};

const DB_URL: &str = "postgres://bsql:bsql@localhost/bsql_test";

/// Generate a unique channel name to prevent cross-test interference.
/// PG delivers NOTIFY to ALL sessions that LISTEN on the same channel,
/// so parallel tests must use distinct names.
fn unique_channel(prefix: &str) -> String {
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    format!("{}_{}", prefix, COUNTER.fetch_add(1, Ordering::Relaxed))
}

#[tokio::test]
async fn listen_and_receive_notification() {
    let ch = unique_channel("test_channel");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    // Send a notification from the same listener connection
    listener.notify(&ch, "hello world").await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.channel(), ch);
    assert_eq!(notif.payload(), "hello world");
}

#[tokio::test]
async fn notification_payload_preserved() {
    let ch = unique_channel("payload_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    let payload = r#"{"event":"created","id":42}"#;
    listener.notify(&ch, payload).await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.payload(), payload);
}

#[tokio::test]
async fn multiple_channels() {
    let ch_a = unique_channel("chan_a");
    let ch_b = unique_channel("chan_b");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch_a).await.unwrap();
    listener.listen(&ch_b).await.unwrap();

    // notify() now uses a separate short-lived connection internally,
    // avoiding the self-notification race condition entirely.
    listener.notify(&ch_a, "from_a").await.unwrap();
    listener.notify(&ch_b, "from_b").await.unwrap();

    let n1 = listener.recv().await.unwrap();
    let n2 = listener.recv().await.unwrap();

    // Both notifications received (order not guaranteed by PG)
    let mut channels: Vec<&str> = vec![n1.channel(), n2.channel()];
    channels.sort();
    let mut expected_channels = vec![ch_a.as_str(), ch_b.as_str()];
    expected_channels.sort();
    assert_eq!(channels, expected_channels);

    let mut payloads: Vec<&str> = vec![n1.payload(), n2.payload()];
    payloads.sort();
    assert_eq!(payloads, vec!["from_a", "from_b"]);
}

#[tokio::test]
async fn unlisten_stops_receiving() {
    let ch = unique_channel("unlisten_test");
    let ch_control = unique_channel("unlisten_control");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();
    listener.unlisten(&ch).await.unwrap();

    // Send a notification -- should NOT be received since we unlistened
    listener.notify(&ch, "should_not_arrive").await.unwrap();

    // Listen on a different channel and send there to prove recv works
    listener.listen(&ch_control).await.unwrap();
    listener.notify(&ch_control, "control").await.unwrap();

    let notif = listener.recv().await.unwrap();
    // We should receive the control notification, not the unlistened one
    assert_eq!(notif.channel(), ch_control);
    assert_eq!(notif.payload(), "control");
}

#[tokio::test]
async fn unlisten_all() {
    let ch_a = unique_channel("all_a");
    let ch_b = unique_channel("all_b");
    let ch_control = unique_channel("all_control");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch_a).await.unwrap();
    listener.listen(&ch_b).await.unwrap();
    listener.unlisten_all().await.unwrap();

    // Neither channel should receive
    listener.notify(&ch_a, "no").await.unwrap();
    listener.notify(&ch_b, "no").await.unwrap();

    // Listen on a control channel
    listener.listen(&ch_control).await.unwrap();
    listener.notify(&ch_control, "yes").await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.channel(), ch_control);
}

#[tokio::test]
async fn empty_channel_name_rejected() {
    let listener = Listener::connect(DB_URL).await.unwrap();
    let result = listener.listen("").await;

    assert!(result.is_err());
    match result.unwrap_err() {
        BsqlError::Connect(e) => {
            assert!(
                e.message.contains("must not be empty"),
                "unexpected: {}",
                e.message
            );
        }
        other => panic!("expected Connect error, got: {other:?}"),
    }
}

#[tokio::test]
async fn empty_payload_notification() {
    let ch = unique_channel("empty_payload");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    listener.notify(&ch, "").await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.channel(), ch);
    assert_eq!(notif.payload(), "");
}

#[tokio::test]
async fn channel_name_with_special_chars() {
    let ch = unique_channel("my-channel.v2");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    // Channel with dashes and dots -- valid PG identifier when quoted
    listener.listen(&ch).await.unwrap();

    listener.notify(&ch, "special").await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.channel(), ch);
    assert_eq!(notif.payload(), "special");
}

#[tokio::test]
async fn payload_with_single_quotes() {
    let ch = unique_channel("quote_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    listener.notify(&ch, "it's a test").await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.payload(), "it's a test");
}

#[tokio::test]
async fn connect_bad_url_fails() {
    let result = Listener::connect("postgres://nobody:wrong@localhost:1/nope").await;
    assert!(result.is_err());
    match result.unwrap_err() {
        BsqlError::Connect(e) => {
            assert!(
                e.message.contains("listener connect failed"),
                "unexpected: {}",
                e.message
            );
        }
        other => panic!("expected Connect error, got: {other:?}"),
    }
}

#[tokio::test]
async fn notification_is_clone() {
    let ch = unique_channel("clone_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    listener.notify(&ch, "data").await.unwrap();

    let notif = listener.recv().await.unwrap();
    let cloned = notif.clone();
    assert_eq!(cloned.channel(), notif.channel());
    assert_eq!(cloned.payload(), notif.payload());
}

#[tokio::test]
async fn receive_notify_from_separate_connection() {
    let ch = unique_channel("cross_conn_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    // Send from a separate connection -- different PG backend than the listener
    let sender = Listener::connect(DB_URL).await.unwrap();
    sender.notify(&ch, "from_sender").await.unwrap();

    // recv() blocks until a notification arrives (sync API)
    let n = listener.recv().await.unwrap();

    assert_eq!(n.channel(), ch);
    assert_eq!(n.payload(), "from_sender");
}

#[tokio::test]
async fn null_byte_in_channel_rejected() {
    let listener = Listener::connect(DB_URL).await.unwrap();
    let result = listener.listen("chan\0nel").await;
    assert!(result.is_err());
    match result.unwrap_err() {
        BsqlError::Connect(e) => {
            assert!(
                e.message.contains("null bytes"),
                "unexpected: {}",
                e.message
            );
        }
        other => panic!("expected Connect error, got: {other:?}"),
    }
}

#[tokio::test]
async fn null_byte_in_payload_rejected() {
    let ch = unique_channel("null_payload_test");
    let listener = Listener::connect(DB_URL).await.unwrap();
    let result = listener.notify(&ch, "pay\0load").await;
    assert!(result.is_err());
    match result.unwrap_err() {
        BsqlError::Connect(e) => {
            assert!(
                e.message.contains("null bytes"),
                "unexpected: {}",
                e.message
            );
        }
        other => panic!("expected Connect error, got: {other:?}"),
    }
}

#[tokio::test]
async fn channel_name_sql_injection_attempt() {
    // Attempt SQL injection via channel name -- should be safely quoted
    let listener = Listener::connect(DB_URL).await.unwrap();
    let result = listener.listen(r#"test"; DROP TABLE users; --"#).await;

    // This should succeed (the channel name is just a weird identifier)
    // OR it should fail with a PG error, but NOT actually drop the table
    if result.is_ok() {
        // Verify users table still exists
        let pool = bsql::Pool::connect(DB_URL).await.unwrap();
        let users = bsql::query!("SELECT id FROM users LIMIT 1")
            .fetch_optional(&pool)
            .await;
        assert!(users.is_ok(), "users table should still exist");
    }
    // If it errored, that's also fine -- the point is no injection
}

#[tokio::test]
async fn listener_drop_cleans_up() {
    {
        let ch = unique_channel("drop_test");
        let listener = Listener::connect(DB_URL).await.unwrap();
        listener.listen(&ch).await.unwrap();
        // listener dropped here -- should not panic or leak
    }
    // If we got here, drop succeeded
}

#[tokio::test]
async fn listener_debug_format() {
    let listener = Listener::connect(DB_URL).await.unwrap();
    let debug = format!("{:?}", listener);
    assert!(debug.contains("Listener"), "debug: {debug}");
    assert!(debug.contains("active"), "debug: {debug}");
}

#[tokio::test]
async fn unlisten_empty_name_rejected() {
    let listener = Listener::connect(DB_URL).await.unwrap();
    let result = listener.unlisten("").await;
    assert!(result.is_err());
    match result.unwrap_err() {
        BsqlError::Connect(e) => {
            assert!(
                e.message.contains("must not be empty"),
                "unexpected: {}",
                e.message
            );
        }
        other => panic!("expected Connect error, got: {other:?}"),
    }
}

#[tokio::test]
async fn notify_empty_channel_rejected() {
    let listener = Listener::connect(DB_URL).await.unwrap();
    let result = listener.notify("", "payload").await;
    assert!(result.is_err());
    match result.unwrap_err() {
        BsqlError::Connect(e) => {
            assert!(
                e.message.contains("must not be empty"),
                "unexpected: {}",
                e.message
            );
        }
        other => panic!("expected Connect error, got: {other:?}"),
    }
}

#[tokio::test]
async fn channel_name_with_double_quotes() {
    let ch = unique_channel(r#"my"chan"#);
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    // Channel name with embedded double quotes -- tests quote_ident escaping
    listener.listen(&ch).await.unwrap();
    listener.notify(&ch, "quoted").await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.channel(), ch);
    assert_eq!(notif.payload(), "quoted");
}

#[tokio::test]
async fn payload_with_multiple_quotes() {
    let ch = unique_channel("multi_quote_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    let payload = "it''s a ''test''";
    listener.notify(&ch, payload).await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.payload(), payload);
}

#[tokio::test]
async fn payload_with_backslash() {
    let ch = unique_channel("backslash_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    let payload = r"C:\Users\test\file.txt";
    listener.notify(&ch, payload).await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.payload(), payload);
}

#[tokio::test]
async fn payload_with_lone_quote() {
    let ch = unique_channel("lone_quote_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    let payload = "it's";
    listener.notify(&ch, payload).await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.payload(), payload);
}

#[tokio::test]
async fn large_payload() {
    let ch = unique_channel("large_payload_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    // PG NOTIFY payloads can be up to ~8000 bytes
    let payload = "x".repeat(4000);
    listener.notify(&ch, &payload).await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.payload().len(), 4000);
}

// ---------------------------------------------------------------------------
// edge case: listen same channel twice (idempotent)
// ---------------------------------------------------------------------------

#[tokio::test]
async fn listen_same_channel_twice() {
    let ch = unique_channel("dup_listen_ch");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();
    // Second listen on the same channel should not error (PG LISTEN is idempotent).
    listener.listen(&ch).await.unwrap();

    // Sending one notification should produce exactly one received message.
    listener.notify(&ch, "once").await.unwrap();

    let notif = listener.recv().await.unwrap();
    assert_eq!(notif.channel(), ch);
    assert_eq!(notif.payload(), "once");

    // Verify there is no duplicate notification waiting.
    let maybe = listener.try_recv().await.unwrap();
    assert!(
        maybe.is_none(),
        "should not receive a duplicate notification"
    );
}

// ---------------------------------------------------------------------------
// edge case: unlisten a channel that was never listened
// ---------------------------------------------------------------------------

#[tokio::test]
async fn unlisten_never_listened_channel() {
    let ch = unique_channel("never_listened_ch");
    let listener = Listener::connect(DB_URL).await.unwrap();
    // PG UNLISTEN on a channel we never LISTENed should not error.
    let result = listener.unlisten(&ch).await;
    assert!(
        result.is_ok(),
        "unlisten on never-listened channel should succeed"
    );
}

// ---------------------------------------------------------------------------
// edge case: try_recv when no notifications pending
// ---------------------------------------------------------------------------

#[tokio::test]
async fn try_recv_empty() {
    let ch = unique_channel("try_recv_empty_ch");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    // No notifications have been sent — try_recv should return None.
    let result = listener.try_recv().await.unwrap();
    assert!(
        result.is_none(),
        "try_recv with no pending notifications should return None"
    );
}

// ---------------------------------------------------------------------------
// subscribed_channels
// ---------------------------------------------------------------------------

#[tokio::test]
async fn subscribed_channels_returns_list() {
    let ch_a = unique_channel("sub_ch_a");
    let ch_b = unique_channel("sub_ch_b");
    let listener = Listener::connect(DB_URL).await.unwrap();

    // Before any listen, subscribed_channels should be empty
    let channels = listener.subscribed_channels();
    assert!(channels.is_empty());

    // Listen to two channels
    listener.listen(&ch_a).await.unwrap();
    listener.listen(&ch_b).await.unwrap();

    let mut channels = listener.subscribed_channels();
    channels.sort();
    let mut expected = vec![ch_a.as_str(), ch_b.as_str()];
    expected.sort();
    assert_eq!(channels, expected);
}

#[tokio::test]
async fn subscribed_channels_updates_on_unlisten() {
    let ch_a = unique_channel("sub_ul_a");
    let ch_b = unique_channel("sub_ul_b");
    let ch_c = unique_channel("sub_ul_c");
    let listener = Listener::connect(DB_URL).await.unwrap();

    listener.listen(&ch_a).await.unwrap();
    listener.listen(&ch_b).await.unwrap();
    listener.listen(&ch_c).await.unwrap();

    let mut channels = listener.subscribed_channels();
    channels.sort();
    let mut expected_abc = vec![ch_a.as_str(), ch_b.as_str(), ch_c.as_str()];
    expected_abc.sort();
    assert_eq!(channels, expected_abc);

    listener.unlisten(&ch_b).await.unwrap();

    let mut channels = listener.subscribed_channels();
    channels.sort();
    let mut expected_ac = vec![ch_a.as_str(), ch_c.as_str()];
    expected_ac.sort();
    assert_eq!(channels, expected_ac);
}

#[tokio::test]
async fn subscribed_channels_empty_after_unlisten_all() {
    let ch_a = unique_channel("sub_ua_a");
    let ch_b = unique_channel("sub_ua_b");
    let listener = Listener::connect(DB_URL).await.unwrap();

    listener.listen(&ch_a).await.unwrap();
    listener.listen(&ch_b).await.unwrap();
    assert_eq!(listener.subscribed_channels().len(), 2);

    listener.unlisten_all().await.unwrap();
    assert!(listener.subscribed_channels().is_empty());
}

#[tokio::test]
async fn subscribed_channels_idempotent_listen() {
    let ch = unique_channel("sub_idem");
    let listener = Listener::connect(DB_URL).await.unwrap();

    listener.listen(&ch).await.unwrap();
    listener.listen(&ch).await.unwrap(); // duplicate

    let channels = listener.subscribed_channels();
    // Should have exactly 1 entry, not 2
    assert_eq!(channels.len(), 1);
    assert_eq!(channels[0], ch);
}

// ---------------------------------------------------------------------------
// Listener edge cases: unlisten then re-listen, unlisten_all then listen new
// ---------------------------------------------------------------------------

#[tokio::test]
async fn listener_unlisten_then_relisten() {
    let ch = unique_channel("relisten_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();

    listener.listen(&ch).await.unwrap();
    listener.unlisten(&ch).await.unwrap();
    listener.listen(&ch).await.unwrap(); // re-subscribe

    // Should receive on re-subscribed channel
    let sender = Listener::connect(DB_URL).await.unwrap();
    sender.notify(&ch, "relisten_test").await.unwrap();

    let notification = listener.recv().await.unwrap();

    assert_eq!(notification.channel(), ch);
    assert_eq!(notification.payload(), "relisten_test");
}

#[tokio::test]
async fn listener_unlisten_all_then_listen_new() {
    let ch1 = unique_channel("ua_old");
    let ch2 = unique_channel("ua_new");
    let mut listener = Listener::connect(DB_URL).await.unwrap();

    listener.listen(&ch1).await.unwrap();
    listener.unlisten_all().await.unwrap();
    listener.listen(&ch2).await.unwrap();

    // Notify on ch2 — should receive
    let sender = Listener::connect(DB_URL).await.unwrap();
    sender.notify(&ch2, "after_unlisten_all").await.unwrap();

    let notification = listener.recv().await.unwrap();

    assert_eq!(notification.channel(), ch2);
    assert_eq!(notification.payload(), "after_unlisten_all");
}

// ---------------------------------------------------------------------------
// Notification burst: send 100 notifications rapidly, verify none are lost
// ---------------------------------------------------------------------------

#[tokio::test]
async fn listener_notification_burst() {
    let ch = unique_channel("burst_test");
    let mut listener = Listener::connect(DB_URL).await.unwrap();
    listener.listen(&ch).await.unwrap();

    // Send 100 notifications in rapid succession from a separate connection
    let sender = Listener::connect(DB_URL).await.unwrap();
    for i in 0..100u32 {
        sender.notify(&ch, &i.to_string()).await.unwrap();
    }

    // Receive all 100 with a deadline
    let mut received = 0u32;
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
    while received < 100 {
        if std::time::Instant::now() >= deadline {
            break;
        }
        // try_recv to avoid blocking forever; if nothing yet, yield and retry
        match listener.try_recv().await {
            Ok(Some(notif)) => {
                assert_eq!(notif.channel(), ch);
                received += 1;
            }
            Ok(None) => {
                // No notification ready yet — brief yield then retry
                tokio::task::yield_now().await;
            }
            Err(e) => panic!("recv error: {e}"),
        }
    }
    assert_eq!(
        received, 100,
        "all 100 notifications should be received, got {received}"
    );
}