giong-postgres 1.5.6

A PostgreSQL client and connection pool for the giong thread-per-core runtime
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
//! The per-worker pool.

use std::cell::Cell;
use std::rc::Rc;
use std::time::Duration;

use giong::time::{sleep, timeout, Instant};
use giong_postgres::{Client, ErrorKind, Pool};

use crate::run;

#[test]
fn shared_statements_spread_over_connections_up_to_the_maximum() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 3);
        assert_eq!(pool.stats().connections, 0, "connections open on demand");
        let tasks: Vec<_> = (0..100i32)
            .map(|i| {
                let pool = pool.clone();
                giong::spawn(async move {
                    let row = pool.query_one("SELECT $1::int4 + 1", &[&i]).await.unwrap();
                    row.get::<i32>(0).unwrap()
                })
            })
            .collect();
        for (i, t) in tasks.into_iter().enumerate() {
            assert_eq!(t.await.unwrap(), i as i32 + 1);
        }
        let stats = pool.stats();
        assert!(
            stats.connections <= 3 && stats.connections >= 1,
            "{stats:?}"
        );
        assert_eq!(stats.opened, stats.connections as u64);
        assert_eq!(stats.in_flight, 0);
        assert_eq!(stats.taken, 0);
    });
}

#[test]
fn a_taken_connection_is_exclusive_and_comes_back_clean() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1).wait_timeout(Duration::from_millis(300));
        pool.batch_execute("DROP TABLE IF EXISTS pool_tx; CREATE TABLE pool_tx (id int)")
            .await
            .unwrap();
        {
            let mut conn = pool.get().await.unwrap();
            assert_eq!(pool.stats().taken, 1);
            // The only connection is taken: shared statements wait, then time
            // out.
            let e = pool.query("SELECT 1", &[]).await.unwrap_err();
            assert_eq!(e.kind(), ErrorKind::PoolTimeout);
            assert!(e.to_string().contains("max_size"), "{e}");
            let tx = conn.transaction().await.unwrap();
            tx.execute("INSERT INTO pool_tx VALUES (1)", &[])
                .await
                .unwrap();
            // Dropped without commit, and the connection goes back.
        }
        assert_eq!(pool.stats().taken, 0);
        let n: i64 = pool
            .query_one("SELECT count(*) FROM pool_tx", &[])
            .await
            .unwrap()
            .get(0)
            .unwrap();
        assert_eq!(n, 0, "rolled back");

        // A transaction left open by hand is rolled back on return too.
        {
            let conn = pool.get().await.unwrap();
            conn.batch_execute("BEGIN; INSERT INTO pool_tx VALUES (2)")
                .await
                .unwrap();
        }
        let e = pool.batch_execute("SAVEPOINT s").await.unwrap_err();
        assert_eq!(
            e.server_code(),
            Some("25P01"),
            "the shared connection is not in a transaction: {e}"
        );
        assert_eq!(pool.stats().timeouts, 1);
    });
}

#[test]
fn a_waiting_task_gets_the_connection_when_it_is_returned() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1);
        let conn = pool.get().await.unwrap();
        let waiter = giong::spawn({
            let pool = pool.clone();
            async move { pool.get().await.map(|c| c.in_flight()) }
        });
        giong::time::sleep(Duration::from_millis(50)).await;
        assert_eq!(pool.stats().waiting, 1);
        drop(conn);
        assert_eq!(waiter.await.unwrap().unwrap(), 0);
    });
}

#[test]
fn broken_connections_are_replaced() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 2);
        let pid: i32 = pool
            .query_one("SELECT pg_backend_pid()", &[])
            .await
            .unwrap()
            .get(0)
            .unwrap();
        s.psql(&format!("SELECT pg_terminate_backend({pid})"));
        for _ in 0..100 {
            if pool.stats().connections == 0 {
                break;
            }
            // Looking at the pool drops connections that have closed.
            let _ = pool.get().await.map(drop);
            giong::time::sleep(Duration::from_millis(10)).await;
        }
        let other: i32 = pool
            .query_one("SELECT pg_backend_pid()", &[])
            .await
            .unwrap()
            .get(0)
            .unwrap();
        assert_ne!(other, pid);
    });
}

#[test]
fn connect_errors_are_reported_and_counted() {
    run(|s| async move {
        let pool = Pool::new(s.local().database("missing_db"), 2);
        let e = pool.query("SELECT 1", &[]).await.unwrap_err();
        assert_eq!(e.server_code(), Some("3D000"), "{e}");
        assert_eq!(pool.stats().connect_errors, 1);
        assert_eq!(pool.stats().connections, 0);
    });
}

#[test]
fn a_slow_statement_does_not_hold_up_the_ones_behind_it() {
    run(|s| async move {
        // The server ends the slow statement after 2 s.
        let pool = Pool::new(s.local().statement_timeout(Duration::from_secs(2)), 2);
        // Open both connections.
        let warm: Vec<_> = (0..2)
            .map(|_| {
                let pool = pool.clone();
                giong::spawn(async move { pool.execute("SELECT pg_sleep(0.05)", &[]).await })
            })
            .collect();
        for t in warm {
            t.await.unwrap().unwrap();
        }
        assert_eq!(pool.stats().connections, 2);

        let slow = giong::spawn({
            let pool = pool.clone();
            async move { pool.execute("SELECT pg_sleep(10)", &[]).await }
        });
        giong::time::sleep(Duration::from_millis(50)).await;
        // Many at once, so the connection with the slow statement is the
        // least busy by count.
        let started = giong::time::Instant::now();
        let quick: Vec<_> = (0..20i32)
            .map(|i| {
                let pool = pool.clone();
                giong::spawn(async move {
                    let row = pool.query_one("SELECT $1::int4", &[&i]).await.unwrap();
                    assert_eq!(row.get::<i32>(0).unwrap(), i);
                })
            })
            .collect();
        for t in quick {
            t.await.unwrap();
        }
        let took = started.elapsed();
        // Generous, for slow runs under valgrind or ThreadSanitizer; behind
        // the slow statement they would take 2 s.
        assert!(
            took < Duration::from_secs(1),
            "the quick statements waited behind the slow one: {took:?}"
        );
        let e = slow.await.unwrap().unwrap_err();
        assert_eq!(e.server_code(), Some("57014"), "{e}");
    });
}

#[test]
fn transactions_never_run_on_shared_connections() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1);
        pool.batch_execute("DROP TABLE IF EXISTS pool_leak; CREATE TABLE pool_leak (id int)")
            .await
            .unwrap();
        // Refused before anything is sent.
        for sql in [
            "BEGIN",
            "  /* a /* nested */ comment */ begin isolation level serializable",
            "-- note\nSTART TRANSACTION",
            "commit",
            "END",
            "ROLLBACK",
            "abort",
            "SAVEPOINT s",
            "RELEASE SAVEPOINT s",
            "PREPARE TRANSACTION 'x'",
        ] {
            let e = pool.execute(sql, &[]).await.unwrap_err();
            assert_eq!(e.kind(), ErrorKind::Statement, "{sql:?}: {e}");
            assert!(e.to_string().contains("pool.get()"), "{e}");
            let e = pool.query(sql, &[]).await.unwrap_err();
            assert_eq!(e.kind(), ErrorKind::Statement, "{sql:?}: {e}");
        }
        // Not transaction statements, though they look a little like them.
        let e = pool
            .execute("COMMIT PREPARED 'nothing'", &[])
            .await
            .unwrap_err();
        assert_ne!(e.kind(), ErrorKind::Statement, "{e}");
        pool.execute("SELECT 1 AS begin", &[]).await.unwrap();

        // A whole transaction in one batch is fine.
        pool.batch_execute("BEGIN; INSERT INTO pool_leak VALUES (1); COMMIT")
            .await
            .unwrap();

        // A batch that leaves one open closes its connection: the other
        // task's insert, pipelined behind it on the only connection, would
        // otherwise have run inside it and never been committed.
        let leak = giong::spawn({
            let pool = pool.clone();
            async move {
                pool.batch_execute("BEGIN; INSERT INTO pool_leak VALUES (2)")
                    .await
            }
        });
        let other = giong::spawn({
            let pool = pool.clone();
            async move { pool.execute("INSERT INTO pool_leak VALUES (3)", &[]).await }
        });
        let e = leak.await.unwrap().unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Closed, "{e}");
        assert!(e.to_string().contains("left a transaction open"), "{e}");
        let e = other.await.unwrap().unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Closed, "{e}");

        // Both rolled back; the pool goes on with a new connection.
        let n: i64 = pool
            .query_one("SELECT count(*) FROM pool_leak", &[])
            .await
            .unwrap()
            .get(0)
            .unwrap();
        assert_eq!(n, 1);
        assert_eq!(pool.stats().opened, 2);
    });
}

#[test]
fn a_connection_given_back_with_begin_in_flight_is_rolled_back() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1);
        pool.batch_execute("DROP TABLE IF EXISTS pool_late; CREATE TABLE pool_late (id int)")
            .await
            .unwrap();
        {
            let conn = pool.get().await.unwrap();
            // The caller gives up before BEGIN is answered: the last answer
            // the connection has says no transaction is open.
            let late = timeout(
                Duration::from_millis(20),
                conn.batch_execute("SELECT pg_sleep(0.2); BEGIN"),
            )
            .await;
            assert!(late.is_err());
        }
        pool.execute("INSERT INTO pool_late VALUES (1)", &[])
            .await
            .unwrap();
        // Committed: another session sees it.
        let other = Client::connect(&s.local()).await.unwrap();
        let n: i64 = other
            .query_one("SELECT count(*) FROM pool_late", &[])
            .await
            .unwrap()
            .get(0)
            .unwrap();
        assert_eq!(n, 1);
    });
}

#[test]
fn a_waiting_task_hears_when_a_dropped_statement_frees_the_connection() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1).wait_timeout(Duration::from_secs(3));
        pool.execute("SELECT 1", &[]).await.unwrap();
        // Dropped while the server still runs it.
        let dropped = timeout(
            Duration::from_millis(20),
            pool.execute("SELECT pg_sleep(0.3)", &[]),
        )
        .await;
        assert!(dropped.is_err());
        // A task of its own, polled only when woken.
        let waiter = giong::spawn({
            let pool = pool.clone();
            async move {
                let started = Instant::now();
                let conn = pool.get().await?;
                Ok::<_, giong_postgres::Error>((started.elapsed(), conn.in_flight()))
            }
        });
        let (waited, in_flight) = waiter.await.unwrap().unwrap();
        assert_eq!(in_flight, 0);
        // Free after about 0.3 s; nothing announced it, so the wait ran
        // into the timeout.
        assert!(waited < Duration::from_secs(2), "waited {waited:?}");
        sleep(Duration::from_millis(1)).await;
    });
}

#[test]
fn a_connection_comes_back_from_get_with_a_clean_session() {
    run(|s| async move {
        s.psql(
            "DROP ROLE IF EXISTS pool_tenant; CREATE ROLE pool_tenant; \
             DROP SCHEMA IF EXISTS pool_tenant_b CASCADE; CREATE SCHEMA pool_tenant_b",
        );
        let pool = Pool::new(s.local().param("search_path", "public"), 1);
        {
            let conn = pool.get().await.unwrap();
            conn.batch_execute(
                "SET ROLE pool_tenant; SET search_path = pool_tenant_b; \
                 SET statement_timeout = 0; SET work_mem = '77MB'; \
                 CREATE TEMP TABLE pool_scratch (a int); LISTEN pool_jobs; \
                 PREPARE pool_mine AS SELECT 1; \
                 BEGIN; DECLARE pool_cursor CURSOR WITH HOLD FOR SELECT 1; COMMIT",
            )
            .await
            .unwrap();
            conn.query_one("SELECT pg_advisory_lock(42)", &[])
                .await
                .unwrap();
        }
        // The next shared statement runs after the reset, on the same and
        // only connection.
        let row = pool
            .query_one(
                "SELECT current_user::text, current_setting('search_path'), \
                 current_setting('statement_timeout'), current_setting('work_mem'), \
                 (SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' \
                  AND pid = pg_backend_pid()), \
                 (SELECT count(*) FROM pg_listening_channels()), \
                 (SELECT count(*) FROM pg_cursors WHERE name = 'pool_cursor'), \
                 (SELECT count(*) FROM pg_prepared_statements WHERE name = 'pool_mine'), \
                 (SELECT count(*) FROM pg_class WHERE relname = 'pool_scratch')",
                &[],
            )
            .await
            .unwrap();
        assert_eq!(row.get::<&str>(0).unwrap(), "postgres");
        // Back to the value Config::param gave, not the server's default.
        assert_eq!(row.get::<&str>(1).unwrap(), "public");
        // The configured statement timeout survives RESET ALL.
        assert_eq!(row.get::<&str>(2).unwrap(), "30s");
        assert_eq!(row.get::<&str>(3).unwrap(), "4MB");
        for (i, what) in [
            (4, "advisory locks"),
            (5, "channels listened to"),
            (6, "cursors"),
            (7, "SQL-level prepared statements"),
            (8, "temporary tables"),
        ] {
            assert_eq!(row.get::<i64>(i).unwrap(), 0, "{what} left behind");
        }
        // The statement cache survived, apart from after DEALLOCATE ALL: the
        // pool's statements still run.
        for _ in 0..3 {
            pool.query_one("SELECT 1", &[]).await.unwrap();
        }
        // The next get() finds the same clean session, and may PREPARE the
        // same name again.
        let conn = pool.get().await.unwrap();
        conn.batch_execute("PREPARE pool_mine AS SELECT 2")
            .await
            .unwrap();
        assert_eq!(pool.stats().opened, 1, "one connection all along");
    });
}

#[test]
fn an_unused_connection_from_get_is_not_reset() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1);
        pool.query_one("SELECT 1", &[]).await.unwrap();
        drop(pool.get().await.unwrap());
        // Nothing queued for it.
        assert_eq!(pool.stats().in_flight, 0);
    });
}

#[test]
fn shared_methods_refuse_statements_that_change_the_session() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1);
        pool.batch_execute("DROP TABLE IF EXISTS pool_copy; CREATE TABLE pool_copy (a int)")
            .await
            .unwrap();
        for sql in [
            "SET search_path = x",
            "SET ROLE nobody",
            "set session authorization nobody",
            "RESET ALL",
            "DISCARD ALL",
            "LISTEN x",
            "PREPARE p AS SELECT 1",
            "DEALLOCATE ALL",
            "DECLARE c CURSOR WITH HOLD FOR SELECT 1",
            "CREATE TEMP TABLE t (a int)",
            "COPY pool_copy FROM STDIN",
        ] {
            let e = pool.execute(sql, &[]).await.unwrap_err();
            assert_eq!(e.kind(), ErrorKind::Statement, "{sql:?}: {e}");
            assert!(e.to_string().contains("nothing was sent"), "{e}");
            let batch = format!("SELECT 1; {sql}");
            let e = pool.batch_execute(&batch).await.unwrap_err();
            assert_eq!(e.kind(), ErrorKind::Statement, "{batch:?}: {e}");
        }
        // Transaction-scoped settings are fine, in a batch with its
        // transaction or on their own.
        pool.batch_execute(
            "BEGIN; SET LOCAL work_mem = '8MB'; INSERT INTO pool_copy VALUES (1); COMMIT",
        )
        .await
        .unwrap();
        pool.execute("SET LOCAL work_mem = '8MB'", &[])
            .await
            .unwrap();
        // A COPY in one task never fails another's statement: it is refused
        // before anything is sent.
        let other = giong::spawn({
            let pool = pool.clone();
            async move { pool.query_one("SELECT 1", &[]).await.map(|_| ()) }
        });
        assert!(pool
            .batch_execute("COPY pool_copy FROM STDIN")
            .await
            .is_err());
        other.await.unwrap().unwrap();
        assert_eq!(pool.stats().opened, 1);
    });
}

/// Tasks running shared statements back to back until `stop`, counting them.
fn hammer(pool: &Pool, tasks: usize) -> (Rc<Cell<bool>>, Rc<Cell<u64>>) {
    let stop = Rc::new(Cell::new(false));
    let done = Rc::new(Cell::new(0u64));
    for _ in 0..tasks {
        let (pool, stop, done) = (pool.clone(), stop.clone(), done.clone());
        giong::spawn(async move {
            while !stop.get() {
                pool.query_one("SELECT 1", &[]).await.unwrap();
                done.set(done.get() + 1);
            }
        });
    }
    (stop, done)
}

#[test]
fn get_is_served_while_shared_statements_keep_every_connection_busy() {
    run(|s| async move {
        for size in [1, 2] {
            let pool = Pool::new(s.local(), size).wait_timeout(Duration::from_secs(5));
            let (stop, done) = hammer(&pool, 32);
            sleep(Duration::from_millis(100)).await;
            for _ in 0..5 {
                let started = Instant::now();
                let before = done.get();
                let conn = pool.get().await.unwrap();
                let waited = started.elapsed();
                conn.query_one("SELECT 1", &[]).await.unwrap();
                drop(conn);
                // Before the fix every get() here timed out, while hundreds
                // of thousands of shared statements ran.
                assert!(
                    waited < Duration::from_secs(1),
                    "pool of {size}: waited {waited:?}"
                );
                assert!(
                    done.get() > before || size == 1,
                    "shared statements went on"
                );
                sleep(Duration::from_millis(20)).await;
            }
            stop.set(true);
            assert_eq!(pool.stats().timeouts, 0);
        }
    });
}

#[test]
fn shared_statements_keep_a_connection_while_gets_queue_up() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 2).wait_timeout(Duration::from_secs(10));
        let (stop, done) = hammer(&pool, 8);
        sleep(Duration::from_millis(50)).await;
        // More tasks want a connection of their own than the pool has, each
        // holding it a while.
        let order = Rc::new(std::cell::RefCell::new(Vec::new()));
        let gets: Vec<_> = (0..4)
            .map(|i| {
                let (pool, order) = (pool.clone(), order.clone());
                giong::spawn(async move {
                    let conn = pool.get().await.unwrap();
                    order.borrow_mut().push(i);
                    conn.query_one("SELECT pg_sleep(0.1)", &[]).await.unwrap();
                })
            })
            .collect();
        sleep(Duration::from_millis(150)).await;
        let before = done.get();
        sleep(Duration::from_millis(100)).await;
        assert!(
            done.get() > before,
            "shared statements stopped while get() calls waited"
        );
        for g in gets {
            g.await.unwrap();
        }
        // Served in the order they came.
        assert_eq!(*order.borrow(), [0, 1, 2, 3]);
        stop.set(true);
    });
}

#[test]
fn a_cancel_token_stops_working_when_its_connection_goes_back() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1);
        let token = {
            let conn = pool.get().await.unwrap();
            let token = conn.cancel_token();
            // While the connection is out, the token works.
            let cancel = giong::spawn({
                let token = token.clone();
                async move {
                    sleep(Duration::from_millis(100)).await;
                    token.cancel().await
                }
            });
            let slow = conn.query("SELECT pg_sleep(5)", &[]).await;
            cancel.await.unwrap().unwrap();
            assert_eq!(slow.unwrap_err().server_code(), Some("57014"));
            token
        };
        // Back in the pool, where another task runs a statement on it.
        let other = giong::spawn({
            let pool = pool.clone();
            async move { pool.query("SELECT pg_sleep(0.3)", &[]).await.map(|_| ()) }
        });
        sleep(Duration::from_millis(100)).await;
        let e = token.cancel().await.unwrap_err();
        assert_eq!(e.kind(), ErrorKind::Closed, "{e}");
        assert!(e.to_string().contains("gone back to its pool"), "{e}");
        other.await.unwrap().unwrap();
    });
}

#[test]
fn a_connection_goes_back_closed_while_a_cancel_from_its_lease_is_on_its_way() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1);
        let pid = |row: giong_postgres::Row| row.get::<i32>(0).unwrap();
        // The cancel must still be on its way when the connection goes back.
        // Its first poll connects, sends the request and reads until the
        // server closes; on a readiness driver a loopback connect and send
        // can finish at once, and the server can read and close before this
        // process looks (#389, under tsan). Then the cancel is over before the
        // lease ends, the connection may rightly be lent again, and the try
        // proves nothing: it runs again, the connection back in the pool.
        for attempt in 1.. {
            let conn = pool.get().await.unwrap();
            let first = pid(conn
                .query_one("SELECT pg_backend_pid()", &[])
                .await
                .unwrap());
            let token = conn.cancel_token();
            // The cancel passes the check and starts connecting...
            let mut cancel = std::pin::pin!(token.cancel());
            let polled = std::future::poll_fn(|cx| {
                std::task::Poll::Ready(std::future::Future::poll(cancel.as_mut(), cx))
            })
            .await;
            if let std::task::Poll::Ready(sent) = polled {
                sent.unwrap();
                drop(conn);
                assert!(
                    attempt < 50,
                    "every cancel finished in its first poll, so none was on its way"
                );
                continue;
            }
            // ...and the connection goes back before the request arrives: the
            // next task must not get that connection.
            drop(conn);
            let next = pool.get().await.unwrap();
            let second = pid(next
                .query_one("SELECT pg_backend_pid()", &[])
                .await
                .unwrap());
            assert_ne!(
                second, first,
                "a connection a cancel request is on its way to"
            );
            cancel.await.unwrap();
            next.query("SELECT pg_sleep(0.2)", &[]).await.unwrap();
            break;
        }
    });
}

#[test]
fn idle_connections_are_closed() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 3).idle_timeout(Duration::from_millis(200));
        // A burst opens all three.
        let held: Vec<_> = (0..3).map(|_| pool.get()).collect();
        let mut conns = Vec::new();
        for get in held {
            conns.push(get.await.unwrap());
        }
        for conn in &conns {
            conn.query_one("SELECT 1", &[]).await.unwrap();
        }
        drop(conns);
        assert_eq!(pool.stats().connections, 3);
        let started = Instant::now();
        while pool.stats().connections > 0 {
            assert!(
                started.elapsed() < Duration::from_secs(5),
                "{:?}",
                pool.stats()
            );
            sleep(Duration::from_millis(20)).await;
        }
        let stats = pool.stats();
        assert_eq!(stats.recycled, stats.opened);
        // And opened again when needed.
        pool.query_one("SELECT 1", &[]).await.unwrap();
        assert_eq!(pool.stats().connections, 1);
    });
}

#[test]
fn old_connections_are_replaced_even_under_load() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 2).max_lifetime(Duration::from_millis(200));
        let (stop, done) = hammer(&pool, 16);
        let pid = |pool: Pool| async move {
            let row = pool
                .query_one("SELECT pg_backend_pid()", &[])
                .await
                .unwrap();
            row.get::<i32>(0).unwrap()
        };
        let first = pid(pool.clone()).await;
        sleep(Duration::from_millis(600)).await;
        let stats = pool.stats();
        assert!(stats.recycled >= 2, "{stats:?}");
        assert!(stats.connections <= 2, "{stats:?}");
        let before = done.get();
        sleep(Duration::from_millis(50)).await;
        assert!(done.get() > before, "statements go on");
        let mut later = Vec::new();
        for _ in 0..10 {
            later.push(pid(pool.clone()).await);
        }
        assert!(!later.contains(&first), "{first} still in use: {later:?}");
        stop.set(true);
        assert_eq!(pool.stats().timeouts, 0);
    });
}

#[test]
fn a_connection_carries_at_most_1024_statements_at_a_time() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 2).wait_timeout(Duration::from_secs(20));
        // One connection holds a lock; the statements of 1500 tasks wait for
        // it on the other.
        let holder = pool.get().await.unwrap();
        holder
            .query_one("SELECT pg_advisory_lock(7)", &[])
            .await
            .unwrap();
        let tasks: Vec<_> = (0..1500)
            .map(|_| {
                let pool = pool.clone();
                giong::spawn(async move {
                    pool.query_one("SELECT pg_advisory_xact_lock_shared(7)", &[])
                        .await
                        .map(|_| ())
                })
            })
            .collect();
        let started = Instant::now();
        while pool.stats().waiting < 1500 - 1024 {
            assert!(
                started.elapsed() < Duration::from_secs(10),
                "{:?}",
                pool.stats()
            );
            sleep(Duration::from_millis(10)).await;
        }
        let stats = pool.stats();
        assert!(stats.in_flight <= 1024, "{stats:?}");
        // Back to the pool: its reset releases the lock, and everything runs.
        drop(holder);
        for t in tasks {
            t.await.unwrap().unwrap();
        }
        assert_eq!(pool.stats().timeouts, 0);
    });
}

#[test]
fn timeouts_too_long_for_the_clock_mean_no_limit() {
    run(|s| async move {
        let pool = Pool::new(s.local(), 1)
            .idle_timeout(Duration::MAX)
            .max_lifetime(Duration::MAX);
        pool.query_one("SELECT 1", &[]).await.unwrap();
        drop(pool.get().await.unwrap());
        pool.query_one("SELECT 1", &[]).await.unwrap();
        assert_eq!(pool.stats().opened, 1);
    });
}

/// Runs `n` copies of `SELECT $1::int4 + 1` at once on `pool`, after one
/// that prepares the statement (only prepared statements join a batch).
async fn add_one_at_once(pool: &Pool, n: i32) {
    pool.query_one("SELECT $1::int4 + 1", &[&0i32])
        .await
        .unwrap();
    let tasks: Vec<_> = (0..n)
        .map(|i| {
            let pool = pool.clone();
            giong::spawn(async move {
                let row = pool.query_one("SELECT $1::int4 + 1", &[&i]).await.unwrap();
                row.get::<i32>(0).unwrap()
            })
        })
        .collect();
    for (i, t) in tasks.into_iter().enumerate() {
        assert_eq!(t.await.unwrap(), i as i32 + 1);
    }
}

#[test]
fn batched_reads_return_each_statement_its_own_rows() {
    run(|s| async move {
        let pool = Pool::new(s.local().batch_reads(true), 2);
        add_one_at_once(&pool, 500).await;
        // Several rows, and none.
        pool.query("SELECT generate_series(1, 3)", &[])
            .await
            .unwrap();
        let tasks: Vec<_> = (0..50i32)
            .map(|i| {
                let pool = pool.clone();
                giong::spawn(async move {
                    let rows = pool
                        .query("SELECT generate_series(1, $1::int4)", &[&(i % 4)])
                        .await
                        .unwrap();
                    (i, rows.len())
                })
            })
            .collect();
        for t in tasks {
            let (i, n) = t.await.unwrap();
            assert_eq!(n, (i % 4) as usize);
        }
        // Statements of one batch share one transaction, so one `now()`.
        pool.query_one("SELECT now()::text", &[]).await.unwrap();
        let tasks: Vec<_> = (0..16)
            .map(|_| {
                let pool = pool.clone();
                giong::spawn(async move {
                    let row = pool.query_one("SELECT now()::text", &[]).await.unwrap();
                    row.get::<String>(0).unwrap()
                })
            })
            .collect();
        let mut times = Vec::new();
        for t in tasks {
            times.push(t.await.unwrap());
        }
        times.sort();
        times.dedup();
        assert!(
            times.len() < 16,
            "no two statements shared a batch: {times:?}"
        );
        let stats = pool.stats();
        assert_eq!(stats.in_flight, 0, "{stats:?}");
    });
}

#[test]
fn an_error_in_a_batch_fails_only_its_own_statement() {
    run(|s| async move {
        let pool = Pool::new(s.local().batch_reads(true), 1);
        pool.query_one("SELECT 1 / $1::int4", &[&1i32])
            .await
            .unwrap();
        for round in 0..20 {
            let tasks: Vec<_> = (0..40i32)
                .map(|i| {
                    let pool = pool.clone();
                    giong::spawn(async move {
                        // Every seventh divides by zero.
                        let d = if (i + round) % 7 == 0 { 0 } else { i + 1 };
                        (d, pool.query_one("SELECT 1 / $1::int4", &[&d]).await)
                    })
                })
                .collect();
            for t in tasks {
                match t.await.unwrap() {
                    (0, r) => {
                        let e = r.unwrap_err();
                        assert_eq!(e.server_code(), Some("22012"), "{e}");
                    }
                    (d, r) => assert_eq!(r.unwrap().get::<i32>(0).unwrap(), 1 / d),
                }
            }
        }
    });
}

#[test]
fn a_select_that_writes_runs_alone_and_its_write_stays() {
    run(|s| async move {
        let pool = Pool::new(s.local().batch_reads(true), 1);
        pool.batch_execute(
            "DROP TABLE IF EXISTS batch_writes; CREATE TABLE batch_writes (id int);
             CREATE OR REPLACE FUNCTION batch_write(i int) RETURNS int
             LANGUAGE sql AS 'INSERT INTO batch_writes VALUES (i) RETURNING id'",
        )
        .await
        .unwrap();
        pool.query_one("SELECT $1::int4 + 1", &[&0i32])
            .await
            .unwrap();
        pool.query_one("SELECT batch_write($1)", &[&0i32])
            .await
            .unwrap();
        for round in 1..=5i32 {
            let tasks: Vec<_> = (0..30i32)
                .map(|i| {
                    let pool = pool.clone();
                    giong::spawn(async move {
                        let id = round * 100 + i;
                        if i % 5 == 0 {
                            let row = pool.query_one("SELECT batch_write($1)", &[&id]).await;
                            assert_eq!(row.unwrap().get::<i32>(0).unwrap(), id);
                        } else {
                            let row = pool.query_one("SELECT $1::int4 + 1", &[&id]).await;
                            assert_eq!(row.unwrap().get::<i32>(0).unwrap(), id + 1);
                        }
                    })
                })
                .collect();
            for t in tasks {
                t.await.unwrap();
            }
        }
        let row = pool
            .query_one("SELECT count(*)::int4 FROM batch_writes", &[])
            .await
            .unwrap();
        assert_eq!(row.get::<i32>(0).unwrap(), 1 + 5 * 6);
        // A locking read is a write too.
        pool.batch_execute("INSERT INTO batch_writes VALUES (-1)")
            .await
            .unwrap();
        let tasks: Vec<_> = (0..20)
            .map(|_| {
                let pool = pool.clone();
                giong::spawn(async move {
                    pool.query_one("SELECT id FROM batch_writes WHERE id = -1 FOR UPDATE", &[])
                        .await
                        .unwrap()
                        .get::<i32>(0)
                        .unwrap()
                })
            })
            .collect();
        for t in tasks {
            assert_eq!(t.await.unwrap(), -1);
        }
        // A batch's 25006 is not a standby's: the connection stays.
        assert_eq!(pool.stats().opened, 1, "{:?}", pool.stats());
    });
}

#[test]
fn batched_reads_see_writes_made_before_them() {
    run(|s| async move {
        let pool = Pool::new(s.local().batch_reads(true), 2);
        pool.batch_execute("DROP TABLE IF EXISTS batch_seen; CREATE TABLE batch_seen (id int)")
            .await
            .unwrap();
        pool.query_one("SELECT count(*)::int4 FROM batch_seen", &[])
            .await
            .unwrap();
        for i in 1..=20i32 {
            pool.execute("INSERT INTO batch_seen VALUES ($1)", &[&i])
                .await
                .unwrap();
            let tasks: Vec<_> = (0..10)
                .map(|_| {
                    let pool = pool.clone();
                    giong::spawn(async move {
                        pool.query_one("SELECT count(*)::int4 FROM batch_seen", &[])
                            .await
                            .unwrap()
                            .get::<i32>(0)
                            .unwrap()
                    })
                })
                .collect();
            for t in tasks {
                assert_eq!(t.await.unwrap(), i);
            }
        }
    });
}

#[test]
fn batches_survive_statements_leaving_the_cache() {
    run(|s| async move {
        let config = s.local().batch_reads(true).statement_cache_capacity(2);
        let pool = Pool::new(config, 1);
        for round in 0..10i32 {
            let tasks: Vec<_> = (0..60i32)
                .map(|i| {
                    let pool = pool.clone();
                    giong::spawn(async move {
                        // Five statements take turns in a cache of two.
                        let sql = format!("SELECT $1::int4 + {}", (i + round) % 5);
                        let row = pool.query_one(&sql, &[&i]).await.unwrap();
                        assert_eq!(row.get::<i32>(0).unwrap(), i + (i + round) % 5);
                    })
                })
                .collect();
            for t in tasks {
                t.await.unwrap();
            }
        }
        assert_eq!(pool.stats().in_flight, 0);
    });
}

/// Runs `a` and `b` on `pool` from two tasks started in the same tick, so
/// that both are written together: in one batch, with batches on.
async fn together(
    pool: &Pool,
    a: (&'static str, i32),
    b: (&'static str, i32),
) -> [Result<Vec<giong_postgres::Row>, giong_postgres::Error>; 2] {
    let run = |(sql, arg): (&'static str, i32)| {
        let pool = pool.clone();
        giong::spawn(async move { pool.query(sql, &[&arg]).await })
    };
    let (ta, tb) = (run(a), run(b));
    [ta.await.unwrap(), tb.await.unwrap()]
}

#[test]
fn an_error_at_a_batch_commit_fails_only_its_statement() {
    run(|s| async move {
        let pool = Pool::new(s.local().batch_reads(true), 1);
        pool.batch_execute(
            "CREATE OR REPLACE FUNCTION batch_commit_setup() RETURNS int LANGUAGE plpgsql AS $$
             BEGIN
               CREATE TEMP TABLE IF NOT EXISTS batch_commit
                 (x int UNIQUE DEFERRABLE INITIALLY DEFERRED);
               RETURN 0;
             END $$;
             CREATE OR REPLACE FUNCTION batch_commit_dup(i int) RETURNS int LANGUAGE plpgsql AS
               'BEGIN INSERT INTO batch_commit VALUES (i), (i); RETURN i; END'",
        )
        .await
        .unwrap();
        // Not batched (`execute`), and on the pool's one connection, whose
        // session the temporary table belongs to.
        pool.execute("SELECT batch_commit_setup()", &[])
            .await
            .unwrap();
        for round in 0..20 {
            let tasks: Vec<_> = (0..20i32)
                .map(|i| {
                    let pool = pool.clone();
                    giong::spawn(async move {
                        if i == round % 20 {
                            // Its two rows break the deferred constraint at
                            // the commit, after the statement ran.
                            let e = pool
                                .query("SELECT batch_commit_dup($1)", &[&i])
                                .await
                                .unwrap_err();
                            assert_eq!(e.server_code(), Some("23505"), "{e}");
                        } else {
                            let row = pool.query_one("SELECT $1::int4 + 1", &[&i]).await;
                            assert_eq!(row.unwrap().get::<i32>(0).unwrap(), i + 1);
                        }
                    })
                })
                .collect();
            for t in tasks {
                t.await.unwrap();
            }
        }
        let row = pool
            .query_one("SELECT $1::int4 + 1", &[&41i32])
            .await
            .unwrap();
        assert_eq!(row.get::<i32>(0).unwrap(), 42);
        let stats = pool.stats();
        assert_eq!((stats.in_flight, stats.opened), (0, 1), "{stats:?}");
    });
}

#[test]
fn statements_before_a_failure_in_their_batch_run_again() {
    run(|s| async move {
        let pool = Pool::new(s.local().batch_reads(true), 1);
        pool.batch_execute(
            "CREATE OR REPLACE FUNCTION batch_again_setup() RETURNS int LANGUAGE plpgsql AS $$
             BEGIN
               CREATE TEMP TABLE IF NOT EXISTS batch_again (x int);
               RETURN 0;
             END $$;
             CREATE OR REPLACE FUNCTION batch_again_add(i int) RETURNS int LANGUAGE plpgsql AS
               'BEGIN INSERT INTO batch_again VALUES (i); RETURN i; END'",
        )
        .await
        .unwrap();
        pool.execute("SELECT batch_again_setup()", &[])
            .await
            .unwrap();
        // Temporary tables can be written in a read-only transaction, so
        // the insert runs in the batch, which the division by zero after it
        // rolls back.
        for i in 0..10 {
            let [added, failed] = together(
                &pool,
                ("SELECT batch_again_add($1)", i),
                ("SELECT 1 / $1::int4", 0),
            )
            .await;
            assert_eq!(added.unwrap()[0].get::<i32>(0).unwrap(), i);
            assert_eq!(failed.unwrap_err().server_code(), Some("22012"));
        }
        let row = pool
            .query_one("SELECT count(*)::int4 FROM batch_again", &[])
            .await
            .unwrap();
        assert_eq!(row.get::<i32>(0).unwrap(), 10, "each insert stayed, once");
    });
}

#[test]
fn transaction_local_settings_stay_with_their_statement_unless_hidden() {
    run(|s| async move {
        // A setting made with `set_config(.., true)` lasts until the end of
        // the transaction. A statement that calls it runs alone, so the
        // setting reaches no other statement; a call inside a function is
        // not seen and reaches the rest of its batch, as `Config::batch_reads`
        // warns.
        const SET: &str = "SELECT set_config('giong.tenant', $1::int4::text, true)";
        const HIDDEN: &str = "SELECT giong_set_tenant($1::int4)";
        const GET: &str =
            "SELECT coalesce(current_setting('giong.tenant', true), '') || $1::int4::text";
        for batch in [false, true] {
            let pool = Pool::new(s.local().batch_reads(batch), 1);
            pool.get()
                .await
                .unwrap()
                .batch_execute(
                    "CREATE OR REPLACE FUNCTION giong_set_tenant(i int4) RETURNS text \
                     LANGUAGE plpgsql AS $$ BEGIN \
                     RETURN set_config('giong.tenant', i::text, true); END $$",
                )
                .await
                .unwrap();
            // Prepared, so that each may join a batch.
            for sql in [SET, HIDDEN, GET] {
                pool.query(sql, &[&0i32]).await.unwrap();
            }
            for i in 1..=5 {
                let [_, seen] = together(&pool, (SET, i), (GET, 0)).await;
                let seen: String = seen.unwrap()[0].get::<&str>(0).unwrap().to_owned();
                assert_eq!(seen, "0", "no setting from another statement");

                let [_, seen] = together(&pool, (HIDDEN, i), (GET, 0)).await;
                let seen: String = seen.unwrap()[0].get::<&str>(0).unwrap().to_owned();
                if batch {
                    assert_eq!(seen, format!("{i}0"), "the hidden call's setting");
                } else {
                    assert_eq!(seen, "0", "no setting from another statement");
                }
            }
        }
    });
}

#[test]
fn the_read_only_guard_is_prepared_again_after_a_deallocate() {
    run(|s| async move {
        let pool = Pool::new(s.local().batch_reads(true), 1);
        const NOW: &str = "SELECT now()::text || $1::int4::text";
        pool.query(NOW, &[&0i32]).await.unwrap();
        let shared = |[a, b]: [Result<Vec<giong_postgres::Row>, giong_postgres::Error>; 2]| {
            let a: String = a.unwrap()[0].get::<&str>(0).unwrap().to_owned();
            let b: String = b.unwrap()[0].get::<&str>(0).unwrap().to_owned();
            a[..a.len() - 1] == b[..b.len() - 1]
        };
        assert!(
            shared(together(&pool, (NOW, 1), (NOW, 2)).await),
            "one batch"
        );
        {
            let conn = pool.get().await.unwrap();
            conn.batch_execute("DEALLOCATE \"__giong_read_only\"")
                .await
                .unwrap();
        }
        // The guard fails (26000): both are sent again alone.
        let [a, b] = together(&pool, (NOW, 1), (NOW, 2)).await;
        a.unwrap();
        b.unwrap();
        // Then the guard is prepared again, and batches work as before.
        assert!(
            shared(together(&pool, (NOW, 1), (NOW, 2)).await),
            "one batch again"
        );
        assert_eq!(pool.stats().opened, 1);
    });
}

/// Statements of a batch hold the locks they take until the batch ends, so
/// two batches taking two locks in opposite orders could deadlock where the
/// same statements one by one never do: a statement of a batch gives up a
/// lock wait within 50 ms and runs again alone.
#[test]
fn batches_taking_locks_in_opposite_orders_do_not_deadlock() {
    for batch in [false, true] {
        run(move |s| async move {
            let a = Pool::new(s.local().batch_reads(batch), 1);
            let b = Pool::new(s.local().batch_reads(batch), 1);
            let slow = "SELECT 1::int4 FROM pg_advisory_xact_lock($1::int8), pg_sleep(0.3)";
            let fast = "SELECT 2::int4 FROM pg_advisory_xact_lock($1::int8)";
            for pool in [&a, &b] {
                pool.query_one(slow, &[&100i64]).await.unwrap();
                pool.query_one(fast, &[&100i64]).await.unwrap();
            }
            let mut tasks = Vec::new();
            for (pool, first, second) in [(&a, 1i64, 2i64), (&b, 2, 1)] {
                let p = pool.clone();
                tasks.push(giong::spawn(
                    async move { p.query_one(slow, &[&first]).await },
                ));
                let p = pool.clone();
                tasks.push(giong::spawn(
                    async move { p.query_one(fast, &[&second]).await },
                ));
            }
            for t in tasks {
                if let Err(e) = t.await.unwrap() {
                    panic!("batch_reads({batch}): {e}");
                }
            }
        });
    }
}

/// A migration that locks two tables while a batch reads both: without the
/// cap on the batch's lock waits, the two deadlock and the server rolls the
/// migration back.
#[test]
fn a_migration_locking_two_tables_is_not_rolled_back_by_a_batch() {
    for batch in [false, true] {
        run(move |s| async move {
            let pool = Pool::new(s.local().batch_reads(batch), 2);
            pool.batch_execute(
                "DROP TABLE IF EXISTS ta, tb; CREATE TABLE ta (x int); CREATE TABLE tb (x int)",
            )
            .await
            .unwrap();
            let s1 = "SELECT count(*)::int4 FROM ta, pg_sleep(0.3)";
            let s2 = "SELECT count(*)::int4 FROM tb";
            let reads = Pool::new(s.local().batch_reads(batch), 1);
            reads.query_one(s1, &[]).await.unwrap();
            reads.query_one(s2, &[]).await.unwrap();
            let r = reads.clone();
            let t1 = giong::spawn(async move { r.query_one(s1, &[]).await.map(|_| ()) });
            let r = reads.clone();
            let t2 = giong::spawn(async move { r.query_one(s2, &[]).await.map(|_| ()) });
            let migrate = giong::spawn({
                let pool = pool.clone();
                async move {
                    giong::time::sleep(std::time::Duration::from_millis(100)).await;
                    let c = pool.get().await.unwrap();
                    c.batch_execute("BEGIN; LOCK tb; SELECT pg_sleep(0.05); LOCK ta; COMMIT")
                        .await
                }
            });
            t1.await.unwrap().unwrap();
            t2.await.unwrap().unwrap();
            if let Err(e) = migrate.await.unwrap() {
                panic!("batch_reads({batch}): the migration failed: {e}");
            }
        });
    }
}

/// A local port nothing listens on.
fn closed_port() -> u16 {
    let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    l.local_addr().unwrap().port()
}

#[test]
fn tasks_waiting_while_the_server_is_down_share_one_connect_attempt() {
    run(|s| async move {
        let port = closed_port();
        let pool = Pool::new(s.local().host("127.0.0.1").port(port), 4);
        let calls = Rc::new(Cell::new(0u64));
        let end = Instant::now() + Duration::from_millis(300);
        let tasks: Vec<_> = (0..64)
            .map(|_| {
                let (pool, calls) = (pool.clone(), calls.clone());
                giong::spawn(async move {
                    while Instant::now() < end {
                        let e = pool.query("SELECT 1", &[]).await.unwrap_err();
                        // The server's own refusal, never a pool timeout.
                        assert_eq!(e.kind(), ErrorKind::Connect, "{e}");
                        calls.set(calls.get() + 1);
                        sleep(Duration::from_millis(1)).await;
                    }
                })
            })
            .collect();
        for t in tasks {
            t.await.unwrap();
        }
        let attempts = pool.stats().connect_errors;
        let calls = calls.get();
        // Each task used to make an attempt of its own, on every call.
        assert!(
            attempts * 8 <= calls,
            "{attempts} connect attempts for {calls} calls"
        );
        // And it comes back at once with the server.
        let pool = Pool::new(s.local(), 4);
        pool.query("SELECT 1", &[]).await.unwrap();
    });
}

#[test]
fn a_pool_whose_server_stays_down_spaces_its_connect_attempts() {
    run(|s| async move {
        let port = closed_port();
        let pool = Pool::new(s.local().host("127.0.0.1").port(port), 4);
        let mut calls = 0u64;
        // Windows tries a refused connect twice more, half a second apart:
        // each attempt there takes about 2 s, so the window is longer.
        let window = Duration::from_millis(if cfg!(windows) { 7_000 } else { 400 });
        let end = giong::time::Instant::now() + window;
        // One task calling over and over, as a request handler does.
        while giong::time::Instant::now() < end {
            let e = pool.query("SELECT 1", &[]).await.unwrap_err();
            assert_eq!(e.kind(), ErrorKind::Connect, "{e}");
            calls += 1;
            giong::time::sleep(Duration::from_millis(1)).await;
        }
        // Each call used to make an attempt of its own. Pauses of 10 ms
        // doubling (less up to half) allow at most 8 in 400 ms (and fewer
        // still on Windows, where each attempt also takes 2 s).
        // Fewer calls fit on Windows, where each attempt takes 2 s.
        let min_calls = if cfg!(windows) { 20 } else { 100 };
        let attempts = pool.stats().connect_errors;
        assert!(
            (2..=8).contains(&attempts) && calls >= min_calls,
            "{attempts} connect attempts for {calls} calls"
        );
        // A pause ends: the next call tries again (and only one does).
        giong::time::sleep(Duration::from_millis(1050)).await;
        pool.query("SELECT 1", &[]).await.unwrap_err();
        pool.query("SELECT 1", &[]).await.unwrap_err();
        assert_eq!(pool.stats().connect_errors, attempts + 1);
    });
}