krabka-client-consumer 0.7.1

Subscribe-style consumer client for Apache Kafka in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
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
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
//! `Consumer::poll` issues one `Fetch` that covers every assigned partition,
//! advances next-offsets, and returns the decoded records.

use std::{
    collections::{BTreeMap, BTreeSet, HashMap},
    sync::Arc,
};

use krabka_client_core::VersionBoundedRequest;
#[cfg(test)]
use krabka_client_core::mock_test_support;
use krabka_ids::LeaderEpoch;
#[cfg(test)]
use krabka_protocol::owned::fetch_request;
#[cfg(test)]
use krabka_protocol::records::RecordsPayload;
use krabka_protocol::{
    ProtocolRequest,
    owned::{
        fetch_request::{FetchPartition, FetchRequest, FetchTopic, ForgottenTopic},
        fetch_response,
        fetch_response::FetchResponse,
        list_offsets_request::{self, ListOffsetsPartition, ListOffsetsRequest, ListOffsetsTopic},
        list_offsets_response::ListOffsetsResponse,
    },
    primitives::uuid,
    records::{Record, RecordBatch},
};
use krabka_units::{
    ByteSize, Time,
    convert::{ByteSizeExt as _, TimeExt as _},
    mebibytes,
};

#[cfg(test)]
use crate::poll;
#[cfg(test)]
use crate::test_support;
use crate::{
    assignor,
    builder::{AutoOffsetReset, IsolationLevel},
    consumer,
    consumer::{Consumer, ConsumerRecord, Header, TimestampType},
    control, coordinator,
    error::ConsumerError,
    fetch_buffer::BufferedPartition,
    fetch_session::{
        FINAL_EPOCH, FetchSession, INVALID_SESSION_ID, SessionPartition, SessionRequest,
    },
    offset_wire,
    position::PartitionPosition,
};

/// Synthetic leader id that means "leader unknown → use the bootstrap
/// connection".
///
/// It matches `BrokerPool`'s bootstrap slot, so a fallback Fetch goes out
/// through `Client::send` and not `Client::broker(id)`.
const BOOTSTRAP_LEADER: i32 = -1;
const UNKNOWN_FETCH_OFFSET: i64 = -1;
const UNKNOWN_LEADER_ID: i32 = -1;
/// `OFFSET_OUT_OF_RANGE`.
const OFFSET_OUT_OF_RANGE: i16 = 1;
/// The offset the wire uses for "the broker did not answer this".
const UNKNOWN_OFFSET: i64 = -1;
pub(crate) const DEFAULT_FETCH_PARTITION_MAX: ByteSize = mebibytes(1);
pub(crate) const DEFAULT_FETCH_MAX: ByteSize = mebibytes(50);

/// One fetchable partition's request fields:
/// `(partition, fetch_offset, current_leader_epoch, last_fetched_epoch)`.
type FetchSpec = (i32, i64, LeaderEpoch, LeaderEpoch);

/// Partitions to fetch, grouped first by leader id, then by topic.
type FetchByLeader = HashMap<i32, HashMap<String, Vec<FetchSpec>>>;

fn fetch_leader_id(leader_id: i32, knows_leader: bool) -> i32 {
    if leader_id >= 0 && knows_leader {
        leader_id
    } else {
        BOOTSTRAP_LEADER
    }
}

fn should_use_bootstrap_leader(leader: i32) -> bool {
    leader == BOOTSTRAP_LEADER
}

fn fetch_offset_or_unknown(offsets: &HashMap<(String, i32), i64>, key: &(String, i32)) -> i64 {
    offsets.get(key).copied().unwrap_or(UNKNOWN_FETCH_OFFSET)
}

fn is_read_committed(isolation_level: IsolationLevel) -> bool {
    isolation_level == IsolationLevel::ReadCommitted
}

pub(crate) fn is_transient_transport_error(e: &krabka_client_core::ClientError) -> bool {
    crate::error::is_transport_error(e, true)
}

fn is_transient_poll_error(e: &ConsumerError) -> bool {
    matches!(e, ConsumerError::Client(client) if is_transient_transport_error(client))
}

/// What [`Consumer::poll`] does with one partition row of a `Fetch` response,
/// chosen by the row's `error_code`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum FetchPartitionAction {
    /// `NONE`: decode the records.
    Records,
    /// `OFFSET_OUT_OF_RANGE`: resolve a new position with the reset policy.
    ResetOffset,
    /// `NOT_LEADER_OR_FOLLOWER`: adopt the leader hint, or refresh metadata.
    Reroute,
    /// `FENCED_LEADER_EPOCH` or `UNKNOWN_LEADER_EPOCH`: validate the position
    /// against fresher metadata.
    RevalidateEpoch,
    /// `UNKNOWN_TOPIC_OR_PARTITION` or `UNKNOWN_TOPIC_ID`: skip the row and
    /// refresh metadata. The next poll fetches the partition again.
    RefreshMetadata,
    /// Any other code: fail the poll with `ConsumerError::Server(code)`.
    Fail(i16),
}

/// Map a `Fetch` partition `error_code` to its [`FetchPartitionAction`].
///
/// Kafka's `FetchCollector.handleInitializeErrors` also requests a metadata
/// update for `REPLICA_NOT_AVAILABLE` (9), `KAFKA_STORAGE_ERROR` (56),
/// `OFFSET_NOT_AVAILABLE` (78) and `INCONSISTENT_TOPIC_ID` (103), the errors of
/// a follower that serves a fetch (KIP-392).
///
/// Kafka's `FetchCollector.handleInitializeErrors` requests a metadata update
/// for `UNKNOWN_TOPIC_OR_PARTITION` (3) and for `UNKNOWN_TOPIC_ID` (100), and
/// does not raise an error to the application. A Fetch v13 or later names the
/// topic by id only. The broker answers 100 when its metadata image does not
/// hold that id yet, or when the topic was deleted and created again.
fn classify_fetch_partition_error(error_code: i16) -> FetchPartitionAction {
    match error_code {
        0 => FetchPartitionAction::Records,
        1 /* OFFSET_OUT_OF_RANGE */ => FetchPartitionAction::ResetOffset,
        6 /* NOT_LEADER_OR_FOLLOWER */ => FetchPartitionAction::Reroute,
        74 /* FENCED_LEADER_EPOCH */ | 75 /* UNKNOWN_LEADER_EPOCH */ => {
            FetchPartitionAction::RevalidateEpoch
        }
        3 /* UNKNOWN_TOPIC_OR_PARTITION */
        | 9 /* REPLICA_NOT_AVAILABLE */
        | 56 /* KAFKA_STORAGE_ERROR */
        | 78 /* OFFSET_NOT_AVAILABLE */
        | 100 /* UNKNOWN_TOPIC_ID */
        | 103 /* INCONSISTENT_TOPIC_ID */ => FetchPartitionAction::RefreshMetadata,
        other => FetchPartitionAction::Fail(other),
    }
}

fn aborted_txn_started(first_offset: i64, batch_base_offset: i64) -> bool {
    first_offset <= batch_base_offset
}

fn should_drop_aborted_batch(
    read_committed: bool,
    is_transactional: bool,
    producer_is_aborted: bool,
) -> bool {
    read_committed && is_transactional && producer_is_aborted
}

fn record_offset(base_offset: i64, offset_delta: i32) -> i64 {
    base_offset + i64::from(offset_delta)
}

/// The timestamp and timestamp type of `record` in `batch`.
///
/// Kafka's `DefaultRecordBatch` reads `max_timestamp` as the log append time
/// when the batch attributes say `LogAppendTime`. `DefaultRecord.readFrom`
/// then gives that time to every record and ignores the record delta.
pub(crate) fn record_timestamp(batch: &RecordBatch, record: &Record) -> (i64, TimestampType) {
    let timestamp_type = TimestampType::from(batch.attributes.timestamp_type());
    let timestamp = match timestamp_type {
        TimestampType::CreateTime => batch.base_timestamp + record.timestamp_delta,
        TimestampType::LogAppendTime => batch.max_timestamp,
    };
    (timestamp, timestamp_type)
}

/// The `max_bytes` of one partition in a Fetch request.
fn session_partition(
    topic_id: uuid::Uuid,
    (_, fetch_offset, leader_epoch, last_fetched_epoch): FetchSpec,
    partition_max: ByteSize,
) -> SessionPartition {
    SessionPartition {
        topic_id,
        fetch_offset,
        // Unwrap the leader epochs to raw wire `int32` at the FetchRequest
        // encode boundary.
        current_leader_epoch: leader_epoch.get(),
        last_fetched_epoch: last_fetched_epoch.get(),
        partition_max_bytes: partition_max.bytes_i32(),
    }
}

/// Build the Fetch request of one broker from its session request.
///
/// Kafka's `AbstractFetch.createFetchRequest`: `max_wait_ms` is
/// `fetch.max.wait.ms`, and the session id, epoch, partitions and forgotten
/// partitions come from the fetch session (KIP-227).
fn build_fetch_request(
    max_wait_ms: i32,
    isolation_level: IsolationLevel,
    min: ByteSize,
    max: ByteSize,
    rack_id: &str,
    session: SessionRequest,
) -> FetchRequest {
    let mut topics: Vec<FetchTopic> = Vec::new();
    for ((topic, partition), data) in session.partitions {
        if topics.last().is_none_or(|last| last.topic != topic) {
            topics.push(FetchTopic {
                topic: topic.clone(),
                topic_id: data.topic_id,
                ..Default::default()
            });
        }
        if let Some(last) = topics.last_mut() {
            last.partitions.push(FetchPartition {
                partition,
                fetch_offset: data.fetch_offset,
                current_leader_epoch: data.current_leader_epoch,
                last_fetched_epoch: data.last_fetched_epoch,
                partition_max_bytes: data.partition_max_bytes,
                ..Default::default()
            });
        }
    }
    let mut forgotten_topics_data: Vec<ForgottenTopic> = Vec::new();
    for ((topic, partition), topic_id) in session.forgotten {
        if forgotten_topics_data
            .last()
            .is_none_or(|last| last.topic != topic)
        {
            forgotten_topics_data.push(ForgottenTopic {
                topic: topic.clone(),
                topic_id,
                ..Default::default()
            });
        }
        if let Some(last) = forgotten_topics_data.last_mut() {
            last.partitions.push(partition);
        }
    }
    FetchRequest {
        max_wait_ms,
        min_bytes: min.bytes_i32(),
        max_bytes: max.bytes_i32(),
        isolation_level: isolation_level.wire(),
        session_id: session.session_id,
        session_epoch: session.session_epoch,
        topics,
        forgotten_topics_data,
        // KIP-392: `AbstractFetch.createFetchRequest` sends `client.rack`.
        rack_id: rack_id.to_owned(),
        ..Default::default()
    }
}

/// A Fetch that runs in a task, with the fetch offset of each partition that
/// it asked for.
struct InFlightFetch {
    handle: tokio::task::JoinHandle<()>,
    /// The result of the request. The task sends it before it notifies
    /// `Fetches::completed`, so a waiter that wakes can always take it.
    result: tokio::sync::oneshot::Receiver<Result<FetchResponse, krabka_client_core::ClientError>>,
    requested: HashMap<(String, i32), i64>,
    /// The partitions that this Fetch asked from a preferred read replica.
    from_replica: std::collections::HashSet<(String, i32)>,
}

/// A Fetch whose task ended: its broker, the fetch offsets it asked for, and
/// its result, or `None` when the task ended without a result.
struct CompletedFetch {
    leader: i32,
    requested: HashMap<(String, i32), i64>,
    from_replica: std::collections::HashSet<(String, i32)>,
    result: Option<Result<FetchResponse, krabka_client_core::ClientError>>,
}

/// The Fetch requests of a consumer: at most one in flight per broker, and the
/// fetch session of each broker.
///
/// Kafka's `AbstractFetch` sends a Fetch to every broker that has no pending
/// Fetch, and keeps a `FetchSessionHandler` per broker. A response that arrives
/// after `poll` returned waits here for the next `poll`.
#[derive(Default)]
pub(crate) struct Fetches {
    sessions: HashMap<i32, FetchSession>,
    in_flight: HashMap<i32, InFlightFetch>,
    /// The Fetch requests whose result the consumer took from `in_flight` and
    /// did not process yet.
    ready: Vec<CompletedFetch>,
    completed: Arc<tokio::sync::Notify>,
    /// KIP-392: the replica that a broker named in `preferred_read_replica`,
    /// with the time until which the consumer fetches from it. Kafka's
    /// `SubscriptionState.TopicPartitionState.preferredReadReplica`.
    preferred_read_replicas: HashMap<(String, i32), (i32, tokio::time::Instant)>,
}

impl Fetches {
    /// Move each Fetch whose task sent its result, or ended, to `ready`.
    fn collect_ready(&mut self) {
        let leaders: Vec<i32> = self.in_flight.keys().copied().collect();
        for leader in leaders {
            let Some(fetch) = self.in_flight.get_mut(&leader) else {
                continue;
            };
            let result = match fetch.result.try_recv() {
                Ok(result) => Some(result),
                Err(tokio::sync::oneshot::error::TryRecvError::Closed) => None,
                Err(tokio::sync::oneshot::error::TryRecvError::Empty) => continue,
            };
            if let Some(fetch) = self.in_flight.remove(&leader) {
                self.ready.push(CompletedFetch {
                    leader,
                    requested: fetch.requested,
                    from_replica: fetch.from_replica,
                    result,
                });
            }
        }
    }
}

impl Drop for Fetches {
    fn drop(&mut self) {
        for fetch in self.in_flight.values() {
            fetch.handle.abort();
        }
    }
}

/// `ListOffsets` timestamp that asks for the log end offset.
pub(crate) const LATEST_TIMESTAMP: i64 = -1;
/// `ListOffsets` timestamp that asks for the log start offset.
pub(crate) const EARLIEST_TIMESTAMP: i64 = -2;

/// Placeholder for "reset with `ListOffsets`", resolved before the next Fetch:
/// the log end, or the offset for the timestamp of `by_duration`.
pub(crate) const LATEST_SENTINEL: i64 = i64::MAX;

/// Placeholder for the reset of [`Consumer::seek_to_beginning`], resolved with
/// `ListOffsets(-2)`: Kafka's `requestOffsetReset(partitions, EARLIEST)`.
/// `auto_offset_reset = Earliest` plants the same sentinel at build time and
/// on `OFFSET_OUT_OF_RANGE`, so `position()` never reports an offset before
/// the round trip resolves the real log start.
pub(crate) const BEGINNING_SENTINEL: i64 = i64::MAX - 1;

/// Placeholder for the reset of [`Consumer::seek_to_end`]: Kafka's
/// `requestOffsetReset(partitions, LATEST)`, resolved with `ListOffsets(-1)`.
pub(crate) const END_SENTINEL: i64 = i64::MAX - 2;

/// Placeholder for the position of a manually assigned partition: the
/// committed offset, or a reset when the group has none. Kafka's
/// `TopicPartitionState.shouldInitialize`.
pub(crate) const COMMITTED_SENTINEL: i64 = i64::MAX - 3;

/// Placeholder for a partition with no committed offset under
/// `auto_offset_reset = None`. Kafka never auto-resets under `none`
/// (`SubscriptionState.resetInitializingPositions`,
/// `OffsetFetcherUtils.offsetResetStrategyWithValidTimestamp`); it raises
/// `NoOffsetForPartitionException` from `poll()` and `position()` instead of
/// planting a `ListOffsets` sentinel. This placeholder is therefore never
/// resolved by [`Consumer::resolve_reset_sentinels`]: it stays until a commit
/// or a seek gives the partition a real position.
pub(crate) const NO_OFFSET_SENTINEL: i64 = i64::MAX - 4;

/// Whether `next_offset` is a placeholder that a position update did not
/// resolve yet: a reset (Kafka's `TopicPartitionState.awaitingReset`), the
/// committed offset of a new manual assignment, or a partition with no offset
/// under `auto_offset_reset = None`. Such a partition has no valid position,
/// so the consumer does not fetch or commit it.
pub(crate) fn is_reset_sentinel(next_offset: i64) -> bool {
    next_offset >= NO_OFFSET_SENTINEL
}

/// The `ListOffsets` timestamp that resolves a [`LATEST_SENTINEL`] for the
/// reset policy at `now_ms`.
///
/// Kafka's `AutoOffsetResetStrategy.timestamp` gives `-1` for `latest` and
/// `now - duration` for `by_duration`.
fn reset_timestamp(policy: AutoOffsetReset, now_ms: i64) -> i64 {
    match policy {
        AutoOffsetReset::ByDuration(duration) => {
            now_ms.saturating_sub(i64::try_from(duration.as_millis()).unwrap_or(i64::MAX))
        }
        AutoOffsetReset::Earliest | AutoOffsetReset::Latest | AutoOffsetReset::None => {
            LATEST_TIMESTAMP
        }
    }
}

/// Run `future`, or return [`ConsumerError::Wakeup`] when `wakeup` comes
/// first.
async fn until_woken<F: std::future::Future>(
    wakeup: Option<&control::WakeupHandle>,
    future: F,
) -> Result<F::Output, ConsumerError> {
    match wakeup {
        None => Ok(future.await),
        Some(wakeup) => tokio::select! {
            biased;
            () = wakeup.woken() => Err(ConsumerError::Wakeup),
            output = future => Ok(output),
        },
    }
}

/// Milliseconds since the Unix epoch.
fn unix_now_ms() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |elapsed| {
            i64::try_from(elapsed.as_millis()).unwrap_or(i64::MAX)
        })
}

/// `ListOffsets` partitions for one broker, by topic:
/// `(partition, current_leader_epoch)`.
type ListOffsetsSpecs = BTreeMap<String, Vec<(i32, LeaderEpoch)>>;

/// Build the consumer's `ListOffsets` request for one broker.
///
/// Kafka's `ListOffsetsRequest.Builder.forConsumer` sends the consumer's own
/// isolation level, so a `read_committed` consumer that asks for the latest
/// offset gets the last stable offset and not the high watermark. The broker
/// reads `isolation_level` from version 2, so a `read_committed` consumer
/// sends [`ReadCommittedListOffsets`], which negotiates version 2 or higher.
/// `current_leader_epoch` (KIP-320, version 4) comes from metadata and is `-1`
/// when the epoch is unknown, as in `OffsetFetcher.groupListOffsetRequests`.
fn build_offsets_request(
    by_topic: ListOffsetsSpecs,
    timestamp: i64,
    isolation_level: IsolationLevel,
) -> ListOffsetsRequest {
    let topics: Vec<ListOffsetsTopic> = by_topic
        .into_iter()
        .map(|(name, partitions)| ListOffsetsTopic {
            name,
            partitions: partitions
                .into_iter()
                .map(|(partition_index, leader_epoch)| ListOffsetsPartition {
                    partition_index,
                    // Unwrap the leader epoch to the raw wire `int32`.
                    current_leader_epoch: leader_epoch.get(),
                    timestamp,
                    ..Default::default()
                })
                .collect(),
            ..Default::default()
        })
        .collect();
    ListOffsetsRequest {
        replica_id: -1,
        isolation_level: isolation_level.wire(),
        topics,
        ..Default::default()
    }
}

/// A `ListOffsets` request from a `read_committed` consumer, from version 2.
///
/// `isolation_level` exists from version 2. At version 0 or 1 the codec
/// leaves it out, and the broker answers with `read_uncommitted` semantics:
/// the high watermark and not the last stable offset. Kafka's
/// `ListOffsetsRequest.Builder.forConsumer` sets the oldest allowed version
/// to 2 for `READ_COMMITTED`. This type gives the same range to version
/// negotiation. When the broker supports only version 0 or 1, the send fails
/// with `ClientError::IncompatibleVersion` before the request goes out.
/// Kafka fails with `UnsupportedVersionException` in that case.
pub(crate) type ReadCommittedListOffsets =
    VersionBoundedRequest<ListOffsetsRequest, 2, { list_offsets_request::MAX_VERSION }>;

/// Group `ListOffsets` partitions by the broker that gets the request.
///
/// A partition goes to its leader when the leader id is known and the pool
/// can dial it, and to the bootstrap connection otherwise. That is the rule
/// that `Consumer::group_fetches` uses for Fetch. Each partition carries the
/// leader epoch from its position, which is `-1` when metadata has not
/// reported one.
fn group_list_offsets(
    keys: &[(String, i32)],
    positions: &HashMap<(String, i32), PartitionPosition>,
    knows_broker: impl Fn(i32) -> bool,
) -> BTreeMap<i32, ListOffsetsSpecs> {
    let mut grouped: BTreeMap<i32, ListOffsetsSpecs> = BTreeMap::new();
    for key in keys {
        let position = positions.get(key).copied().unwrap_or_default();
        let leader = fetch_leader_id(position.leader_id, knows_broker(position.leader_id));
        grouped
            .entry(leader)
            .or_default()
            .entry(key.0.clone())
            .or_default()
            .push((key.1, position.leader_epoch));
    }
    grouped
}

/// What the consumer does with one partition row of a `ListOffsets` answer.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ListOffsetsRowAction {
    /// `NONE` with a known offset: set the position to the offset.
    Position(i64),
    /// `TOPIC_AUTHORIZATION_FAILED`: fail the poll.
    Unauthorized,
    /// Any other row: keep the position and try again after a metadata
    /// refresh.
    Retry,
}

/// Map a `ListOffsets` partition row to its [`ListOffsetsRowAction`].
///
/// Kafka's `OffsetFetcherUtils.handleListOffsetResponse` sets a position only
/// for `NONE` with an offset other than `-1`. It throws
/// `TopicAuthorizationException` for `TOPIC_AUTHORIZATION_FAILED`. It retries
/// every other code, which includes codes that it does not name.
fn classify_list_offsets_row(error_code: i16, offset: i64) -> ListOffsetsRowAction {
    match error_code {
        0 if offset != UNKNOWN_OFFSET => ListOffsetsRowAction::Position(offset),
        29 /* TOPIC_AUTHORIZATION_FAILED */ => ListOffsetsRowAction::Unauthorized,
        _ => ListOffsetsRowAction::Retry,
    }
}

/// What the consumer takes from the `ListOffsets` answers for a set of
/// partitions.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
struct ListOffsetsResult {
    /// The offset for each requested partition that got one.
    offsets: HashMap<(String, i32), i64>,
    /// `true` when a requested partition got no offset and must be tried
    /// again after a metadata refresh.
    retry: bool,
    /// The topics that answered `TOPIC_AUTHORIZATION_FAILED`.
    unauthorized: BTreeSet<String>,
}

impl ListOffsetsResult {
    /// Collect the answers for `requested`.
    ///
    /// A requested partition that no answer names is retried. An answer with
    /// an unauthorized row gives no offsets, because Kafka's
    /// `handleListOffsetResponse` throws for the whole response.
    fn collect(requested: &[(String, i32)], answers: &[ListOffsetsResponse]) -> Self {
        let mut result = Self::default();
        for answer in answers {
            let mut offsets = HashMap::new();
            let mut unauthorized = BTreeSet::new();
            for topic in &answer.topics {
                for row in &topic.partitions {
                    match classify_list_offsets_row(row.error_code, row.offset) {
                        ListOffsetsRowAction::Position(offset) => {
                            offsets.insert((topic.name.clone(), row.partition_index), offset);
                        }
                        ListOffsetsRowAction::Unauthorized => {
                            unauthorized.insert(topic.name.clone());
                        }
                        ListOffsetsRowAction::Retry => {}
                    }
                }
            }
            if unauthorized.is_empty() {
                result.offsets.extend(offsets);
            } else {
                result.unauthorized.extend(unauthorized);
            }
        }
        result.offsets.retain(|key, _| requested.contains(key));
        result.retry = requested
            .iter()
            .any(|key| !result.offsets.contains_key(key));
        result
    }

    /// `Err(TopicAuthorizationFailed)` when a topic was not authorized.
    fn authorization(&self) -> Result<(), ConsumerError> {
        if self.unauthorized.is_empty() {
            Ok(())
        } else {
            Err(ConsumerError::TopicAuthorizationFailed(
                self.unauthorized.clone(),
            ))
        }
    }
}

impl Consumer {
    /// Returns the records from every v2 batch that the broker returned per
    /// assigned partition, or an empty vec on timeout.
    ///
    /// Under `read_committed` isolation, this method filters out control
    /// batches and records that belong to aborted transactions on the client
    /// side, with the response's `aborted_transactions` list. The broker returns
    /// verbatim bytes.
    ///
    /// `poll` returns at most `max_poll_records` records. It keeps the rest of
    /// a fetch for the next call, which then sends no Fetch.
    ///
    /// A rebalance that the coordinator asks for starts only when the
    /// application calls `poll`. The internal coordinator task then runs the
    /// join and mutates the live `assigned` snapshot in place. For the eager
    /// protocol, `poll` waits for that join up to `timeout` and fetches nothing
    /// before it completes. `poll` also resets the `max_poll_interval` timer.
    #[tracing::instrument(
        name = "consumer.poll",
        level = "debug",
        skip_all,
        fields(
            group_id = %self.group_id,
            timeout_ms = timeout.millis_i64_trunc(),
            assigned_partitions = tracing::field::Empty,
            leaders = tracing::field::Empty,
            records = tracing::field::Empty,
        ),
        err(level = "debug")
    )]
    /// # Errors
    /// Returns an error when configuration is invalid, protocol encoding fails, the broker rejects the request, or transport I/O fails.
    pub async fn poll(&mut self, timeout: Time) -> Result<Vec<ConsumerRecord>, ConsumerError> {
        let deadline = tokio::time::Instant::now()
            + std::time::Duration::from_millis(
                u64::try_from(timeout.millis_i64_trunc()).unwrap_or(0),
            );
        // Kafka's `ClassicKafkaConsumer.poll` throws `IllegalStateException`
        // without a subscription.
        if self.subscription.borrow().is_none() && !self.subscription.borrow().manual_assignment {
            return Err(ConsumerError::NotSubscribed);
        }
        // Kafka's `ConsumerNetworkClient.maybeTriggerWakeup`: a pending
        // wakeup fails the `poll` before it blocks.
        if self.wakeup.take() {
            return Err(ConsumerError::Wakeup);
        }
        if !self.prepare_poll(deadline).await? {
            return Ok(Vec::new());
        }
        if !self.wait_for_rebalance(deadline).await? {
            return Ok(Vec::new());
        }
        let wakeup = self.wakeup.clone();

        // Records of an earlier fetch come first, without a new Fetch.
        let buffered = self.drain_fetch_buffer().await;
        if !buffered.is_empty() {
            tracing::Span::current().record("records", buffered.len());
            return Ok(buffered);
        }

        // 2. Build a FetchRequest covering every assigned partition.
        let assigned = self.assigned.lock().await.clone();
        tracing::Span::current().record("assigned_partitions", assigned.len());
        if assigned.is_empty() {
            tokio::select! {
                () = tokio::time::sleep_until(deadline) => return Ok(Vec::new()),
                () = wakeup.woken() => return Err(ConsumerError::Wakeup),
            }
        }

        let by_leader = self.group_fetches(&assigned).await;
        tracing::Span::current().record("leaders", by_leader.len());
        let topic_ids = self.topic_ids.lock().await.clone();
        self.send_fetches(by_leader, &topic_ids);
        self.fetches.collect_ready();
        if self.fetches.in_flight.is_empty() && self.fetches.ready.is_empty() {
            tokio::select! {
                () = self.wait_without_fetches(&assigned, deadline) => return Ok(Vec::new()),
                () = wakeup.woken() => return Err(ConsumerError::Wakeup),
            }
        }
        tokio::select! {
            biased;
            () = self.wait_for_fetches(deadline) => {}
            // The Fetch requests stay in flight. The next `poll` takes their
            // results.
            () = wakeup.woken() => return Err(ConsumerError::Wakeup),
        }
        let responses = self.take_completed_fetches(&topic_ids).await?;

        self.process_fetch_responses(responses, &topic_ids).await?;
        let records = self.drain_fetch_buffer().await;
        tracing::Span::current().record("records", records.len());
        Ok(records)
    }

    /// Wait until the join that the coordinator task runs for this `poll`
    /// completes, or until `deadline`. Return `false` when the join did not
    /// complete in time.
    ///
    /// For the eager protocol, Kafka's `ConsumerCoordinator.onJoinPrepare`
    /// revokes every partition before the `JoinGroup` inside `poll`, so that
    /// `poll` fetches nothing until the join completes. The cooperative
    /// protocol keeps the owned partitions, and `poll` fetches them while the
    /// join runs. A member without partitions has nothing to fetch either.
    ///
    /// The rebalance listener calls that the join asks for run here, inside
    /// `poll`.
    ///
    /// # Errors
    ///
    /// Returns [`ConsumerError::RebalanceListenerFailed`] when a listener
    /// callback failed.
    pub(crate) async fn wait_for_rebalance(
        &mut self,
        deadline: tokio::time::Instant,
    ) -> Result<bool, ConsumerError> {
        // Kafka's `onJoinComplete` keeps the first callback error and throws
        // it after the rebalance step. A failed callback therefore does not
        // end this `poll` while the join that queued it still runs.
        let mut first_error = self.run_pending_listener_calls().await.err();
        if !*self.rebalance_pending.borrow() {
            return first_error.map_or(Ok(true), Err);
        }
        let eager = self.rebalance_protocol == assignor::RebalanceProtocol::Eager;
        if first_error.is_none() && !eager && !self.assigned.lock().await.is_empty() {
            return Ok(true);
        }
        // The coordinator task can request the rejoin after the start of this
        // `poll` signalled it. Signal again, so that this `poll` starts the
        // join that it waits for, as Kafka's `ensureActiveGroup` does.
        coordinator::note_poll(&self.poll_signal);
        let wakeup = self.wakeup.clone();
        let joined = loop {
            // A slow callback must not keep this `poll` past its timeout. The
            // next `poll` runs the calls that come later.
            if tokio::time::Instant::now() >= deadline {
                break false;
            }
            let call = tokio::select! {
                biased;
                call = self.listener_calls.recv() => call,
                joined = self.rebalance_pending.wait_for(|pending| !*pending) => {
                    // A closed channel means that the coordinator task
                    // stopped. `poll` then continues with the assignment that
                    // it has.
                    let _ = joined;
                    break true;
                }
                () = tokio::time::sleep_until(deadline) => break false,
                // A wakeup ends the wait between two callbacks, never inside
                // one.
                () = wakeup.woken() => {
                    first_error.get_or_insert(ConsumerError::Wakeup);
                    break false;
                }
            };
            match call {
                Some(call) => {
                    if let Err(error) = self.complete_listener_call(call).await {
                        first_error.get_or_insert(error);
                    }
                }
                // No listener calls can come: wait for the join alone.
                None => {
                    break tokio::select! {
                        biased;
                        joined = self.rebalance_pending.wait_for(|pending| !*pending) => {
                            let _ = joined;
                            true
                        }
                        () = tokio::time::sleep_until(deadline) => false,
                        () = wakeup.woken() => {
                            first_error.get_or_insert(ConsumerError::Wakeup);
                            false
                        }
                    };
                }
            }
        };
        // The join can end with calls that are still waiting, for example the
        // assign callback.
        if let Err(error) = self.run_pending_listener_calls().await {
            first_error.get_or_insert(error);
        }
        first_error.map_or(Ok(joined), Err)
    }

    /// Return up to `max_poll_records` buffered records, and move the consumed
    /// positions past them.
    ///
    /// The `assigned` guard lives until the positions move, so the coordinator
    /// cannot revoke a partition between the ownership check and the drain.
    /// The lock order is `assigned`, then `next_offsets`, then `positions`, as
    /// in the coordinator task.
    async fn drain_fetch_buffer(&mut self) -> Vec<ConsumerRecord> {
        let ownership_ids = self.commit_identity.lock().await.ownership_ids.clone();
        let paused = self.paused_of(&ownership_ids);
        let assigned_guard = self.assigned.lock().await;
        let assigned: std::collections::HashSet<(String, i32)> =
            assigned_guard.iter().cloned().collect();
        let mut offsets = self.next_offsets.lock().await;
        let mut positions = self.positions.lock().await;
        let records = self.fetch_buffer.drain(
            self.max_poll_records,
            &assigned,
            &paused,
            &mut offsets,
            &mut positions,
        );
        drop(positions);
        drop(offsets);
        drop(assigned_guard);
        records
    }
    /// Wait when this `poll` sends no Fetch and none is in flight, for example
    /// when every partition is paused.
    ///
    /// Kafka's `ClassicKafkaConsumer.pollForFetches` waits for the poll timeout,
    /// or for the retry backoff while a partition has no valid position. A
    /// listener call that waits also ends the wait after the retry backoff, so
    /// the next `poll` runs it.
    async fn wait_without_fetches(
        &self,
        assigned: &[(String, i32)],
        deadline: tokio::time::Instant,
    ) {
        let all_positions = {
            let offsets = self.next_offsets.lock().await;
            let positions = self.positions.lock().await;
            assigned.iter().all(|key| {
                offsets
                    .get(key)
                    .is_some_and(|offset| !is_reset_sentinel(*offset))
                    && !positions
                        .get(key)
                        .is_some_and(|position| position.awaiting_validation)
            })
        };
        let until = if all_positions && self.listener_calls.is_empty() {
            deadline
        } else {
            deadline.min(tokio::time::Instant::now() + self.retry_policy.initial_backoff)
        };
        // A rebalance that starts during the wait queues listener calls that
        // this consumer must run, so it ends the wait, as Kafka's
        // `pollForFetches` waits at most `coordinator.timeToNextPoll`.
        let mut rebalance_pending = self.rebalance_pending.clone();
        rebalance_pending.borrow_and_update();
        let rebalance = async {
            // Without a coordinator task nothing changes the flag.
            if rebalance_pending.changed().await.is_err() {
                std::future::pending::<()>().await;
            }
        };
        tokio::select! {
            () = tokio::time::sleep_until(until) => {}
            () = rebalance => {}
        }
    }

    /// Decode the fetch responses into the fetch buffer, and act on the
    /// partition errors.
    async fn process_fetch_responses(
        &mut self,
        responses: Vec<fetch_response::FetchResponse>,
        topic_ids: &HashMap<String, uuid::Uuid>,
    ) -> Result<(), ConsumerError> {
        // 3. Decode each partition's RecordBatches, advance next-offsets.
        //
        // The wire-level `records` field can carry multiple concatenated
        // RecordBatches; we iterate every v2 batch, emit one ConsumerRecord
        // per Record, and bump next_offsets to the highest seen offset + 1.
        // Reverse-map topic_id → name. At Fetch v ≥ 13 the response carries
        // only `topic_id`; `topic.topic` is empty.
        let id_to_name = offset_wire::id_to_name(topic_ids);

        // Re-snapshot the assignment: a cooperative rebalance may have
        // revoked partitions while this Fetch was in flight. Records for
        // partitions we no longer own must be dropped — the new owner will
        // serve them from the offset we committed at revoke time. Snapshot
        // before locking `next_offsets` to keep the coordinator's
        // assigned→next_offsets lock order (avoids deadlock).
        let still_owned: std::collections::HashSet<(String, i32)> =
            self.assigned.lock().await.iter().cloned().collect();

        let mut fetched: Vec<BufferedPartition> = Vec::new();
        let mut refresh_after_processing = false;
        // The partitions that answered `OFFSET_OUT_OF_RANGE` under `Earliest`
        // or `None`, with the offset each was fetched from. Both policies need
        // the broker's real log start, and reading that is an RPC, so the loop
        // records them and the code below the guard resolves them.
        let mut out_of_range: Vec<((String, i32), i64)> = Vec::new();
        // The fetch positions that this loop resets, for the auto commit
        // before a `JoinGroup`.
        let mut polled_resets: Vec<((String, i32), Option<i64>)> = Vec::new();
        // KIP-392 preferred read replica changes: `Some(replica)` to fetch from
        // it, `None` to go back to the leader.
        let mut replica_updates: Vec<((String, i32), Option<i32>)> = Vec::new();
        let mut offsets = self.next_offsets.lock().await;
        for topic in responses.iter().flat_map(|resp| &resp.responses) {
            let topic_name = if topic.topic.is_empty() {
                id_to_name.get(&topic.topic_id).cloned().unwrap_or_default()
            } else {
                topic.topic.clone()
            };
            for part in &topic.partitions {
                // Drop records for partitions revoked while this Fetch was
                // in flight (cooperative rebalance transparency).
                if !still_owned.contains(&(topic_name.clone(), part.partition_index)) {
                    continue;
                }

                let key = (topic_name.clone(), part.partition_index);

                // KIP-320 in-band truncation: leader served no records and told
                // us where to truncate (diverging_epoch.end_offset >= 0).
                if part.diverging_epoch.end_offset >= 0 {
                    self.handle_truncation_in_poll(
                        &mut offsets,
                        &key,
                        part.diverging_epoch.end_offset,
                    )?;
                    polled_resets.push((key.clone(), Some(part.diverging_epoch.end_offset)));
                    continue;
                }
                // Error-first: inspect the partition error_code before decoding.
                let action = classify_fetch_partition_error(part.error_code);
                // Kafka's `FetchCollector.handleInitializeErrors` clears the
                // preferred read replica with each metadata update request.
                if matches!(
                    action,
                    FetchPartitionAction::Reroute
                        | FetchPartitionAction::RevalidateEpoch
                        | FetchPartitionAction::RefreshMetadata
                ) {
                    replica_updates.push((key.clone(), None));
                }
                match action {
                    FetchPartitionAction::Records => {
                        // `FetchCollector.updatePartitionState`.
                        if part.preferred_read_replica >= 0 {
                            replica_updates.push((key.clone(), Some(part.preferred_read_replica)));
                        }
                    }
                    FetchPartitionAction::ResetOffset => {
                        // The response cannot say where the log now starts. Apache
                        // Kafka builds an errored partition with `log_start_offset`,
                        // `high_watermark` and `last_stable_offset` all set to -1, so
                        // `part.log_start_offset` is -1 here whatever the real log
                        // start is. Reading it and fetching from it wedges the
                        // partition: every following Fetch asks for -1 and gets
                        // OFFSET_OUT_OF_RANGE again. A hardcoded 0 wedges it the same
                        // way once retention has moved the log start past 0.
                        //
                        // So every policy resolves its position with a ListOffsets
                        // instead. Latest and by_duration plant a sentinel that
                        // `resolve_reset_sentinels` replaces before the next Fetch.
                        // Earliest and None instead go to `out_of_range`, so the
                        // true log start can be read once the offsets guard is
                        // released; None then reports the error instead of using it.
                        match self.auto_offset_reset {
                            AutoOffsetReset::Latest | AutoOffsetReset::ByDuration(_) => {
                                offsets.insert(key.clone(), LATEST_SENTINEL);
                                polled_resets.push((key.clone(), None));
                            }
                            AutoOffsetReset::Earliest | AutoOffsetReset::None => {
                                let fetch_offset = fetch_offset_or_unknown(&offsets, &key);
                                out_of_range.push((key.clone(), fetch_offset));
                            }
                        }
                        continue;
                    }
                    FetchPartitionAction::Reroute => {
                        // A routing miss, NOT a truncation: we sent the Fetch to
                        // a broker that no longer leads this partition (e.g. a
                        // leadership change since the last metadata refresh).
                        // Re-target the leader so the next poll routes correctly;
                        // do NOT set awaiting_validation (nothing diverged).
                        let mut positions = self.positions.lock().await;
                        if part.current_leader.leader_id >= 0 {
                            // The broker handed us the new leader inline (KIP-320
                            // current_leader hint). Adopt it immediately.
                            let p = positions.entry(key.clone()).or_default();
                            p.leader_id = part.current_leader.leader_id;
                            // Wrap the KIP-320 current-leader hint (raw wire
                            // `int32`) at the Fetch-response decode boundary.
                            p.leader_epoch = LeaderEpoch(part.current_leader.leader_epoch);
                        } else {
                            // No hint: force a metadata refresh after this loop
                            // so the next poll learns the new leader. Reset the
                            // stale leader id so the bootstrap fallback (and a
                            // re-flag, if metadata advances the epoch) kicks in.
                            if let Some(p) = positions.get_mut(&key) {
                                p.leader_id = UNKNOWN_LEADER_ID;
                            }
                            drop(positions);
                            refresh_after_processing = true;
                        }
                        continue;
                    }
                    FetchPartitionAction::RevalidateEpoch => {
                        let mut positions = self.positions.lock().await;
                        if let Some(p) = positions.get_mut(&key) {
                            // Force refresh_leader_epochs to re-flag against
                            // fresher metadata next poll (any real epoch >= 0 > -1).
                            p.leader_epoch = LeaderEpoch(UNKNOWN_LEADER_ID);
                            self.client.metadata_topics().request_update();
                            // Only gate on validation when we have a consumed epoch
                            // to validate against. A never-consumed partition
                            // (offset_epoch < 0) has nothing to validate; flagging it
                            // would wedge it — validate_positions skips offset_epoch
                            // < 0, and the fetch builder skips awaiting_validation.
                            if p.offset_epoch.is_known() {
                                p.awaiting_validation = true;
                            }
                        }
                        continue;
                    }
                    FetchPartitionAction::RefreshMetadata => {
                        // The broker does not know the topic or the topic id. Skip the
                        // row and refresh metadata, as Kafka's consumer does. The
                        // refresh at the start of the next poll also stores a new
                        // topic id for a topic that was created again.
                        tracing::warn!(
                            topic = %topic_name,
                            partition = part.partition_index,
                            error_code = part.error_code,
                            "fetch partition names a topic the broker does not know; refreshing metadata"
                        );
                        refresh_after_processing = true;
                        continue;
                    }
                    FetchPartitionAction::Fail(code) => {
                        return Err(ConsumerError::Server(code));
                    }
                }

                record_readable_end(
                    &mut *self.end_offsets.lock().await,
                    key.clone(),
                    self.isolation_level,
                    part.high_watermark,
                    part.last_stable_offset,
                );

                if let Some(partition) =
                    self.process_partition_records(&offsets, &key, &topic_name, part)
                {
                    fetched.push(partition);
                }
            }
        }
        if let Some(auto_commit) = &self.auto_commit {
            auto_commit.reset_polled(polled_resets).await;
        }
        // Drop the offsets guard before any `.await`: refreshing metadata is an
        // RPC, and we must never hold a Mutex guard across an await point.
        drop(offsets);
        let now = tokio::time::Instant::now();
        for (key, replica) in replica_updates {
            match replica {
                // `TopicPartitionState.updatePreferredReadReplica` restarts the
                // expiry only when the replica changes.
                Some(replica) => {
                    let expires = now + self.metadata_max_age.to_std();
                    let entry = self
                        .fetches
                        .preferred_read_replicas
                        .entry(key)
                        .or_insert((replica, expires));
                    if entry.0 != replica {
                        *entry = (replica, expires);
                    }
                }
                None => {
                    self.fetches.preferred_read_replicas.remove(&key);
                }
            }
        }
        for partition in fetched {
            self.fetch_buffer.push(partition);
        }
        if !out_of_range.is_empty() && self.recover_out_of_range(&out_of_range).await? {
            refresh_after_processing = true;
        }
        if refresh_after_processing {
            // A NOT_LEADER_OR_FOLLOWER without a current_leader hint means our
            // cached leader is stale, and UNKNOWN_TOPIC_OR_PARTITION and
            // UNKNOWN_TOPIC_ID mean our topic metadata is stale. Kafka's
            // fetcher calls `Metadata.requestUpdate`; the next poll refreshes
            // before it routes a fetch.
            self.client.metadata_topics().request_update();
        }
        Ok(())
    }

    /// Send a Fetch to each leader in `by_leader` that has no Fetch in flight.
    /// Each request runs in its own task, so the leaders answer in parallel.
    fn send_fetches(&mut self, by_leader: FetchByLeader, topic_ids: &HashMap<String, uuid::Uuid>) {
        let max_wait_ms = consumer::protocol_millis_i32(self.fetch_max_wait);
        for (leader, by_topic) in by_leader {
            if self.fetches.in_flight.contains_key(&leader) {
                continue;
            }
            let wanted: BTreeMap<(String, i32), SessionPartition> = by_topic
                .into_iter()
                .flat_map(|(topic, specs)| {
                    let topic_id = topic_ids.get(&topic).copied().unwrap_or_default();
                    let partition_max = self.fetch_partition_max;
                    specs.into_iter().map(move |spec| {
                        (
                            (topic.clone(), spec.0),
                            session_partition(topic_id, spec, partition_max),
                        )
                    })
                })
                .collect();
            let requested = wanted
                .iter()
                .map(|(key, partition)| (key.clone(), partition.fetch_offset))
                .collect();
            let from_replica = wanted
                .keys()
                .filter(|key| {
                    self.fetches
                        .preferred_read_replicas
                        .get(*key)
                        .is_some_and(|(replica, _)| *replica == leader)
                })
                .cloned()
                .collect();
            let session = self
                .fetches
                .sessions
                .entry(leader)
                .or_default()
                .build(wanted);
            let request = build_fetch_request(
                max_wait_ms,
                self.isolation_level,
                self.fetch_min,
                self.fetch_max,
                self.client_rack.as_deref().unwrap_or_default(),
                session,
            );
            let client = self.client.clone();
            let completed = Arc::clone(&self.fetches.completed);
            let (sender, result) = tokio::sync::oneshot::channel();
            let handle = tokio::spawn(async move {
                let response = if should_use_bootstrap_leader(leader) {
                    client.send(request).await
                } else {
                    client.broker(leader).send(request).await
                };
                let _ = sender.send(response);
                completed.notify_one();
            });
            self.fetches.in_flight.insert(
                leader,
                InFlightFetch {
                    handle,
                    result,
                    requested,
                    from_replica,
                },
            );
        }
    }

    /// Wait until a Fetch in flight completes, or until `deadline`. Kafka's
    /// `ClassicKafkaConsumer.pollForFetches` bounds the network wait with the
    /// poll timer and returns as soon as a fetch is available.
    async fn wait_for_fetches(&mut self, deadline: tokio::time::Instant) {
        loop {
            self.fetches.collect_ready();
            if self.fetches.in_flight.is_empty() || !self.fetches.ready.is_empty() {
                return;
            }
            tokio::select! {
                () = self.fetches.completed.notified() => {}
                () = tokio::time::sleep_until(deadline) => return,
            }
        }
    }

    /// Take the responses of the completed Fetch requests, and update the
    /// fetch session of each broker.
    ///
    /// The function drops the data of a partition whose fetch position changed
    /// after the request, as Kafka's `FetchCollector.initialize` discards a
    /// stale fetch. A response that the fetch session rejects gives no data, as
    /// in `AbstractFetch.handleFetchSuccess`.
    ///
    /// # Errors
    ///
    /// Returns a transport error that is not transient.
    async fn take_completed_fetches(
        &mut self,
        topic_ids: &HashMap<String, uuid::Uuid>,
    ) -> Result<Vec<FetchResponse>, ConsumerError> {
        self.fetches.collect_ready();
        if self.fetches.ready.is_empty() {
            return Ok(Vec::new());
        }
        let id_to_name = offset_wire::id_to_name(topic_ids);
        let offsets = self.next_offsets.lock().await.clone();
        let mut responses = Vec::new();
        let mut failure = None;
        for fetch in std::mem::take(&mut self.fetches.ready) {
            let leader = fetch.leader;
            let session = self.fetches.sessions.entry(leader).or_default();
            match fetch.result {
                Some(Ok(mut response)) => {
                    let partitions = response
                        .responses
                        .iter()
                        .map(|topic| topic.partitions.len())
                        .sum();
                    if !session.handle_response(
                        response.error_code,
                        response.session_id,
                        partitions,
                        response.throttle_time_ms,
                    ) {
                        tracing::debug!(
                            leader,
                            error_code = response.error_code,
                            "fetch session answered with an error; the next fetch is a full fetch"
                        );
                        continue;
                    }
                    let mut replica_out_of_range = Vec::new();
                    for topic in &mut response.responses {
                        let name = if topic.topic.is_empty() {
                            id_to_name.get(&topic.topic_id).cloned().unwrap_or_default()
                        } else {
                            topic.topic.clone()
                        };
                        topic.partitions.retain(|partition| {
                            let key = (name.clone(), partition.partition_index);
                            // A preferred replica that answers out of range
                            // gives no reset: Kafka's `handleInitializeErrors`
                            // clears the replica and fetches from the leader.
                            // The request decides this, as the preference can
                            // change while the Fetch is in flight.
                            if partition.error_code == OFFSET_OUT_OF_RANGE
                                && fetch.from_replica.contains(&key)
                            {
                                replica_out_of_range.push(key);
                                return false;
                            }
                            fetch.requested.get(&key).is_some_and(|requested| {
                                offsets.get(&key).copied().unwrap_or(0) == *requested
                            })
                        });
                    }
                    for key in replica_out_of_range {
                        self.fetches.preferred_read_replicas.remove(&key);
                    }
                    responses.push(response);
                }
                Some(Err(error)) => {
                    session.handle_error();
                    // `AbstractFetch.handleFetchFailure` clears the preferred read
                    // replica of each partition of the failed session.
                    for key in fetch.requested.keys() {
                        self.fetches.preferred_read_replicas.remove(key);
                    }
                    if is_transient_transport_error(&error) {
                        if should_use_bootstrap_leader(leader) {
                            self.client.reconnect_bootstrap().await;
                        } else {
                            self.client.evict_broker(leader);
                        }
                    } else if failure.is_none() {
                        failure = Some(error);
                    }
                }
                // The task ended without a result: it panicked or was aborted.
                None => session.handle_error(),
            }
        }
        match failure {
            Some(error) => Err(error.into()),
            None => Ok(responses),
        }
    }

    /// Close the fetch session of each broker. Kafka's `AbstractFetch.close`
    /// sends a Fetch with the session id, epoch `-1` and no partitions to each
    /// broker that holds a session.
    pub(crate) async fn close_fetch_sessions(&mut self, timeout: std::time::Duration) {
        let max_wait_ms = consumer::protocol_millis_i32(self.fetch_max_wait);
        let closes: Vec<_> = self
            .fetches
            .sessions
            .iter()
            .filter(|(_, session)| session.session_id() != INVALID_SESSION_ID)
            .map(|(leader, session)| {
                let request = build_fetch_request(
                    max_wait_ms,
                    self.isolation_level,
                    self.fetch_min,
                    self.fetch_max,
                    self.client_rack.as_deref().unwrap_or_default(),
                    SessionRequest {
                        session_id: session.session_id(),
                        session_epoch: FINAL_EPOCH,
                        partitions: BTreeMap::new(),
                        forgotten: Vec::new(),
                    },
                );
                let client = self.client.clone();
                let leader = *leader;
                async move {
                    let _ = if should_use_bootstrap_leader(leader) {
                        client.send(request).await
                    } else {
                        client.broker(leader).send(request).await
                    };
                }
            })
            .collect();
        let _ = tokio::time::timeout(timeout, futures_util::future::join_all(closes)).await;
    }

    pub(crate) async fn group_fetches(&mut self, assigned: &[(String, i32)]) -> FetchByLeader {
        let subscription = self.subscription.borrow().clone();
        let awaiting_callback = self
            .assigned_callback_pending
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .clone();
        let ownership_ids = self.commit_identity.lock().await.ownership_ids.clone();
        let paused = self.paused_of(&ownership_ids);
        let buffered = self.fetch_buffer.buffered_partitions();
        let mut grouped: FetchByLeader = HashMap::new();
        let now = tokio::time::Instant::now();
        let mut refresh_metadata = false;
        {
            let offsets = self.next_offsets.lock().await;
            let positions = self.positions.lock().await;
            for (t, p) in assigned {
                if awaiting_callback.contains(&(t.clone(), *p)) {
                    continue;
                }
                // Kafka's `SubscriptionState.isFetchableAndSubscribed`: with a
                // topic subscription, a partition of a topic that the
                // consumer no longer subscribes to is not fetched. A pattern
                // subscription has no topic list of the application, and a
                // regular expression subscription has none at all, because the
                // group coordinator matches it.
                if !subscription.manual_assignment
                    && subscription.pattern.is_none()
                    && subscription.regex.is_none()
                    && !subscription.contains(t)
                {
                    continue;
                }
                // Skip partitions still awaiting validation — they must not be
                // fetched until proven consistent.
                if positions
                    .get(&(t.clone(), *p))
                    .is_some_and(|x| x.awaiting_validation)
                {
                    continue;
                }
                let next = offsets.get(&(t.clone(), *p)).copied().unwrap_or(0);
                // A reset sentinel is a reset that `ListOffsets` did not
                // resolve yet. Kafka does not fetch a partition that awaits a
                // reset, and the sentinel is no offset to fetch from.
                if is_reset_sentinel(next) {
                    continue;
                }
                // Kafka's `AbstractFetch.fetchablePartitions` leaves out a
                // paused partition and a partition with buffered records.
                if paused.contains(&(t.clone(), *p)) || buffered.contains(&(t.clone(), *p)) {
                    continue;
                }
                let pos = positions.get(&(t.clone(), *p)).copied().unwrap_or_default();
                // Route to the leader when its id is known AND the pool has a
                // dialable address for it; otherwise fall back to the bootstrap
                // connection. `knows_broker` is a synchronous registry lookup
                // (no await), so it's safe to call while the offsets/positions
                // guards are held. A leader whose advertised address is unusable
                // (e.g. port 0 from an in-process test broker) is treated as
                // unknown — the bootstrap broker is the leader in that
                // single-broker case anyway.
                let mut leader =
                    fetch_leader_id(pos.leader_id, self.client.knows_broker(pos.leader_id));
                // KIP-392: Kafka's `AbstractFetch.selectReadReplica` fetches from
                // the preferred read replica until it expires. A replica that is
                // not in the metadata clears the preference and refreshes it.
                let key = (t.clone(), *p);
                match self.fetches.preferred_read_replicas.get(&key).copied() {
                    Some((_, expires)) if now > expires => {
                        self.fetches.preferred_read_replicas.remove(&key);
                    }
                    Some((replica, _)) if self.client.knows_broker(replica) => leader = replica,
                    Some(_) => {
                        self.fetches.preferred_read_replicas.remove(&key);
                        refresh_metadata = true;
                    }
                    None => {}
                }
                // Kafka's `AbstractFetch.prepareFetchRequests` skips a broker
                // with a pending Fetch.
                if self.fetches.in_flight.contains_key(&leader) {
                    continue;
                }
                grouped
                    .entry(leader)
                    .or_default()
                    .entry(t.clone())
                    .or_default()
                    .push((*p, next, pos.leader_epoch, pos.offset_epoch));
            }
        }
        if refresh_metadata {
            // The next poll refreshes, as for the other metadata updates of
            // the fetch path.
            self.client.metadata_topics().request_update();
        }

        grouped
    }

    async fn prepare_poll(
        &mut self,
        deadline: tokio::time::Instant,
    ) -> Result<bool, ConsumerError> {
        // Kafka's consumer raises a fatal `OffsetFetch` error from `poll()`.
        // A rejoin in the coordinator task leaves such an error here.
        if let Some(error) = coordinator::take_poll_error(&self.poll_error) {
            return Err(error);
        }
        // Kafka's `ConsumerCoordinator.poll` sends the interval auto commit
        // before `updateFetchPositions`.
        self.maybe_auto_commit_async().await;
        // This `poll` resets the poll timer of the coordinator task, and lets
        // it start a rejoin that waits for a `poll`. The positions for the
        // commit before that join are already recorded above.
        coordinator::note_poll(&self.poll_signal);
        // Metadata comes first. A partition that has no position yet (a new
        // assignment without a committed offset) gets its leader id and
        // epoch here, so its first `ListOffsets` goes to the leader and not
        // to the bootstrap broker. Kafka's `OffsetFetcher.groupListOffsetRequests`
        // routes with `metadata.currentLeader(tp)` in the same way.
        self.sync_metadata_topics();
        let wakeup = self.wakeup.clone();
        self.update_fetch_positions(Some(&wakeup), Some(deadline))
            .await
    }

    /// Name the subscribed topics in the metadata requests of this consumer.
    /// The coordinator task changes the topics of a pattern subscription on
    /// its own client, so `poll` learns the leaders of a topic that a pattern
    /// adds. Kafka's `ConsumerMetadata` reads the subscription of each update.
    fn sync_metadata_topics(&self) {
        let topics = {
            let subscription = self.subscription.borrow();
            (!subscription.is_none()).then(|| subscription.topics.clone())
        };
        if let Some(topics) = topics
            && self.client.metadata_topics().names() != topics
        {
            self.client.metadata_topics().set(topics);
        }
    }

    /// Whether the metadata must be refreshed before this consumer fetches.
    ///
    /// Kafka's consumer refreshes when `Metadata.updateRequested` holds: the
    /// metadata is older than `metadata.max.age.ms`, the topics changed, a
    /// fetch error asked for an update, or an assigned partition has no known
    /// leader. It does not refresh on every `poll`. A refresh on every poll
    /// pipelines a `Metadata` request behind a parked long-poll `Fetch` on the
    /// same connection, and the broker answers it only when the fetch
    /// returns, so the poll stalls for up to `fetch.max.wait.ms` and sends no
    /// fetch to the other leaders meanwhile (krabka-io/krabka-broker#1394).
    pub(crate) async fn metadata_due(&self) -> bool {
        let topics = self.client.metadata_topics();
        if topics.update_requested()
            || topics
                .last_refreshed()
                .is_none_or(|at| at.elapsed() >= self.metadata_max_age.to_std())
        {
            return true;
        }
        let assigned = self.assigned.lock().await.clone();
        let positions = self.positions.lock().await;
        assigned.iter().any(|key| {
            positions
                .get(key)
                .is_none_or(|position| position.leader_id < 0)
        })
    }

    /// Refresh the leader epochs when [`metadata_due`](Self::metadata_due),
    /// resolve the offset resets and validate the positions. Kafka's
    /// `KafkaConsumer.updateFetchPositions`.
    ///
    /// Return `false` after a transient error, when the caller must try again
    /// later.
    ///
    /// With `wakeup`, a wakeup ends each request of the update, as Kafka's
    /// `ConsumerNetworkClient.poll` throws `WakeupException` while it waits.
    /// The truncation that the validation found is applied without a wakeup
    /// check.
    pub(crate) async fn update_fetch_positions(
        &self,
        wakeup: Option<&control::WakeupHandle>,
        deadline: Option<tokio::time::Instant>,
    ) -> Result<bool, ConsumerError> {
        if self.metadata_due().await
            && let Err(error) = until_woken(wakeup, self.refresh_leader_epochs()).await?
        {
            if is_transient_poll_error(&error) {
                self.client.reconnect_bootstrap().await;
                return Ok(false);
            }
            return Err(error);
        }
        if let Err(error) = until_woken(wakeup, self.resolve_committed_sentinels(deadline)).await? {
            if is_transient_poll_error(&error) {
                self.client.reconnect_bootstrap().await;
                return Ok(false);
            }
            return Err(error);
        }
        // Kafka's `resetInitializingPositions` throws `NoOffsetForPartitionException`
        // for every partition that has no committed offset and no reset
        // policy, before it ever sends a `ListOffsets`.
        self.fail_partitions_without_offset().await?;
        if let Err(error) = until_woken(wakeup, self.resolve_reset_sentinels()).await? {
            if is_transient_poll_error(&error) {
                self.client.reconnect_bootstrap().await;
                return Ok(false);
            }
            return Err(error);
        }
        let truncated = match until_woken(wakeup, self.validate_positions()).await? {
            Ok(truncated) => truncated,
            Err(error) if is_transient_poll_error(&error) => {
                self.client.reconnect_bootstrap().await;
                return Ok(false);
            }
            Err(error) => return Err(error),
        };
        if !truncated.is_empty() {
            self.apply_truncation(&truncated).await?;
        }
        Ok(true)
    }

    /// Decode the records of one partition row into a [`BufferedPartition`].
    /// The consumed position does not move here. It moves when `poll` returns
    /// the records.
    fn process_partition_records(
        &self,
        offsets: &HashMap<(String, i32), i64>,
        key: &(String, i32),
        topic_name: &str,
        part: &fetch_response::PartitionData,
    ) -> Option<BufferedPartition> {
        // Legacy MessageSet payloads are skipped here; the consumer
        // only handles v2 batches.
        let batches = part.records.as_ref()?.as_v2()?;
        // The broker returns whole record batches whose last offset is
        // >= the requested fetch_offset, even when the batch starts
        // before it (e.g. after an OFFSET_OUT_OF_RANGE reset or when
        // a single large batch straddles log_start). Kafka's JVM
        // client skips any records below the position; we do the same.
        // Capture the position now — before `next_offset_after` updates
        // it — so the filter baseline matches the actual fetch offset.
        let fetch_floor = offsets.get(key).copied().unwrap_or(0);
        let mut records = std::collections::VecDeque::new();
        // read_committed filtering happens entirely client-side: the
        // broker returns verbatim on-disk bytes (control batches,
        // aborted records and all) plus an `aborted_transactions`
        // list. We replay Kafka's algorithm — walk batches in offset
        // order, tracking which producer_ids have an open aborted
        // transaction, and drop transactional records from those.
        let read_committed = is_read_committed(self.isolation_level);
        // Aborted txns sorted by first_offset; consumed front-to-back
        // as batch offsets advance past each entry's start.
        let mut aborted: std::collections::VecDeque<(i64, i64)> = if read_committed {
            let mut v: Vec<(i64, i64)> = part
                .aborted_transactions
                .as_deref()
                .unwrap_or(&[])
                .iter()
                .map(|a| (a.first_offset, a.producer_id))
                .collect();
            v.sort_unstable();
            v.into()
        } else {
            std::collections::VecDeque::new()
        };
        // producer_ids with a currently-open aborted transaction.
        let mut aborted_pids: std::collections::HashSet<i64> = std::collections::HashSet::new();
        for batch in batches {
            // Move every aborted txn that starts at or before this
            // batch into the active set.
            if read_committed {
                while let Some(&(first_offset, pid)) = aborted.front() {
                    if aborted_txn_started(first_offset, batch.base_offset) {
                        aborted_pids.insert(pid);
                        aborted.pop_front();
                    } else {
                        break;
                    }
                }
            }
            // Control batches (commit/abort markers) carry no user
            // records. A control batch for a producer ends its aborted
            // transaction; drop the batch either way.
            if batch.attributes.is_control_batch() {
                if read_committed {
                    aborted_pids.remove(&batch.producer_id);
                }
                continue;
            }
            // Drop transactional records belonging to an aborted txn.
            if should_drop_aborted_batch(
                read_committed,
                batch.attributes.is_transactional(),
                aborted_pids.contains(&batch.producer_id),
            ) {
                continue;
            }
            for r in &batch.records {
                let offset = record_offset(batch.base_offset, r.offset_delta);
                // Skip records that precede the fetch floor: the broker
                // returned a whole batch whose base_offset < our
                // position (straddle case — see fetch_floor comment).
                if offset < fetch_floor {
                    continue;
                }
                let (timestamp, timestamp_type) = record_timestamp(batch, r);
                records.push_back(ConsumerRecord {
                    topic: topic_name.to_string(),
                    partition: part.partition_index,
                    offset,
                    leader_epoch: batch.partition_leader_epoch,
                    timestamp,
                    timestamp_type,
                    key: r.key.clone(),
                    value: r.value.clone(),
                    headers: r
                        .headers
                        .iter()
                        .map(|h| Header {
                            key: h.key.clone(),
                            value: h.value.clone(),
                        })
                        .collect(),
                });
            }
        }
        // When `poll` returns the last record, the position moves past every
        // fetched batch, and the position's offset_epoch becomes the highest
        // batch leader epoch, so the next Fetch sends the correct
        // last_fetched_epoch (KIP-320).
        let next_offset = next_offset_after(batches)?;
        Some(BufferedPartition {
            key: key.clone(),
            position: fetch_floor,
            records,
            next_offset,
            // Wrap the batch's raw wire `partition_leader_epoch` (`int32`) at
            // the RecordBatch decode boundary.
            last_epoch: batches
                .iter()
                .map(|batch| batch.partition_leader_epoch)
                .max()
                .map(LeaderEpoch),
        })
    }
}

const fn readable_end_offset(
    isolation: IsolationLevel,
    high_watermark: i64,
    last_stable_offset: i64,
) -> i64 {
    match isolation {
        IsolationLevel::ReadCommitted => last_stable_offset,
        IsolationLevel::ReadUncommitted => high_watermark,
    }
}

fn record_readable_end(
    ends: &mut HashMap<(String, i32), i64>,
    key: (String, i32),
    isolation: IsolationLevel,
    high_watermark: i64,
    last_stable_offset: i64,
) {
    ends.insert(
        key,
        readable_end_offset(isolation, high_watermark, last_stable_offset),
    );
}

/// The offset to fetch next after consuming `batches`: one past the highest
/// `base_offset + last_offset_delta` across all decoded batches.
///
/// This function returns `None` when there are no batches, which leaves the
/// offset unchanged. The consumer uses it to advance past control and aborted
/// batches that emit no records, instead of re-fetching them.
fn next_offset_after(batches: &[krabka_protocol::records::RecordBatch]) -> Option<i64> {
    batches
        .iter()
        .map(|b| b.base_offset + i64::from(b.last_offset_delta) + 1)
        .max()
}

impl Consumer {
    /// Replace each reset sentinel in `next_offsets` with the offset from
    /// `ListOffsets`.
    ///
    /// A `LATEST_SENTINEL` resolves to the log end (`timestamp=-1`), or for
    /// `by_duration` to the first offset at or after now minus the duration.
    /// `auto_offset_reset = Latest` plants those sentinels at build time, and
    /// the `OFFSET_OUT_OF_RANGE` arm of the poll loop plants them again.
    /// [`Consumer::seek_to_beginning`] plants a `BEGINNING_SENTINEL`
    /// (`timestamp=-2`) and [`Consumer::seek_to_end`] an `END_SENTINEL`
    /// (`timestamp=-1`).
    ///
    /// This runs in `prepare_poll`, after the metadata refresh. A partition
    /// whose row has an error keeps its sentinel. `group_fetches` skips it, and
    /// the metadata refresh at the start of the next `prepare_poll` lets that
    /// poll try again.
    ///
    /// # Errors
    ///
    /// Returns `TopicAuthorizationFailed` when the broker does not authorize
    /// a topic, and a transport error that is not transient.
    #[tracing::instrument(
        name = "consumer.resolve_reset_sentinels",
        level = "debug",
        skip_all,
        fields(group_id = %self.group_id, sentinels = tracing::field::Empty),
        err(level = "debug")
    )]
    async fn resolve_reset_sentinels(&self) -> Result<(), ConsumerError> {
        let by_sentinel = {
            let offsets = self.next_offsets.lock().await;
            [
                (
                    LATEST_SENTINEL,
                    reset_timestamp(self.auto_offset_reset, unix_now_ms()),
                ),
                (BEGINNING_SENTINEL, EARLIEST_TIMESTAMP),
                (END_SENTINEL, LATEST_TIMESTAMP),
            ]
            .map(|(sentinel, timestamp)| (sentinel, timestamp, keys_at(&offsets, sentinel)))
        };
        let count: usize = by_sentinel.iter().map(|(_, _, keys)| keys.len()).sum();
        if count == 0 {
            return Ok(());
        }
        tracing::Span::current().record("sentinels", count);
        let mut results = Vec::new();
        for (sentinel, timestamp, keys) in by_sentinel {
            if !keys.is_empty() {
                results.push((sentinel, self.list_offsets(&keys, timestamp).await?));
            }
        }
        {
            // A seek or a rebalance can change a position while the request
            // is in flight. Replace only a sentinel that is still there.
            let mut offsets = self.next_offsets.lock().await;
            for (sentinel, result) in &results {
                for (key, offset) in &result.offsets {
                    if let Some(next) = offsets.get_mut(key)
                        && *next == *sentinel
                    {
                        *next = *offset;
                    }
                }
            }
        }
        // A row with a retriable error keeps its sentinel. Kafka's
        // `OffsetFetcher` requests a metadata update for it, so the next poll
        // sends the retry to the current leader. An unauthorized topic fails
        // the poll instead.
        if results
            .iter()
            .any(|(_, result)| result.retry && result.unauthorized.is_empty())
        {
            self.client.metadata_topics().request_update();
        }
        results
            .into_iter()
            .try_for_each(|(_, result)| result.authorization())
    }

    /// Fail with [`ConsumerError::NoOffsetForPartition`] when a partition has
    /// no committed offset under `auto_offset_reset = None`.
    ///
    /// `auto_offset_reset = None` never plants a `ListOffsets` sentinel: a
    /// partition without a commit keeps [`NO_OFFSET_SENTINEL`] in
    /// `next_offsets` until a commit or a seek gives it a real position, so
    /// this check fires again on every `poll()` and `position()` until then,
    /// as Kafka's `resetInitializingPositions` does.
    ///
    /// # Errors
    ///
    /// Returns [`ConsumerError::NoOffsetForPartition`] naming every partition
    /// with no offset.
    async fn fail_partitions_without_offset(&self) -> Result<(), ConsumerError> {
        let missing: BTreeSet<(String, i32)> = self
            .next_offsets
            .lock()
            .await
            .iter()
            .filter(|&(_, &offset)| offset == NO_OFFSET_SENTINEL)
            .map(|(key, _)| key.clone())
            .collect();
        if missing.is_empty() {
            Ok(())
        } else {
            Err(ConsumerError::NoOffsetForPartition(missing))
        }
    }

    /// Replace each `COMMITTED_SENTINEL` with the committed offset of the
    /// group, or with the reset of `auto_offset_reset` when the group has none
    /// or the consumer has no group id. Kafka's
    /// `refreshCommittedOffsetsIfNeeded` and `resetInitializingPositions`.
    ///
    /// # Errors
    ///
    /// Returns the error of the `OffsetFetch` request.
    pub(crate) async fn resolve_committed_sentinels(
        &self,
        deadline: Option<tokio::time::Instant>,
    ) -> Result<(), ConsumerError> {
        let keys = keys_at(&*self.next_offsets.lock().await, COMMITTED_SENTINEL);
        if keys.is_empty() {
            return Ok(());
        }
        // Kafka's `updateFetchPositions` gives the committed offsets the timer
        // of the `poll`.
        let deadline = deadline
            .unwrap_or_else(|| tokio::time::Instant::now() + self.default_api_timeout.to_std());
        let committed = if self.group_id.is_empty() {
            HashMap::new()
        } else {
            self.committed_until(&keys, deadline).await?
        };
        let reset = consumer::reset_starting_offset(self.auto_offset_reset);
        let mut offsets = self.next_offsets.lock().await;
        let mut positions = self.positions.lock().await;
        for key in keys {
            let Some(next) = offsets.get_mut(&key) else {
                continue;
            };
            if *next != COMMITTED_SENTINEL {
                continue;
            }
            match committed.get(&key).cloned().flatten() {
                Some(offset) => {
                    *next = offset.offset;
                    let position = positions.entry(key).or_default();
                    position.offset_epoch = LeaderEpoch(offset.leader_epoch.unwrap_or(-1));
                    // Kafka's `TopicPartitionState.seekUnvalidated` waits for
                    // `OffsetForLeaderEpoch` when the committed epoch is older
                    // than the leader epoch of the metadata.
                    position.awaiting_validation = crate::validate::should_await_validation(
                        position.leader_epoch,
                        position.offset_epoch,
                    );
                }
                None => *next = reset,
            }
        }
        Ok(())
    }

    /// Send `ListOffsets(timestamp)` for `keys` to each partition leader.
    ///
    /// This is Kafka's `OffsetFetcher.groupListOffsetRequests` and
    /// `sendListOffsetRequest`: one request per leader, with the consumer's
    /// isolation level and the leader epoch from metadata. No lock is held
    /// across a request. A transient transport error drops that connection,
    /// and its partitions are retried, as Kafka retries a
    /// `RetriableException`.
    ///
    /// # Errors
    ///
    /// Returns a transport error that is not transient.
    #[cfg_attr(test, mutants::skip)] // cargo-mutants: ListOffsets RPC orchestration, exercised by the mock-broker test
    async fn list_offsets(
        &self,
        keys: &[(String, i32)],
        timestamp: i64,
    ) -> Result<ListOffsetsResult, ConsumerError> {
        let by_leader = group_list_offsets(keys, &*self.positions.lock().await, |id| {
            self.client.knows_broker(id)
        });
        let mut answers = Vec::with_capacity(by_leader.len());
        for (leader, by_topic) in by_leader {
            let request = build_offsets_request(by_topic, timestamp, self.isolation_level);
            let answer = if is_read_committed(self.isolation_level) {
                self.send_list_offsets(leader, ReadCommittedListOffsets::new(request))
                    .await
            } else {
                self.send_list_offsets(leader, request).await
            };
            match answer {
                Ok(answer) => answers.push(answer),
                Err(e) if is_transient_transport_error(&e) => {
                    if should_use_bootstrap_leader(leader) {
                        self.client.reconnect_bootstrap().await;
                    } else {
                        self.client.evict_broker(leader);
                    }
                }
                Err(e) => return Err(e.into()),
            }
        }
        Ok(ListOffsetsResult::collect(keys, &answers))
    }

    /// Send one `ListOffsets` request to `leader`, or to the bootstrap
    /// connection when the leader is not known.
    async fn send_list_offsets<R>(
        &self,
        leader: i32,
        request: R,
    ) -> Result<ListOffsetsResponse, krabka_client_core::ClientError>
    where
        R: ProtocolRequest<Response = ListOffsetsResponse>,
    {
        if should_use_bootstrap_leader(leader) {
            self.client.send(request).await
        } else {
            self.client.broker(leader).send(request).await
        }
    }

    /// Recover the partitions that answered `OFFSET_OUT_OF_RANGE`.
    ///
    /// An errored Fetch partition carries no usable `log_start_offset`, so one
    /// `ListOffsets(timestamp=-2)` reads the real log start for the whole set.
    /// `Earliest` then fetches from there. `None` reports the first partition
    /// as a truncation, and the log start is the safe offset the caller seeks
    /// to.
    ///
    /// The caller has already released the `next_offsets` guard, so the RPC
    /// here holds no lock. A partition whose row has an error keeps its
    /// position, so the next Fetch gets `OFFSET_OUT_OF_RANGE` again and this
    /// path runs again. The return value is `true` when the caller must
    /// refresh metadata before that.
    ///
    /// # Errors
    ///
    /// Returns `TopicAuthorizationFailed` when the broker does not authorize
    /// a topic, and `LogTruncation` under `auto.offset.reset=none`.
    #[tracing::instrument(
        name = "consumer.recover_out_of_range",
        level = "debug",
        skip_all,
        fields(group_id = %self.group_id, partitions = out_of_range.len()),
        err(level = "debug")
    )]
    async fn recover_out_of_range(
        &self,
        out_of_range: &[((String, i32), i64)],
    ) -> Result<bool, ConsumerError> {
        let keys: Vec<(String, i32)> = out_of_range.iter().map(|(key, _)| key.clone()).collect();
        let result = self.list_offsets(&keys, EARLIEST_TIMESTAMP).await?;
        result.authorization()?;

        if let AutoOffsetReset::None = self.auto_offset_reset {
            if let Some(truncation) = out_of_range_truncation(out_of_range, &result.offsets) {
                return Err(truncation);
            }
            return Ok(false);
        }

        let mut offsets = self.next_offsets.lock().await;
        let log_starts = out_of_range_positions(out_of_range, &result.offsets);
        for (key, log_start) in &log_starts {
            offsets.insert(key.clone(), *log_start);
        }
        if let Some(auto_commit) = &self.auto_commit {
            auto_commit
                .reset_polled(
                    log_starts
                        .into_iter()
                        .map(|(key, log_start)| (key, Some(log_start))),
                )
                .await;
        }
        Ok(result.retry)
    }
}

/// The fetch position to write for each out-of-range partition.
///
/// A partition the answer does not mention keeps the position it has. The next
/// poll fetches it again and gets `OFFSET_OUT_OF_RANGE` again, which retries
/// this path. That is better than inventing an offset for it.
fn out_of_range_positions(
    out_of_range: &[((String, i32), i64)],
    log_starts: &HashMap<(String, i32), i64>,
) -> Vec<((String, i32), i64)> {
    out_of_range
        .iter()
        .filter_map(|(key, _)| log_starts.get(key).map(|start| (key.clone(), *start)))
        .collect()
}

/// The truncation that `auto.offset.reset=none` reports.
///
/// It names the first out-of-range partition, the offset the fetch asked for,
/// and the log start the caller seeks to. `UNKNOWN_OFFSET` as the safe offset
/// says the broker gave no answer for that partition.
fn out_of_range_truncation(
    out_of_range: &[((String, i32), i64)],
    log_starts: &HashMap<(String, i32), i64>,
) -> Option<ConsumerError> {
    let ((topic, partition), fetch_offset) = out_of_range.first()?;
    Some(ConsumerError::LogTruncation {
        topic: topic.clone(),
        partition: *partition,
        fetch_offset: *fetch_offset,
        safe_offset: log_starts
            .get(&(topic.clone(), *partition))
            .copied()
            .unwrap_or(UNKNOWN_OFFSET),
    })
}

/// The keys whose next offset is exactly `sentinel`.
fn keys_at(offsets: &HashMap<(String, i32), i64>, sentinel: i64) -> Vec<(String, i32)> {
    offsets
        .iter()
        .filter(|(_, value)| **value == sentinel)
        .map(|(key, _)| key.clone())
        .collect()
}

impl Consumer {
    /// Apply the truncations that the proactive validate pass detected to
    /// `next_offsets`.
    ///
    /// This method honors `auto.offset.reset`. With `None` it errors on the
    /// first truncated partition.
    #[tracing::instrument(
        name = "consumer.apply_truncation",
        level = "debug",
        skip_all,
        fields(group_id = %self.group_id, truncated = truncated.len()),
        err(level = "debug")
    )]
    async fn apply_truncation(
        &self,
        truncated: &HashMap<(String, i32), i64>,
    ) -> Result<(), ConsumerError> {
        let mut offsets = self.next_offsets.lock().await;
        for (key, safe_offset) in truncated {
            if let AutoOffsetReset::None = self.auto_offset_reset {
                let fetch_offset = fetch_offset_or_unknown(&offsets, key);
                return Err(ConsumerError::LogTruncation {
                    topic: key.0.clone(),
                    partition: key.1,
                    fetch_offset,
                    safe_offset: *safe_offset,
                });
            }
            offsets.insert(key.clone(), *safe_offset);
        }
        if let Some(auto_commit) = &self.auto_commit {
            auto_commit
                .reset_polled(
                    truncated
                        .iter()
                        .map(|(key, safe_offset)| (key.clone(), Some(*safe_offset))),
                )
                .await;
        }
        Ok(())
    }

    /// In-band `diverging_epoch` handler for use inside the poll loop, while the
    /// `next_offsets` guard is already held.
    fn handle_truncation_in_poll(
        &self,
        offsets: &mut HashMap<(String, i32), i64>,
        key: &(String, i32),
        safe_offset: i64,
    ) -> Result<(), ConsumerError> {
        if let AutoOffsetReset::None = self.auto_offset_reset {
            let fetch_offset = fetch_offset_or_unknown(offsets, key);
            return Err(ConsumerError::LogTruncation {
                topic: key.0.clone(),
                partition: key.1,
                fetch_offset,
                safe_offset,
            });
        }
        offsets.insert(key.clone(), safe_offset);
        Ok(())
    }
}

#[cfg(test)]
mod offset_advance_tests {
    use std::collections::HashMap;

    use assert2::check;
    use krabka_protocol::{
        owned::{
            fetch_request::ReplicaState,
            list_offsets_response::{ListOffsetsPartitionResponse, ListOffsetsTopicResponse},
        },
        primitives::uuid::Uuid as WireUuid,
        records::{RecordBatch, RecordsPayload},
        tagged_fields::UnknownTaggedFields,
    };

    use super::*;

    fn id(n: u8) -> WireUuid {
        let mut b = [0u8; 16];
        b[15] = n;
        WireUuid(b)
    }

    #[test]
    fn fetch_partition_error_codes_map_to_kafka_consumer_actions() {
        for (name, error_code, expected) in [
            ("none", 0, FetchPartitionAction::Records),
            ("offset out of range", 1, FetchPartitionAction::ResetOffset),
            (
                "unknown topic or partition",
                3,
                FetchPartitionAction::RefreshMetadata,
            ),
            ("not leader or follower", 6, FetchPartitionAction::Reroute),
            (
                "fenced leader epoch",
                74,
                FetchPartitionAction::RevalidateEpoch,
            ),
            (
                "unknown leader epoch",
                75,
                FetchPartitionAction::RevalidateEpoch,
            ),
            (
                "unknown topic id",
                100,
                FetchPartitionAction::RefreshMetadata,
            ),
            (
                "replica not available",
                9,
                FetchPartitionAction::RefreshMetadata,
            ),
            (
                "kafka storage error",
                56,
                FetchPartitionAction::RefreshMetadata,
            ),
            (
                "offset not available",
                78,
                FetchPartitionAction::RefreshMetadata,
            ),
            (
                "inconsistent topic id",
                103,
                FetchPartitionAction::RefreshMetadata,
            ),
            (
                "topic authorization failed",
                29,
                FetchPartitionAction::Fail(29),
            ),
        ] {
            check!(
                classify_fetch_partition_error(error_code) == expected,
                "case {name}"
            );
        }
    }

    #[test]
    fn fetch_leader_id_uses_known_non_negative_leader_or_bootstrap() {
        check!(BOOTSTRAP_LEADER == -1);
        check!(UNKNOWN_LEADER_ID == -1);
        check!(UNKNOWN_FETCH_OFFSET == -1);
        for (name, leader, known, expected) in [
            ("known non-negative", 3, true, 3),
            ("negative leader", -1, true, BOOTSTRAP_LEADER),
            ("unknown broker", 3, false, BOOTSTRAP_LEADER),
        ] {
            check!(fetch_leader_id(leader, known) == expected, "case {name}");
        }
        for (name, leader, expected) in [
            ("bootstrap sentinel", BOOTSTRAP_LEADER, true),
            ("broker leader", 3, false),
        ] {
            check!(
                should_use_bootstrap_leader(leader) == expected,
                "case {name}"
            );
        }
    }

    #[test]
    fn readable_end_respects_consumer_isolation() {
        check!(readable_end_offset(IsolationLevel::ReadUncommitted, 12, 9) == 12);
        check!(readable_end_offset(IsolationLevel::ReadCommitted, 12, 9) == 9);

        let mut ends = HashMap::new();
        record_readable_end(
            &mut ends,
            ("topic-a".into(), 0),
            IsolationLevel::ReadCommitted,
            12,
            9,
        );
        check!(ends.get(&("topic-a".into(), 0)) == Some(&9));
    }

    #[test]
    fn poll_helpers_preserve_sentinel_filter_and_record_math_boundaries() {
        let key = ("topic-a".to_string(), 2);
        let mut offsets = HashMap::new();

        assert2::assert!(fetch_offset_or_unknown(&offsets, &key) == UNKNOWN_FETCH_OFFSET);
        offsets.insert(key.clone(), 42);
        check!(fetch_offset_or_unknown(&offsets, &key) == 42);

        for (name, isolation, expected) in [
            ("read committed", IsolationLevel::ReadCommitted, true),
            ("read uncommitted", IsolationLevel::ReadUncommitted, false),
        ] {
            check!(is_read_committed(isolation) == expected, "case {name}");
        }
        for (name, first, offset, expected) in [
            ("at first offset", 10, 10, true),
            ("after first offset", 10, 11, true),
            ("before first offset", 11, 10, false),
        ] {
            check!(
                aborted_txn_started(first, offset) == expected,
                "case {name}"
            );
        }
        for (name, committed, aborted, started, expected) in [
            ("drop aborted", true, true, true, true),
            ("uncommitted isolation", false, true, true, false),
            ("not aborted", true, false, true, false),
            ("transaction not started", true, true, false, false),
        ] {
            check!(
                should_drop_aborted_batch(committed, aborted, started) == expected,
                "case {name}"
            );
        }
        check!(record_offset(100, 7) == 107);
    }

    #[test]
    fn transient_transport_error_classification_is_narrow() {
        use std::io;

        use krabka_client_core::ClientError;

        let transport_cases = vec![
            ("disconnected", ClientError::Disconnected, true),
            (
                "timeout",
                ClientError::Timeout(krabka_units::millis(10)),
                true,
            ),
            (
                "connection reset",
                ClientError::Io(io::Error::new(io::ErrorKind::ConnectionReset, "reset")),
                true,
            ),
            (
                "connect refused",
                ClientError::Connect {
                    addr: "127.0.0.1:9092".parse().unwrap(),
                    source: io::Error::new(io::ErrorKind::ConnectionRefused, "refused"),
                },
                true,
            ),
            ("server error", ClientError::Server { error_code: 6 }, false),
            (
                "incompatible version",
                ClientError::IncompatibleVersion {
                    api_key: 1,
                    broker_min: 0,
                    broker_max: 1,
                    client_min: 2,
                    client_max: 3,
                },
                false,
            ),
        ];
        for (name, error, expected) in &transport_cases {
            check!(
                is_transient_transport_error(error) == *expected,
                "case {name}"
            );
        }
        let poll_cases = vec![
            (
                "client connect",
                ConsumerError::Client(ClientError::Connect {
                    addr: "127.0.0.1:9092".parse().unwrap(),
                    source: io::Error::new(io::ErrorKind::ConnectionRefused, "refused"),
                }),
                true,
            ),
            ("server", ConsumerError::Server(6), false),
        ];
        for (name, error, expected) in &poll_cases {
            check!(is_transient_poll_error(error) == *expected, "case {name}");
        }
    }

    /// Kafka's `AbstractFetch.createFetchRequest` and `FetchRequest.Builder`:
    /// the limits, the isolation level, the session fields, the partitions by
    /// topic, and the forgotten partitions by topic.
    #[test]
    fn build_fetch_request_carries_limits_session_partitions_and_forgotten_topics() {
        let partition = |fetch_offset| crate::fetch_session::SessionPartition {
            topic_id: id(7),
            fetch_offset,
            current_leader_epoch: 5,
            last_fetched_epoch: 4,
            partition_max_bytes: 128 * 1024,
        };
        let req = build_fetch_request(
            500,
            IsolationLevel::ReadCommitted,
            krabka_units::bytes(7),
            mebibytes(2),
            "az-1",
            SessionRequest {
                session_id: 77,
                session_epoch: 3,
                partitions: maplit::btreemap! {("topic-a".to_owned(), 2) => partition(42), ("topic-a".to_owned(), 3) => partition(43)},
                forgotten: vec![
                    (("topic-b".to_owned(), 0), id(8)),
                    (("topic-b".to_owned(), 1), id(8)),
                ],
            },
        );
        let fetch_partition = |partition, fetch_offset| FetchPartition {
            partition,
            current_leader_epoch: 5,
            fetch_offset,
            last_fetched_epoch: 4,
            log_start_offset: -1,
            partition_max_bytes: 128 * 1024,
            replica_directory_id: WireUuid::default(),
            high_watermark: i64::MAX,
            unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
        };
        assert2::assert!(
            req == FetchRequest {
                replica_id: -1,
                max_wait_ms: 500,
                min_bytes: 7,
                max_bytes: 2 * 1024 * 1024,
                isolation_level: 1, // read_committed wire value
                session_id: 77,
                session_epoch: 3,
                topics: vec![FetchTopic {
                    topic: "topic-a".into(),
                    topic_id: id(7),
                    partitions: vec![fetch_partition(2, 42), fetch_partition(3, 43)],
                    unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
                }],
                forgotten_topics_data: vec![ForgottenTopic {
                    topic: "topic-b".into(),
                    topic_id: id(8),
                    partitions: vec![0, 1],
                    unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
                }],
                rack_id: "az-1".into(),
                cluster_id: None,
                replica_state: ReplicaState {
                    replica_id: -1,
                    replica_epoch: -1,
                    unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
                },
                unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
            }
        );
    }

    /// The request carries the caller's timestamp verbatim, the consumer's
    /// isolation level and the leader epoch, and asks as a consumer, which
    /// is `replica_id = -1`.
    ///
    /// `-1` asks for the log end and `-2` for the log start. The
    /// `OFFSET_OUT_OF_RANGE` recovery path depends on the second one, because
    /// an errored Fetch partition reports no usable `log_start_offset`.
    #[test]
    fn offsets_request_carries_the_timestamp_isolation_and_leader_epoch() {
        for (timestamp, isolation, isolation_level, leader_epoch) in [
            (LATEST_TIMESTAMP, IsolationLevel::ReadCommitted, 1, 4),
            (LATEST_TIMESTAMP, IsolationLevel::ReadUncommitted, 0, -1),
            (EARLIEST_TIMESTAMP, IsolationLevel::ReadCommitted, 1, -1),
            (EARLIEST_TIMESTAMP, IsolationLevel::ReadUncommitted, 0, 9),
        ] {
            let by_topic =
                maplit::btreemap! {"topic-a".to_string() => vec![(3, LeaderEpoch(leader_epoch))]};
            let req = build_offsets_request(by_topic, timestamp, isolation);

            check!(
                req == ListOffsetsRequest {
                    replica_id: -1,
                    isolation_level,
                    topics: vec![ListOffsetsTopic {
                        name: "topic-a".into(),
                        partitions: vec![ListOffsetsPartition {
                            partition_index: 3,
                            current_leader_epoch: leader_epoch,
                            timestamp,
                            unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
                        }],
                        unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
                    }],
                    timeout_ms: 0,
                    unknown_tagged_fields: UnknownTaggedFields(Vec::new()),
                },
                "timestamp {timestamp}, isolation {isolation:?}"
            );
        }
    }

    /// A partition goes to its known leader with the metadata epoch. A
    /// partition without a known, dialable leader goes to the bootstrap
    /// connection with the epoch that its position has.
    #[test]
    fn list_offsets_partitions_group_by_leader_with_the_leader_epoch() {
        let position = |leader_id, leader_epoch| PartitionPosition {
            leader_id,
            leader_epoch: LeaderEpoch(leader_epoch),
            ..Default::default()
        };
        let positions = maplit::hashmap! {("topic-a".to_string(), 0) => position(1, 5), ("topic-a".to_string(), 1) => position(2, 6), ("topic-b".to_string(), 0) => position(1, 3), ("topic-b".to_string(), 1) => position(3, 8)};
        let keys = [
            ("topic-a".to_string(), 0),
            ("topic-a".to_string(), 1),
            ("topic-b".to_string(), 0),
            ("topic-b".to_string(), 1),
            ("topic-c".to_string(), 0),
        ];
        let grouped = group_list_offsets(&keys, &positions, |id| id != 3);
        assert2::assert!(
            grouped
                == maplit::btreemap! {
                    BOOTSTRAP_LEADER => maplit::btreemap! {
                        "topic-b".to_string() => vec![(1, LeaderEpoch(8))],
                        "topic-c".to_string() => vec![(0, LeaderEpoch(-1))],
                    },
                    1 => maplit::btreemap! {
                        "topic-a".to_string() => vec![(0, LeaderEpoch(5))],
                        "topic-b".to_string() => vec![(0, LeaderEpoch(3))],
                    },
                    2 => maplit::btreemap! {
                        "topic-a".to_string() => vec![(1, LeaderEpoch(6))],
                    },
                }
        );
    }

    /// `OffsetFetcherUtils.handleListOffsetResponse` sets a position only for
    /// `NONE` with a known offset, fails for `TOPIC_AUTHORIZATION_FAILED`, and
    /// retries every other code.
    #[test]
    fn list_offsets_rows_map_to_kafka_consumer_actions() {
        for (name, error_code, offset, expected) in [
            ("none", 0, 40, ListOffsetsRowAction::Position(40)),
            ("none at zero", 0, 0, ListOffsetsRowAction::Position(0)),
            ("none without an offset", 0, -1, ListOffsetsRowAction::Retry),
            ("not leader or follower", 6, -1, ListOffsetsRowAction::Retry),
            (
                "unknown topic or partition",
                3,
                -1,
                ListOffsetsRowAction::Retry,
            ),
            ("leader not available", 5, -1, ListOffsetsRowAction::Retry),
            ("replica not available", 9, -1, ListOffsetsRowAction::Retry),
            ("offset not available", 78, -1, ListOffsetsRowAction::Retry),
            ("fenced leader epoch", 74, -1, ListOffsetsRowAction::Retry),
            ("unknown leader epoch", 75, -1, ListOffsetsRowAction::Retry),
            ("kafka storage error", 56, -1, ListOffsetsRowAction::Retry),
            (
                "unsupported for message format",
                43,
                -1,
                ListOffsetsRowAction::Retry,
            ),
            ("unexpected code", 2, -1, ListOffsetsRowAction::Retry),
            (
                "topic authorization failed",
                29,
                -1,
                ListOffsetsRowAction::Unauthorized,
            ),
        ] {
            check!(
                classify_list_offsets_row(error_code, offset) == expected,
                "case {name}"
            );
        }
    }

    /// The answers become offsets for the requested partitions. A partition
    /// with an error row or without a row is retried. An answer with an
    /// unauthorized row gives no offsets.
    #[test]
    fn list_offsets_result_collects_offsets_retries_and_unauthorized_topics() {
        let row = |partition_index, error_code, offset| ListOffsetsPartitionResponse {
            partition_index,
            error_code,
            offset,
            ..Default::default()
        };
        let answer = |name: &str, rows| ListOffsetsResponse {
            topics: vec![ListOffsetsTopicResponse {
                name: name.into(),
                partitions: rows,
                ..Default::default()
            }],
            ..Default::default()
        };
        let requested = [("topic-a".to_string(), 0), ("topic-a".to_string(), 1)];
        let retry_with_first_offset = ListOffsetsResult {
            offsets: maplit::hashmap! {("topic-a".to_string(), 0) => 5},
            retry: true,
            unauthorized: BTreeSet::new(),
        };
        for (name, answers, expected) in [
            (
                "every partition answered",
                vec![answer("topic-a", vec![row(0, 0, 5), row(1, 0, 9)])],
                ListOffsetsResult {
                    offsets: maplit::hashmap! {("topic-a".to_string(), 0) => 5, ("topic-a".to_string(), 1) => 9},
                    retry: false,
                    unauthorized: BTreeSet::new(),
                },
            ),
            (
                "an error row is retried",
                vec![answer("topic-a", vec![row(0, 0, 5), row(1, 6, -1)])],
                retry_with_first_offset.clone(),
            ),
            (
                "a missing row is retried and an unrequested row is ignored",
                vec![answer("topic-a", vec![row(0, 0, 5), row(2, 0, 3)])],
                retry_with_first_offset.clone(),
            ),
            (
                "an unauthorized answer gives no offsets",
                vec![answer("topic-a", vec![row(0, 0, 5), row(1, 29, -1)])],
                ListOffsetsResult {
                    offsets: HashMap::new(),
                    retry: true,
                    unauthorized: maplit::btreeset! {"topic-a".to_string()},
                },
            ),
        ] {
            check!(
                ListOffsetsResult::collect(&requested, &answers) == expected,
                "case {name}"
            );
        }
    }

    /// Only an unauthorized topic fails the reset.
    #[test]
    fn list_offsets_result_authorization_names_the_unauthorized_topics() {
        let mut result = ListOffsetsResult::default();
        check!(result.authorization().map_err(|e| e.to_string()) == Ok(()));
        result.unauthorized.insert("topic-a".to_string());
        check!(
            result.authorization().map_err(|e| e.to_string())
                == Err(ConsumerError::TopicAuthorizationFailed(
                    maplit::btreeset! {"topic-a".to_string()}
                )
                .to_string())
        );
    }

    /// The recovery writes the broker's log start, and leaves a partition the
    /// broker did not answer for alone.
    ///
    /// Inventing a position for an unanswered partition is what the old code
    /// did: it wrote the -1 that an errored Fetch reports, and every following
    /// Fetch then asked for -1 and was out of range again.
    #[test]
    fn out_of_range_positions_uses_the_log_start_and_skips_the_unanswered() {
        let out_of_range = vec![
            (("topic-a".to_string(), 0), 0),
            (("topic-a".to_string(), 1), 3),
        ];
        let log_starts = maplit::hashmap! {("topic-a".to_string(), 0) => 5};
        assert2::assert!(
            out_of_range_positions(&out_of_range, &log_starts)
                == vec![(("topic-a".to_string(), 0), 5)]
        );
    }

    /// Under `auto.offset.reset=none` the first out-of-range partition becomes
    /// the error, with the log start as the offset the caller seeks to.
    ///
    /// An answer that omits the partition gives `UNKNOWN_OFFSET`, which says
    /// the broker did not report one. It is not a position to fetch from.
    #[test]
    fn out_of_range_truncation_names_the_first_partition_and_its_log_start() {
        for (answered, expected_safe) in [(true, 5), (false, UNKNOWN_OFFSET)] {
            let log_starts = if answered {
                maplit::hashmap! {("topic-a".to_string(), 0) => 5}
            } else {
                HashMap::new()
            };
            let out_of_range = vec![
                (("topic-a".to_string(), 0), 2),
                (("topic-a".to_string(), 1), 3),
            ];
            assert2::assert!(
                out_of_range_truncation(&out_of_range, &log_starts).map(|e| e.to_string())
                    == Some(
                        ConsumerError::LogTruncation {
                            topic: "topic-a".to_string(),
                            partition: 0,
                            fetch_offset: 2,
                            safe_offset: expected_safe,
                        }
                        .to_string()
                    )
            );
        }
    }

    /// Nothing out of range means nothing to report.
    #[test]
    fn out_of_range_truncation_is_none_for_an_empty_set() {
        assert2::assert!(out_of_range_truncation(&[], &HashMap::new()).is_none());
    }

    /// `keys_at` picks exactly the partitions parked on a sentinel.
    #[test]
    fn keys_at_selects_only_the_sentinel_partitions() {
        let mut offsets: HashMap<(String, i32), i64> = HashMap::new();
        offsets.insert(("topic-a".to_string(), 0), LATEST_SENTINEL);
        offsets.insert(("topic-a".to_string(), 1), 42);
        assert2::assert!(keys_at(&offsets, LATEST_SENTINEL) == vec![("topic-a".to_string(), 0)]);
    }

    #[test]
    fn advance_target_uses_last_offset_delta_not_record_count() {
        // A batch spanning offsets 10..=14 (last_offset_delta = 4) but carrying
        // zero surviving records must still advance the fetch offset to 15.
        let batch = RecordBatch {
            base_offset: 10,
            last_offset_delta: 4,
            records: vec![],
            ..Default::default()
        };
        let payload = RecordsPayload::V2(vec![batch]);
        let batches = payload.as_v2().unwrap();
        assert2::assert!(super::next_offset_after(batches) == Some(15));
    }

    #[test]
    fn advance_target_none_for_empty() {
        let payload = RecordsPayload::V2(vec![]);
        assert2::assert!(super::next_offset_after(payload.as_v2().unwrap()) == None);
    }
}

#[cfg(test)]
pub(crate) mod partition_error_tests {
    use std::{
        sync::{
            Arc,
            atomic::{AtomicU16, AtomicUsize, Ordering},
        },
        time::Duration,
    };

    use assert2::check;
    use krabka_client_core::{Client, MockBroker};
    use krabka_protocol::{
        Encode,
        owned::{
            api_versions_request,
            fetch_response::{
                EpochEndOffset, FetchResponse, FetchableTopicResponse, LeaderIdAndEpoch,
                PartitionData,
            },
            metadata_request,
            metadata_response::MetadataResponse,
        },
        primitives::uuid::Uuid as WireUuid,
    };
    use krabka_units::secs;

    use super::*;
    use crate::Assignor;

    const TOPIC_ID: WireUuid = WireUuid([7; 16]);

    fn encode(response: &impl Encode, version: i16) -> Vec<u8> {
        let mut buf = bytes::BytesMut::new();
        response.encode(&mut buf, version).unwrap();
        buf.to_vec()
    }

    /// A mock broker that answers `ApiVersions` and a non-flexible `Metadata`,
    /// and counts the `Metadata` requests.
    async fn metadata_counting_broker(metadata_requests: Arc<AtomicUsize>) -> MockBroker {
        MockBroker::start(move |api_key, version, _corr_id, _body| {
            if api_key == api_versions_request::API_KEY {
                let versions = mock_test_support::api_versions_response([
                    (api_versions_request::API_KEY, 3),
                    (metadata_request::API_KEY, 8),
                ]);
                return Some(encode(&versions, 0));
            }
            if api_key == metadata_request::API_KEY {
                metadata_requests.fetch_add(1, Ordering::SeqCst);
                return Some(encode(&MetadataResponse::default(), version));
            }
            None
        })
        .await
    }

    async fn consumer_on(broker: &MockBroker) -> Consumer {
        let client = Client::builder()
            .bootstrap(broker.addr.to_string())
            .build()
            .await
            .unwrap();
        consumer_with_client(client)
    }

    /// A consumer of `group-a` on `client` that owns `orders-0` at offset 5.
    pub(crate) fn consumer_with_client(client: Client) -> Consumer {
        Consumer::test_builder(client)
            .commit_identity(test_support::orders_partition_commit_identity(1))
            .subscription(crate::subscription::shared(
                vec!["orders".into()],
                None,
                true,
            ))
            .assigned(vec![("orders".into(), 0)])
            .next_offsets(maplit::hashmap! {("orders".into(), 0) => 5})
            .topic_ids(maplit::hashmap! {"orders".into() => TOPIC_ID})
            .build()
    }

    /// A Fetch v13 response row: the topic by id only, one partition with
    /// `error_code`, no records and no leader hint.
    fn fetch_response(error_code: i16) -> FetchResponse {
        FetchResponse {
            responses: vec![FetchableTopicResponse {
                topic: String::new(),
                topic_id: TOPIC_ID,
                partitions: vec![PartitionData {
                    partition_index: 0,
                    error_code,
                    high_watermark: -1,
                    last_stable_offset: -1,
                    log_start_offset: -1,
                    diverging_epoch: EpochEndOffset {
                        epoch: -1,
                        end_offset: -1,
                        ..Default::default()
                    },
                    current_leader: LeaderIdAndEpoch {
                        leader_id: -1,
                        leader_epoch: -1,
                        ..Default::default()
                    },
                    ..Default::default()
                }],
                ..Default::default()
            }],
            ..Default::default()
        }
    }

    /// How one poll handled a partition error row.
    #[derive(Debug, PartialEq)]
    struct PollOutcome {
        /// `Ok(record_count)`, `Err(Some(code))` for
        /// `ConsumerError::Server(code)`, or `Err(None)` for any other error.
        result: Result<usize, Option<i16>>,
        /// The `Metadata` requests that processing the response sent.
        metadata_requests: usize,
        /// Whether processing asked for a refresh before the next poll.
        metadata_update_requested: bool,
        next_offset: Option<i64>,
    }

    /// Kafka's consumer raises a fatal `OffsetFetch` error from `poll()`. A
    /// rejoin leaves the error for `poll`, which returns it once. The next poll
    /// runs as usual.
    #[tokio::test]
    async fn poll_returns_a_fatal_rejoin_error_once() {
        let broker = metadata_counting_broker(Arc::default()).await;
        let mut consumer = consumer_on(&broker).await;
        let topics = maplit::btreeset! {"orders".to_string()};
        coordinator::report_rejoin_error(
            &consumer.poll_error,
            ConsumerError::TopicAuthorizationFailed(topics.clone()),
        );

        let first = consumer
            .prepare_poll(tokio::time::Instant::now() + Duration::from_secs(5))
            .await
            .map_err(|error| match error {
                ConsumerError::TopicAuthorizationFailed(topics) => Some(topics),
                _ => None,
            });
        let second = consumer
            .prepare_poll(tokio::time::Instant::now() + Duration::from_secs(5))
            .await
            .map_err(|_| None::<std::collections::BTreeSet<String>>);

        broker.stop();
        assert2::assert!((first, second) == (Err(Some(topics)), Ok(true)));
    }

    /// A partition that the coordinator revokes while `poll` drains the fetch
    /// buffer is either still owned for the whole drain, or not drained at
    /// all. The drain never returns records of a partition that left the
    /// assignment before the drain completed.
    #[tokio::test]
    async fn drain_never_returns_records_of_a_partition_revoked_during_the_drain() {
        let broker = metadata_counting_broker(Arc::default()).await;
        let mut consumer = consumer_on(&broker).await;
        consumer
            .fetch_buffer
            .push(crate::fetch_buffer::BufferedPartition {
                key: ("orders".into(), 0),
                position: 5,
                records: std::collections::VecDeque::from([ConsumerRecord {
                    topic: "orders".into(),
                    partition: 0,
                    offset: 5,
                    leader_epoch: 0,
                    timestamp: 0,
                    timestamp_type: crate::TimestampType::CreateTime,
                    key: None,
                    value: None,
                    headers: Vec::new(),
                }]),
                next_offset: 6,
                last_epoch: None,
            });
        let assigned = Arc::clone(&consumer.assigned);
        let next_offsets = Arc::clone(&consumer.next_offsets);
        // Block the drain between the ownership check and the position update.
        let offsets_guard = next_offsets.lock().await;
        let drain = tokio::spawn(async move {
            let records = consumer.drain_fetch_buffer().await;
            (consumer, records)
        });
        tokio::time::sleep(Duration::from_millis(50)).await;
        // The coordinator revokes the partition, if it can take the lock.
        let revoked = match tokio::time::timeout(Duration::from_millis(50), assigned.lock()).await {
            Ok(mut owned) => {
                owned.clear();
                true
            }
            Err(_) => false,
        };
        drop(offsets_guard);
        let (consumer, records) = drain.await.expect("drain task");
        drop(consumer);
        broker.stop();
        assert2::assert!(!revoked || records.is_empty());
    }

    /// While a join runs, Kafka's eager `onJoinPrepare` has revoked every
    /// partition, so `poll` fetches nothing until the join completes or the
    /// poll timeout passes. A cooperative member keeps fetching its owned
    /// partitions.
    #[tokio::test(start_paused = true)]
    async fn poll_waits_for_a_pending_eager_join_but_not_for_a_cooperative_one() {
        let broker = metadata_counting_broker(Arc::default()).await;
        let mut actual = Vec::new();
        let mut wanted = Vec::new();
        for (name, assignor, owns_partitions, pending, join_completes_after, expected) in [
            ("no join", Assignor::Range, true, false, None, (true, 0)),
            (
                "eager join completes in time",
                Assignor::Range,
                true,
                true,
                Some(100),
                (true, 100),
            ),
            (
                "eager join does not complete in time",
                Assignor::Range,
                true,
                true,
                None,
                (false, 500),
            ),
            (
                "cooperative join with owned partitions",
                Assignor::CooperativeSticky,
                true,
                true,
                None,
                (true, 0),
            ),
            (
                "cooperative join without partitions",
                Assignor::CooperativeSticky,
                false,
                true,
                Some(200),
                (true, 200),
            ),
        ] {
            let mut consumer = consumer_on(&broker).await;
            consumer.rebalance_protocol =
                assignor::rebalance_protocol_of(&[assignor]).expect("assignor protocol");
            if !owns_partitions {
                consumer.assigned.lock().await.clear();
            }
            let pending_sender = tokio::sync::watch::Sender::new(pending);
            consumer.rebalance_pending = pending_sender.subscribe();
            let completion = join_completes_after.map(|millis| {
                let pending_sender = pending_sender.clone();
                tokio::spawn(async move {
                    tokio::time::sleep(Duration::from_millis(millis)).await;
                    pending_sender.send_replace(false);
                })
            });
            let start = tokio::time::Instant::now();
            let joined = consumer
                .wait_for_rebalance(start + Duration::from_millis(500))
                .await
                .expect("no listener");
            let elapsed_ms = u64::try_from(start.elapsed().as_millis()).expect("millis");
            if let Some(completion) = completion {
                completion.abort();
            }
            actual.push((name, (joined, elapsed_ms)));
            wanted.push((name, expected));
        }
        broker.stop();
        assert2::assert!(actual == wanted);
    }

    /// One `poll` of the record cap case: the number of records, the first and
    /// the last offset, and the fetch position after the `poll`.
    #[derive(Debug, PartialEq)]
    struct CappedPoll {
        records: usize,
        first_offset: Option<i64>,
        last_offset: Option<i64>,
        next_offset: Option<i64>,
    }

    /// Kafka's `FetchCollector.collectFetch` returns at most
    /// `max.poll.records` records, and keeps the rest of the completed fetch for
    /// the next `poll`. The position moves only past the returned records, and
    /// the next `poll` sends no Fetch while buffered records remain.
    #[tokio::test]
    async fn poll_returns_at_most_max_poll_records_and_keeps_the_rest_without_a_fetch() {
        let fetches = Arc::new(AtomicUsize::new(0));
        let fetches_in_mock = Arc::clone(&fetches);
        let broker = MockBroker::start(move |api_key, version, _corr_id, _body| {
            if api_key == api_versions_request::API_KEY {
                let versions = mock_test_support::api_versions_response_ranges([
                    (api_versions_request::API_KEY, 0, 3),
                    (metadata_request::API_KEY, 0, 8),
                    (fetch_request::API_KEY, 4, 11),
                ]);
                return Some(encode(&versions, 0));
            }
            if api_key == metadata_request::API_KEY {
                return Some(encode(&MetadataResponse::default(), version));
            }
            if api_key != fetch_request::API_KEY {
                return None;
            }
            fetches_in_mock.fetch_add(1, Ordering::SeqCst);
            let batch = krabka_protocol::records::RecordBatch {
                base_offset: 5,
                last_offset_delta: 1199,
                records: (0..1200)
                    .map(|offset_delta| krabka_protocol::records::Record {
                        offset_delta,
                        value: Some(bytes::Bytes::from_static(b"v")),
                        ..Default::default()
                    })
                    .collect(),
                ..Default::default()
            };
            let mut response = fetch_response(0);
            response.responses[0].topic = "orders".into();
            response.responses[0].partitions[0].high_watermark = 1205;
            response.responses[0].partitions[0].records = Some(RecordsPayload::V2(vec![batch]));
            Some(encode(&response, version))
        })
        .await;
        let mut consumer = consumer_on(&broker).await;

        let mut polls = Vec::new();
        for _ in 0..3 {
            let records = consumer.poll(secs(5)).await.expect("poll");
            polls.push(CappedPoll {
                records: records.len(),
                first_offset: records.first().map(|record| record.offset),
                last_offset: records.last().map(|record| record.offset),
                next_offset: consumer
                    .next_offsets
                    .lock()
                    .await
                    .get(&("orders".to_string(), 0))
                    .copied(),
            });
        }

        broker.stop();
        assert2::assert!(
            (polls, fetches.load(Ordering::SeqCst))
                == (
                    vec![
                        CappedPoll {
                            records: 500,
                            first_offset: Some(5),
                            last_offset: Some(504),
                            next_offset: Some(505),
                        },
                        CappedPoll {
                            records: 500,
                            first_offset: Some(505),
                            last_offset: Some(1004),
                            next_offset: Some(1005),
                        },
                        CappedPoll {
                            records: 200,
                            first_offset: Some(1005),
                            last_offset: Some(1204),
                            next_offset: Some(1205),
                        },
                    ],
                    1,
                )
        );
    }

    /// What changes between two fetch-position updates.
    #[derive(Clone, Copy, Debug)]
    enum BetweenPolls {
        Nothing,
        UpdateRequested,
        MaxAgeElapsed,
        LeaderUnknown,
    }

    /// Kafka's consumer refreshes metadata only when `Metadata.updateRequested`
    /// holds, not on every poll. A refresh on every poll pipelines a `Metadata`
    /// request behind a parked long-poll `Fetch` on the same connection, and
    /// the poll then stalls for up to `fetch.max.wait.ms` (krabka-io/krabka-broker#1394).
    #[tokio::test]
    async fn fetch_positions_refresh_metadata_only_when_it_is_due() {
        // (what changes between the polls, `Metadata` requests of the second)
        let cases = [
            (BetweenPolls::Nothing, 0),
            (BetweenPolls::UpdateRequested, 1),
            (BetweenPolls::MaxAgeElapsed, 1),
            (BetweenPolls::LeaderUnknown, 1),
        ];
        for (between, expected) in cases {
            let metadata_requests = Arc::new(AtomicUsize::new(0));
            let broker = metadata_counting_broker(Arc::clone(&metadata_requests)).await;
            let mut consumer = consumer_on(&broker).await;
            let key = ("orders".to_string(), 0);
            consumer
                .positions
                .lock()
                .await
                .entry(key.clone())
                .or_default()
                .leader_id = 1;
            let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
            check!(
                consumer
                    .update_fetch_positions(None, Some(deadline))
                    .await
                    .unwrap()
            );
            check!(
                metadata_requests.swap(0, Ordering::SeqCst) == 1,
                "{between:?}: first poll"
            );
            match between {
                BetweenPolls::Nothing => {}
                BetweenPolls::UpdateRequested => consumer.client.metadata_topics().request_update(),
                BetweenPolls::MaxAgeElapsed => consumer.metadata_max_age = Time::ZERO,
                BetweenPolls::LeaderUnknown => {
                    consumer
                        .positions
                        .lock()
                        .await
                        .entry(key)
                        .or_default()
                        .leader_id = UNKNOWN_LEADER_ID;
                }
            }
            check!(
                consumer
                    .update_fetch_positions(None, Some(deadline))
                    .await
                    .unwrap()
            );
            check!(
                metadata_requests.load(Ordering::SeqCst) == expected,
                "{between:?}"
            );
            broker.stop();
        }
    }

    /// Kafka's `FetchCollector.handleInitializeErrors` requests a metadata
    /// update for 3, 6 and 100 and does not raise an error. The poll returns
    /// no records, keeps the fetch position, and asks for a refresh, which
    /// the next poll makes before it fetches. It sends no `Metadata` request
    /// of its own. Any code that Kafka does not handle fails the poll.
    #[tokio::test]
    async fn fetch_partition_errors_refresh_metadata_or_fail_the_poll() {
        for (name, error_code, expected) in [
            (
                "unknown topic or partition",
                3,
                PollOutcome {
                    result: Ok(0),
                    metadata_requests: 0,
                    metadata_update_requested: true,
                    next_offset: Some(5),
                },
            ),
            (
                "not leader or follower",
                6,
                PollOutcome {
                    result: Ok(0),
                    metadata_requests: 0,
                    metadata_update_requested: true,
                    next_offset: Some(5),
                },
            ),
            (
                "unknown topic id",
                100,
                PollOutcome {
                    result: Ok(0),
                    metadata_requests: 0,
                    metadata_update_requested: true,
                    next_offset: Some(5),
                },
            ),
            (
                "topic authorization failed",
                29,
                PollOutcome {
                    result: Err(Some(29)),
                    metadata_requests: 0,
                    metadata_update_requested: false,
                    next_offset: Some(5),
                },
            ),
        ] {
            let metadata_requests = Arc::new(AtomicUsize::new(0));
            let broker = metadata_counting_broker(Arc::clone(&metadata_requests)).await;
            let mut consumer = consumer_on(&broker).await;
            let topic_ids = consumer.topic_ids.lock().await.clone();

            let result = consumer
                .process_fetch_responses(vec![fetch_response(error_code)], &topic_ids)
                .await
                .map(|()| consumer.fetch_buffer.len())
                .map_err(|error| match error {
                    ConsumerError::Server(code) => Some(code),
                    _ => None,
                });
            let outcome = PollOutcome {
                result,
                metadata_requests: metadata_requests.load(Ordering::SeqCst),
                metadata_update_requested: consumer.client.metadata_topics().update_requested(),
                next_offset: consumer
                    .next_offsets
                    .lock()
                    .await
                    .get(&("orders".to_string(), 0))
                    .copied(),
            };

            broker.stop();
            check!(outcome == expected, "case {name}");
        }
    }

    /// How the test resets the fetch position of `orders-0` after the start of
    /// `poll`.
    #[derive(Clone, Copy, Debug)]
    enum PollReset {
        /// A Fetch row without an error and without records.
        None,
        /// A Fetch row with `OFFSET_OUT_OF_RANGE` under `auto.offset.reset=latest`.
        LatestOutOfRange,
        /// A Fetch row with `OFFSET_OUT_OF_RANGE` under
        /// `auto.offset.reset=earliest`. The leader answers `ListOffsets` with
        /// log start 2.
        EarliestOutOfRange,
        /// A Fetch row with a diverging epoch that ends at offset 3.
        FetchTruncation,
        /// The validation pass finds a truncation to offset 3.
        ValidationTruncation,
    }

    /// Kafka's `onJoinPrepare` commits `SubscriptionState.allConsumed`, which
    /// reads the current fetch positions. A reset in `poll` therefore changes
    /// what the commit before a `JoinGroup` sends. A position that waits for
    /// `ListOffsets` is not valid, and `allConsumed` skips it.
    #[tokio::test]
    async fn a_reset_in_poll_moves_the_position_of_the_commit_before_join() {
        let orders_0 = ("orders".to_string(), 0);
        let cases = [
            (
                "no reset: the position at the start of poll",
                PollReset::None,
                maplit::hashmap! {orders_0.clone() => (5, -1)},
            ),
            (
                "out of range under latest: no position until ListOffsets",
                PollReset::LatestOutOfRange,
                HashMap::new(),
            ),
            (
                "out of range under earliest: the log start",
                PollReset::EarliestOutOfRange,
                maplit::hashmap! {orders_0.clone() => (2, -1)},
            ),
            (
                "truncation in a Fetch response: the end offset of the diverging epoch",
                PollReset::FetchTruncation,
                maplit::hashmap! {orders_0.clone() => (3, -1)},
            ),
            (
                "truncation from validation: the safe offset",
                PollReset::ValidationTruncation,
                maplit::hashmap! {orders_0.clone() => (3, -1)},
            ),
        ];
        let mut actual = Vec::new();
        let mut wanted = Vec::new();
        for (name, reset, expected) in cases {
            let leader_port = Arc::new(AtomicU16::new(0));
            let broker = list_offsets_broker(
                "leader",
                Arc::clone(&leader_port),
                (0, 2),
                list_offsets_request::MAX_VERSION,
                Arc::default(),
                Arc::default(),
            )
            .await;
            leader_port.store(broker.addr.port(), Ordering::SeqCst);
            let mut consumer = consumer_on(&broker).await;
            let auto_commit = crate::commit::AutoCommit::new(std::time::Duration::from_secs(5));
            consumer.auto_commit = Some(auto_commit.clone());
            // The start of `poll` records the positions. The interval has not
            // passed, so it sends no commit.
            consumer.maybe_auto_commit_async().await;
            let topic_ids = consumer.topic_ids.lock().await.clone();

            let result = match reset {
                PollReset::None => {
                    consumer
                        .process_fetch_responses(vec![fetch_response(0)], &topic_ids)
                        .await
                }
                PollReset::LatestOutOfRange => {
                    consumer.auto_offset_reset = AutoOffsetReset::Latest;
                    consumer
                        .process_fetch_responses(vec![fetch_response(1)], &topic_ids)
                        .await
                }
                PollReset::EarliestOutOfRange => {
                    consumer.auto_offset_reset = AutoOffsetReset::Earliest;
                    consumer
                        .refresh_leader_epochs()
                        .await
                        .expect("metadata for the leader route");
                    consumer
                        .process_fetch_responses(vec![fetch_response(1)], &topic_ids)
                        .await
                }
                PollReset::FetchTruncation => {
                    let mut response = fetch_response(0);
                    response.responses[0].partitions[0].diverging_epoch = EpochEndOffset {
                        epoch: 2,
                        end_offset: 3,
                        ..Default::default()
                    };
                    consumer
                        .process_fetch_responses(vec![response], &topic_ids)
                        .await
                }
                PollReset::ValidationTruncation => {
                    consumer
                        .apply_truncation(&maplit::hashmap! {orders_0.clone() => 3})
                        .await
                }
            };
            result.expect("the reset succeeds");
            let offsets = auto_commit
                .polled_offsets(&maplit::hashmap! {orders_0.clone() => 1})
                .await;

            broker.stop();
            actual.push((name, offsets));
            wanted.push((name, expected));
        }
        assert2::assert!(actual == wanted);
    }

    /// The broker id that the metadata names as the leader of `orders-0`.
    const LEADER_ID: i32 = 1;
    /// The leader epoch that the metadata reports for `orders-0`.
    const LEADER_EPOCH: i32 = 7;

    /// The `ListOffsets` requests that the mock brokers decoded, with the
    /// name of the broker that received each one and the request version.
    type SentListOffsets = Arc<std::sync::Mutex<Vec<(&'static str, i16, ListOffsetsRequest)>>>;

    /// The response bytes for a `ListOffsets` request at `version` with one
    /// `orders-0` row.
    fn list_offsets_answer_bytes(version: i16, error_code: i16, offset: i64) -> Vec<u8> {
        use krabka_protocol::owned::{
            list_offsets_request::FLEXIBLE_MIN,
            list_offsets_response::{ListOffsetsPartitionResponse, ListOffsetsTopicResponse},
        };
        let answer = ListOffsetsResponse {
            topics: vec![ListOffsetsTopicResponse {
                name: "orders".into(),
                partitions: vec![ListOffsetsPartitionResponse {
                    partition_index: 0,
                    error_code,
                    offset,
                    ..Default::default()
                }],
                ..Default::default()
            }],
            ..Default::default()
        };
        let mut bytes = Vec::new();
        if version >= FLEXIBLE_MIN {
            // The flexible response header has an empty tagged-field section.
            bytes.push(0);
        }
        bytes.extend(encode(&answer, version));
        bytes
    }

    /// A mock broker that answers `ApiVersions`, `Metadata` and `ListOffsets`.
    ///
    /// It records each decoded `ListOffsets` request under `name` and answers
    /// it with one `orders-0` row. The `Metadata` answer names broker
    /// `LEADER_ID` at `leader_port` as the leader of `orders-0`, with epoch
    /// `LEADER_EPOCH`.
    async fn list_offsets_broker(
        name: &'static str,
        leader_port: Arc<AtomicU16>,
        row: (i16, i64),
        list_offsets_max_version: i16,
        sent: SentListOffsets,
        metadata_requests: Arc<AtomicUsize>,
    ) -> MockBroker {
        use krabka_protocol::owned::list_offsets_request;
        MockBroker::start(move |api_key, version, _corr_id, mut body| {
            if api_key == api_versions_request::API_KEY {
                let versions = mock_test_support::api_versions_response([
                    (api_versions_request::API_KEY, 3),
                    (metadata_request::API_KEY, 8),
                    (list_offsets_request::API_KEY, list_offsets_max_version),
                ]);
                return Some(encode(&versions, 0));
            }
            if api_key == metadata_request::API_KEY {
                metadata_requests.fetch_add(1, Ordering::SeqCst);
                let metadata = test_support::OrdersMetadata::builder()
                    .topic_id(TOPIC_ID.0)
                    .leader_id(LEADER_ID)
                    .leader_epoch(LEADER_EPOCH)
                    .brokers(vec![
                        krabka_protocol::owned::metadata_response::MetadataResponseBroker {
                            node_id: LEADER_ID,
                            host: "127.0.0.1".into(),
                            port: i32::from(leader_port.load(Ordering::SeqCst)),
                            ..Default::default()
                        },
                    ])
                    .build();
                return Some(encode(&metadata, version));
            }
            if api_key == list_offsets_request::API_KEY {
                let request = test_support::decode_list_offsets_request(&mut body, version);
                sent.lock()
                    .expect("sent lock")
                    .push((name, version, request));
                return Some(list_offsets_answer_bytes(version, row.0, row.1));
            }
            None
        })
        .await
    }

    /// The `ListOffsets` request that the consumer sends for `orders-0`.
    fn expected_list_offsets(isolation_level: i8, timestamp: i64) -> ListOffsetsRequest {
        ListOffsetsRequest {
            replica_id: -1,
            isolation_level,
            topics: vec![ListOffsetsTopic {
                name: "orders".into(),
                partitions: vec![ListOffsetsPartition {
                    partition_index: 0,
                    current_leader_epoch: LEADER_EPOCH,
                    timestamp,
                    ..Default::default()
                }],
                ..Default::default()
            }],
            ..Default::default()
        }
    }

    /// Why a poll step failed, in a form that tests can compare.
    #[derive(Debug, PartialEq)]
    enum PollFailure {
        /// `ConsumerError::TopicAuthorizationFailed` with its topics.
        TopicAuthorizationFailed(std::collections::BTreeSet<String>),
        /// `ClientError::IncompatibleVersion` as
        /// `(api_key, broker_min, broker_max, client_min, client_max)`.
        IncompatibleVersion(i16, i16, i16, i16, i16),
        /// Any other error.
        Other,
    }

    impl From<ConsumerError> for PollFailure {
        fn from(error: ConsumerError) -> Self {
            match error {
                ConsumerError::TopicAuthorizationFailed(topics) => {
                    Self::TopicAuthorizationFailed(topics)
                }
                ConsumerError::Client(krabka_client_core::ClientError::IncompatibleVersion {
                    api_key,
                    broker_min,
                    broker_max,
                    client_min,
                    client_max,
                }) => Self::IncompatibleVersion(
                    api_key, broker_min, broker_max, client_min, client_max,
                ),
                _ => Self::Other,
            }
        }
    }

    /// How the consumer handled one `ListOffsets` exchange.
    #[derive(Debug, PartialEq)]
    struct ListOffsetsOutcome {
        /// Each decoded request, with the name of the broker that received it
        /// and the version.
        requests: Vec<(&'static str, i16, ListOffsetsRequest)>,
        result: Result<(), PollFailure>,
        /// The `Metadata` requests after the setup.
        metadata_requests: usize,
        /// Whether the exchange asked for a refresh before the next poll.
        metadata_update_requested: bool,
        next_offset: Option<i64>,
    }

    /// How the test reaches the `ListOffsets` path.
    #[derive(Clone, Copy, Debug)]
    enum Reset {
        /// `auto.offset.reset=latest`: a sentinel that `prepare_poll` resolves.
        Latest,
        /// `auto.offset.reset=by_duration:PT1H`: a sentinel that
        /// `prepare_poll` resolves with the timestamp of one hour ago.
        ByOneHour,
        /// `auto.offset.reset=earliest`: a Fetch row with
        /// `OFFSET_OUT_OF_RANGE`.
        EarliestOutOfRange,
        /// `seek_to_beginning` of every partition, then `position`.
        SeekToBeginning,
        /// `seek_to_end` of every partition, then `position`.
        SeekToEnd,
    }

    /// One `ListOffsets` scenario on two mock brokers.
    #[derive(Clone, Copy, Debug)]
    struct Exchange {
        isolation: IsolationLevel,
        reset: Reset,
        /// The `(error_code, offset)` row that the leader answers.
        row: (i16, i64),
        /// The highest `ListOffsets` version that both brokers advertise.
        list_offsets_max_version: i16,
        /// `true` when a metadata refresh fills the positions map before the
        /// exchange, as after an earlier poll. `false` is a new assignment
        /// without a committed offset, whose position has no leader yet.
        primed: bool,
    }

    /// Run `exchange` with a consumer that bootstraps from a broker that is
    /// not the leader of `orders-0`.
    ///
    /// The bootstrap broker answers each `ListOffsets` with
    /// `NOT_LEADER_OR_FOLLOWER`.
    async fn run_list_offsets_exchange(exchange: Exchange) -> ListOffsetsOutcome {
        let sent: SentListOffsets = Arc::default();
        let metadata_requests = Arc::new(AtomicUsize::new(0));
        let leader_port = Arc::new(AtomicU16::new(0));
        let leader = list_offsets_broker(
            "leader",
            Arc::clone(&leader_port),
            exchange.row,
            exchange.list_offsets_max_version,
            Arc::clone(&sent),
            Arc::clone(&metadata_requests),
        )
        .await;
        leader_port.store(leader.addr.port(), Ordering::SeqCst);
        let bootstrap = list_offsets_broker(
            "bootstrap",
            Arc::clone(&leader_port),
            (6, -1),
            exchange.list_offsets_max_version,
            Arc::clone(&sent),
            Arc::clone(&metadata_requests),
        )
        .await;
        let mut consumer = consumer_on(&bootstrap).await;
        consumer.isolation_level = exchange.isolation;
        if exchange.primed {
            consumer
                .refresh_leader_epochs()
                .await
                .expect("setup metadata");
            metadata_requests.store(0, Ordering::SeqCst);
        }

        let result = match exchange.reset {
            Reset::Latest => {
                consumer.auto_offset_reset = AutoOffsetReset::Latest;
                consumer
                    .next_offsets
                    .lock()
                    .await
                    .insert(("orders".into(), 0), LATEST_SENTINEL);
                consumer
                    .prepare_poll(tokio::time::Instant::now() + Duration::from_secs(5))
                    .await
                    .map(|_| ())
            }
            Reset::ByOneHour => {
                consumer.auto_offset_reset =
                    AutoOffsetReset::ByDuration(std::time::Duration::from_hours(1));
                consumer
                    .next_offsets
                    .lock()
                    .await
                    .insert(("orders".into(), 0), LATEST_SENTINEL);
                consumer
                    .prepare_poll(tokio::time::Instant::now() + Duration::from_secs(5))
                    .await
                    .map(|_| ())
            }
            Reset::EarliestOutOfRange => {
                consumer.auto_offset_reset = AutoOffsetReset::Earliest;
                let topic_ids = consumer.topic_ids.lock().await.clone();
                consumer
                    .process_fetch_responses(vec![fetch_response(1)], &topic_ids)
                    .await
            }
            Reset::SeekToBeginning | Reset::SeekToEnd => {
                // Kafka's `seekToBeginning` and `seekToEnd` ignore
                // `auto.offset.reset`.
                consumer.auto_offset_reset =
                    AutoOffsetReset::ByDuration(std::time::Duration::from_hours(1));
                consumer.default_api_timeout = krabka_units::millis(300);
                let reset = if matches!(exchange.reset, Reset::SeekToBeginning) {
                    consumer.seek_to_beginning(&[]).await
                } else {
                    consumer.seek_to_end(&[]).await
                };
                match reset {
                    Ok(()) => consumer.position("orders", 0).await.map(|_| ()),
                    Err(error) => Err(error),
                }
            }
        };
        let requests = sent.lock().expect("sent lock").clone();
        let outcome = ListOffsetsOutcome {
            requests,
            result: result.map_err(PollFailure::from),
            metadata_requests: metadata_requests.load(Ordering::SeqCst),
            metadata_update_requested: consumer.client.metadata_topics().update_requested(),
            next_offset: consumer
                .next_offsets
                .lock()
                .await
                .get(&("orders".to_string(), 0))
                .copied(),
        };

        bootstrap.stop();
        leader.stop();
        outcome
    }

    /// Kafka's consumer sends `ListOffsets` to the partition leader with its
    /// own isolation level and the leader epoch from metadata
    /// (`OffsetFetcher.groupListOffsetRequests` and
    /// `ListOffsetsRequest.Builder.forConsumer`). A row with an error never
    /// sets a position. `TOPIC_AUTHORIZATION_FAILED` fails the poll, and any
    /// other code retries after a metadata refresh
    /// (`OffsetFetcherUtils.handleListOffsetResponse`).
    #[tokio::test]
    async fn list_offsets_goes_to_the_leader_with_the_isolation_level_and_honours_row_errors() {
        let max = list_offsets_request::MAX_VERSION;
        let latest_request =
            |isolation| vec![("leader", max, expected_list_offsets(isolation, -1))];
        let latest = |isolation, row| Exchange {
            isolation,
            reset: Reset::Latest,
            row,
            list_offsets_max_version: max,
            primed: true,
        };
        let earliest = |isolation, row| Exchange {
            reset: Reset::EarliestOutOfRange,
            ..latest(isolation, row)
        };
        for (name, exchange, expected) in [
            (
                "read committed latest reads the last stable offset",
                latest(IsolationLevel::ReadCommitted, (0, 40)),
                ListOffsetsOutcome {
                    requests: latest_request(1),
                    result: Ok(()),
                    metadata_requests: 1,
                    metadata_update_requested: false,
                    next_offset: Some(40),
                },
            ),
            (
                "read uncommitted latest reads the high watermark",
                latest(IsolationLevel::ReadUncommitted, (0, 50)),
                ListOffsetsOutcome {
                    requests: latest_request(0),
                    result: Ok(()),
                    metadata_requests: 1,
                    metadata_update_requested: false,
                    next_offset: Some(50),
                },
            ),
            (
                "not leader or follower keeps the sentinel and refreshes metadata",
                latest(IsolationLevel::ReadCommitted, (6, -1)),
                ListOffsetsOutcome {
                    requests: latest_request(1),
                    result: Ok(()),
                    metadata_requests: 1,
                    metadata_update_requested: true,
                    next_offset: Some(LATEST_SENTINEL),
                },
            ),
            (
                "topic authorization failed fails the poll",
                latest(IsolationLevel::ReadCommitted, (29, -1)),
                ListOffsetsOutcome {
                    requests: latest_request(1),
                    result: Err(PollFailure::TopicAuthorizationFailed(
                        maplit::btreeset! {"orders".to_string()},
                    )),
                    metadata_requests: 1,
                    metadata_update_requested: false,
                    next_offset: Some(LATEST_SENTINEL),
                },
            ),
            (
                "seek to beginning reads the log start",
                Exchange {
                    reset: Reset::SeekToBeginning,
                    ..latest(IsolationLevel::ReadUncommitted, (0, 7))
                },
                ListOffsetsOutcome {
                    requests: vec![("leader", max, expected_list_offsets(0, -2))],
                    result: Ok(()),
                    metadata_requests: 0,
                    metadata_update_requested: false,
                    next_offset: Some(7),
                },
            ),
            (
                "seek to end reads the last stable offset",
                Exchange {
                    reset: Reset::SeekToEnd,
                    ..latest(IsolationLevel::ReadCommitted, (0, 40))
                },
                ListOffsetsOutcome {
                    requests: latest_request(1),
                    result: Ok(()),
                    metadata_requests: 0,
                    metadata_update_requested: false,
                    next_offset: Some(40),
                },
            ),
            (
                "earliest after out of range reads the log start",
                earliest(IsolationLevel::ReadUncommitted, (0, 7)),
                ListOffsetsOutcome {
                    requests: vec![("leader", max, expected_list_offsets(0, -2))],
                    result: Ok(()),
                    metadata_requests: 0,
                    metadata_update_requested: false,
                    next_offset: Some(7),
                },
            ),
            (
                "earliest with a retriable row keeps the position and refreshes metadata",
                earliest(IsolationLevel::ReadCommitted, (74, -1)),
                ListOffsetsOutcome {
                    requests: vec![("leader", max, expected_list_offsets(1, -2))],
                    result: Ok(()),
                    metadata_requests: 0,
                    metadata_update_requested: true,
                    next_offset: Some(5),
                },
            ),
        ] {
            let outcome = run_list_offsets_exchange(exchange).await;
            check!(outcome == expected, "case {name}");
        }
    }

    /// Kafka's `AutoOffsetResetStrategy.timestamp` for `by_duration` is now
    /// minus the duration, and the reset sends `ListOffsets` with it (KIP-1106).
    #[tokio::test]
    async fn by_duration_resets_with_the_timestamp_of_now_minus_the_duration() {
        let before = unix_now_ms() - 3_600_000;
        let mut outcome = run_list_offsets_exchange(Exchange {
            isolation: IsolationLevel::ReadUncommitted,
            reset: Reset::ByOneHour,
            row: (0, 40),
            list_offsets_max_version: list_offsets_request::MAX_VERSION,
            primed: true,
        })
        .await;
        let after = unix_now_ms() - 3_600_000;
        // The wall clock moves during the exchange: check the range, then
        // compare the whole outcome with the timestamp of the start.
        let mut in_range = Vec::new();
        for (_, _, request) in &mut outcome.requests {
            for topic in &mut request.topics {
                for partition in &mut topic.partitions {
                    in_range.push((before..=after).contains(&partition.timestamp));
                    partition.timestamp = before;
                }
            }
        }

        assert2::assert!(
            (in_range, outcome)
                == (
                    vec![true],
                    ListOffsetsOutcome {
                        requests: vec![(
                            "leader",
                            list_offsets_request::MAX_VERSION,
                            expected_list_offsets(0, before),
                        )],
                        result: Ok(()),
                        metadata_requests: 1,
                        metadata_update_requested: false,
                        next_offset: Some(40),
                    }
                )
        );
    }

    #[test]
    fn reset_timestamp_follows_the_reset_policy() {
        let now = 1_700_000_000_000;
        let actual = [
            AutoOffsetReset::Latest,
            AutoOffsetReset::None,
            AutoOffsetReset::ByDuration(std::time::Duration::from_hours(1)),
            AutoOffsetReset::ByDuration(std::time::Duration::from_millis(1500)),
        ]
        .map(|policy| reset_timestamp(policy, now));
        assert2::assert!(actual == [-1, -1, now - 3_600_000, now - 1500]);
    }

    /// A new assignment without a committed offset has no leader in the
    /// positions map yet. Kafka's `OffsetFetcher.groupListOffsetRequests`
    /// routes with `metadata.currentLeader(tp)`, so the first `ListOffsets`
    /// of the first poll goes to the leader and not to the bootstrap broker.
    #[tokio::test]
    async fn first_poll_sends_list_offsets_to_the_leader_of_a_new_partition() {
        let outcome = run_list_offsets_exchange(Exchange {
            isolation: IsolationLevel::ReadCommitted,
            reset: Reset::Latest,
            row: (0, 40),
            list_offsets_max_version: list_offsets_request::MAX_VERSION,
            primed: false,
        })
        .await;

        assert2::assert!(
            outcome
                == ListOffsetsOutcome {
                    requests: vec![(
                        "leader",
                        list_offsets_request::MAX_VERSION,
                        expected_list_offsets(1, -1),
                    )],
                    result: Ok(()),
                    metadata_requests: 1,
                    metadata_update_requested: false,
                    next_offset: Some(40),
                }
        );
    }

    /// `isolation_level` exists from `ListOffsets` version 2. Kafka's
    /// `ListOffsetsRequest.Builder.forConsumer` sets the oldest allowed
    /// version to 2 for `READ_COMMITTED`, and to 1 (the oldest version) for
    /// `READ_UNCOMMITTED`. A `read_committed` consumer on a broker that
    /// supports only version 1 fails the poll with an unsupported version. It
    /// does not reset to the high watermark.
    #[tokio::test]
    async fn read_committed_list_offsets_needs_version_2() {
        let at = |isolation, list_offsets_max_version| Exchange {
            isolation,
            reset: Reset::Latest,
            row: (0, 40),
            list_offsets_max_version,
            primed: true,
        };
        // Below version 4 the request has no `current_leader_epoch`, and the
        // decoder gives the default.
        let without_epoch = |isolation_level| {
            let mut request = expected_list_offsets(isolation_level, -1);
            request.topics[0].partitions[0].current_leader_epoch =
                ListOffsetsPartition::default().current_leader_epoch;
            request
        };
        for (name, exchange, expected) in [
            (
                "read committed on a version 1 broker fails the poll",
                at(IsolationLevel::ReadCommitted, 1),
                ListOffsetsOutcome {
                    requests: vec![],
                    result: Err(PollFailure::IncompatibleVersion(
                        list_offsets_request::API_KEY,
                        0,
                        1,
                        2,
                        list_offsets_request::MAX_VERSION,
                    )),
                    metadata_requests: 1,
                    metadata_update_requested: false,
                    next_offset: Some(LATEST_SENTINEL),
                },
            ),
            (
                "read committed on a version 2 broker sends version 2",
                at(IsolationLevel::ReadCommitted, 2),
                ListOffsetsOutcome {
                    requests: vec![("leader", 2, without_epoch(1))],
                    result: Ok(()),
                    metadata_requests: 1,
                    metadata_update_requested: false,
                    next_offset: Some(40),
                },
            ),
            (
                "read uncommitted on a version 1 broker sends version 1",
                at(IsolationLevel::ReadUncommitted, 1),
                ListOffsetsOutcome {
                    requests: vec![("leader", 1, without_epoch(0))],
                    result: Ok(()),
                    metadata_requests: 1,
                    metadata_update_requested: false,
                    next_offset: Some(40),
                },
            ),
        ] {
            let outcome = run_list_offsets_exchange(exchange).await;
            check!(outcome == expected, "case {name}");
        }
    }

    /// Kafka's consumer gives each record the timestamp and the timestamp type
    /// of its record batch. A `LogAppendTime` batch gives every record the
    /// batch `max_timestamp`.
    #[tokio::test]
    async fn fetched_records_take_the_batch_timestamp_type() {
        let broker = metadata_counting_broker(Arc::default()).await;
        let consumer = consumer_on(&broker).await;
        let key: (String, i32) = ("orders".into(), 0);

        for case in &timestamp_cases::CASES {
            let part = PartitionData {
                partition_index: 0,
                records: Some(timestamp_cases::batch(case).into()),
                ..Default::default()
            };
            let offsets = maplit::hashmap! {key.clone() => timestamp_cases::BASE_OFFSET};

            let records: Vec<ConsumerRecord> = consumer
                .process_partition_records(&offsets, &key, "orders", &part)
                .map(|partition| partition.records.into_iter().collect())
                .unwrap_or_default();

            let expected: Vec<ConsumerRecord> = case
                .expected
                .iter()
                .zip(timestamp_cases::BASE_OFFSET..)
                .zip(timestamp_cases::VALUES)
                .map(
                    |(((timestamp, timestamp_type), offset), value)| ConsumerRecord {
                        topic: "orders".into(),
                        partition: 0,
                        offset,
                        leader_epoch: timestamp_cases::LEADER_EPOCH,
                        timestamp: *timestamp,
                        timestamp_type: *timestamp_type,
                        key: None,
                        value: Some(bytes::Bytes::from_static(value)),
                        headers: Vec::new(),
                    },
                )
                .collect();
            check!(records == expected, "case {}", case.name);
        }
    }
}

/// Record batch timestamp cases that the classic and the share consumer tests
/// share.
#[cfg(test)]
pub(crate) mod timestamp_cases {
    use bytes::Bytes;
    use consumer::TimestampType;
    use krabka_protocol::records::{Attributes, Record, RecordBatch};

    use crate::consumer;

    pub(crate) const BASE_OFFSET: i64 = 5;
    pub(crate) const LEADER_EPOCH: i32 = 3;
    pub(crate) const VALUES: [&[u8]; 2] = [b"v0", b"v1"];

    /// One batch with two records and the timestamps Kafka gives them.
    pub(crate) struct TimestampCase {
        pub(crate) name: &'static str,
        pub(crate) attributes: Attributes,
        pub(crate) base_timestamp: i64,
        pub(crate) max_timestamp: i64,
        pub(crate) timestamp_deltas: [i64; 2],
        pub(crate) expected: [(i64, TimestampType); 2],
    }

    pub(crate) const CASES: [TimestampCase; 3] = [
        TimestampCase {
            name: "create time adds the delta to the base timestamp",
            attributes: Attributes(0),
            base_timestamp: 1000,
            max_timestamp: 1005,
            timestamp_deltas: [0, 5],
            expected: [
                (1000, TimestampType::CreateTime),
                (1005, TimestampType::CreateTime),
            ],
        },
        TimestampCase {
            name: "log append time uses the max timestamp",
            attributes: Attributes(Attributes::TIMESTAMP_TYPE_BIT),
            base_timestamp: 1000,
            max_timestamp: 9000,
            timestamp_deltas: [0, 5],
            expected: [
                (9000, TimestampType::LogAppendTime),
                (9000, TimestampType::LogAppendTime),
            ],
        },
        TimestampCase {
            name: "log append time in a transactional batch uses the max timestamp",
            attributes: Attributes(Attributes::TIMESTAMP_TYPE_BIT | Attributes::TRANSACTIONAL_BIT),
            base_timestamp: 1000,
            max_timestamp: 9000,
            timestamp_deltas: [0, 5],
            expected: [
                (9000, TimestampType::LogAppendTime),
                (9000, TimestampType::LogAppendTime),
            ],
        },
    ];

    pub(crate) fn batch(case: &TimestampCase) -> RecordBatch {
        RecordBatch {
            base_offset: BASE_OFFSET,
            partition_leader_epoch: LEADER_EPOCH,
            attributes: case.attributes,
            last_offset_delta: 1,
            base_timestamp: case.base_timestamp,
            max_timestamp: case.max_timestamp,
            records: case
                .timestamp_deltas
                .iter()
                .zip(0..)
                .zip(VALUES)
                .map(|((timestamp_delta, offset_delta), value)| Record {
                    attributes: 0,
                    timestamp_delta: *timestamp_delta,
                    offset_delta,
                    key: None,
                    value: Some(Bytes::from_static(value)),
                    headers: Vec::new(),
                })
                .collect(),
            ..Default::default()
        }
    }
}

#[cfg(test)]
mod fetch_path_tests {
    use std::{
        sync::{
            Arc,
            atomic::{AtomicU16, Ordering},
        },
        time::Duration,
    };

    use bytes::Buf as _;
    use krabka_client_core::{Client, MockBroker};
    use krabka_protocol::{
        Decode as _, Encode,
        owned::{
            api_versions_request, fetch_request,
            fetch_response::{FetchableTopicResponse, PartitionData},
            list_offsets_request,
            list_offsets_response::ListOffsetsResponse,
            metadata_request,
            metadata_response::{
                MetadataResponse, MetadataResponseBroker, MetadataResponsePartition,
                MetadataResponseTopic,
            },
        },
        primitives::uuid::Uuid as WireUuid,
    };
    use krabka_units::{millis, secs};
    use test_support::RecordingListener;

    use super::*;

    const TOPIC_ID: WireUuid = WireUuid([7; 16]);

    /// What a mock broker does with a Fetch request.
    #[derive(Clone, Copy, Debug)]
    enum FetchAnswer {
        /// No response.
        Silent,
        /// A response with this top-level error code, session id and no
        /// partition data.
        Respond { error_code: i16, session_id: i32 },
        /// A response with one `orders-0` row without records.
        Partition {
            error_code: i16,
            preferred_read_replica: i32,
        },
    }

    /// The Fetch requests that the mock brokers received: `(broker id,
    /// request)`.
    type SentFetches = Arc<std::sync::Mutex<Vec<(i32, FetchRequest)>>>;

    fn encode(response: &impl Encode, version: i16) -> Vec<u8> {
        let mut buf = bytes::BytesMut::new();
        response.encode(&mut buf, version).expect("encode");
        buf.to_vec()
    }

    /// Start one mock broker per entry of `answers`, with node ids from 1.
    /// Metadata names broker `i + 1` as the leader of `orders-i`. Each broker
    /// answers Fetch v4 to v11 with its entry of `answers`.
    async fn start_brokers(answers: &[Vec<FetchAnswer>], sent: &SentFetches) -> Vec<MockBroker> {
        let ports: Arc<Vec<AtomicU16>> =
            Arc::new(answers.iter().map(|_| AtomicU16::new(0)).collect());
        let mut brokers = Vec::new();
        for (index, answers) in answers.iter().enumerate() {
            let node_id = i32::try_from(index).expect("index") + 1;
            let ports_in_mock = Arc::clone(&ports);
            let sent = Arc::clone(sent);
            let answers = std::sync::Mutex::new(answers.clone().into_iter());
            let last = std::sync::Mutex::new(FetchAnswer::Silent);
            let broker = MockBroker::start(move |api_key, version, _corr_id, mut body| {
                if api_key == api_versions_request::API_KEY {
                    let versions = mock_test_support::api_versions_response_ranges([
                        (api_versions_request::API_KEY, 0, 3),
                        (metadata_request::API_KEY, 0, 8),
                        (fetch_request::API_KEY, 4, 11),
                        (list_offsets_request::API_KEY, 1, 5),
                    ]);
                    return Some(encode(&versions, 0));
                }
                if api_key == metadata_request::API_KEY {
                    let ports = &ports_in_mock;
                    let count = ports.len();
                    let metadata = MetadataResponse {
                        brokers: (0..count)
                            .map(|index| MetadataResponseBroker {
                                node_id: i32::try_from(index).expect("index") + 1,
                                host: "127.0.0.1".into(),
                                port: i32::from(ports[index].load(Ordering::SeqCst)),
                                ..Default::default()
                            })
                            .collect(),
                        topics: vec![MetadataResponseTopic {
                            name: Some("orders".into()),
                            topic_id: TOPIC_ID,
                            partitions: (0..count)
                                .map(|index| {
                                    let index = i32::try_from(index).expect("index");
                                    MetadataResponsePartition {
                                        partition_index: index,
                                        leader_id: index + 1,
                                        leader_epoch: 1,
                                        ..Default::default()
                                    }
                                })
                                .collect(),
                            ..Default::default()
                        }],
                        ..Default::default()
                    };
                    return Some(encode(&metadata, version));
                }
                if api_key == fetch_request::API_KEY {
                    let client_id_len = usize::try_from(body.get_i16()).expect("client id");
                    body.advance(client_id_len);
                    let request = FetchRequest::decode(&mut body, version).expect("decode fetch");
                    sent.lock().expect("sent lock").push((node_id, request));
                    let mut last = last.lock().expect("last lock");
                    if let Some(answer) = answers.lock().expect("answers lock").next() {
                        *last = answer;
                    }
                    return match *last {
                        FetchAnswer::Silent => None,
                        FetchAnswer::Respond {
                            error_code,
                            session_id,
                        } => Some(encode(
                            &FetchResponse {
                                error_code,
                                session_id,
                                ..Default::default()
                            },
                            version,
                        )),
                        FetchAnswer::Partition {
                            error_code,
                            preferred_read_replica,
                        } => Some(encode(
                            &FetchResponse {
                                responses: vec![FetchableTopicResponse {
                                    topic: "orders".into(),
                                    partitions: vec![PartitionData {
                                        partition_index: 0,
                                        error_code,
                                        high_watermark: 5,
                                        last_stable_offset: 5,
                                        preferred_read_replica,
                                        ..Default::default()
                                    }],
                                    ..Default::default()
                                }],
                                ..Default::default()
                            },
                            version,
                        )),
                    };
                }
                // Every partition resolves to offset 0, whichever timestamp
                // the request asked for.
                if api_key == list_offsets_request::API_KEY {
                    return Some(test_support::list_offsets_mock_response(&mut body, version));
                }
                None
            })
            .await;
            ports[index].store(broker.addr.port(), Ordering::SeqCst);
            brokers.push(broker);
        }
        brokers
    }

    /// A consumer that bootstraps from the first broker and owns one partition
    /// of `orders` per broker, each at offset 5.
    async fn consumer_on(brokers: &[MockBroker]) -> Consumer {
        let client = Client::builder()
            .bootstrap(brokers[0].addr.to_string())
            .request_timeout(secs(30))
            .build()
            .await
            .expect("client");
        let partitions: Vec<(String, i32)> = (0..brokers.len())
            .map(|index| ("orders".to_owned(), i32::try_from(index).expect("index")))
            .collect();
        let consumer = poll::partition_error_tests::consumer_with_client(client);
        *consumer.assigned.lock().await = partitions.clone();
        *consumer.next_offsets.lock().await =
            partitions.iter().map(|key| (key.clone(), 5)).collect();
        consumer.commit_identity.lock().await.ownership_ids =
            partitions.into_iter().zip(1..).collect();
        consumer
    }

    struct TwoPartitionFetchSetup {
        brokers: Vec<MockBroker>,
        consumer: Consumer,
        sent: SentFetches,
    }

    async fn two_partition_fetch_setup() -> TwoPartitionFetchSetup {
        let answer = FetchAnswer::Respond {
            error_code: 0,
            session_id: 0,
        };
        let sent = SentFetches::default();
        let brokers = start_brokers(&[vec![answer], vec![answer]], &sent).await;
        let consumer = consumer_on(&brokers).await;
        consumer.refresh_leader_epochs().await.expect("metadata");
        TwoPartitionFetchSetup {
            brokers,
            consumer,
            sent,
        }
    }

    fn stop(brokers: Vec<MockBroker>) {
        for broker in brokers {
            broker.stop();
        }
    }

    /// Kafka's `FetchRequest.max_wait_ms` is `fetch.max.wait.ms` (default
    /// 500), whatever the poll timeout.
    #[tokio::test]
    async fn fetch_waits_fetch_max_wait_and_not_the_poll_timeout() {
        let sent = SentFetches::default();
        let brokers = start_brokers(
            &[vec![FetchAnswer::Respond {
                error_code: 0,
                session_id: 0,
            }]],
            &sent,
        )
        .await;
        let mut consumer = consumer_on(&brokers).await;
        consumer.poll(secs(30)).await.expect("poll");
        let max_waits: Vec<i32> = sent
            .lock()
            .expect("sent lock")
            .iter()
            .map(|(_, request)| request.max_wait_ms)
            .collect();
        drop(consumer);
        stop(brokers);
        assert2::assert!(max_waits == vec![500]);
    }

    /// Kafka sends the Fetch of every leader at once, and `poll` returns by its
    /// timeout, also when no leader answers.
    #[tokio::test]
    async fn fetches_go_to_all_leaders_at_once_and_poll_returns_by_its_timeout() {
        let sent = SentFetches::default();
        let silent = vec![FetchAnswer::Silent];
        let brokers = start_brokers(&[silent.clone(), silent.clone(), silent], &sent).await;
        let mut consumer = consumer_on(&brokers).await;
        consumer.refresh_leader_epochs().await.expect("metadata");
        let started = tokio::time::Instant::now();
        let records = consumer.poll(millis(300)).await.expect("poll");
        let elapsed = started.elapsed();
        // Let the requests reach the brokers.
        tokio::time::sleep(Duration::from_millis(100)).await;
        let mut leaders: Vec<i32> = sent
            .lock()
            .expect("sent lock")
            .iter()
            .map(|(node_id, _)| *node_id)
            .collect();
        leaders.sort_unstable();
        drop(consumer);
        stop(brokers);
        assert2::assert!(
            (
                records.len(),
                elapsed < Duration::from_millis(1500),
                leaders
            ) == (0, true, vec![1, 2, 3])
        );
    }

    /// A partition that waits for its assign callback gets no Fetch. Kafka's
    /// `SubscriptionState.isFetchable` is false while
    /// `pendingOnAssignedCallback` is set.
    #[tokio::test]
    async fn fetch_leaves_out_a_partition_that_waits_for_its_assign_callback() {
        let TwoPartitionFetchSetup {
            sent,
            brokers,
            mut consumer,
        } = two_partition_fetch_setup().await;
        let gate = crate::rebalance_listener::AssignedCallbackGate::new(
            &consumer.assigned_callback_pending,
            &[("orders".to_owned(), 0)],
        );
        consumer.poll(millis(200)).await.expect("poll");
        tokio::time::sleep(Duration::from_millis(50)).await;
        let leaders = |sent: &SentFetches| {
            let mut leaders: Vec<i32> = sent
                .lock()
                .expect("sent lock")
                .drain(..)
                .map(|(node_id, _)| node_id)
                .collect();
            leaders.sort_unstable();
            leaders
        };
        let waiting = leaders(&sent);
        drop(gate);
        consumer.poll(millis(200)).await.expect("poll");
        tokio::time::sleep(Duration::from_millis(50)).await;
        let after = leaders(&sent);
        drop(consumer);
        stop(brokers);
        assert2::assert!((waiting, after) == (vec![2], vec![1, 2]));
    }

    /// Kafka's `seek` sets the position of an assigned partition at once, and
    /// `SubscriptionState.assignedState` throws for a partition that the
    /// consumer does not own. The next Fetch uses the sought offset.
    #[tokio::test]
    async fn seek_moves_an_owned_position_and_rejects_an_unowned_partition() {
        let mut actual = Vec::new();
        let mut wanted = Vec::new();
        for (name, partition, expected_error, expected_offset) in [
            ("owned partition", 0, None, 2),
            (
                "unowned partition",
                9,
                Some("no current assignment for partition orders-9"),
                5,
            ),
        ] {
            let sent = SentFetches::default();
            let brokers = start_brokers(
                &[vec![FetchAnswer::Respond {
                    error_code: 0,
                    session_id: 0,
                }]],
                &sent,
            )
            .await;
            let mut consumer = consumer_on(&brokers).await;
            let error = consumer
                .seek("orders", partition, 2)
                .await
                .err()
                .map(|error| error.to_string());
            consumer.poll(millis(200)).await.expect("poll");
            let offsets: Vec<i64> = sent
                .lock()
                .expect("sent lock")
                .iter()
                .flat_map(|(_, request)| &request.topics)
                .flat_map(|topic| &topic.partitions)
                .map(|partition| partition.fetch_offset)
                .collect();
            drop(consumer);
            stop(brokers);
            actual.push((name, error, offsets));
            wanted.push((
                name,
                expected_error.map(str::to_owned),
                vec![expected_offset],
            ));
        }
        assert2::assert!(actual == wanted);
    }

    /// Kafka's `AbstractFetch.fetchablePartitions` leaves out a paused
    /// partition, so its leader gets no Fetch until `resume`.
    #[tokio::test]
    async fn fetch_leaves_out_a_paused_partition_until_resume() {
        let TwoPartitionFetchSetup {
            sent,
            brokers,
            mut consumer,
        } = two_partition_fetch_setup().await;
        let fetched = |sent: &SentFetches| {
            let mut partitions: Vec<(i32, i32)> = sent
                .lock()
                .expect("sent lock")
                .drain(..)
                .flat_map(|(node_id, request)| {
                    request
                        .topics
                        .into_iter()
                        .flat_map(|topic| topic.partitions)
                        .map(move |partition| (node_id, partition.partition))
                })
                .collect();
            partitions.sort_unstable();
            partitions
        };
        consumer
            .pause(&[("orders".to_owned(), 0)])
            .await
            .expect("pause");
        consumer.poll(millis(200)).await.expect("poll");
        tokio::time::sleep(Duration::from_millis(50)).await;
        let paused = fetched(&sent);
        consumer
            .resume(&[("orders".to_owned(), 0)])
            .await
            .expect("resume");
        consumer.poll(millis(200)).await.expect("poll");
        tokio::time::sleep(Duration::from_millis(50)).await;
        let resumed = fetched(&sent);
        let unowned = consumer
            .position("orders", 9)
            .await
            .map_err(|error| error.to_string());
        drop(consumer);
        stop(brokers);
        assert2::assert!(
            (paused, resumed, unowned)
                == (
                    vec![(2, 1)],
                    vec![(1, 0), (2, 1)],
                    Err("no current assignment for partition orders-9".to_owned())
                )
        );
    }

    /// Kafka's `pollForFetches` waits for the poll timeout when no partition
    /// can be fetched, for example when all are paused.
    #[tokio::test]
    async fn poll_waits_for_its_timeout_when_every_partition_is_paused() {
        let sent = SentFetches::default();
        let brokers = start_brokers(
            &[vec![FetchAnswer::Respond {
                error_code: 0,
                session_id: 0,
            }]],
            &sent,
        )
        .await;
        let mut consumer = consumer_on(&brokers).await;
        consumer
            .pause(&[("orders".to_owned(), 0)])
            .await
            .expect("pause");
        let started = tokio::time::Instant::now();
        let records = consumer.poll(millis(300)).await.expect("poll").len();
        let waited = started.elapsed() >= Duration::from_millis(250);
        let fetches = sent.lock().expect("sent lock").len();
        drop(consumer);
        stop(brokers);
        assert2::assert!((records, waited, fetches) == (0, true, 0));
    }

    /// Kafka's `wakeup` makes the blocked `poll`, or the next one, throw
    /// `WakeupException` once.
    #[tokio::test]
    async fn wakeup_ends_the_poll_that_waits_or_the_next_one() {
        let sent = SentFetches::default();
        let brokers = start_brokers(&[vec![FetchAnswer::Silent]], &sent).await;
        let mut consumer = consumer_on(&brokers).await;
        consumer.wakeup();
        let pending = consumer
            .poll(millis(100))
            .await
            .map_err(|error| error.to_string());
        let handle = consumer.wakeup_handle();
        let waker = tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(100)).await;
            handle.wakeup();
        });
        let started = tokio::time::Instant::now();
        let blocked = consumer
            .poll(secs(10))
            .await
            .map_err(|error| error.to_string());
        let woken_fast = started.elapsed() < Duration::from_secs(5);
        waker.await.expect("waker");
        let next = consumer
            .poll(millis(100))
            .await
            .map(|records| records.len());
        drop(consumer);
        stop(brokers);
        let wakeup = Err("the consumer was woken up".to_owned());
        assert2::assert!(
            (
                pending,
                blocked,
                woken_fast,
                next.map_err(|error| error.to_string())
            ) == (wakeup.clone(), wakeup, true, Ok(0))
        );
    }

    /// Kafka's `wakeup` ends a `poll` that waits for a metadata request of
    /// `updateFetchPositions`, as `ConsumerNetworkClient.poll` throws
    /// `WakeupException` while it waits.
    #[tokio::test]
    async fn wakeup_ends_a_poll_that_waits_for_metadata() {
        let silent = MockBroker::start(|api_key, _version, _corr_id, _body| {
            (api_key == api_versions_request::API_KEY).then(|| {
                encode(
                    &mock_test_support::api_versions_response_ranges([
                        (api_versions_request::API_KEY, 0, 3),
                        (metadata_request::API_KEY, 0, 8),
                    ]),
                    0,
                )
            })
        })
        .await;
        let client = Client::builder()
            .bootstrap(silent.addr.to_string())
            .request_timeout(secs(30))
            .build()
            .await
            .expect("client");
        let mut consumer = poll::partition_error_tests::consumer_with_client(client);
        let handle = consumer.wakeup_handle();
        let waker = tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(100)).await;
            handle.wakeup();
        });
        let polled = tokio::time::timeout(Duration::from_secs(5), consumer.poll(secs(10)))
            .await
            .map(|result| {
                result
                    .map(|records| records.len())
                    .map_err(|error| error.to_string())
            });
        waker.await.expect("waker");
        drop(consumer);
        silent.stop();
        assert2::assert!(polled == Ok(Err("the consumer was woken up".to_owned())));
    }

    /// Kafka's `pollForFetches` waits at most until the coordinator needs the
    /// application thread: a rebalance that starts while every partition is
    /// paused ends the wait.
    #[tokio::test]
    async fn a_rebalance_ends_the_wait_of_a_poll_with_nothing_to_fetch() {
        let sent = SentFetches::default();
        let brokers = start_brokers(
            &[vec![FetchAnswer::Respond {
                error_code: 0,
                session_id: 0,
            }]],
            &sent,
        )
        .await;
        let mut consumer = consumer_on(&brokers).await;
        consumer
            .pause(&[("orders".to_owned(), 0)])
            .await
            .expect("pause");
        let (pending, pending_rx) = tokio::sync::watch::channel(false);
        consumer.rebalance_pending = pending_rx;
        let rebalance = tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(100)).await;
            pending.send_replace(true);
            // Keep the sender, as the coordinator task does.
            tokio::time::sleep(Duration::from_secs(10)).await;
        });
        let started = tokio::time::Instant::now();
        consumer.poll(secs(5)).await.expect("poll");
        let ended_early = started.elapsed() < Duration::from_secs(2);
        rebalance.abort();
        drop(consumer);
        stop(brokers);
        assert2::assert!(ended_early);
    }

    /// Kafka's `ConsumerMetadata` names the current subscription, so the
    /// metadata of `poll` covers a topic that a pattern added in the
    /// coordinator task.
    #[tokio::test]
    async fn poll_names_the_topics_that_a_pattern_added() {
        let sent = SentFetches::default();
        let brokers = start_brokers(&[vec![FetchAnswer::Silent]], &sent).await;
        let consumer = consumer_on(&brokers).await;
        consumer.client.metadata_topics().set(["orders".to_owned()]);
        consumer.subscription.send_modify(|subscription| {
            subscription.pattern = Some(crate::TopicPattern::new(|_| true));
            subscription.topics = vec!["orders".to_owned(), "payments".to_owned()];
        });
        consumer.sync_metadata_topics();
        let names = consumer.client.metadata_topics().names();
        drop(consumer);
        stop(brokers);
        assert2::assert!(names == vec!["orders".to_owned(), "payments".to_owned()]);
    }

    /// Kafka's `updateFetchPositions` gives the committed offsets the timer of
    /// the `poll`, so an unresponsive coordinator does not hold a short poll
    /// for the whole `default.api.timeout.ms`.
    #[tokio::test]
    async fn a_short_poll_does_not_wait_for_the_committed_offsets() {
        let sent = SentFetches::default();
        let brokers = start_brokers(&[vec![FetchAnswer::Silent]], &sent).await;
        let mut consumer = consumer_on(&brokers).await;
        consumer
            .next_offsets
            .lock()
            .await
            .insert(("orders".to_owned(), 0), COMMITTED_SENTINEL);
        let started = tokio::time::Instant::now();
        let result = consumer
            .poll(millis(200))
            .await
            .map(|records| records.len());
        let elapsed = started.elapsed();
        drop(consumer);
        stop(brokers);
        assert2::assert!((result.is_err(), elapsed < Duration::from_secs(5)) == (true, true));
    }

    /// Kafka's consumer without `group.id` fetches the partitions of
    /// `assign` from the reset position, and throws `InvalidGroupIdException`
    /// for the group calls.
    #[tokio::test]
    async fn a_consumer_without_a_group_fetches_its_manual_assignment() {
        let sent = SentFetches::default();
        let brokers = start_brokers(
            &[vec![FetchAnswer::Respond {
                error_code: 0,
                session_id: 0,
            }]],
            &sent,
        )
        .await;
        let calls = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let mut consumer = Consumer::builder()
            .bootstrap(brokers[0].addr.to_string())
            .auto_offset_reset(AutoOffsetReset::Earliest)
            .rebalance_listener(Box::new(
                RecordingListener::builder()
                    .calls(std::sync::Arc::clone(&calls))
                    .build(),
            ))
            .build()
            .await
            .expect("build");
        let before_assign = consumer
            .poll(millis(20))
            .await
            .map(|records| records.len())
            .map_err(|error| error.to_string());
        consumer
            .assign(&[("orders".to_owned(), 0)])
            .await
            .expect("assign");
        consumer.poll(millis(300)).await.expect("poll");
        let fetched: Vec<(i32, String, i32, i64)> = sent
            .lock()
            .expect("sent lock")
            .iter()
            .flat_map(|(node_id, request)| {
                request.topics.iter().flat_map(move |topic| {
                    topic.partitions.iter().map(move |partition| {
                        (
                            *node_id,
                            topic.topic.clone(),
                            partition.partition,
                            partition.fetch_offset,
                        )
                    })
                })
            })
            .collect();
        let group_calls = (
            consumer
                .commit_sync()
                .await
                .map_err(|error| error.to_string()),
            consumer
                .subscribe(["orders"])
                .await
                .map_err(|error| error.to_string()),
            consumer
                .enforce_rebalance(None)
                .map_err(|error| error.to_string()),
        );
        consumer.unsubscribe().await.expect("unsubscribe");
        let after_unsubscribe = (
            consumer.assignment().await,
            consumer.next_offsets.lock().await.len(),
        );
        let listener_calls = calls.lock().expect("calls lock").clone();
        consumer.close().await.expect("close");
        stop(brokers);
        let invalid_group = Err(ConsumerError::InvalidGroupId.to_string());
        assert2::assert!(
            (
                before_assign,
                fetched.first().cloned(),
                group_calls,
                after_unsubscribe,
                listener_calls
            ) == (
                Err("not subscribed to any topic".to_owned()),
                Some((1, "orders".to_owned(), 0, 0)),
                (invalid_group.clone(), invalid_group.clone(), invalid_group),
                (Vec::new(), 0),
                Vec::<test_support::ListenerCall>::new()
            )
        );
    }

    /// One step of a read replica case.
    #[derive(Clone, Copy, Debug)]
    enum ReplicaStep {
        Poll,
        /// Wait longer than the metadata max age of the case.
        WaitForExpiry,
    }

    /// KIP-392: Kafka's Fetch carries `client.rack`, and a
    /// `preferred_read_replica` in a response sends the next Fetch of the
    /// partition to that replica (`AbstractFetch.selectReadReplica`) until it
    /// expires after `metadata.max.age.ms` or the replica answers an error
    /// (`FetchCollector.handleInitializeErrors`).
    #[tokio::test]
    async fn fetch_sends_the_rack_and_follows_the_preferred_read_replica() {
        use FetchAnswer::Partition;
        use ReplicaStep::{Poll, WaitForExpiry};
        let records = Partition {
            error_code: 0,
            preferred_read_replica: -1,
        };
        let prefer_2 = Partition {
            error_code: 0,
            preferred_read_replica: 2,
        };
        let not_leader = Partition {
            error_code: 6,
            preferred_read_replica: -1,
        };
        let mut actual = Vec::new();
        let mut wanted = Vec::new();
        for (name, rack, leader, replica, steps, expected) in [
            (
                "no rack",
                None,
                vec![records],
                vec![records],
                vec![Poll],
                vec![(1, String::new())],
            ),
            (
                "rack",
                Some("az-1"),
                vec![records],
                vec![records],
                vec![Poll],
                vec![(1, "az-1".to_owned())],
            ),
            (
                "preferred read replica",
                Some("az-1"),
                vec![prefer_2],
                vec![records],
                vec![Poll, Poll],
                vec![(1, "az-1".to_owned()), (2, "az-1".to_owned())],
            ),
            (
                "the replica answers not leader or follower",
                Some("az-1"),
                vec![prefer_2, records],
                vec![not_leader],
                vec![Poll, Poll, Poll],
                vec![
                    (1, "az-1".to_owned()),
                    (2, "az-1".to_owned()),
                    (1, "az-1".to_owned()),
                ],
            ),
            (
                "the preference expires",
                Some("az-1"),
                vec![prefer_2, records],
                vec![records],
                vec![Poll, Poll, WaitForExpiry, Poll],
                vec![
                    (1, "az-1".to_owned()),
                    (2, "az-1".to_owned()),
                    (1, "az-1".to_owned()),
                ],
            ),
        ] {
            let sent = SentFetches::default();
            let brokers = start_brokers(&[leader, replica], &sent).await;
            let mut consumer = consumer_on(&brokers).await;
            *consumer.assigned.lock().await = vec![("orders".to_owned(), 0)];
            consumer.client_rack = rack.map(str::to_owned);
            consumer.metadata_max_age = millis(300);
            for step in steps {
                match step {
                    Poll => {
                        let before = sent.lock().expect("sent lock").len();
                        consumer.poll(secs(2)).await.expect("poll");
                        // The fetch sessions of the two brokers are independent;
                        // wait until the Fetch of this poll is recorded.
                        let _ = tokio::time::timeout(Duration::from_secs(2), async {
                            while sent.lock().expect("sent lock").len() == before {
                                tokio::time::sleep(Duration::from_millis(5)).await;
                            }
                        })
                        .await;
                    }
                    WaitForExpiry => tokio::time::sleep(Duration::from_millis(400)).await,
                }
            }
            let requests: Vec<(i32, String)> = sent
                .lock()
                .expect("sent lock")
                .iter()
                .map(|(node_id, request)| (*node_id, request.rack_id.clone()))
                .collect();
            drop(consumer);
            stop(brokers);
            actual.push((name, requests));
            wanted.push((name, expected));
        }
        assert2::assert!(actual == wanted);
    }

    /// One Fetch request of a session case: `(session_id, session_epoch,
    /// fetched partitions, forgotten partitions)`.
    type SessionFields = (i32, i32, Vec<i32>, Vec<i32>);

    fn session_fields(request: &FetchRequest) -> SessionFields {
        (
            request.session_id,
            request.session_epoch,
            request
                .topics
                .iter()
                .flat_map(|topic| topic.partitions.iter().map(|p| p.partition))
                .collect(),
            request
                .forgotten_topics_data
                .iter()
                .flat_map(|topic| topic.partitions.iter().copied())
                .collect(),
        )
    }

    /// Kafka's `FetchSessionHandler`: the first Fetch is full and creates a
    /// session, the next one is incremental and names no unchanged partition.
    /// `FETCH_SESSION_ID_NOT_FOUND` starts a new session with a full Fetch.
    #[tokio::test]
    async fn fetch_sessions_send_incremental_requests_and_recover_a_lost_session() {
        let mut actual = Vec::new();
        let mut wanted = Vec::new();
        for (name, answers, expected) in [
            (
                "session",
                vec![
                    FetchAnswer::Respond {
                        error_code: 0,
                        session_id: 77,
                    },
                    FetchAnswer::Respond {
                        error_code: 0,
                        session_id: 77,
                    },
                ],
                vec![(0, 0, vec![0], vec![]), (77, 1, vec![], vec![])],
            ),
            (
                "session lost",
                vec![
                    FetchAnswer::Respond {
                        error_code: 0,
                        session_id: 77,
                    },
                    FetchAnswer::Respond {
                        error_code: 70,
                        session_id: 0,
                    },
                    FetchAnswer::Respond {
                        error_code: 0,
                        session_id: 78,
                    },
                ],
                vec![
                    (0, 0, vec![0], vec![]),
                    (77, 1, vec![], vec![]),
                    (0, 0, vec![0], vec![]),
                ],
            ),
        ] {
            let sent = SentFetches::default();
            let rounds = answers.len();
            let brokers = start_brokers(&[answers], &sent).await;
            let mut consumer = consumer_on(&brokers).await;
            for _ in 0..rounds {
                consumer.poll(secs(5)).await.expect("poll");
            }
            let requests: Vec<SessionFields> = sent
                .lock()
                .expect("sent lock")
                .iter()
                .map(|(_, request)| session_fields(request))
                .collect();
            drop(consumer);
            stop(brokers);
            actual.push((name, requests));
            wanted.push((name, expected));
        }
        assert2::assert!(actual == wanted);
    }

    /// A Fetch result counts as soon as the task sent it, also while the task
    /// has not ended yet, so `poll` does not sleep until its deadline.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn a_fetch_result_wakes_poll_before_the_task_ends() {
        let sent = SentFetches::default();
        let brokers = start_brokers(&[vec![FetchAnswer::Silent]], &sent).await;
        let mut consumer = consumer_on(&brokers).await;
        let (tx, rx) = tokio::sync::oneshot::channel();
        let completed = Arc::clone(&consumer.fetches.completed);
        consumer.fetches.in_flight.insert(
            -1,
            InFlightFetch {
                handle: tokio::spawn(async move {
                    let _ = tx.send(Ok(FetchResponse::default()));
                    completed.notify_one();
                    // The task ends late.
                    tokio::time::sleep(Duration::from_secs(3)).await;
                }),
                result: rx,
                requested: HashMap::new(),
                from_replica: std::collections::HashSet::new(),
            },
        );
        let started = tokio::time::Instant::now();
        consumer
            .wait_for_fetches(started + Duration::from_secs(2))
            .await;
        let elapsed = started.elapsed();
        drop(consumer);
        stop(brokers);
        assert2::assert!(elapsed < Duration::from_millis(500));
    }

    /// A preferred read replica that answers `OFFSET_OUT_OF_RANGE` gives no
    /// offset reset, also when the preference expired while the Fetch was in
    /// flight: the request decides.
    #[tokio::test]
    async fn out_of_range_from_a_replica_request_gives_no_reset() {
        let sent = SentFetches::default();
        let brokers = start_brokers(&[vec![FetchAnswer::Silent]], &sent).await;
        let mut consumer = consumer_on(&brokers).await;
        consumer.auto_offset_reset = AutoOffsetReset::Latest;
        let key = ("orders".to_owned(), 0);
        let (tx, rx) = tokio::sync::oneshot::channel();
        consumer.fetches.in_flight.insert(
            2,
            InFlightFetch {
                handle: tokio::spawn(async {}),
                result: rx,
                requested: maplit::hashmap! {key.clone() => 5},
                from_replica: maplit::hashset! {key.clone()},
            },
        );
        tx.send(Ok(FetchResponse {
            responses: vec![FetchableTopicResponse {
                topic: "orders".into(),
                partitions: vec![PartitionData {
                    partition_index: 0,
                    error_code: 1,
                    ..Default::default()
                }],
                ..Default::default()
            }],
            ..Default::default()
        }))
        .expect("send response");
        let topic_ids = consumer.topic_ids.lock().await.clone();
        let responses = consumer
            .take_completed_fetches(&topic_ids)
            .await
            .expect("take");
        consumer
            .process_fetch_responses(responses, &topic_ids)
            .await
            .expect("process");
        let position = consumer.next_offsets.lock().await.get(&key).copied();
        drop(consumer);
        stop(brokers);
        assert2::assert!(position == Some(5));
    }

    /// Kafka's `FetchCollector` discards the data of a partition whose position
    /// changed after the Fetch went out, for example after a seek.
    #[tokio::test]
    async fn a_fetch_response_for_a_moved_position_gives_no_records() {
        let sent = SentFetches::default();
        let brokers = start_brokers(&[vec![FetchAnswer::Silent]], &sent).await;
        let mut consumer = consumer_on(&brokers).await;
        let (tx, rx) = tokio::sync::oneshot::channel();
        consumer.fetches.in_flight.insert(
            -1,
            InFlightFetch {
                handle: tokio::spawn(async {}),
                result: rx,
                requested: maplit::hashmap! {("orders".to_owned(), 0) => 5},
                from_replica: std::collections::HashSet::new(),
            },
        );
        // A seek moves the position while the Fetch is in flight.
        consumer
            .next_offsets
            .lock()
            .await
            .insert(("orders".to_owned(), 0), 40);
        let record = krabka_protocol::records::RecordBatch {
            base_offset: 5,
            last_offset_delta: 0,
            records: vec![krabka_protocol::records::Record {
                offset_delta: 0,
                value: Some(bytes::Bytes::from_static(b"old")),
                ..Default::default()
            }],
            ..Default::default()
        };
        tx.send(Ok(FetchResponse {
            responses: vec![FetchableTopicResponse {
                topic: "orders".into(),
                partitions: vec![PartitionData {
                    partition_index: 0,
                    high_watermark: 6,
                    records: Some(RecordsPayload::V2(vec![record])),
                    ..Default::default()
                }],
                ..Default::default()
            }],
            ..Default::default()
        }))
        .expect("send response");
        consumer
            .wait_for_fetches(tokio::time::Instant::now() + Duration::from_secs(5))
            .await;
        let topic_ids = consumer.topic_ids.lock().await.clone();
        let responses = consumer
            .take_completed_fetches(&topic_ids)
            .await
            .expect("take");
        consumer
            .process_fetch_responses(responses, &topic_ids)
            .await
            .expect("process");
        let records = consumer.drain_fetch_buffer().await;
        let position = consumer
            .next_offsets
            .lock()
            .await
            .get(&("orders".to_owned(), 0))
            .copied();
        drop(consumer);
        stop(brokers);
        assert2::assert!((records.len(), position) == (0, Some(40)));
    }

    /// A single mock broker that serves `ApiVersions`, `Metadata`,
    /// `ListOffsets` (always answering `list_offsets_offset` for `orders-0`)
    /// and `Fetch` (an empty, error-free response). It never names a
    /// separately dialable leader address, so every request the consumer
    /// sends for `orders-0` falls back to this bootstrap connection.
    ///
    /// Captures the timestamp of every `ListOffsets` request and the
    /// `fetch_offset` of every `Fetch` request it decodes.
    async fn no_committed_offset_broker(
        list_offsets_offset: i64,
        list_offsets_timestamps: Arc<std::sync::Mutex<Vec<i64>>>,
        fetch_offsets: Arc<std::sync::Mutex<Vec<i64>>>,
    ) -> MockBroker {
        use krabka_protocol::owned::{
            list_offsets_request::FLEXIBLE_MIN,
            list_offsets_response::{ListOffsetsPartitionResponse, ListOffsetsTopicResponse},
        };
        const NODE_ID: i32 = 1;
        MockBroker::start(move |api_key, version, _corr_id, mut body| {
            if api_key == api_versions_request::API_KEY {
                let versions = mock_test_support::api_versions_response_ranges([
                    (api_versions_request::API_KEY, 0, 3),
                    (metadata_request::API_KEY, 0, 8),
                    (
                        list_offsets_request::API_KEY,
                        0,
                        list_offsets_request::MAX_VERSION,
                    ),
                    (fetch_request::API_KEY, 4, 11),
                ]);
                return Some(encode(&versions, 0));
            }
            if api_key == metadata_request::API_KEY {
                // No `brokers` entry for `NODE_ID`: the pool cannot dial it,
                // so `orders-0` falls back to the bootstrap connection, which
                // is this same mock broker.
                let metadata = test_support::OrdersMetadata::builder()
                    .topic_id([9; 16])
                    .leader_id(NODE_ID)
                    .leader_epoch(1)
                    .build();
                return Some(encode(&metadata, version));
            }
            if api_key == list_offsets_request::API_KEY {
                let request = test_support::decode_list_offsets_request(&mut body, version);
                let timestamp = request.topics[0].partitions[0].timestamp;
                list_offsets_timestamps
                    .lock()
                    .expect("lock")
                    .push(timestamp);
                let answer = ListOffsetsResponse {
                    topics: vec![ListOffsetsTopicResponse {
                        name: "orders".into(),
                        partitions: vec![ListOffsetsPartitionResponse {
                            partition_index: 0,
                            error_code: 0,
                            offset: list_offsets_offset,
                            ..Default::default()
                        }],
                        ..Default::default()
                    }],
                    ..Default::default()
                };
                let mut bytes = Vec::new();
                if version >= FLEXIBLE_MIN {
                    // The flexible response header has an empty tagged-field
                    // section.
                    bytes.push(0);
                }
                bytes.extend(encode(&answer, version));
                return Some(bytes);
            }
            if api_key == fetch_request::API_KEY {
                let client_id_len = usize::try_from(body.get_i16()).expect("client id");
                body.advance(client_id_len);
                let request = FetchRequest::decode(&mut body, version).expect("decode fetch");
                if let Some(partition) = request
                    .topics
                    .first()
                    .and_then(|topic| topic.partitions.first())
                {
                    fetch_offsets
                        .lock()
                        .expect("lock")
                        .push(partition.fetch_offset);
                }
                return Some(encode(&FetchResponse::default(), version));
            }
            None
        })
        .await
    }

    /// A consumer of `group-a`, assigned `orders-0` with no committed offset:
    /// the priming a fresh assignment gets when `OffsetFetch` names no commit
    /// (`starting_offset`/`reset_starting_offset` in `consumer.rs`).
    async fn consumer_with_no_committed_offset(
        broker: &MockBroker,
        policy: AutoOffsetReset,
    ) -> Consumer {
        let client = Client::builder()
            .bootstrap(broker.addr.to_string())
            .request_timeout(secs(30))
            .build()
            .await
            .expect("client");
        let mut consumer = poll::partition_error_tests::consumer_with_client(client);
        consumer.auto_offset_reset = policy;
        consumer.next_offsets.lock().await.insert(
            ("orders".to_owned(), 0),
            consumer::reset_starting_offset(policy),
        );
        consumer
    }

    /// Kafka's initial position for a fresh assignment with no committed
    /// offset, by `auto.offset.reset` policy
    /// (<https://github.com/krabka-io/krabka-client-rs/issues/140>):
    ///
    /// - `none` never auto-resets: `poll` fails with
    ///   `NoOffsetForPartition` and sends no `Fetch`.
    /// - `earliest` asks `ListOffsets(-2)` before its first `Fetch`, which
    ///   then starts from the broker's real log start (40 here), never from
    ///   the literal offset 0.
    /// - `latest` asks `ListOffsets(-1)`, unchanged by this fix.
    #[tokio::test]
    async fn initial_position_follows_the_reset_policy_with_no_committed_offset() {
        struct Case {
            name: &'static str,
            policy: AutoOffsetReset,
            expected_list_offsets: Vec<i64>,
            expected_fetch_offsets: Vec<i64>,
            expect_error: bool,
        }
        let cases = [
            Case {
                name: "none has no committed offset and no reset policy: poll fails, no Fetch",
                policy: AutoOffsetReset::None,
                expected_list_offsets: vec![],
                expected_fetch_offsets: vec![],
                expect_error: true,
            },
            Case {
                name: "earliest asks ListOffsets(-2) and fetches from the log start",
                policy: AutoOffsetReset::Earliest,
                expected_list_offsets: vec![EARLIEST_TIMESTAMP],
                expected_fetch_offsets: vec![40],
                expect_error: false,
            },
            Case {
                name: "latest asks ListOffsets(-1)",
                policy: AutoOffsetReset::Latest,
                expected_list_offsets: vec![LATEST_TIMESTAMP],
                expected_fetch_offsets: vec![40],
                expect_error: false,
            },
        ];
        let mut actual = Vec::new();
        let mut wanted = Vec::new();
        for case in cases {
            let list_offsets_timestamps: Arc<std::sync::Mutex<Vec<i64>>> = Arc::default();
            let fetch_offsets: Arc<std::sync::Mutex<Vec<i64>>> = Arc::default();
            let broker = no_committed_offset_broker(
                40,
                Arc::clone(&list_offsets_timestamps),
                Arc::clone(&fetch_offsets),
            )
            .await;
            let mut consumer = consumer_with_no_committed_offset(&broker, case.policy).await;
            let result = consumer.poll(millis(500)).await;
            let list_offsets_timestamps = list_offsets_timestamps.lock().expect("lock").clone();
            let fetch_offsets = fetch_offsets.lock().expect("lock").clone();
            drop(consumer);
            broker.stop();
            actual.push((
                case.name,
                list_offsets_timestamps,
                fetch_offsets,
                result.is_err(),
            ));
            wanted.push((
                case.name,
                case.expected_list_offsets,
                case.expected_fetch_offsets,
                case.expect_error,
            ));
        }
        assert2::assert!(actual == wanted);
    }
}