mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
2 days ago cf2068420f92f25985c22a6cdb16c17d9ceb1efb
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
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.server.backends.jdbc;
 
import org.forgerock.opendj.server.config.server.JDBCBackendCfg;
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.backends.pluggable.spi.AccessMode;
import org.opends.server.backends.pluggable.spi.Importer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.IdentityHashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Logger;
 
import org.mockito.InOrder;
 
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNotSame;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
 
/**
 * The pool every operation of the JDBC backend borrows from must bound both of its phases and
 * report a connect it cannot make, rather than retrying it out of sight of the caller (#872).
 * Needs no database: the dialects are exercised against a socket that never answers and against a
 * driver of this test, so a regression fails the build wherever it runs.
 */
@SuppressWarnings("javadoc")
@Test(groups = { "precommit", "jdbc" }, sequential = true)
public class CachedConnectionTestCase extends DirectoryServerTestCase {
 
    /** A connect attempt of a bounded dialect must give up in about this long, plus room for a slow machine. */
    private static final long BOUND_SECONDS = 2;
    /**
     * Room for a slow machine on top of a bound, and no more than that. A minute of it turned
     * every assertion below into "it does not run forever": a connect that has to be reported at
     * once and a borrow that has to give up at its two second deadline both passed at 59 s.
     */
    private static final long BOUND_MARGIN_MS = 10000;
 
    private final StubDriver stub = new StubDriver();
 
    /** The window as this JVM was started with it, put back after every test that varies it. */
    private static final long CONFIGURED_ALIVE_BYPASS_NANOS = CachedConnection.aliveBypassNanos;
 
    @BeforeClass
    public void registerStubDriver() throws Exception {
        DriverManager.registerDriver(stub);
    }
 
    @AfterClass
    public void deregisterStubDriver() throws Exception {
        DriverManager.deregisterDriver(stub);
    }
 
    /**
     * Most of the tests below seed the pool by hand, and a connection built a moment ago is inside
     * the alive window - they are about what the validation of a borrow does, so the window is off
     * unless the test at hand is one of the window's own.
     */
    @BeforeMethod
    public void validateEveryBorrow() {
        CachedConnection.aliveBypassNanos = 0;
    }
 
    @AfterMethod
    public void clearProperties() {
        System.clearProperty(CachedConnection.CONNECT_TIMEOUT_PROPERTY);
        System.clearProperty(CachedConnection.POOL_TIMEOUT_PROPERTY);
        System.clearProperty(CachedConnection.POOL_MAX_PROPERTY);
        System.clearProperty(CachedConnection.TTL_PROPERTY);
        System.clearProperty(CachedConnection.ALIVE_BYPASS_PROPERTY);
        // what has been reported once is remembered for the life of the jvm: left standing, the key
        // of one test is what the next one finds when it asserts that it reported something itself
        CachedConnection.warnedOnce.clear();
        CachedConnection.aliveBypassNanos = CONFIGURED_ALIVE_BYPASS_NANOS;
    }
 
    /**
     * Nothing used to limit how many connections a backend opened: the pool was an unbounded queue
     * behind a cache with no maximum size, so a burst of concurrent operations opened as many
     * connections as there were threads asking, and the only ceiling left was the max_connections of
     * the database itself (#878).
     */
    @Test(timeOut = 120000)
    public void testThePoolDoesNotGrowPastItsBound() throws Exception {
        final String url = StubDriver.PREFIX + "bounded";
        System.setProperty(CachedConnection.POOL_MAX_PROPERTY, "2");
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "1");
        stub.answerWith(null);
 
        // One thread per borrow, as the worker threads of the server are: two borrows on one thread
        // are nested by definition, and a nested one is allowed past the bound on purpose.
        final Connection first = borrowOnAThreadOfItsOwn(url);
        final Connection second = borrowOnAThreadOfItsOwn(url);
        assertEquals(CachedConnection.poolOf(url).meteredCount(), 2);
        try {
            borrowOnAThreadOfItsOwn(url);
            fail("a third connection was opened past the bound of two");
        } catch (ExecutionException e) {
            assertTrue(e.getCause() instanceof SQLTimeoutException, String.valueOf(e.getCause()));
            assertTrue(e.getCause().getMessage().contains("all 2 connections"), e.getCause().getMessage());
        }
 
        // The bound waits for a returned connection rather than refusing outright: it is a ceiling
        // on the connections held, not on the operations served.
        first.close();
        final Connection third = borrowOnAThreadOfItsOwn(url);
        assertSame(third, first);
        third.close();
        second.close();
        CachedConnection.poolOf(url).drainIdle();
    }
 
    /** Borrows the way the server does, one operation to a thread. */
    private static Connection borrowOnAThreadOfItsOwn(String url) throws Exception {
        return startBorrow(url).get(120, TimeUnit.SECONDS);
    }
 
    /** The same, left running: a borrow that waits has to be looked at while it does. */
    private static FutureTask<Connection> startBorrow(String url) {
        final FutureTask<Connection> borrow = new FutureTask<>(() -> CachedConnection.getConnection(url));
        final Thread thread = new Thread(borrow, "borrow-" + url);
        thread.setDaemon(true);
        thread.start();
        return borrow;
    }
 
    /**
     * A borrow made while this thread already holds a connection must not wait for the bound: the
     * two are held at once, so it would wait for itself. PersistentCompressedSchema.store() opens a
     * write of its own and is reached from inside a transaction by EntryContainer.importEntry and
     * EntryContainer.modifyDN, both of which encode the entry inside it.
     */
    @Test(timeOut = 120000)
    public void testABorrowNestedInAnotherMayPassTheBound() throws Exception {
        final String url = StubDriver.PREFIX + "reentrant";
        System.setProperty(CachedConnection.POOL_MAX_PROPERTY, "1");
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "1");
        stub.answerWith(null);
 
        final Connection outer = CachedConnection.getConnection(url);
        final Connection nested = CachedConnection.getConnection(url);
        assertNotSame(nested, outer);
 
        // It holds no permit of the pool, so pooling it would leave the pool one connection over
        // its bound for good: it is closed instead.
        nested.close();
        verify(((CachedConnection) nested).parent).close();
        assertEquals(CachedConnection.poolOf(url).idleCount(), 0);
 
        outer.close();
        assertEquals(CachedConnection.poolOf(url).idleCount(), 1);
        CachedConnection.poolOf(url).drainIdle();
    }
 
    /**
     * The TTL used to sit on the pool rather than on a connection - keyed by the connection string,
     * and touched by every borrow and every return - so under continuous traffic nothing in it ever
     * expired (#878).
     */
    @Test(timeOut = 120000)
    public void testAnIdleConnectionIsClosedAfterItsTtl() throws Exception {
        final String url = StubDriver.PREFIX + "ttl";
        stub.answerWith(null);
 
        final CachedConnection first = (CachedConnection) CachedConnection.getConnection(url);
        first.close();
        assertEquals(CachedConnection.poolOf(url).idleCount(), 1);
        first.returnedAtMillis = System.currentTimeMillis() - 60000;
        System.setProperty(CachedConnection.TTL_PROPERTY, "1000");
 
        final Connection second = CachedConnection.getConnection(url);
 
        assertNotSame(second, first, "a connection idle far longer than the TTL was handed out");
        verify(first.parent).close();
        second.close();
        CachedConnection.poolOf(url).drainIdle();
    }
 
    /**
     * Expiry has to happen without a borrow behind it: the cache was built without a scheduler, so
     * an entry was only ever expired by a later cache operation - and a backend that has gone idle,
     * the one case the TTL exists for, performs none (#878). This is what the sweeper thread runs.
     */
    @Test(timeOut = 120000)
    public void testTheSweepClosesAnIdleConnectionWithNoBorrowBehindIt() throws Exception {
        final String url = StubDriver.PREFIX + "sweep";
        stub.answerWith(null);
        final CachedConnection con = (CachedConnection) CachedConnection.getConnection(url);
        con.close();
        // The sweeper of the server is running while this case does, over every pool and reading
        // the TTL as it goes: out of its reach, so that the sweep asserted here is the one below.
        System.setProperty(CachedConnection.TTL_PROPERTY, "600000");
        con.returnedAtMillis = System.currentTimeMillis() - 60000;
        final CachedConnection.Pool pool = CachedConnection.poolOf(url);
        assertEquals(pool.idleCount(), 1);
 
        pool.sweep(1000);
 
        assertEquals(pool.idleCount(), 0);
        verify(con.parent).close();
        assertEquals(pool.meteredCount(), 0, "a swept connection kept its place in the pool");
    }
 
    /** A closed backend has no use for its connections; they used to be left open (#878). */
    @Test(timeOut = 120000)
    public void testClosingTheLastUserReleasesTheConnections() throws Exception {
        final String url = StubDriver.PREFIX + "release";
        stub.answerWith(null);
        CachedConnection.openPool(url);
        final CachedConnection con = (CachedConnection) CachedConnection.getConnection(url);
        con.close();
        assertEquals(CachedConnection.poolOf(url).idleCount(), 1);
 
        CachedConnection.closePool(url);
 
        assertEquals(CachedConnection.poolOf(url).idleCount(), 0);
        verify(con.parent).close();
        assertEquals(CachedConnection.poolOf(url).meteredCount(), 0);
    }
 
    /**
     * A pool belongs to a database rather than to a backend: two backends may address one database,
     * and closing one of them must not take the connections of the other with it.
     */
    @Test(timeOut = 120000)
    public void testConnectionsSurviveWhileAnotherBackendStillUsesTheDatabase() throws Exception {
        final String url = StubDriver.PREFIX + "shared";
        stub.answerWith(null);
        CachedConnection.openPool(url);
        CachedConnection.openPool(url);
        final CachedConnection con = (CachedConnection) CachedConnection.getConnection(url);
        con.close();
 
        CachedConnection.closePool(url);
        assertEquals(CachedConnection.poolOf(url).idleCount(), 1, "the second backend lost its connections");
 
        CachedConnection.closePool(url);
        assertEquals(CachedConnection.poolOf(url).idleCount(), 0);
        verify(con.parent).close();
    }
 
    /** A connection out on loan when the last backend closed is closed when it comes back. */
    @Test(timeOut = 120000)
    public void testAConnectionReturnedAfterTheLastUserLeftIsClosed() throws Exception {
        final String url = StubDriver.PREFIX + "return-after-close";
        stub.answerWith(null);
        CachedConnection.openPool(url);
        final CachedConnection con = (CachedConnection) CachedConnection.getConnection(url);
 
        CachedConnection.closePool(url);
        con.close();
 
        verify(con.parent).close();
        assertEquals(CachedConnection.poolOf(url).idleCount(), 0);
    }
 
    /** A backend closed and opened again pools its connections as before: addUser() clears the flag. */
    @Test(timeOut = 120000)
    public void testABackendClosedAndOpenedAgainPoolsItsConnections() throws Exception {
        final String url = StubDriver.PREFIX + "reopen";
        stub.answerWith(null);
        CachedConnection.openPool(url);
        CachedConnection.getConnection(url).close();
        CachedConnection.closePool(url);
        assertEquals(CachedConnection.poolOf(url).idleCount(), 0);
 
        CachedConnection.openPool(url);
        CachedConnection.getConnection(url).close();
 
        assertEquals(CachedConnection.poolOf(url).idleCount(), 1, "a reopened backend stopped pooling its connections");
        CachedConnection.closePool(url);
    }
 
    /**
     * A connect that fails with something other than a SQLException must not cost the pool a
     * permit. DriverManager catches SQLException alone, so an unchecked failure of a driver reaches
     * the borrow: Connector/J hands a url with a "%" in it to URLDecoder, and this backend keeps
     * its credentials in the url. Only a live connection carries a permit, so one left behind is
     * left behind for good - after as many failures as the bound the pool would report that every
     * connection is in use while holding none (#878).
     */
    @Test(timeOut = 120000)
    public void testAConnectFailingUncheckedCostsThePoolNothing() throws Exception {
        final String url = StubDriver.PREFIX + "unchecked";
        System.setProperty(CachedConnection.POOL_MAX_PROPERTY, "2");
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "1");
        final CachedConnection.Pool pool = CachedConnection.poolOf(url);
        stub.failWith(new IllegalArgumentException("URLDecoder: Illegal hex characters in escape (%) pattern"),
            StubDriver.ALWAYS);
 
        for (int i = 1; i <= 2 * pool.max(); i++) {
            try {
                CachedConnection.getConnection(url);
                fail("the connect did not fail");
            } catch (IllegalArgumentException expected) {
                // reported to the caller, as a configuration error has to be
            }
            assertEquals(pool.meteredCount(), 0, "attempt " + i + " kept a permit of the pool");
        }
 
        // and the pool still serves, rather than reporting connections it does not hold as in use
        stub.answerWith(null);
        final Connection con = CachedConnection.getConnection(url);
        assertNotNull(con);
        con.close();
        CachedConnection.poolOf(url).drainIdle();
    }
 
    /**
     * The exemption of a nested borrow belongs to one pool: a thread holding a connection to one
     * database holds nothing of another, so the bound of that other pool applies and its connection
     * comes back to it rather than being closed.
     */
    @Test(timeOut = 120000)
    public void testHoldingAConnectionToOneDatabaseDoesNotExemptABorrowFromAnother() throws Exception {
        final String first = StubDriver.PREFIX + "held-first";
        final String second = StubDriver.PREFIX + "held-second";
        stub.answerWith(null);
 
        final Connection held = CachedConnection.getConnection(first);
        final Connection other = CachedConnection.getConnection(second);
        assertEquals(CachedConnection.poolOf(second).meteredCount(), 1, "the borrow passed the bound of the other pool");
        other.close();
 
        assertEquals(CachedConnection.poolOf(second).idleCount(), 1, "the borrow was taken for a nested one and closed");
        held.close();
        CachedConnection.poolOf(first).drainIdle();
        CachedConnection.poolOf(second).drainIdle();
    }
 
    /**
     * A connection returned on a thread other than the one that borrowed it still lowers the depth
     * of the borrower. The depth used to be lowered only where the returning thread was the
     * borrowing one, and nulled either way, so a cross-thread return left the borrower standing at a
     * depth it could never come down from: that thread was taken for a nested borrow for the life of
     * the server, exempt from the wait at the bound, and every operation on it opened an unmetered
     * connection that the return then closed - a physical connect apiece, past a bound the operator
     * set (#878).
     */
    @Test(timeOut = 120000)
    public void testAReturnOnAnotherThreadLowersTheDepthOfTheBorrower() throws Exception {
        final String url = StubDriver.PREFIX + "cross-thread-return";
        System.setProperty(CachedConnection.POOL_MAX_PROPERTY, "1");
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "1");
        stub.answerWith(null);
        final CachedConnection.Pool pool = CachedConnection.poolOf(url);
        final ExecutorService borrower = Executors.newSingleThreadExecutor(runnable -> {
            final Thread thread = new Thread(runnable, "cross-thread-borrower");
            thread.setDaemon(true);
            return thread;
        });
 
        try {
            // borrowed there, returned here
            final Connection borrowed = borrower.submit(() -> CachedConnection.getConnection(url))
                .get(120, TimeUnit.SECONDS);
            borrowed.close();
            assertEquals(pool.idleCount(), 1, "the connection was not pooled by the return");
 
            // the one place of the pool goes to somebody else, so the borrower thread has to wait
            // for it - and, having no connection of its own any more, has to give up when it does
            // not come
            final Connection held = borrowOnAThreadOfItsOwn(url);
            try {
                borrower.submit(() -> CachedConnection.getConnection(url)).get(120, TimeUnit.SECONDS);
                fail("the borrower thread was taken for a nested borrow and passed the bound of the pool");
            } catch (ExecutionException expected) {
                assertTrue(expected.getCause() instanceof SQLTimeoutException,
                    "the bound was passed rather than waited out: " + expected.getCause());
            }
            held.close();
        } finally {
            borrower.shutdownNow();
        }
        CachedConnection.poolOf(url).drainIdle();
    }
 
    /** JDBC makes close() on a closed connection a no-op; a second return would pool the same one twice. */
    @Test(timeOut = 120000)
    public void testASecondCloseDoesNotPoolTheConnectionTwice() throws Exception {
        final String url = StubDriver.PREFIX + "double-close";
        stub.answerWith(null);
        final Connection con = CachedConnection.getConnection(url);
        con.close();
        con.close();
 
        assertEquals(CachedConnection.poolOf(url).idleCount(), 1, "one connection was pooled twice");
        CachedConnection.poolOf(url).drainIdle();
    }
 
    /**
     * What the sweeper runs hands the close elsewhere instead of running it. The sweep of every
     * pool shares one thread and scheduleWithFixedDelay never overlaps its runs, so one close that
     * does not return would stop the expiry of every pool in the JVM, silently (#878).
     */
    @Test(timeOut = 120000)
    public void testTheSweepDoesNotCloseOnTheSweeperThread() throws Exception {
        final String url = StubDriver.PREFIX + "sweep-elsewhere";
        stub.answerWith(null);
        final CachedConnection con = (CachedConnection) CachedConnection.getConnection(url);
        con.close();
        System.setProperty(CachedConnection.TTL_PROPERTY, "600000"); // see the case above
        con.returnedAtMillis = System.currentTimeMillis() - 60000;
        final CachedConnection.Pool pool = CachedConnection.poolOf(url);
        final List<Runnable> handedOff = new ArrayList<>();
 
        pool.sweep(1000, handedOff::add);
 
        assertEquals(pool.idleCount(), 0, "the expired connection kept its place in the pool");
        verify(con.parent, never()).close();
        assertEquals(handedOff.size(), 1);
 
        handedOff.get(0).run();
        verify(con.parent).close();
        assertEquals(pool.meteredCount(), 0, "a swept connection kept its permit");
    }
 
    /**
     * And the sweep the scheduled sweeper actually runs closes elsewhere too: the case above
     * supplies an executor of its own, so it would pass just as well with the production one left
     * closing inline.
     */
    @Test(timeOut = 120000)
    public void testTheScheduledSweepClosesOnAThreadOfItsOwn() throws Exception {
        final String url = StubDriver.PREFIX + "sweeper-thread";
        stub.answerWith(null);
        final CachedConnection con = (CachedConnection) CachedConnection.getConnection(url);
        con.close();
        final AtomicReference<String> closedOn = new AtomicReference<>();
        doAnswer(invocation -> {
            closedOn.set(Thread.currentThread().getName());
            return null;
        }).when(con.parent).close();
        con.returnedAtMillis = System.currentTimeMillis() - 60000;
        System.setProperty(CachedConnection.TTL_PROPERTY, "1000");
 
        CachedConnection.sweep(); // what the scheduled sweeper runs, with nothing supplied to it
 
        for (int i = 0; i < 200 && closedOn.get() == null; i++) {
            Thread.sleep(50);
        }
        assertNotNull(closedOn.get(), "the sweep never closed the expired connection");
        assertFalse(closedOn.get().contains("sweeper"), "the close ran on the sweeper thread: " + closedOn.get());
        assertTrue(closedOn.get().startsWith("JDBC backend connection pool closer"), closedOn.get());
    }
 
    /**
     * A borrow may not outlast the deadline it was given while emptying the pool. A poll of no
     * duration still hands out whatever the deque holds, and a connection whose socket is half-open
     * - a moved VIP, a firewall that dropped the idle sockets - costs the validation timeout to
     * discard, so draining a pool of its full bound overran the deadline by minutes, before the
     * connect that follows it had even started (#878).
     */
    @Test(timeOut = 120000)
    public void testABorrowStopsAtItsDeadlineRatherThanDrainingThePool() throws Exception {
        final String url = StubDriver.PREFIX + "deadline-drain";
        System.setProperty(CachedConnection.POOL_MAX_PROPERTY, "6");
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "1");
        final CachedConnection.Pool pool = CachedConnection.poolOf(url);
 
        // Fresh by the TTL, and each one a second to find broken: the pool a burst of traffic left
        // behind, against a database that has stopped answering.
        for (int i = 0; i < 6; i++) {
            final Connection halfOpen = mock(Connection.class);
            when(halfOpen.isValid(anyInt())).thenAnswer(invocation -> {
                Thread.sleep(1000);
                return false;
            });
            assertTrue(pool.tryReserve());
            pool.addIdle(new CachedConnection(url, halfOpen, pool, true, true));
        }
        stub.answerWith(null);
 
        final long startedAt = System.currentTimeMillis();
        final Connection borrowed = CachedConnection.getConnection(url);
        final long elapsed = System.currentTimeMillis() - startedAt;
 
        assertNotNull(borrowed);
        assertTrue(elapsed < 3500, "the borrow drained the pool past its deadline: " + elapsed + " ms");
        borrowed.close();
        CachedConnection.poolOf(url).drainIdle();
    }
 
    /** 0 means "no bound" for the size of the pool, and an invalid value means "the default". */
    @Test(timeOut = 120000)
    public void testTheBoundOfThePoolReadsItsBoundaryValues() throws Exception {
        assertEquals(poolWithMax("unbounded", "0").max(), Integer.MAX_VALUE, "0 must mean no bound");
        assertEquals(poolWithMax("negative", "-1").max(), CachedConnection.DEFAULT_POOL_MAX);
        assertEquals(poolWithMax("not-a-number", "sixteen").max(), CachedConnection.DEFAULT_POOL_MAX);
    }
 
    private static CachedConnection.Pool poolWithMax(String name, String max) {
        System.setProperty(CachedConnection.POOL_MAX_PROPERTY, max);
        return CachedConnection.poolOf(StubDriver.PREFIX + "bound-" + name); // read when the pool is built
    }
 
    /** 0 means "wait without limit" for a borrow, rather than "give up at once". */
    @Test(timeOut = 120000)
    public void testABorrowWithNoDeadlineWaitsForAReturnedConnection() throws Exception {
        final String url = StubDriver.PREFIX + "no-deadline";
        System.setProperty(CachedConnection.POOL_MAX_PROPERTY, "1");
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "0");
        stub.answerWith(null);
 
        final Connection held = borrowOnAThreadOfItsOwn(url);
        final FutureTask<Connection> waiting = startBorrow(url);
        try {
            waiting.get(1500, TimeUnit.MILLISECONDS);
            fail("the borrow gave up although it was given no deadline");
        } catch (TimeoutException expected) {
            // still waiting for the connection of the pool to come back, which is the point
        }
 
        held.close();
        final Connection served = waiting.get(120, TimeUnit.SECONDS);
        assertSame(served, held, "the borrow was served by something other than the returned connection");
        served.close();
        CachedConnection.poolOf(url).drainIdle();
    }
 
    /** 0 means "keep nothing" for the TTL: an idle connection is not handed out again. */
    @Test(timeOut = 120000)
    public void testAZeroTtlKeepsNoIdleConnection() throws Exception {
        final String url = StubDriver.PREFIX + "zero-ttl";
        stub.answerWith(null);
        final CachedConnection first = (CachedConnection) CachedConnection.getConnection(url);
        first.close();
        first.returnedAtMillis = System.currentTimeMillis() - 5;
        System.setProperty(CachedConnection.TTL_PROPERTY, "0");
 
        final Connection second = CachedConnection.getConnection(url);
 
        assertNotSame(second, first, "a connection was kept although the TTL keeps none");
        verify(first.parent).close();
        second.close();
        CachedConnection.poolOf(url).drainIdle();
    }
 
    /**
     * The storage borrows from the pool it registered with, and gives that registration back when
     * it closes. db-directory may be changed on a running backend - applyConfigurationChange takes
     * it and nothing refuses it - and a borrow that followed the change would leave the pool this
     * storage registered with holding a user that never borrows, while the pool it borrowed from
     * has none: the leak of #878 back through the configuration, and a pool another backend may
     * drain while this one is still borrowing from it.
     */
    @Test(timeOut = 120000)
    public void testTheStorageBorrowsFromThePoolItRegisteredWith() throws Exception {
        final String registered = StubDriver.PREFIX + "storage-registered";
        final String changed = StubDriver.PREFIX + "storage-changed";
        stub.answerWith(null);
        final JDBCBackendCfg cfg = mock(JDBCBackendCfg.class);
        when(cfg.getDBDirectory()).thenReturn(registered);
        final JDBCStorage storage = new JDBCStorage(cfg, null);
        storage.open(AccessMode.READ_WRITE);
 
        when(cfg.getDBDirectory()).thenReturn(changed); // the configuration changed under it
        try (final Connection con = storage.getConnection()) {
            assertEquals(((CachedConnection) con).connectionString, registered,
                "the borrow left the pool this storage registered with");
        }
        assertEquals(CachedConnection.poolOf(changed).meteredCount(), 0, "a pool with no user was borrowed from");
 
        storage.close();
 
        assertEquals(CachedConnection.poolOf(registered).idleCount(), 0,
            "close() left the connections of the pool it registered with behind");
    }
 
    /**
     * An open that failed has to leave the storage saying so. The status used to be set inside the
     * try-with-resources of the validating borrow, so a throw from the implicit close() - the return
     * rolls back, and the rollback goes to the database - left the storage at working() while open()
     * failed and gave the registration of the pool back. write() and ImporterImpl both skip the
     * re-open when the status says working, so the pool was left with no user at all: every
     * connection returned to it destroyed on the spot, pooling off for that database for as long as
     * the server runs (#878).
     */
    @Test(timeOut = 120000)
    public void testAnOpenThatFailsOnTheReturnLeavesTheStorageClosed() throws Exception {
        final String url = StubDriver.PREFIX + "open-return-failure";
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        doThrow(new SQLException("the socket went away")).when(parent).rollback();
        stub.answerWith(parent);
        final JDBCBackendCfg cfg = mock(JDBCBackendCfg.class);
        when(cfg.getDBDirectory()).thenReturn(url);
        final JDBCStorage storage = new JDBCStorage(cfg, null);
 
        try {
            storage.open(AccessMode.READ_WRITE);
            fail("a validated borrow that could not be returned must be reported");
        } catch (SQLException expected) {
            assertEquals(expected.getMessage(), "the socket went away");
        }
        assertFalse(storage.getStorageStatus().isWorking(), "an open that failed left the storage reporting working");
 
        // and the open that follows is not skipped: it registers with the pool again, which is what
        // makes the connections returned to it pooled rather than destroyed on the spot
        doNothing().when(parent).rollback();
        storage.open(AccessMode.READ_WRITE);
        assertTrue(storage.getStorageStatus().isWorking(), "the storage did not reopen");
        assertEquals(CachedConnection.poolOf(url).idleCount(), 1,
            "the reopened storage stopped pooling its connections");
        storage.close();
    }
 
    /**
     * An import gives its connection back however its commit went. The commit used to be guarded
     * against SQLException alone, so an Error out of a bulk import - or a driver failing unchecked
     * - left the connection borrowed and its permit with it; a pool is never removed from the map,
     * so that permit was gone for the life of the server and enough imports walked the bound of
     * the pool down to nothing (#878).
     */
    @Test(timeOut = 120000)
    public void testAnImportGivesItsConnectionBackWhenTheCommitFailsUnchecked() throws Exception {
        final String url = StubDriver.PREFIX + "import-unchecked-commit";
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        doThrow(new Error("out of memory while importing")).when(parent).commit();
        stub.answerWith(parent);
        final JDBCBackendCfg cfg = mock(JDBCBackendCfg.class);
        when(cfg.getDBDirectory()).thenReturn(url);
        final JDBCStorage storage = new JDBCStorage(cfg, null);
        storage.open(AccessMode.READ_WRITE);
        final Importer importer = storage.startImport();
 
        try {
            importer.close();
            fail("the failure of the commit was not reported");
        } catch (Error expected) {
            // reported to the caller, which is what an Error out of an import has to be
        }
 
        assertEquals(CachedConnection.poolOf(url).idleCount(), 1, "the import kept the connection of the pool");
        storage.close();
        assertEquals(CachedConnection.poolOf(url).meteredCount(), 0, "the import kept a permit of the pool");
    }
 
    /**
     * A driver that is not on the classpath - the JDBC backend needs one dropped into
     * lib/extensions by hand - is a configuration error the caller has to see. Retried, it is
     * indistinguishable from a database that hangs.
     */
    @Test(timeOut = 120000)
    public void testMissingDriverIsReportedAtOnce() throws Exception {
        final long startedAt = System.currentTimeMillis();
        try {
            CachedConnection.getConnection("jdbc:nosuchengine://127.0.0.1:5432/opendj");
            fail("a connection string no registered driver accepts must be reported");
        } catch (SQLException expected) {
            assertTrue(expected.getMessage().contains("No suitable driver"), expected.getMessage());
        }
        assertElapsedWithinBound(startedAt, 0);
    }
 
    /**
     * ... and the report of it carries no password. This is the path the credentials leave by: the
     * jdk itself builds "No suitable driver found for " + url, a missing driver jar is the ordinary
     * oracle misconfiguration, and what this class throws reaches the server error log in full -
     * JDBCStorage.open() hands it to RootContainer, which makes the message of the cause the
     * message of what it throws, and BackendConfigManager logs that at ERROR and answers a config
     * change with it. Every link of the chain is asserted, not only the message on top: everything
     * that prints a failure prints its causes along with it.
     */
    @Test(timeOut = 120000)
    public void testAReportedConnectCarriesNoCredentials() throws Exception {
        final String url = "jdbc:nosuchengine://opendj:S3cretOfTheBackend@127.0.0.1:5432/opendj";
        try {
            CachedConnection.getConnection(url);
            fail("a connection string no registered driver accepts must be reported");
        } catch (SQLException expected) {
            assertNoCredentials(expected);
            assertTrue(expected.getMessage().contains("No suitable driver"),
                "the failure has to stay recognizable: " + expected.getMessage());
            assertTrue(expected.getMessage().contains("127.0.0.1:5432"),
                "the host is what a report of a connect is read for: " + expected.getMessage());
        }
    }
 
    /**
     * A url this class knows no timeout properties for is reported once. The properties bounding a
     * connect are the ones of a driver, so a driver outside the four - an admin-added mariadb, an
     * h2 - leaves every attempt unbounded, and the deadline of the borrow cannot reach into a
     * connect already under way: the driver is the only thing holding the socket. Silently, that
     * is #872 again, for a backend nobody thinks of as unbounded.
     */
    @Test(timeOut = 120000)
    public void testAUrlThisBackendCannotBoundIsReportedOnce() throws Exception {
        final String url = "jdbc:nosuchengine-unbounded://127.0.0.1:5432/opendj";
        final String key = CachedConnection.safeUrl(url) + "|unknown-dialect";
        // the set is the gate warnOnce() logs behind, so it has to start empty for this to be a
        // test of what this borrow reported rather than of what some earlier one left behind
        CachedConnection.warnedOnce.clear();
        borrowExpectingFailure(url);
        assertEquals(CachedConnection.warnedOnce, Collections.singleton(key),
            "a connection string no bound of this class can reach was not reported");
 
        // ... and once: every operation of the backend borrows through here, so a report per borrow
        // is one nobody reads. A second borrow that would log again is one that adds a key again.
        CachedConnection.warnedOnce.remove(key);
        borrowExpectingFailure(url);
        assertEquals(CachedConnection.warnedOnce, Collections.singleton(key),
            "the report is not the one warnOnce() gates: " + CachedConnection.warnedOnce);
        borrowExpectingFailure(url);
        assertEquals(CachedConnection.warnedOnce, Collections.singleton(key),
            "the same url was reported a second time");
    }
 
    /** A borrow that has to fail: what the test is after is what was logged on the way. */
    private static void borrowExpectingFailure(String url) throws Exception {
        try {
            CachedConnection.getConnection(url);
            fail("a connection string no registered driver accepts must be reported");
        } catch (SQLException expected) {
            // the point of the test is what was logged on the way, not what came back
        }
    }
 
    /**
     * ... and so is a postgresql url that turns the read bound off: a parameter of one outranks the
     * property this class supplies, so a "socketTimeout=0" there cannot be replaced. Nothing is
     * left to end a borrow that reaches a database accepting the connection and answering nothing,
     * and an administrator who wrote that zero has to be able to find it in the log.
     */
    @Test
    public void testAReadBoundTurnedOffInAPostgresUrlIsReportedOnce() throws Exception {
        final String url = "jdbc:postgresql://reported:5432/db?socketTimeout=0";
        final String key = CachedConnection.safeUrl(url) + "|unbounded|socketTimeout";
        CachedConnection.warnedOnce.clear();
        assertFalse(CachedConnection.ConnectDialect.POSTGRES.bound(url, new Properties(), 7));
        assertEquals(CachedConnection.warnedOnce, Collections.singleton(key),
            "a url leaving the reads of its login unbounded was not reported");
 
        assertFalse(CachedConnection.ConnectDialect.POSTGRES.bound(url, new Properties(), 7));
        assertEquals(CachedConnection.warnedOnce, Collections.singleton(key),
            "the same url was reported a second time");
    }
 
    /**
     * ... and every parameter of it that is turned off, not only the first one. A url is free to
     * turn off the read bound and the login bound both, and the login bound is the per-host budget
     * of a failover url - safeUrl() keeps neither parameter, so a key of the url alone would name
     * the first offender, remember the url as reported, and leave the rest of them unmentionable.
     */
    @Test
    public void testEveryBoundTurnedOffInAPostgresUrlIsReported() throws Exception {
        final String url = "jdbc:postgresql://reported-twice:5432/db?socketTimeout=0&loginTimeout=0";
        final String safe = CachedConnection.safeUrl(url);
        CachedConnection.warnedOnce.clear();
 
        CachedConnection.ConnectDialect.POSTGRES.bound(url, new Properties(), 7);
 
        assertTrue(CachedConnection.warnedOnce.contains(safe + "|unbounded|socketTimeout"),
            "the read bound left at 0 was not reported: " + CachedConnection.warnedOnce);
        assertTrue(CachedConnection.warnedOnce.contains(safe + "|unbounded|loginTimeout"),
            "the login bound left at 0 was not reported: " + CachedConnection.warnedOnce);
    }
 
    /**
     * Nothing in the chain of a failure names the password of the backend, however deep it stands.
     * Walked by identity rather than link by link: a driver is free to make the cause and the next
     * exception of a link the same failure, which is the very shape the sibling test builds, and a
     * helper looping on it would hang the run it is checking for exactly that.
     */
    private static void assertNoCredentials(Throwable failure) {
        final Set<Throwable> seen = Collections.newSetFromMap(new IdentityHashMap<Throwable, Boolean>());
        final Deque<Throwable> pending = new ArrayDeque<>();
        enqueue(pending, seen, failure);
        while (!pending.isEmpty()) {
            final Throwable t = pending.poll();
            assertFalse(String.valueOf(t.getMessage()).contains("S3cretOfTheBackend"),
                "the password of the backend reached a message: " + t);
            if (t instanceof SQLException) {
                enqueue(pending, seen, ((SQLException) t).getNextException());
            }
            enqueue(pending, seen, t.getCause());
        }
    }
 
    private static void enqueue(Deque<Throwable> pending, Set<Throwable> seen, Throwable t) {
        if (t != null && seen.add(t)) {
            pending.add(t);
        }
    }
 
    /**
     * A link of a chain carries a cause and a next exception both, and a driver is free to make the
     * two the same failure. Rebuilt under a bound on the depth alone, a chain of those is copied
     * twice over at every step - 2^32 links for one reaching the bound, which is a report of a
     * failed connect that never comes back. What bounds the redaction is the number of links it
     * rebuilds, the way the walk looking for credentials is bounded by the ones it visits.
     */
    @Test(timeOut = 60000)
    public void testAFailureWhoseCauseIsItsNextExceptionIsRedactedInBoundedTime() throws Exception {
        final String url = "jdbc:postgresql://opendj:S3cretOfTheBackend@127.0.0.1:5432/opendj";
        SQLException chain = new SQLException("connect to " + url + " failed", "08006", 1);
        for (int i = 0; i < 64; i++) {
            final SQLException link = new SQLException("link " + i + " of " + url, "08006", i);
            link.setNextException(chain);
            link.initCause(chain);
            chain = link;
        }
        final SQLException reported = CachedConnection.reported(chain, url);
        assertNoCredentials(reported);
        assertEquals(reported.getSQLState(), "08006", "the SQLState of a link has to survive its redaction");
    }
 
    /**
     * ... and the chain of one is walked link by link rather than copy by copy. Enqueued twice, a
     * failure whose cause is its own next exception fans out into a level twice the size of the one
     * above it, so five levels of it are enough to spend the whole budget of the walk: the link
     * that names the url stands at level six of seven and was never reached - and a walk that ends
     * without finding credentials is one that reports the failure as it stands, password and all.
     */
    @Test(timeOut = 60000)
    public void testACredentialBehindADuplicatedChainIsStillRedacted() throws Exception {
        final String url = "jdbc:postgresql://opendj:S3cretOfTheBackend@127.0.0.1:5432/opendj";
        SQLException chain = new SQLException("connect to " + url + " failed", "08006", 1);
        for (int i = 0; i < 6; i++) {
            final SQLException link = new SQLException("wrapper " + i, "08006", i);
            link.setNextException(chain);
            link.initCause(chain);
            chain = link;
        }
 
        assertNoCredentials(CachedConnection.reported(chain, url));
    }
 
    /**
     * The tail a rebuild has no budget left for is named rather than dropped. Without it the same
     * failure keeps its root cause where the url of the backend carries no password and loses it
     * without a word where it does - and the root cause is what a report of a failed connect is
     * read for.
     */
    @Test(timeOut = 60000)
    public void testTheTailOfALongChainIsNamedRatherThanDropped() throws Exception {
        final String url = "jdbc:postgresql://opendj:S3cretOfTheBackend@127.0.0.1:5432/opendj";
        SQLException chain = new SQLException("Connection to 127.0.0.1:5432 refused", "08006", 1);
        for (int i = 0; i < 40; i++) {
            final SQLException link = new SQLException("wrapper " + i + " of " + url, "08006", i);
            link.initCause(chain);
            chain = link;
        }
 
        final SQLException reported = CachedConnection.reported(chain, url);
 
        assertNoCredentials(reported);
        Throwable last = reported;
        while (last.getCause() != null) {
            last = last.getCause();
        }
        assertTrue(String.valueOf(last.getMessage()).contains("left out"),
            "the tail a rebuild had no budget for has to say so: " + last.getMessage());
    }
 
    /**
     * A database that is not listening at all: every dialect reports it instead of retrying the
     * refused connect until the caller gives up on the operation.
     */
    @Test(timeOut = 120000)
    public void testRefusedConnectIsReportedAtOnce() throws Exception {
        final int closedPort = closedPort();
        System.setProperty(CachedConnection.CONNECT_TIMEOUT_PROPERTY, Long.toString(BOUND_SECONDS));
        for (final String url : urlsOf(closedPort)) {
            final long startedAt = System.currentTimeMillis();
            try {
                CachedConnection.getConnection(url);
                fail("a refused connect must be reported: " + CachedConnection.safeUrl(url));
            } catch (SQLException expected) {
                // the failure of the moment, reported rather than retried
            }
            assertElapsedWithinBound(startedAt, BOUND_SECONDS * 1000);
        }
    }
 
    /**
     * The failure this bound exists for: a database that completes the TCP connection and then
     * says nothing - a moved VIP, a proxy at its connection limit, a host that lost its answer -
     * leaving the login of the driver, and with it the operation, without an end. The connection
     * of the accept queue is never answered here, so every dialect has to give up on its own.
     */
    @Test(timeOut = 300000)
    public void testLoginIsBoundedWhenTheDatabaseNeverAnswers() throws Exception {
        System.setProperty(CachedConnection.CONNECT_TIMEOUT_PROPERTY, Long.toString(BOUND_SECONDS));
        // a socket that is bound and never accepted: the kernel completes the handshake, so the
        // connect of the driver succeeds and every read of the login that follows hangs
        try (final ServerSocket blackhole = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) {
            for (final String url : urlsOf(blackhole.getLocalPort())) {
                // borrowed on a thread of its own: a bound that a driver does not honour has to
                // fail this test at once, and not by hanging the run it is part of
                final FutureTask<Connection> borrow = new FutureTask<>(() -> CachedConnection.getConnection(url));
                final Thread thread = new Thread(borrow, "borrow-" + CachedConnection.safeUrl(url));
                thread.setDaemon(true);
                thread.start();
                try {
                    final Connection con = borrow.get(BOUND_SECONDS * 1000 + BOUND_MARGIN_MS, TimeUnit.MILLISECONDS);
                    fail("a database that never answers must not hand out a connection: " + con);
                } catch (TimeoutException e) {
                    fail("the login of " + CachedConnection.safeUrl(url) + " is not bounded: it never gave up");
                } catch (ExecutionException expected) {
                    assertTrue(expected.getCause() instanceof SQLException, String.valueOf(expected.getCause()));
                }
            }
        }
    }
 
    /**
     * The deadline of the borrow bounds the attempt inside it as well: the pool timeout stands for
     * the whole borrow, and an attempt left to run out its own bound would overrun it by that bound.
     */
    @Test(timeOut = 120000)
    public void testTheAttemptIsBoundedByTheDeadlineOfTheBorrow() throws Exception {
        System.setProperty(CachedConnection.CONNECT_TIMEOUT_PROPERTY, "600");
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "2");
        try (final ServerSocket blackhole = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) {
            final String url = "jdbc:postgresql://127.0.0.1:" + blackhole.getLocalPort() + "/opendj?user=opendj&password=opendj";
            final long startedAt = System.currentTimeMillis();
            try {
                CachedConnection.getConnection(url);
                fail("a database that never answers must not hand out a connection");
            } catch (SQLException expected) {
                // reported, and within the borrow it was given rather than the 600 s of the attempt
            }
            final long elapsed = System.currentTimeMillis() - startedAt;
            assertTrue(elapsed < 2000 + BOUND_MARGIN_MS,
                "the attempt outlived the deadline of the borrow: " + elapsed + " ms");
        }
    }
 
    /** Pool exhaustion stays a retry - one of our own connections is on its way back to the pool. */
    @Test(timeOut = 120000)
    public void testConnectionLimitIsRetried() throws Exception {
        final String url = StubDriver.PREFIX + "retried";
        stub.failWith(tooManyConnections(), 2);
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "30");
 
        final Connection con = CachedConnection.getConnection(url);
 
        assertNotNull(con);
        assertEquals(stub.attempts.get(), 3, "the connect must be retried while the database is at its limit");
    }
 
    /** ... but under a deadline: the retry used to double its wait from 1 ms with no end to it. */
    @Test(timeOut = 120000)
    public void testConnectionLimitGivesUpAtTheDeadline() throws Exception {
        final String url = StubDriver.PREFIX + "deadline";
        stub.failWith(tooManyConnections(), StubDriver.ALWAYS);
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "2");
 
        final long startedAt = System.currentTimeMillis();
        try {
            CachedConnection.getConnection(url);
            fail("a database that stays at its connection limit must be reported, not waited out forever");
        } catch (SQLTimeoutException expected) {
            assertTrue(expected.getMessage().contains("2s"), expected.getMessage());
            assertEquals(((SQLException) expected.getCause()).getSQLState(), "53300");
            // the state of a connect that did not happen, rather than none at all: this is the
            // failure of a borrow, and monitoring reading the state off what it caught would
            // otherwise see null where the driver's own exception carried one
            assertEquals(expected.getSQLState(), "08001");
        }
        final long elapsed = System.currentTimeMillis() - startedAt;
        assertTrue(elapsed >= 2000, "gave up after " + elapsed + " ms, before the deadline it was given");
        assertElapsedWithinBound(startedAt, 2000);
        assertTrue(stub.attempts.get() > 1, "the connect must be retried while the deadline lasts");
    }
 
    /**
     * A database on its way up - starting, recovering, shutting down - says so, and says it for
     * seconds: the backend it belongs to would otherwise stay locked down until the next restart
     * of the server, since nothing above JDBCStorage.open() attempts it a second time.
     */
    @Test(timeOut = 120000)
    public void testDatabaseOnItsWayUpIsRetried() throws Exception {
        final String url = StubDriver.PREFIX + "starting-up";
        stub.failWith(new SQLException("the database system is starting up", "57P03"), 2);
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "30");
 
        final Connection con = CachedConnection.getConnection(url);
 
        assertNotNull(con);
        assertEquals(stub.attempts.get(), 3, "a database that is starting up must be waited out");
    }
 
    /** ... and it is recognized however the driver wrapped it: a SQLException carries two chains. */
    @Test(timeOut = 120000)
    public void testTheWholeChainOfTheFailureIsLookedAt() throws Exception {
        final String url = StubDriver.PREFIX + "wrapped";
        final SQLException wrapped = new SQLException("could not connect to the server", "08006");
        wrapped.setNextException(tooManyConnections());
        stub.failWith(wrapped, 1);
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "30");
 
        assertNotNull(CachedConnection.getConnection(url));
        assertEquals(stub.attempts.get(), 2, "the failure behind the one reported must be looked at");
    }
 
    /**
     * The rest of the insufficient_resources class is not worth waiting out: a server out of disk
     * is not made whole by a connection of ours coming back to the pool.
     */
    @Test(timeOut = 120000)
    public void testDiskFullIsNotRetried() throws Exception {
        final String url = StubDriver.PREFIX + "disk-full";
        stub.failWith(new SQLException("could not extend file: No space left on device", "53100"), StubDriver.ALWAYS);
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "30");
 
        try {
            CachedConnection.getConnection(url);
            fail("a database out of disk must be reported to the caller");
        } catch (SQLException expected) {
            assertEquals(expected.getSQLState(), "53100");
        }
        assertEquals(stub.attempts.get(), 1, "a failure that waiting cannot clear must be attempted once");
    }
 
    /**
     * Every other failure is the caller's to report. A password the database does not accept is
     * never going to be accepted by waiting, and the retry that swallowed it left the operation
     * hanging with nothing in the log.
     */
    @Test(timeOut = 120000)
    public void testRejectedLoginIsNotRetried() throws Exception {
        final String url = StubDriver.PREFIX + "rejected";
        stub.failWith(new SQLException("password authentication failed", "28P01"), StubDriver.ALWAYS);
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "30");
 
        try {
            CachedConnection.getConnection(url);
            fail("a rejected login must be reported to the caller");
        } catch (SQLException expected) {
            assertEquals(expected.getSQLState(), "28P01");
        }
        assertEquals(stub.attempts.get(), 1, "a rejected login must be attempted once");
    }
 
    /** A connection the setup of which failed belongs to nobody: it has to be closed, not leaked. */
    @Test(timeOut = 120000)
    public void testConnectionIsClosedWhenItsSetupFails() throws Exception {
        final String url = StubDriver.PREFIX + "setup-failure";
        final Connection broken = mock(Connection.class);
        doThrow(new SQLException("read only")).when(broken).setAutoCommit(false);
        stub.answerWith(broken);
 
        try {
            CachedConnection.getConnection(url);
            fail("a connection that cannot be set up must be reported");
        } catch (SQLException expected) {
            assertEquals(expected.getMessage(), "read only");
        }
        verify(broken).close();
    }
 
    /** The same, for a driver whose failure in the setup is not a SQLException but an unchecked one. */
    @Test(timeOut = 120000)
    public void testConnectionIsClosedWhenItsSetupFailsWithAnUncheckedError() throws Exception {
        final String url = StubDriver.PREFIX + "setup-unchecked";
        final Connection broken = mock(Connection.class);
        doThrow(new IllegalStateException("driver internal")).when(broken).setTransactionIsolation(anyInt());
        stub.answerWith(broken);
 
        try {
            CachedConnection.getConnection(url);
            fail("a connection that cannot be set up must be reported");
        } catch (IllegalStateException expected) {
            assertEquals(expected.getMessage(), "driver internal");
        }
        verify(broken).close();
    }
 
    /**
     * isValid(n) is not a bound at the socket on every driver - the SQL Server driver turns it
     * into a query timeout, which needs an answer from the server to fire - and the read bound of
     * the login was lifted the moment the connection was established, so the socket carries the
     * bound of the validation, for the length of the validation only.
     */
    @Test(timeOut = 120000)
    public void testValidationOfAPooledConnectionIsBoundedAtTheSocket() throws Exception {
        final String url = StubDriver.PREFIX + "validation-bound";
        final Connection pooled = mock(Connection.class);
        when(pooled.isValid(anyInt())).thenReturn(true);
        when(pooled.getNetworkTimeout()).thenReturn(0);
        seedPool(url, pooled);
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertSame(((CachedConnection) borrowed).parent, pooled);
        final InOrder inOrder = inOrder(pooled);
        inOrder.verify(pooled).setNetworkTimeout(any(Executor.class), eq(CachedConnection.VALIDATION_TIMEOUT_SECONDS * 1000));
        inOrder.verify(pooled).isValid(CachedConnection.VALIDATION_TIMEOUT_SECONDS);
        inOrder.verify(pooled).setNetworkTimeout(any(Executor.class), eq(0));
    }
 
    /**
     * A driver that takes the call bounding the validation and then fails inside it is told apart
     * from one that never takes a network timeout at all: it is free to have applied the bound
     * before failing, so the connection is discarded rather than validated unbounded and handed
     * out - pooled, it would carry five seconds of ours into every statement for the rest of its
     * life, and the import batch of a backend open is the first thing to die of that.
     */
    @Test(timeOut = 120000)
    public void testAPooledConnectionThatCannotBeBoundedForItsValidationIsDiscarded() throws Exception {
        final String url = StubDriver.PREFIX + "validation-bound-fails";
        final Connection pooled = mock(Connection.class);
        when(pooled.getNetworkTimeout()).thenReturn(0);
        when(pooled.isValid(anyInt())).thenReturn(true);
        doThrow(new SQLException("the driver took the bound and then failed"))
            .when(pooled).setNetworkTimeout(any(Executor.class), anyInt());
        seedPool(url, pooled);
        final Connection fresh = mock(Connection.class);
        stub.answerWith(fresh);
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertSame(((CachedConnection) borrowed).parent, fresh, "a connection this could not bound was handed out");
        verify(pooled, never()).isValid(anyInt());
        verify(pooled).close();
    }
 
    /**
     * A driver answering a negative network timeout is outside the contract of the call - 0 is no
     * limit and nothing below it stands for anything - and taken back as it is, it is one of the
     * two sentinels this class tells its own outcomes apart by: the bound of the validation would
     * be read as a bound that was never set, and the connection would go back into the pool still
     * carrying five seconds of ours into every statement of the next borrower.
     */
    @Test(timeOut = 120000)
    public void testAPooledConnectionWhoseDriverAnswersANegativeBoundIsPutBackUnbounded() throws Exception {
        final String url = StubDriver.PREFIX + "validation-negative-bound";
        final Connection pooled = mock(Connection.class);
        when(pooled.isValid(anyInt())).thenReturn(true);
        when(pooled.getNetworkTimeout()).thenReturn(-1);
        CachedConnection.poolOf(url).addIdle(new CachedConnection(url, pooled));
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertSame(((CachedConnection) borrowed).parent, pooled);
        final InOrder inOrder = inOrder(pooled);
        inOrder.verify(pooled)
            .setNetworkTimeout(any(Executor.class), eq(CachedConnection.VALIDATION_TIMEOUT_SECONDS * 1000));
        inOrder.verify(pooled).isValid(CachedConnection.VALIDATION_TIMEOUT_SECONDS);
        inOrder.verify(pooled).setNetworkTimeout(any(Executor.class), eq(0));
    }
 
    /** A read bound of the connection string is tighter than ours and stays untouched. */
    @Test(timeOut = 120000)
    public void testValidationLeavesTheBoundOfTheConnectionStringAlone() throws Exception {
        final String url = StubDriver.PREFIX + "validation-tighter";
        final Connection pooled = mock(Connection.class);
        when(pooled.isValid(anyInt())).thenReturn(true);
        when(pooled.getNetworkTimeout()).thenReturn(2000);
        seedPool(url, pooled);
 
        assertNotNull(CachedConnection.getConnection(url));
 
        verify(pooled, never()).setNetworkTimeout(any(Executor.class), anyInt());
    }
 
    /**
     * The pool has no upper bound on the number of connections it holds, and a validation is a
     * round trip: after a failover that left them half-open, draining the pool must not outlive
     * the deadline of the borrow - establishing a connection is the faster answer past it.
     */
    @Test(timeOut = 120000)
    public void testDrainOfThePoolStopsAtTheDeadline() throws Exception {
        final String url = StubDriver.PREFIX + "drain-deadline";
        final int pooled = 8;
        final AtomicInteger validated = new AtomicInteger();
        for (int i = 0; i < pooled; i++) {
            final Connection stale = mock(Connection.class);
            when(stale.isValid(anyInt())).thenAnswer(invocation -> {
                validated.incrementAndGet();
                Thread.sleep(500); // a database that no longer answers: every validation waits out its bound
                return false;
            });
            seedPool(url, stale);
        }
        final Connection fresh = mock(Connection.class);
        stub.answerWith(fresh);
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "1");
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertSame(((CachedConnection) borrowed).parent, fresh);
        assertTrue(validated.get() > 0 && validated.get() < pooled,
            "the drain has to start and to stop at the deadline: " + validated.get() + " of " + pooled);
    }
 
    /** A pooled connection that no longer validates is closed and replaced, not handed out. */
    @Test(timeOut = 120000)
    public void testBrokenPooledConnectionIsDiscarded() throws Exception {
        final String url = StubDriver.PREFIX + "broken-pooled";
        final Connection stale = mock(Connection.class);
        when(stale.isValid(anyInt())).thenReturn(false);
        seedPool(url, stale);
        final Connection fresh = mock(Connection.class);
        when(fresh.isValid(anyInt())).thenReturn(true);
        stub.answerWith(fresh);
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertSame(((CachedConnection) borrowed).parent, fresh);
        verify(stale).close();
        // the validation of a pooled connection needs a bound of its own as well
        verify(stale, never()).isValid(0);
        verify(stale).isValid(CachedConnection.VALIDATION_TIMEOUT_SECONDS);
    }
 
    /**
     * The same for a driver whose answer to the validation is an unchecked failure: it unwinds
     * through poll(), which stands outside every try of the borrow, so the connection it was
     * raised over is already out of the pool and would be held by nobody.
     */
    @Test(timeOut = 120000)
    public void testAPooledConnectionWhoseValidationThrowsIsDiscarded() throws Exception {
        final String url = StubDriver.PREFIX + "validation-unchecked";
        final Connection broken = mock(Connection.class);
        when(broken.isValid(anyInt())).thenThrow(new IllegalStateException("driver internal"));
        seedPool(url, broken);
        final Connection fresh = mock(Connection.class);
        stub.answerWith(fresh);
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertSame(((CachedConnection) borrowed).parent, fresh);
        verify(broken).close();
    }
 
    /** A connection that cannot be rolled back must not go back into the pool - nor be dropped. */
    @Test(timeOut = 120000)
    public void testConnectionThatCannotBeRolledBackIsClosed() throws Exception {
        final String url = StubDriver.PREFIX + "rollback-failure";
        final Connection parent = mock(Connection.class);
        doThrow(new SQLException("connection is closed")).when(parent).rollback();
 
        try {
            new CachedConnection(url, parent).close();
            fail("a failed rollback must be reported");
        } catch (SQLException expected) {
            assertEquals(expected.getMessage(), "connection is closed");
        }
        verify(parent).close();
        assertEquals(CachedConnection.poolOf(url).idleCount(), 0, "a connection that cannot be rolled back was pooled");
    }
 
    /**
     * The same for the unchecked failure a driver is free to throw instead of a SQLException. close()
     * runs past the CAS that makes it the one return of this connection, so a rollback escaping it
     * leaves the connection closed by nothing at all - and its permit released by nothing either,
     * since only destroy() gives one back. A pool is never removed from the map, so that place in
     * the bound would be gone for the life of the server, and enough of them leave every borrow to
     * fail with a SQLTimeoutException while the pool holds no connection at all (#878).
     */
    @Test(timeOut = 120000)
    public void testAConnectionWhoseRollbackFailsUncheckedIsClosed() throws Exception {
        final String url = StubDriver.PREFIX + "rollback-unchecked";
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        doThrow(new IllegalStateException("the connection handle is no longer valid")).when(parent).rollback();
        stub.answerWith(parent);
        final CachedConnection.Pool pool = CachedConnection.poolOf(url);
 
        final Connection con = CachedConnection.getConnection(url);
        assertEquals(pool.meteredCount(), 1, "the borrow took no permit of the pool");
        try {
            con.close();
            fail("a rollback that failed unchecked must be reported");
        } catch (IllegalStateException expected) {
            assertEquals(expected.getMessage(), "the connection handle is no longer valid");
        }
 
        verify(parent).close();
        assertEquals(pool.idleCount(), 0, "a connection that could not be rolled back was pooled");
        assertEquals(pool.meteredCount(), 0, "the return kept a permit of the pool");
    }
 
    @Test
    public void testDialectIsRecognizedByTheConnectionString() throws Exception {
        assertEquals(CachedConnection.ConnectDialect.of("jdbc:postgresql://h:5432/db"), CachedConnection.ConnectDialect.POSTGRES);
        assertEquals(CachedConnection.ConnectDialect.of("jdbc:mysql://h:3306/db"), CachedConnection.ConnectDialect.MYSQL);
        assertEquals(CachedConnection.ConnectDialect.of("jdbc:oracle:thin:@//h:1521/svc"), CachedConnection.ConnectDialect.ORACLE);
        assertEquals(CachedConnection.ConnectDialect.of("jdbc:sqlserver://h:1433;databaseName=db"), CachedConnection.ConnectDialect.MICROSOFT);
        assertNull(CachedConnection.ConnectDialect.of("jdbc:h2:mem:db"), "an unknown engine must not be fed the properties of another");
    }
 
    /** Both phases are bounded, in the units of the driver: the connect alone leaves the login open. */
    @Test
    public void testBothPhasesOfTheLoginAreBounded() throws Exception {
        // pgjdbc puts an SO_TIMEOUT on the login socket only where socketTimeout is set: without it
        // loginTimeout bounds the caller alone, and the thread the driver runs the login on stays
        // parked in the read it abandoned
        final Properties postgres = new Properties();
        assertTrue(CachedConnection.ConnectDialect.POSTGRES.bound("jdbc:postgresql://h:5432/db", postgres, 7),
            "the read bound of postgresql outlives the login and has to be lifted");
        assertEquals(postgres.getProperty("connectTimeout"), "7");
        assertEquals(postgres.getProperty("socketTimeout"), "7");
        assertEquals(postgres.getProperty("loginTimeout"), "7", "the bound of a url naming more than one host");
 
        final Properties mysql = new Properties();
        assertTrue(CachedConnection.ConnectDialect.MYSQL.bound("jdbc:mysql://h:3306/db", mysql, 7),
            "the read bound of mysql outlives the login and has to be lifted");
        assertEquals(mysql.getProperty("connectTimeout"), "7000");
        assertEquals(mysql.getProperty("socketTimeout"), "7000");
 
        final Properties oracle = new Properties();
        assertTrue(CachedConnection.ConnectDialect.ORACLE.bound("jdbc:oracle:thin:@//h:1521/svc", oracle, 7));
        assertEquals(oracle.getProperty("oracle.net.CONNECT_TIMEOUT"), "7000");
        assertEquals(oracle.getProperty("oracle.jdbc.ReadTimeout"), "7000");
 
        // the loginTimeout of the sql server driver leaves the read of the prelogin answer open
        final Properties microsoft = new Properties();
        assertTrue(CachedConnection.ConnectDialect.MICROSOFT.bound("jdbc:sqlserver://h:1433;databaseName=db", microsoft, 7));
        assertEquals(microsoft.getProperty("loginTimeout"), "7");
        assertEquals(microsoft.getProperty("socketTimeout"), "7000");
    }
 
    /**
     * A driver with a range of its own for its connect property is never handed a value beyond it:
     * SQLServerDriverIntProperty.LOGIN_TIMEOUT is validated against [0, 65535], so a bound past
     * that would not widen the connect, it would fail every one of them.
     */
    @Test
    public void testConnectBoundStaysInTheRangeTheDriverTakes() throws Exception {
        final Properties microsoft = new Properties();
        CachedConnection.ConnectDialect.MICROSOFT.bound("jdbc:sqlserver://h:1433;databaseName=db", microsoft, 100000);
        assertEquals(microsoft.getProperty("loginTimeout"), "65535");
        assertEquals(microsoft.getProperty("socketTimeout"), "100000000", "the read bound takes any value");
    }
 
    /**
     * A bound the administrator put into the connection string by hand - the only workaround this
     * backend had - keeps precedence, property by property.
     */
    @Test
    public void testConnectionStringKeepsPrecedence() throws Exception {
        final Properties postgres = new Properties();
        CachedConnection.ConnectDialect.POSTGRES.bound(
            "jdbc:postgresql://h:5432/db?user=u&password=p&loginTimeout=30&socketTimeout=300", postgres, 7);
        assertNull(postgres.getProperty("loginTimeout"), "the setting of the connection string was overridden");
        assertNull(postgres.getProperty("socketTimeout"), "the setting of the connection string was overridden");
        assertNull(postgres.getProperty("connectTimeout"),
            "the connect side is one budget: a bound of the administrator under either of its names is theirs");
 
        // the sql server driver gives a supplied property precedence over the one of the url
        final Properties microsoft = new Properties();
        CachedConnection.ConnectDialect.MICROSOFT.bound("jdbc:sqlserver://h:1433;loginTimeout=45;databaseName=db", microsoft, 7);
        assertNull(microsoft.getProperty("loginTimeout"), "the setting of the connection string was overridden");
        assertEquals(microsoft.getProperty("socketTimeout"), "7000");
 
        // inside the descriptor of an oracle tns url the property goes by the last segment of its name
        final Properties oracle = new Properties();
        CachedConnection.ConnectDialect.ORACLE.bound(
            "jdbc:oracle:thin:@(DESCRIPTION=(CONNECT_TIMEOUT=3)(ADDRESS=(HOST=h)(PORT=1521)))", oracle, 7);
        assertNull(oracle.getProperty("oracle.net.CONNECT_TIMEOUT"));
        assertEquals(oracle.getProperty("oracle.jdbc.ReadTimeout"), "7000");
 
        // inside the descriptor the read bound goes by READ_TIMEOUT, and one of the administrator
        // is never lifted after the login, because ours is not set on top of it
        final Properties read = new Properties();
        assertFalse(CachedConnection.ConnectDialect.ORACLE.bound(
            "jdbc:oracle:thin:@(DESCRIPTION=(READ_TIMEOUT=30)(ADDRESS=(HOST=h)(PORT=1521)))", read, 7),
            "a read bound of the connection string must not be lifted once the login is through");
        assertNull(read.getProperty("oracle.jdbc.ReadTimeout"));
 
        // ... while RECV_TIMEOUT is a parameter of sqlnet.ora and of the listener that ojdbc8 does
        // not read - the name appears nowhere in the driver - so a descriptor carrying one is not
        // a read bound of the connection and must not take ours off it
        final Properties recv = new Properties();
        assertTrue(CachedConnection.ConnectDialect.ORACLE.bound(
            "jdbc:oracle:thin:@(DESCRIPTION=(RECV_TIMEOUT=30)(ADDRESS=(HOST=h)(PORT=1521)))", recv, 7),
            "a parameter no driver reads left the login of this connection unbounded");
        assertEquals(recv.getProperty("oracle.jdbc.ReadTimeout"), "7000");
 
        // a name that only appears as the tail of another parameter is not a setting of its own
        final Properties mysql = new Properties();
        CachedConnection.ConnectDialect.MYSQL.bound("jdbc:mysql://h:3306/db?xconnectTimeout=1&socketTimeoutX=2", mysql, 7);
        assertEquals(mysql.getProperty("connectTimeout"), "7000");
        assertEquals(mysql.getProperty("socketTimeout"), "7000");
    }
 
    /**
     * A parameter is recognized the way the driver of its dialect recognizes it: pgjdbc and
     * Connector/J look their properties up by their exact name, so a name of another case is a
     * parameter of nobody - neither side bounds anything by it - and must not pass for a bound the
     * administrator set, while the other two match either way.
     */
    @Test
    public void testTheCaseOfAParameterIsTheOneOfItsDriver() throws Exception {
        final Properties postgres = new Properties();
        CachedConnection.ConnectDialect.POSTGRES.bound("jdbc:postgresql://h:5432/db?ConnectTimeout=5", postgres, 7);
        assertEquals(postgres.getProperty("connectTimeout"), "7", "pgjdbc ignores a parameter of another case");
 
        // PropertyKey.fromValue("SocketTimeout") answers null, and Connector/J then reads no bound
        // out of the url either: a mis-cased parameter left the borrower parked on a host that
        // completes the handshake and says nothing
        final Properties mysql = new Properties();
        CachedConnection.ConnectDialect.MYSQL.bound("jdbc:mysql://h:3306/db?SocketTimeout=1", mysql, 7);
        assertEquals(mysql.getProperty("socketTimeout"), "7000", "Connector/J ignores a parameter of another case");
 
        final Properties microsoft = new Properties();
        CachedConnection.ConnectDialect.MICROSOFT.bound("jdbc:sqlserver://h:1433;LoginTimeout=45", microsoft, 7);
        assertNull(microsoft.getProperty("loginTimeout"), "the sql server driver normalizes the name of a property");
 
        // the keywords of an oracle descriptor are matched without regard to case as well
        final Properties oracle = new Properties();
        CachedConnection.ConnectDialect.ORACLE.bound(
            "jdbc:oracle:thin:@(description=(connect_timeout=3)(address=(host=h)(port=1521)))", oracle, 7);
        assertNull(oracle.getProperty("oracle.net.CONNECT_TIMEOUT"), "an oracle descriptor is read without case");
    }
 
    /**
     * The connection string is not the only channel of the administrator: the oracle driver reads
     * some of its properties out of the system properties as well, which is how a whole jvm is
     * bounded with -Doracle.jdbc.ReadTimeout. A property supplied to the driver outranks that one
     * without a word, and this class would then lift it once the login is through as if it were
     * its own - leaving a connection with no read bound at all where the administrator set one.
     */
    @Test
    public void testASystemPropertyOfTheAdministratorKeepsPrecedence() throws Exception {
        System.setProperty("oracle.jdbc.ReadTimeout", "30000");
        try {
            final Properties oracle = new Properties();
            assertFalse(CachedConnection.ConnectDialect.ORACLE.bound("jdbc:oracle:thin:@//h:1521/svc", oracle, 7),
                "a read bound of the administrator must not be lifted once the login is through");
            assertNull(oracle.getProperty("oracle.jdbc.ReadTimeout"), "the setting of the administrator was overridden");
            assertEquals(oracle.getProperty("oracle.net.CONNECT_TIMEOUT"), "7000",
                "the property it leaves open must still be bounded");
        } finally {
            System.clearProperty("oracle.jdbc.ReadTimeout");
        }
 
        // the connect property of the same driver, over the same channel
        System.setProperty("oracle.net.CONNECT_TIMEOUT", "30000");
        try {
            final Properties oracle = new Properties();
            CachedConnection.ConnectDialect.ORACLE.bound("jdbc:oracle:thin:@//h:1521/svc", oracle, 7);
            assertNull(oracle.getProperty("oracle.net.CONNECT_TIMEOUT"), "the setting of the administrator was overridden");
        } finally {
            System.clearProperty("oracle.net.CONNECT_TIMEOUT");
        }
 
        // a plain name is common enough to be somebody else's system property: only a name a
        // driver of these actually reads out of them is one of the administrator's
        System.setProperty("socketTimeout", "30000");
        try {
            final Properties mysql = new Properties();
            assertTrue(CachedConnection.ConnectDialect.MYSQL.bound("jdbc:mysql://h:3306/db", mysql, 7));
            assertEquals(mysql.getProperty("socketTimeout"), "7000");
        } finally {
            System.clearProperty("socketTimeout");
        }
    }
 
    /**
     * ... and a dotted name is not a name a driver reads out of the system properties by the shape
     * of it. ojdbc8 resolves oracle.jdbc.ReadTimeout and oracle.net.CONNECT_TIMEOUT in three tiers
     * (the properties it was supplied, then System.getProperty, then the properties of the data
     * source), while oracle.net.READ_TIMEOUT - a dotted name of the same driver, and the name the
     * socket option is finally read under - reaches the socket from the connection properties
     * alone: the classes carrying the literal hand it to Properties.get, none of them to
     * System.getProperty. Taken for a bound of the administrator, a -D of it leaves the login with
     * no read bound whatever: theirs is not read and ours is not set.
     */
    @Test
    public void testASystemPropertyNoDriverReadsIsNoBound() throws Exception {
        System.setProperty("oracle.net.READ_TIMEOUT", "30000");
        try {
            final Properties oracle = new Properties();
            assertTrue(CachedConnection.ConnectDialect.ORACLE.bound("jdbc:oracle:thin:@//h:1521/svc", oracle, 7),
                "a -D the driver never reads left this login with no read bound at all");
            assertEquals(oracle.getProperty("oracle.jdbc.ReadTimeout"), "7000");
        } finally {
            System.clearProperty("oracle.net.READ_TIMEOUT");
        }
 
        // ... while the same name written into the connection string is one the driver does read
        final Properties declared = new Properties();
        assertFalse(CachedConnection.ConnectDialect.ORACLE.bound(
            "jdbc:oracle:thin:@(DESCRIPTION=(READ_TIMEOUT=30)(ADDRESS=(HOST=h)(PORT=1521)))", declared, 7));
        assertNull(declared.getProperty("oracle.jdbc.ReadTimeout"));
    }
 
    /**
     * A property the administrator set to 0 is not a bound of theirs: every one of these drivers
     * reads 0 as "wait as long as it takes", which is the default this class exists to replace -
     * and on the three whose driver lets a supplied property win, ours is set on top of it.
     * Postgresql is the one where it cannot be, and is covered on its own below.
     */
    @Test
    public void testAZeroIsNotABoundOfTheAdministrator() throws Exception {
        final Properties mysql = new Properties();
        CachedConnection.ConnectDialect.MYSQL.bound("jdbc:mysql://h:3306/db?connectTimeout=0&socketTimeout=0", mysql, 7);
        assertEquals(mysql.getProperty("connectTimeout"), "7000");
        assertEquals(mysql.getProperty("socketTimeout"), "7000");
 
        // ... and neither is a property left without a value
        final Properties microsoft = new Properties();
        CachedConnection.ConnectDialect.MICROSOFT.bound("jdbc:sqlserver://h:1433;socketTimeout=;databaseName=db", microsoft, 7);
        assertEquals(microsoft.getProperty("socketTimeout"), "7000");
 
        // the same of a system property, and of the descriptor of an oracle url
        System.setProperty("oracle.jdbc.ReadTimeout", "0");
        try {
            final Properties oracle = new Properties();
            assertTrue(CachedConnection.ConnectDialect.ORACLE.bound(
                "jdbc:oracle:thin:@(DESCRIPTION=(READ_TIMEOUT=0)(ADDRESS=(HOST=h)(PORT=1521)))", oracle, 7));
            assertEquals(oracle.getProperty("oracle.jdbc.ReadTimeout"), "7000");
        } finally {
            System.clearProperty("oracle.jdbc.ReadTimeout");
        }
 
        // a value that is no number is left to the driver it belongs to: it is not this class's to read
        final Properties unreadable = new Properties();
        assertFalse(CachedConnection.ConnectDialect.MYSQL.bound(
            "jdbc:mysql://h:3306/db?socketTimeout=PT30S", unreadable, 7));
        assertNull(unreadable.getProperty("socketTimeout"));
    }
 
    /**
     * On postgresql a parameter of the url outranks the property this class supplies: Driver
     * .connect copies what it was handed into a flat map and parseURL then writes the parameters of
     * the url on top of it. So a "socketTimeout=0" there cannot be replaced, and setting ours
     * regardless would leave this class believing it bounded a login that carries no bound - and
     * lifting a read bound after it that was never in force. The effective values are read back
     * through the parser of the driver itself, since asserting on the map handed to it is
     * asserting on the half of the story this bug lived in.
     */
    @Test
    public void testAParameterOfAPostgresUrlOutranksTheBoundOfThisClass() throws Exception {
        final String url = "jdbc:postgresql://h:5432/db?connectTimeout=0&socketTimeout=0&loginTimeout=0";
        final Properties supplied = new Properties();
        assertFalse(CachedConnection.ConnectDialect.POSTGRES.bound(url, supplied, 7),
            "a read bound that never reaches the driver must not be reported as one to lift");
        assertNull(supplied.getProperty("socketTimeout"));
        assertNull(supplied.getProperty("connectTimeout"));
        assertNull(supplied.getProperty("loginTimeout"));
 
        final Properties effective = org.postgresql.Driver.parseURL(url, supplied);
        assertEquals(effective.getProperty("socketTimeout"), "0", "the url is what the driver ends up reading");
        assertEquals(effective.getProperty("connectTimeout"), "0");
        assertEquals(effective.getProperty("loginTimeout"), "0");
    }
 
    /**
     * The connect side of a dialect is one budget rather than a set of independent knobs, so a
     * bound of the administrator under any of its names leaves all of them alone. On postgresql
     * connectTimeout bounds the socket connect and loginTimeout the login behind it: filling in the
     * one they left out caps the one they set, since Driver.connect hands the login to a thread of
     * its own as soon as loginTimeout is anything but 0 and gives up on it there.
     */
    @Test
    public void testAConnectBudgetOfTheAdministratorIsNotCappedByThisClass() throws Exception {
        final String url = "jdbc:postgresql://h:5432/db?connectTimeout=300";
        final Properties supplied = new Properties();
        assertTrue(CachedConnection.ConnectDialect.POSTGRES.bound(url, supplied, 30),
            "the read bound is a budget of its own and is still set");
        assertNull(supplied.getProperty("loginTimeout"),
            "a loginTimeout of ours caps the connectTimeout the administrator set");
        assertNull(supplied.getProperty("connectTimeout"));
        assertEquals(supplied.getProperty("socketTimeout"), "30");
 
        final Properties effective = org.postgresql.Driver.parseURL(url, supplied);
        assertEquals(effective.getProperty("connectTimeout"), "300", "the budget of the administrator, in full");
        assertNull(effective.getProperty("loginTimeout"), "nothing of ours hands this login to a thread to abandon");
    }
 
    /**
     * The bound handed to a driver stays inside the range an int of milliseconds takes. Where the
     * per-attempt property is off, the attempt takes what is left of the deadline of the borrow,
     * and the pool timeout has no upper bound of its own - while the SQL Server driver rejects a
     * socketTimeout past Integer.MAX_VALUE outright, failing every connect of that backend with
     * the name of a property nobody typed.
     */
    @Test(timeOut = 120000)
    public void testTheBoundHandedToADriverStaysInTheRangeAnIntTakes() throws Exception {
        final long deadline = System.currentTimeMillis() + 3000000L * 1000; // 34 days: past 2^31 ms
        assertEquals(CachedConnection.attemptSeconds(0, deadline), Integer.MAX_VALUE / 1000);
 
        System.setProperty(CachedConnection.CONNECT_TIMEOUT_PROPERTY, "0");
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "3000000");
        // a socket that answers the connect and closes it, rather than a port bound and released:
        // with every bound of this borrow turned off, anything taking that port in between would
        // leave the test hanging on the timeOut instead of failing
        try (final ServerSocket rejecting = rejectingSocket()) {
            final String url = "jdbc:sqlserver://127.0.0.1:" + rejecting.getLocalPort()
                + ";databaseName=opendj;user=opendj;password=opendj;encrypt=false";
            final long startedAt = System.currentTimeMillis();
            try {
                CachedConnection.getConnection(url);
                fail("a connect the database closes must be reported");
            } catch (SQLException expected) {
                assertFalse(expected.getMessage().contains("socketTimeout"),
                    "the driver was handed a bound it does not take: " + expected.getMessage());
            }
            assertElapsedWithinBound(startedAt, 0);
        }
    }
 
    /** The clamp of the range holds where the borrow has no deadline to take it from either. */
    @Test
    public void testTheBoundOfAnAttemptStaysInRangeWithoutADeadline() throws Exception {
        assertEquals(CachedConnection.attemptSeconds(Long.MAX_VALUE, Long.MAX_VALUE), Integer.MAX_VALUE / 1000);
        assertEquals(CachedConnection.attemptSeconds(0, Long.MAX_VALUE), 0, "0 stands for an attempt with no bound");
        assertEquals(CachedConnection.attemptSeconds(30, Long.MAX_VALUE), 30);
    }
 
    /** The connection string holds the credentials of the backend: a stall report must not carry them. */
    @Test
    public void testLoggedConnectionStringCarriesNoCredentials() throws Exception {
        assertEquals(CachedConnection.safeUrl("jdbc:postgresql://h:5432/db?user=u&password=secret"), "jdbc:postgresql://h:5432/db");
        // the parameter naming the database stays: two backends of one sql server host answer to
        // the same url up to it, and a stall report that cannot tell them apart is one of neither
        assertEquals(CachedConnection.safeUrl("jdbc:sqlserver://h:1433;databaseName=db;password=secret"),
            "jdbc:sqlserver://h:1433;databaseName=db");
        // ... and the token naming the kind of oracle driver stays as well: thin against oci is a
        // first question of an oracle connect, and it stands in front of the credentials
        assertEquals(CachedConnection.safeUrl("jdbc:oracle:thin:scott/secret@//h:1521/svc"), "jdbc:oracle:thin:@//h:1521/svc");
        assertEquals(CachedConnection.safeUrl("jdbc:mysql://u:secret@h:3306/db"), "jdbc:mysql://h:3306/db");
        assertEquals(CachedConnection.safeUrl("jdbc:oracle:thin:@//h:1521/svc"), "jdbc:oracle:thin:@//h:1521/svc");
        // a password holding the parameter separator of another dialect: ";" separates nothing on
        // an oracle url, so the credentials are cut in front of the "@" rather than inside them
        assertEquals(CachedConnection.safeUrl("jdbc:oracle:thin:scott/pa;ss@//h:1521/svc"), "jdbc:oracle:thin:@//h:1521/svc");
        // ... and one holding a ":" is cut in front of it too: the token of the driver is the one
        // behind the subprotocol, not the last one standing in front of the "@"
        assertEquals(CachedConnection.safeUrl("jdbc:oracle:thin:scott/pa:ss@//h:1521/svc"), "jdbc:oracle:thin:@//h:1521/svc");
        // ... and an "@" that stands inside a parameter is not the end of credentials: the host survives
        assertEquals(CachedConnection.safeUrl("jdbc:postgresql://h:5432/db?user=u@example.com&password=secret"),
            "jdbc:postgresql://h:5432/db");
        // a password holding the parameter separator of its own dialect: on an oracle url the
        // parameters stand behind the descriptor, so a "?" in front of the "@" is part of the
        // password and cutting there would leave the start of it in the log
        assertEquals(CachedConnection.safeUrl("jdbc:oracle:thin:scott/pa?ss@//h:1521/svc"), "jdbc:oracle:thin:@//h:1521/svc");
        // the same inside an authority, where the credentials end at the path rather than at a "?"
        assertEquals(CachedConnection.safeUrl("jdbc:mysql://u:sec?ret@h:3306/db"), "jdbc:mysql://h:3306/db");
        // a descriptor of an oracle url carries no credentials and survives whole
        assertEquals(CachedConnection.safeUrl("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=h)(PORT=1521)))"),
            "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=h)(PORT=1521)))");
        // a first host with nothing in front of the comma keeps the comma: what is left is the
        // hosts of a url, not one host of it
        assertEquals(CachedConnection.safeUrl("jdbc:mysql://,h2:3306/db"), "jdbc:mysql://,h2:3306/db");
        // a password under a name of its own, and one numbered by the factor it belongs to
        assertEquals(CachedConnection.safeUrl("jdbc:mysql://(host=h,user=u,password2=secret)/db"),
            "jdbc:mysql://(host=h,user=u,password2=***)/db");
 
        // a url of Connector/J gives every host of it credentials of its own, and every one of
        // them goes: the second used to stay in the message with the password of the failover host
        assertEquals(CachedConnection.safeUrl("jdbc:mysql://u:p@h1:3306,u2:p2@h2:3306/db"),
            "jdbc:mysql://h1:3306,h2:3306/db");
        // ... including a url whose subprotocol names the kind of connection in front of the hosts
        assertEquals(CachedConnection.safeUrl("jdbc:mysql:replication://master:p1@h1:3306,slave:p2@h2:3306/db"),
            "jdbc:mysql:replication://h1:3306,h2:3306/db");
        // the key-value host syntax of Connector/J puts the credentials inside the authority, where
        // neither the userinfo nor the parameters of a url stand
        assertEquals(CachedConnection.safeUrl("jdbc:mysql://address=(host=h)(port=3306)(user=u)(password=secret)/db"),
            "jdbc:mysql://address=(host=h)(port=3306)(user=u)(password=***)/db");
        assertEquals(CachedConnection.safeUrl("jdbc:mysql://(host=h,port=3306,user=u,password=secret)/db"),
            "jdbc:mysql://(host=h,port=3306,user=u,password=***)/db");
        assertEquals(CachedConnection.safeUrl("jdbc:sqlserver://h:1433;databaseName=db;PWD=secret"),
            "jdbc:sqlserver://h:1433;databaseName=db");
 
        // a shape none of this took apart is not logged past its subprotocol: a password holding a
        // "/" ends the authority in front of the "@" that would have given the credentials away
        assertEquals(CachedConnection.safeUrl("jdbc:mysql://u:pa/ss@h:3306/db"),
            "jdbc:mysql:" + CachedConnection.CREDENTIALS_HIDDEN);
        // ... and so does a password that holds an "@" and was quoted for the driver
        assertEquals(CachedConnection.safeUrl("jdbc:oracle:thin:scott/\"pa@ss\"@//h:1521/svc"),
            "jdbc:oracle:" + CachedConnection.CREDENTIALS_HIDDEN);
        // a string that is no connection string of any driver carries nothing to the log either
        assertEquals(CachedConnection.safeUrl("h:5432/db?password=secret"), CachedConnection.CREDENTIALS_HIDDEN);
    }
 
    /**
     * A driver is free to quote the connection string it was handed back into the message of its
     * failure, and that message travels: RootContainer makes it the message of what it throws,
     * BackendConfigManager logs it at ERROR and answers a config change with it. So the message is
     * redacted rather than the two call sites that happen to log a url.
     */
    @Test
    public void testAMessageOfADriverCarriesNoCredentials() throws Exception {
        final String url = "jdbc:postgresql://opendj:S3cret@h:5432/db";
        assertEquals(CachedConnection.redact("No suitable driver found for " + url, url),
            "No suitable driver found for jdbc:postgresql://h:5432/db");
        // a driver naming the credentials alone, without the url around them
        assertEquals(CachedConnection.redact("authentication of opendj:S3cret failed", url),
            "authentication of " + CachedConnection.CREDENTIALS_HIDDEN + " failed");
        // ... and naming the password alone
        assertEquals(CachedConnection.redact("the password S3cret was not accepted", url),
            "the password " + CachedConnection.CREDENTIALS_HIDDEN + " was not accepted");
        // the credentials of an oracle url stand in front of its descriptor
        final String oracle = "jdbc:oracle:thin:scott/S3cret@//h:1521/svc";
        assertEquals(CachedConnection.redact("IO Error connecting to " + oracle, oracle),
            "IO Error connecting to jdbc:oracle:thin:@//h:1521/svc");
        // a password of a parameter is blanked wherever the message carries it
        assertEquals(CachedConnection.redact("bad url jdbc:sqlserver://h:1433;password=S3cret",
            "jdbc:sqlserver://h:1433;password=S3cret"), "bad url jdbc:sqlserver://h:1433");
        // a message naming nothing of the connection string is left as it stands
        assertEquals(CachedConnection.redact("Connection to h:5432 refused", url), "Connection to h:5432 refused");
        assertNull(CachedConnection.redact(null, url));
 
        // the stall report is the other way a driver's message reaches the log, and it carries the
        // url of the backend alongside it
        final String stall = CachedConnection.stallMessage(url, 3, 4000,
            new SQLException("FATAL: too many connections for " + url));
        assertFalse(stall.contains("S3cret"), stall);
        assertTrue(stall.contains("jdbc:postgresql://h:5432/db"), stall);
        assertTrue(stall.contains("4000 ms") && stall.contains("(3 attempts)"), stall);
    }
 
    /**
     * A password is free to be one character long, and a bare one of those stands inside half the
     * lines a driver writes. Replaced wherever it is found, it takes the diagnostic apart along
     * with the credential - and, since the walk looking for credentials asks the redaction whether
     * it changed anything, it makes every failure of that backend one whose chain is rebuilt.
     */
    @Test
    public void testAShortPasswordDoesNotRewriteTheMessageOfADriver() throws Exception {
        final String url = "jdbc:oracle:thin:opendj/1@//h:1521/svc";
        // the "1" of an ORA number is part of a number, not a credential of anybody
        assertEquals(CachedConnection.redact("ORA-12541: TNS:no listener", url), "ORA-12541: TNS:no listener");
        // ... while the same password quoted back on its own is still taken out
        assertEquals(CachedConnection.redact("the password 1 was not accepted", url),
            "the password " + CachedConnection.CREDENTIALS_HIDDEN + " was not accepted");
        assertEquals(CachedConnection.redact("IO Error connecting to " + url, url),
            "IO Error connecting to jdbc:oracle:thin:@//h:1521/svc");
    }
 
    /**
     * A driver is free to name a parameter in the middle of a sentence. The value of one ends at
     * the first space: reaching to the end of the string, it would take the host, the port and the
     * cause of the failure into the blank along with the password - the very information the
     * redaction of a connection string goes out of its way to keep.
     */
    @Test
    public void testAPasswordParameterDoesNotSwallowTheRestOfTheMessage() throws Exception {
        final String url = "jdbc:postgresql://h:5432/db?user=u&password=hunter2";
        assertEquals(CachedConnection.redact("Connection refused: password=hunter2 for user u at h:5432", url),
            "Connection refused: password=*** for user u at h:5432");
    }
 
    /**
     * The credentials of an oracle url are separated by a "/", so a password holding a "//" of
     * its own used to start an authority inside itself: what was taken off as a userinfo was the
     * tail of the password, the "@" the last guard of safeUrl() looks for went with it, and the
     * user name and the head of the password stayed in the message of a stall.
     */
    @Test
    public void testAPasswordHoldingASlashPairIsNotLeftInTheLog() throws Exception {
        final String easyConnect = "jdbc:oracle:thin:opendj/pa//ss@//h:1521/svc";
        assertEquals(CachedConnection.safeUrl(easyConnect), "jdbc:oracle:thin:@//h:1521/svc");
        // ... and the same password in front of a host that names no "//" of its own
        final String sid = "jdbc:oracle:thin:opendj/pa//ss@h:1521:svc";
        assertEquals(CachedConnection.safeUrl(sid), "jdbc:oracle:thin:@h:1521:svc");
        // the message of a driver quoting the url back carries no more of it than the log does
        assertEquals(CachedConnection.redact("IO Error connecting to " + easyConnect, easyConnect),
            "IO Error connecting to jdbc:oracle:thin:@//h:1521/svc");
    }
 
    /**
     * pgjdbc enforces loginTimeout out of process: Driver.connect hands the login to a daemon
     * thread of its own and gives up on the thread rather than on the login. Against the database
     * this bound exists for - one that completes the handshake and then says nothing - an
     * unbounded read there leaves that thread, and the socket it holds, behind on every borrow;
     * a few operations a second are enough to run the server out of threads and file descriptors.
     */
    @Test(timeOut = 300000)
    public void testTheLoginThreadOfPostgresDoesNotOutliveTheBorrow() throws Exception {
        System.setProperty(CachedConnection.CONNECT_TIMEOUT_PROPERTY, Long.toString(BOUND_SECONDS));
        // counted as a delta of this borrow rather than as a count of the jvm: three tests of this
        // file open a pgjdbc login against a socket that never answers, and the order they run in
        // is not contractual - a thread left by any of them would be reported here
        final int before = loginThreadsOfPostgres();
        try (final ServerSocket blackhole = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) {
            final String url = "jdbc:postgresql://127.0.0.1:" + blackhole.getLocalPort() + "/opendj?user=opendj&password=opendj";
            try {
                CachedConnection.getConnection(url);
                fail("a database that never answers must not hand out a connection");
            } catch (SQLException expected) {
                // reported to the caller, as the bound of the attempt promises
            }
            final long giveUpAt = System.currentTimeMillis() + BOUND_SECONDS * 1000 + BOUND_MARGIN_MS;
            while (loginThreadsOfPostgres() > before && System.currentTimeMillis() < giveUpAt) {
                Thread.sleep(100);
            }
            assertTrue(loginThreadsOfPostgres() <= before,
                "the login thread pgjdbc abandoned outlived the borrow: the read of the login is not bounded");
        }
    }
 
    private static int loginThreadsOfPostgres() {
        int alive = 0;
        for (final Thread thread : Thread.getAllStackTraces().keySet()) {
            if (thread.isAlive() && thread.getName().startsWith("PostgreSQL JDBC driver connection thread")) {
                alive++;
            }
        }
        return alive;
    }
 
    /**
     * The deadline of the borrow stands for the whole borrow, so it bounds the attempt inside it
     * even where the per-attempt property gives it no bound of its own: turning that property off
     * must not turn the bound of the borrow off with it.
     */
    @Test(timeOut = 300000)
    public void testTheDeadlineBoundsAnAttemptTheConnectPropertyDoesNot() throws Exception {
        System.setProperty(CachedConnection.CONNECT_TIMEOUT_PROPERTY, "0");
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "2");
        try (final ServerSocket blackhole = new ServerSocket(0, 50, InetAddress.getLoopbackAddress())) {
            final String url = "jdbc:postgresql://127.0.0.1:" + blackhole.getLocalPort() + "/opendj?user=opendj&password=opendj";
            final long startedAt = System.currentTimeMillis();
            try {
                CachedConnection.getConnection(url);
                fail("a database that never answers must not hand out a connection");
            } catch (SQLException expected) {
                // bounded by what is left of the deadline of the borrow
            }
            // the wait is the point of this one as much as its end is: a borrow against a socket that
            // never answers cannot be over before the deadline unless something else ended it
            final long elapsed = System.currentTimeMillis() - startedAt;
            assertTrue(elapsed >= 1000, "the borrow was over after " + elapsed + " ms, before its deadline");
            assertElapsedWithinBound(startedAt, 2000);
        }
    }
 
    /**
     * The deadline stops the drain of the pool; it does not throw away the connection in hand. A
     * database at its connection limit has no other source of connections than the ones coming
     * back to the pool, and closing one unvalidated takes it out of that source for good - while
     * the borrow that closed it fails with a timeout anyway.
     */
    @Test(timeOut = 120000)
    public void testAPooledConnectionIsNotDiscardedUnvalidatedAtTheDeadline() throws Exception {
        final String url = StubDriver.PREFIX + "unvalidated-at-deadline";
        final Connection stale = mock(Connection.class);
        when(stale.isValid(anyInt())).thenAnswer(invocation -> {
            Thread.sleep(1500); // a database that no longer answers: the validation waits out its bound
            return false;
        });
        final Connection good = mock(Connection.class);
        when(good.isValid(anyInt())).thenReturn(true);
        seedPool(url, stale, good);
        final Connection fresh = mock(Connection.class);
        stub.answerWith(fresh);
        System.setProperty(CachedConnection.POOL_TIMEOUT_PROPERTY, "1");
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertSame(((CachedConnection) borrowed).parent, fresh, "the drain must stop at the deadline");
        verify(stale).close();
        verify(good, never()).close();
        assertEquals(CachedConnection.poolOf(url).idleCount(), 1,
            "a connection the deadline was reached in front of was lost");
    }
 
    /**
     * A connection the validation of which failed is on its way out, and its driver knows it:
     * Connector/J answers a failed validation by aborting the connection and the SQL Server driver
     * by terminating it. Putting the previous bound back on it fails, and warns about statements
     * of a connection that is being closed - over an idle connection the server reaped, which is
     * nobody's problem.
     */
    @Test(timeOut = 120000)
    public void testAConnectionOnItsWayOutIsNotGivenItsBoundBack() throws Exception {
        final String url = StubDriver.PREFIX + "reaped-idle";
        final Connection reaped = mock(Connection.class);
        when(reaped.getNetworkTimeout()).thenReturn(0);
        when(reaped.isValid(anyInt())).thenReturn(false);
        seedPool(url, reaped);
        final Connection fresh = mock(Connection.class);
        stub.answerWith(fresh);
 
        assertSame(((CachedConnection) CachedConnection.getConnection(url)).parent, fresh);
 
        verify(reaped).setNetworkTimeout(any(Executor.class), eq(CachedConnection.VALIDATION_TIMEOUT_SECONDS * 1000));
        verify(reaped, never()).setNetworkTimeout(any(Executor.class), eq(0));
        verify(reaped).close();
    }
 
    /**
     * The read bound of the login is lifted once the login is through, because left in place it
     * fails every statement slower than it. A driver that will not take it back leaves a
     * connection that must not be pooled: it would carry that bound into every borrow the pool
     * hands it to, an import batch among them.
     */
    @Test(timeOut = 120000)
    public void testAConnectionStillCarryingTheBoundOfItsLoginIsNotPooled() throws Exception {
        final String url = StubDriver.PREFIX + "unliftable-bound";
        final Connection parent = mock(Connection.class);
        doThrow(new SQLException("setNetworkTimeout is not supported"))
            .when(parent).setNetworkTimeout(any(Executor.class), eq(0));
        stub.answerWith(parent);
 
        final CachedConnection.Pool pool = CachedConnection.poolOf(url);
        final CachedConnection borrowed = CachedConnection.connect(url, CachedConnection.ConnectDialect.MYSQL, 30,
            pool, false);
        borrowed.close();
 
        verify(parent).close();
        assertEquals(CachedConnection.poolOf(url).idleCount(), 0,
            "a connection still carrying the read bound of its login went back into the pool");
    }
 
    /**
     * The case the window exists for: the connection this borrow takes out answered the database a
     * moment ago, and asking it again costs the round trip the operation came to make.
     */
    @Test(timeOut = 120000)
    public void testAConnectionProvenAliveIsNotValidatedAgainWithinTheWindow() throws Exception {
        final String url = StubDriver.PREFIX + "within-the-window";
        CachedConnection.aliveBypassNanos = TimeUnit.HOURS.toNanos(1);
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        stub.answerWith(parent);
 
        final Connection first = CachedConnection.getConnection(url); // established: it has just answered
        first.close();
        final Connection second = CachedConnection.getConnection(url);
 
        assertSame(second, first, "the pooled connection was not the one handed back");
        assertEquals(stub.attempts.get(), 1, "the pool established a second connection");
        verify(parent, never()).isValid(anyInt());
    }
 
    /** Past the window it is the connection the database or a firewall may have dropped meanwhile. */
    @Test(timeOut = 120000)
    public void testAConnectionIsValidatedAgainOnceTheWindowHasPassed() throws Exception {
        final String url = StubDriver.PREFIX + "past-the-window";
        CachedConnection.aliveBypassNanos = TimeUnit.MILLISECONDS.toNanos(1);
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        stub.answerWith(parent);
 
        final Connection first = CachedConnection.getConnection(url);
        first.close();
        Thread.sleep(20);
        final Connection second = CachedConnection.getConnection(url);
 
        assertSame(second, first);
        verify(parent).isValid(CachedConnection.VALIDATION_TIMEOUT_SECONDS);
    }
 
    /** The window switched off validates every borrow, the way this pool did before it existed. */
    @Test(timeOut = 120000)
    public void testAWindowOfZeroValidatesEveryBorrow() throws Exception {
        final String url = StubDriver.PREFIX + "window-of-zero";
        CachedConnection.aliveBypassNanos = 0;
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        stub.answerWith(parent);
 
        final Connection first = CachedConnection.getConnection(url);
        first.close();
        final Connection second = CachedConnection.getConnection(url);
 
        assertSame(second, first);
        verify(parent).isValid(CachedConnection.VALIDATION_TIMEOUT_SECONDS);
    }
 
    /**
     * A connection is trusted for the window that follows the last answer it gave, never for the
     * window that follows its return to the pool. pgjdbc short-circuits both rollback() and
     * commit() when the transaction state is IDLE, so a borrow that issued no statement - the open
     * of a backend, a configuration change that leaves the base DNs alone, an import of nothing -
     * puts a connection back without a byte reaching the server: stamping the return would mark a
     * connection the database dropped meanwhile as the freshest one in the pool.
     */
    @Test(timeOut = 120000)
    public void testTheReturnToThePoolIsNotTakenForProofOfLife() throws Exception {
        final String url = StubDriver.PREFIX + "silent-return";
        CachedConnection.aliveBypassNanos = TimeUnit.MILLISECONDS.toNanos(50);
        final Connection dropped = mock(Connection.class);
        when(dropped.isValid(anyInt())).thenReturn(false); // dropped while it was out of the pool
        stub.answerWith(dropped);
 
        final Connection borrowed = CachedConnection.getConnection(url);
        Thread.sleep(80); // the answer of the login ages out of the window
        borrowed.close(); // and the rollback of this return never leaves the driver
        final Connection fresh = mock(Connection.class);
        when(fresh.isValid(anyInt())).thenReturn(true);
        stub.answerWith(fresh);
 
        final Connection next = CachedConnection.getConnection(url);
 
        verify(dropped).isValid(CachedConnection.VALIDATION_TIMEOUT_SECONDS);
        verify(dropped).close();
        assertSame(((CachedConnection) next).parent, fresh,
            "a connection the database dropped was handed out on the strength of its return to the pool");
    }
 
    /**
     * A proof taken while the database was going away does not outlive the distrust that reported it.
     * <p>
     * The stamp stands for the moment the connection was asked, not the moment its answer was filed:
     * a validation is given {@link CachedConnection#VALIDATION_TIMEOUT_SECONDS}, and one that started
     * before another operation reported a drop and returned after it would otherwise be the younger of
     * the two. The connection would then be handed out unvalidated for the rest of the window - and
     * LIFO puts it at the head of the deque, so it is the very one the next borrow takes - by the
     * check that exists to stop exactly that.
     */
    @Test(timeOut = 120000)
    public void testAProofTakenWhileTheDatabaseWentAwayIsNotTrusted() throws Exception {
        final String url = StubDriver.PREFIX + "proof-across-a-drop";
        CachedConnection.aliveBypassNanos = TimeUnit.HOURS.toNanos(1); // nothing here ages out of the window
        final AtomicInteger validations = new AtomicInteger();
        final Connection parent = mock(Connection.class);
        // the database goes away while this validation is in flight: another operation of the backend
        // reports the drop of its own connection before this one has answered
        when(parent.isValid(anyInt())).thenAnswer(invocation -> {
            CachedConnection.distrustPool(url);
            validations.incrementAndGet();
            return true;
        });
        stub.answerWith(parent);
 
        CachedConnection.getConnection(url).close(); // established and returned, proven by its login
        CachedConnection.distrustPool(url);          // an operation reports a drop: what the pool holds predates it
        CachedConnection.getConnection(url).close(); // validated, and a second drop is reported while it is
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertEquals(validations.get(), 2,
            "a connection was trusted on a proof that started before the drop it is compared against");
        assertSame(((CachedConnection) borrowed).parent, parent, "the connection answered and was still discarded");
    }
 
    /**
     * The pool hands out the connection returned last. Without it the window would rarely apply: a
     * connection reached only after a whole cycle of the pool has been idle far longer than it.
     */
    @Test(timeOut = 120000)
    public void testTheConnectionReturnedLastIsBorrowedFirst() throws Exception {
        final String url = StubDriver.PREFIX + "returned-last";
        CachedConnection.aliveBypassNanos = TimeUnit.HOURS.toNanos(1);
        final Connection older = mock(Connection.class);
        when(older.isValid(anyInt())).thenReturn(true);
        stub.answerWith(older);
        final Connection first = CachedConnection.getConnection(url);
        final Connection newer = mock(Connection.class);
        when(newer.isValid(anyInt())).thenReturn(true);
        stub.answerWith(newer);
        final Connection second = CachedConnection.getConnection(url);
        assertNotSame(second, first);
        first.close();
        second.close();
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertSame(((CachedConnection) borrowed).parent, newer, "the pool cycled round to its coldest connection");
    }
 
    /**
     * Whatever dropped one connection - a restart, a failover, a network that went away - dropped
     * every connection established before it, and a borrow inside the window asks the database
     * nothing: so the operation that saw the failure tells the pool, and the rest of that
     * generation is validated once before it is trusted again.
     */
    @Test(timeOut = 120000)
    public void testThePoolIsValidatedAgainAfterTheDatabaseDroppedAConnection() throws Exception {
        final String url = StubDriver.PREFIX + "distrusted-generation";
        CachedConnection.aliveBypassNanos = TimeUnit.HOURS.toNanos(1);
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        stub.answerWith(parent);
        CachedConnection.getConnection(url).close();
 
        CachedConnection.distrustPool(url);
        final Connection next = CachedConnection.getConnection(url);
        next.close();
        CachedConnection.getConnection(url).close();
 
        // once for the generation the drop condemned, and not again for the borrow behind it
        verify(parent, times(1)).isValid(CachedConnection.VALIDATION_TIMEOUT_SECONDS);
    }
 
    /**
     * The whole of the trade the window makes, end to end: a connection the database dropped
     * inside the window is handed out unvalidated - that is the cost - the operation it broke
     * reports the drop, and from there the pool validates the generation the drop condemned
     * instead of handing out the rest of it the same way.
     */
    @Test(timeOut = 120000)
    public void testAConnectionDroppedInsideTheWindowIsHandedOutOnceAndThenValidated() throws Exception {
        final String url = StubDriver.PREFIX + "dropped-inside-the-window";
        CachedConnection.aliveBypassNanos = TimeUnit.HOURS.toNanos(1);
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        stub.answerWith(parent);
        CachedConnection.getConnection(url).close();
 
        when(parent.isValid(anyInt())).thenReturn(false); // the database dropped it where it lay
        final Connection dropped = CachedConnection.getConnection(url);
        assertSame(((CachedConnection) dropped).parent, parent, "the pooled connection was not the one handed back");
        verify(parent, never()).isValid(anyInt()); // handed out on the strength of its last answer
 
        // the statement of the caller is where the drop surfaces, and the caller reports it
        CachedConnection.distrustPool(url);
        dropped.close();
        final Connection fresh = mock(Connection.class);
        when(fresh.isValid(anyInt())).thenReturn(true);
        stub.answerWith(fresh);
 
        final Connection next = CachedConnection.getConnection(url);
 
        verify(parent).isValid(CachedConnection.VALIDATION_TIMEOUT_SECONDS);
        verify(parent).close();
        assertSame(((CachedConnection) next).parent, fresh, "the rest of the generation was handed out unvalidated");
    }
 
    /**
     * Seeds the pool the way {@link CachedConnection#close()} fills it - at the end a borrow takes
     * from - so that the connection named first here is the one the next borrow gets.
     */
    private static void seedPool(String url, Connection... parents) {
        final CachedConnection.Pool pool = CachedConnection.poolOf(url);
        for (int i = parents.length - 1; i >= 0; i--) {
            pool.addIdle(new CachedConnection(url, parents[i]));
        }
    }
 
    /**
     * The borrows nothing compensates a dropped connection on - the open of a backend, the removal
     * of its files, the start of an import - ask for a connection the pool validates whatever the
     * window says. Each of them is one borrow of a cold path, and the one that opens a backend
     * issues no statement at all: a connection dropped inside the window would surface there out of
     * the rollback that releases it, with no statement to replay and nothing to tell the pool.
     */
    @Test(timeOut = 120000)
    public void testTheBorrowsNothingCompensatesAreValidated() throws Exception {
        final String url = StubDriver.PREFIX + "validated-borrow";
        CachedConnection.aliveBypassNanos = TimeUnit.HOURS.toNanos(1);
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        stub.answerWith(parent);
        CachedConnection.getConnection(url).close();
 
        final Connection borrowed = CachedConnection.getConnection(url, false);
 
        assertSame(((CachedConnection) borrowed).parent, parent, "the pooled connection was not the one handed back");
        verify(parent).isValid(CachedConnection.VALIDATION_TIMEOUT_SECONDS);
    }
 
    /**
     * A connection closed under the borrow is not handed out on the strength of its last answer:
     * the removal listener of the pool closes every connection it finds in the deque when the pool
     * expires, and it iterates a weakly consistent view. The validation the window replaces
     * answered that as well, out of a flag of the driver rather than out of a round trip.
     */
    @Test(timeOut = 120000)
    public void testAConnectionThePoolClosedIsNotHandedOut() throws Exception {
        final String url = StubDriver.PREFIX + "closed-inside-the-window";
        CachedConnection.aliveBypassNanos = TimeUnit.HOURS.toNanos(1);
        final Connection parent = mock(Connection.class);
        when(parent.isValid(anyInt())).thenReturn(true);
        stub.answerWith(parent);
        CachedConnection.getConnection(url).close();
        // closed where it lay, by the expiry of the pool: a closed connection answers isValid() with
        // false as well, which is what discards it once the window stops trusting it
        when(parent.isClosed()).thenReturn(true);
        when(parent.isValid(anyInt())).thenReturn(false);
        final Connection fresh = mock(Connection.class);
        when(fresh.isValid(anyInt())).thenReturn(true);
        stub.answerWith(fresh);
 
        final Connection borrowed = CachedConnection.getConnection(url);
 
        assertSame(((CachedConnection) borrowed).parent, fresh, "a closed connection was handed out on its last answer");
    }
 
    /**
     * The window is clamped twice: to the idle time the pool keeps a connection for, since a window
     * longer than that is one the pool can never back - the connection it was meant for is gone
     * before it closes - and to {@link CachedConnection#MAX_ALIVE_BYPASS_MS} behind it, since the
     * ttl has no upper bound of its own and a value the unit conversion saturates on would leave
     * every connection of the pool trusted for the life of the server.
     * <p>
     * About the value the class settles on at initialization: the field the pool reads is assigned
     * once, so a ttl set after that changes neither it nor the idle time it was clamped to.
     */
    @Test(timeOut = 120000)
    public void testTheWindowIsClampedToTheIdleTimeOfThePool() {
        System.setProperty(CachedConnection.TTL_PROPERTY, "15000");
        System.setProperty(CachedConnection.ALIVE_BYPASS_PROPERTY, "500");
        assertEquals(CachedConnection.getAliveBypassMillis(), 500L, "a window inside the ttl was not left alone");
 
        System.setProperty(CachedConnection.ALIVE_BYPASS_PROPERTY, Long.toString(Long.MAX_VALUE));
        assertEquals(CachedConnection.getAliveBypassMillis(), 15000L, "a window of Long.MAX_VALUE was not clamped");
 
        System.setProperty(CachedConnection.TTL_PROPERTY, "100");
        assertEquals(CachedConnection.getAliveBypassMillis(), 100L, "the clamp is the configured ttl, not the default");
 
        // the ttl has no upper bound of its own, so the clamp to it does not bound the window either: both set
        // to a value the conversion to nanoseconds saturates on would leave every connection of the pool
        // trusted for the life of the server, which is the outcome this javadoc says the clamp rules out
        System.setProperty(CachedConnection.TTL_PROPERTY, Long.toString(Long.MAX_VALUE));
        System.setProperty(CachedConnection.ALIVE_BYPASS_PROPERTY, Long.toString(Long.MAX_VALUE));
        assertEquals(CachedConnection.getAliveBypassMillis(), CachedConnection.MAX_ALIVE_BYPASS_MS,
            "a window the ttl did not bound was left to saturate");
    }
 
    /**
     * A setting worth warning about must leave the class usable. Both properties are read by the
     * initializer of {@code aliveBypassNanos}, and both report an unusable value through the set of
     * what has been said once already - which, declared below that initializer, would still be null
     * when the initializer reaches it (JLS 12.4.2). A window longer than the ttl is exactly the
     * tuning the javadoc of the property invites, and it would turn a log line into an
     * ExceptionInInitializerError on the first borrow and a causeless NoClassDefFoundError on every
     * one after it: no connection can be borrowed, so the backend cannot open at all.
     * <p>
     * Asserted on the class loaded afresh rather than on this one, which was initialized long
     * before the property was set.
     */
    @Test(timeOut = 120000, dataProvider = "settingsWorthWarningAbout")
    public void testASettingWorthWarningAboutStillInitializesTheClass(String ttl, String window, long expectedMillis)
            throws Exception {
        System.setProperty(CachedConnection.TTL_PROPERTY, ttl);
        System.setProperty(CachedConnection.ALIVE_BYPASS_PROPERTY, window);
 
        final Class<?> reloaded = loadedAfresh(CachedConnection.class);
 
        assertNotSame(reloaded, CachedConnection.class, "the class under test was not loaded afresh");
        final Field field = reloaded.getDeclaredField("aliveBypassNanos");
        field.setAccessible(true);
        assertEquals(field.getLong(null), TimeUnit.MILLISECONDS.toNanos(expectedMillis),
            "the window the reloaded class settled on");
    }
 
    @DataProvider
    public Object[][] settingsWorthWarningAbout() {
        return new Object[][]{
            // a window longer than the ttl: reported once and used as the ttl
            {"15000", "60000", 15000L},
            // not a number, and negative: reported once and ignored in favour of the default
            {"15000", "half a second", CachedConnection.DEFAULT_ALIVE_BYPASS_MS},
            {"15000", "-1", CachedConnection.DEFAULT_ALIVE_BYPASS_MS},
            // the ttl is read by the same initializer, and reports its own value the same way
            {"30s", "500", 500L}
        };
    }
 
    /**
     * The class again, defined by a loader of this test rather than taken from the one that has
     * already initialized it: a static initializer runs once per loader, and this is about what it
     * does. Every other class is delegated to the parent, so the reloaded one shares the types it
     * is written against.
     */
    private static Class<?> loadedAfresh(Class<?> type) throws Exception {
        final String name = type.getName();
        final ClassLoader parent = type.getClassLoader();
        final ClassLoader loader = new ClassLoader(parent) {
            @Override
            protected Class<?> loadClass(String candidate, boolean resolve) throws ClassNotFoundException {
                if (!name.equals(candidate)) {
                    return super.loadClass(candidate, resolve);
                }
                Class<?> defined = findLoadedClass(candidate);
                if (defined == null) {
                    final byte[] bytecode = bytecodeOf(candidate, parent);
                    defined = defineClass(candidate, bytecode, 0, bytecode.length);
                }
                if (resolve) {
                    resolveClass(defined);
                }
                return defined;
            }
        };
        return Class.forName(name, true, loader);
    }
 
    private static byte[] bytecodeOf(String name, ClassLoader from) throws ClassNotFoundException {
        try (final InputStream in = from.getResourceAsStream(name.replace('.', '/') + ".class")) {
            if (in == null) {
                throw new ClassNotFoundException(name);
            }
            final ByteArrayOutputStream bytecode = new ByteArrayOutputStream();
            final byte[] chunk = new byte[8192];
            for (int read; (read = in.read(chunk)) >= 0; ) {
                bytecode.write(chunk, 0, read);
            }
            return bytecode.toByteArray();
        } catch (IOException e) {
            throw new ClassNotFoundException(name, e);
        }
    }
 
    private static SQLException tooManyConnections() {
        // 53300, too_many_connections, of the insufficient_resources class
        return new SQLException("sorry, too many clients already", "53300");
    }
 
    /** A connection string of every dialect pointing at one host and port. */
    private static String[] urlsOf(int port) {
        return new String[]{
            "jdbc:postgresql://127.0.0.1:" + port + "/opendj?user=opendj&password=opendj",
            "jdbc:mysql://127.0.0.1:" + port + "/opendj?user=opendj&password=opendj",
            "jdbc:oracle:thin:opendj/opendj@//127.0.0.1:" + port + "/free",
            "jdbc:sqlserver://127.0.0.1:" + port + ";databaseName=opendj;user=opendj;password=opendj;encrypt=false"
        };
    }
 
    private static int closedPort() throws Exception {
        try (final ServerSocket socket = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) {
            return socket.getLocalPort();
        } // closed again: nothing listens there any more
    }
 
    /**
     * A socket that answers a connect and closes it at once. A port bound and released is the
     * shape of a refused connect a test would reach for, but it races whatever else on the host
     * may take that port; this one is the failure it stands for and belongs to nobody else.
     */
    private static ServerSocket rejectingSocket() throws Exception {
        final ServerSocket socket = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
        final Thread accepting = new Thread(() -> {
            while (!socket.isClosed()) {
                try {
                    socket.accept().close();
                } catch (Exception closed) {
                    return;
                }
            }
        }, "opendj-test-rejecting-socket");
        accepting.setDaemon(true);
        accepting.start();
        return socket;
    }
 
    // The margin grows with the bound instead of dwarfing it: what this has to catch is a bound
    // that is not in force, and a bound of 2 s that is not in force is not a wait of 12 s - it is
    // the 30 s of the connect property, the 600 s of a driver, or no end at all.
    private static void assertElapsedWithinBound(long startedAt, long boundMs) {
        final long elapsed = System.currentTimeMillis() - startedAt;
        final long margin = Math.max(BOUND_MARGIN_MS, boundMs);
        assertTrue(elapsed < boundMs + margin,
            "gave up only after " + elapsed + " ms, past the " + boundMs + " ms it was bounded by");
    }
 
    /**
     * Stands in for a database whose answer to a connect is the point of the test: the vendor
     * codes and SQL states below are what the retry has to tell apart, and no engine is needed to
     * produce them.
     */
    private static final class StubDriver implements Driver {
        static final String PREFIX = "jdbc:opendj-stub:";
        static final int ALWAYS = -1;
 
        final AtomicInteger attempts = new AtomicInteger();
        /** A SQLException, or the unchecked failure a driver is free to throw at DriverManager instead. */
        private volatile Throwable failure;
        private volatile int failuresLeft;
        private volatile Connection answer;
 
        void failWith(Throwable failure, int times) {
            this.failure = failure;
            this.failuresLeft = times;
            this.answer = null;
            this.attempts.set(0);
        }
 
        void answerWith(Connection answer) {
            this.failure = null;
            this.failuresLeft = 0;
            this.answer = answer;
            this.attempts.set(0);
        }
 
        @Override
        public Connection connect(String url, Properties info) throws SQLException {
            if (!acceptsURL(url)) {
                return null;
            }
            attempts.incrementAndGet();
            if (failuresLeft != 0) {
                if (failuresLeft > 0) {
                    failuresLeft--;
                }
                if (failure instanceof SQLException) {
                    throw (SQLException) failure;
                }
                throw (RuntimeException) failure;
            }
            if (answer != null) {
                return answer;
            }
            final Connection con = mock(Connection.class);
            when(con.isValid(anyInt())).thenReturn(true);
            return con;
        }
 
        @Override
        public boolean acceptsURL(String url) {
            return url != null && url.startsWith(PREFIX);
        }
 
        @Override
        public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
            return new DriverPropertyInfo[0];
        }
 
        @Override
        public int getMajorVersion() {
            return 1;
        }
 
        @Override
        public int getMinorVersion() {
            return 0;
        }
 
        @Override
        public boolean jdbcCompliant() {
            return false;
        }
 
        @Override
        public Logger getParentLogger() {
            return Logger.getLogger(StubDriver.class.getName());
        }
    }
}