File: hash-field-expire.tcl

package info (click to toggle)
redis 5%3A8.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 22,304 kB
  • sloc: ansic: 216,903; tcl: 51,562; sh: 4,625; perl: 4,214; cpp: 3,568; python: 2,954; makefile: 2,055; ruby: 639; javascript: 30; csh: 7
file content (1930 lines) | stat: -rw-r--r-- 84,837 bytes parent folder | download
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
######## HEXPIRE family commands
# Field does not exists
set E_NO_FIELD     -2
# Specified NX | XX | GT | LT condition not met
set E_FAIL         0
# expiration time set/updated
set E_OK           1
# Field deleted because the specified expiration time is in the past
set E_DELETED      2

######## HTTL family commands
set T_NO_FIELD    -2
set T_NO_EXPIRY   -1

######## HPERIST
set P_NO_FIELD    -2
set P_NO_EXPIRY   -1
set P_OK           1

############################### AUX FUNCS ######################################

proc get_stat_subexpiry {r} {
    set input_string [r info keyspace]
    set hash_count 0

    foreach line [split $input_string \n] {
        if {[regexp {subexpiry=(\d+)} $line -> value]} {
            return $value
        }
    }

    return 0
}

proc get_keys {l} {
    set res {}
    foreach entry $l {
        set key [lindex $entry 0]
        lappend res $key
    }
    return $res
}

proc dumpAllHashes {client} {
    set keyAndFields(0,0) 0
    unset keyAndFields
    # keep keys sorted for comparison
    foreach key [lsort [$client keys *]] {
        set fields [$client hgetall $key]
        foreach f $fields {
            set keyAndFields($key,$f) [$client hpexpiretime $key FIELDS 1 $f]
        }
    }
    return [array get keyAndFields]
}

############################### TESTS #########################################

start_server {tags {"external:skip needs:debug"}} {
    foreach type {listpackex hashtable} {
        if {$type eq "hashtable"} {
            r config set hash-max-listpack-entries 0
        } else {
            r config set hash-max-listpack-entries 512
        }

        test "HEXPIRE/HEXPIREAT/HPEXPIRE/HPEXPIREAT - Returns array if the key does not exist" {
            r del myhash
            assert_equal [r HEXPIRE myhash 1000 FIELDS 1 a] [list $E_NO_FIELD]
            assert_equal [r HEXPIREAT myhash 1000 FIELDS 1 a] [list $E_NO_FIELD]
            assert_equal [r HPEXPIRE myhash 1000 FIELDS 2 a b] [list $E_NO_FIELD $E_NO_FIELD]
            assert_equal [r HPEXPIREAT myhash 1000 FIELDS 2 a b] [list $E_NO_FIELD $E_NO_FIELD]
        }

        test "HEXPIRE/HEXPIREAT/HPEXPIRE/HPEXPIREAT - Verify that the expire time does not overflow" {
            r del myhash
            r hset myhash f1 v1
            # The expire time can't be negative.
            assert_error {ERR invalid expire time, must be >= 0} {r HEXPIRE myhash -1 FIELDS 1 f1}
            assert_error {ERR invalid expire time, must be >= 0} {r HEXPIRE myhash -9223372036854775808 FIELDS 1 f1}
            # The expire time can't be greater than the EB_EXPIRE_TIME_MAX
            assert_error {ERR invalid expire time in 'hexpire' command} {r HEXPIRE myhash [expr (1<<48) / 1000] FIELDS 1 f1}
            assert_error {ERR invalid expire time in 'hexpireat' command} {r HEXPIREAT myhash [expr (1<<48) / 1000 + [clock seconds] + 100] FIELDS 1 f1}
            assert_error {ERR invalid expire time in 'hpexpire' command} {r HPEXPIRE myhash [expr (1<<48)] FIELDS 1 f1}
            assert_error {ERR invalid expire time in 'hpexpireat' command} {r HPEXPIREAT myhash [expr (1<<48) + [clock milliseconds] + 100] FIELDS 1 f1}
        }

        test "HPEXPIRE(AT) - Test 'NX' flag ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hpexpire myhash 1000 NX FIELDS 1 field1] [list  $E_OK]
            assert_equal [r hpexpire myhash 1000 NX FIELDS 2 field1 field2] [list  $E_FAIL  $E_OK]

            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+1000)*1000}] NX FIELDS 1 field1] [list  $E_OK]
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+1000)*1000}] NX FIELDS 2 field1 field2] [list  $E_FAIL  $E_OK]
        }

        test "HPEXPIRE(AT) - Test 'XX' flag ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hpexpire myhash 1000 NX FIELDS 2 field1 field2] [list  $E_OK  $E_OK]
            assert_equal [r hpexpire myhash 1000 XX FIELDS 2 field1 field3] [list  $E_OK  $E_FAIL]

            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+1000)*1000}] NX FIELDS 2 field1 field2] [list  $E_OK  $E_OK]
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+1000)*1000}] XX FIELDS 2 field1 field3] [list  $E_OK  $E_FAIL]
        }

        test "HPEXPIRE(AT) - Test 'GT' flag ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2
            assert_equal [r hpexpire myhash 1000 NX FIELDS 1 field1] [list  $E_OK]
            assert_equal [r hpexpire myhash 2000 NX FIELDS 1 field2] [list  $E_OK]
            assert_equal [r hpexpire myhash 1500 GT FIELDS 2 field1 field2] [list  $E_OK  $E_FAIL]

            r del myhash
            r hset myhash field1 value1 field2 value2
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+1000)*1000}] NX FIELDS 1 field1] [list  $E_OK]
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+2000)*1000}] NX FIELDS 1 field2] [list  $E_OK]
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+1500)*1000}] GT FIELDS 2 field1 field2] [list  $E_OK  $E_FAIL]
        }

        test "HPEXPIRE(AT) - Test 'LT' flag ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hpexpire myhash 1000 NX FIELDS 1 field1] [list  $E_OK]
            assert_equal [r hpexpire myhash 2000 NX FIELDS 1 field2] [list  $E_OK]
            assert_equal [r hpexpire myhash 1500 LT FIELDS 3 field1 field2 field3] [list  $E_FAIL $E_OK $E_OK]

            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+1000)*1000}] NX FIELDS 1 field1] [list  $E_OK]
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+2000)*1000}] NX FIELDS 1 field2] [list  $E_OK]
            assert_equal [r hpexpireat myhash [expr {([clock seconds]+1500)*1000}] LT FIELDS 3 field1 field2 field3] [list  $E_FAIL $E_OK $E_OK]
        }

        test "HPEXPIREAT - field not exists or TTL is in the past ($type)" {
            r del myhash
            r hset myhash f1 v1 f2 v2 f4 v4
            r hexpire myhash 1000 NX FIELDS 1 f4
            assert_equal [r hpexpireat myhash [expr {([clock seconds]-1)*1000}] NX FIELDS 4 f1 f2 f3 f4] "$E_DELETED $E_DELETED $E_NO_FIELD $E_FAIL"
            assert_equal [r hexists myhash field1] 0
        }

        test "HPEXPIRE - wrong number of arguments ($type)" {
            r del myhash
            r hset myhash f1 v1
            assert_error {*Parameter `numFields` should be greater than 0} {r hpexpire myhash 1000 NX FIELDS 0 f1 f2 f3}
            # <count> not match with actual number of fields
            assert_error {*parameter must match the number*} {r hpexpire myhash 1000 NX FIELDS 4 f1 f2 f3}
            assert_error {*parameter must match the number*} {r hpexpire myhash 1000 NX FIELDS 2 f1 f2 f3}
        }

        test "HPEXPIRE - parameter expire-time near limit of  2^46 ($type)" {
            r del myhash
            r hset myhash f1 v1
            # below & above
            assert_equal [r hpexpire myhash [expr (1<<46) - [clock milliseconds] - 1000 ] FIELDS 1 f1] [list  $E_OK]
            assert_error {*invalid expire time*} {r hpexpire myhash [expr (1<<46) - [clock milliseconds] + 100 ] FIELDS 1 f1}
        }

        test "Lazy Expire - fields are lazy deleted ($type)" {
            r debug set-active-expire 0
            r del myhash

            r hset myhash f1 v1 f2 v2 f3 v3
            r hpexpire myhash 1 NX FIELDS 3 f1 f2 f3
            after 5

            # Verify that still exists even if all fields are expired
            assert_equal 1 [r EXISTS myhash]

            # Verify that len counts also expired fields
            assert_equal 3 [r HLEN myhash]

            # Trying access to expired field should delete it. Len should be updated
            assert_equal 0 [r hexists myhash f1]
            assert_equal 2 [r HLEN myhash]

            # Trying access another expired field should delete it. Len should be updated
            assert_equal "" [r hget myhash f2]
            assert_equal 1 [r HLEN myhash]

            # Trying access last expired field should delete it. hash shouldn't exists afterward.
            assert_equal 0 [r hstrlen myhash f3]
            assert_equal 0 [r HLEN myhash]
            assert_equal 0 [r EXISTS myhash]

            # Restore default
            r debug set-active-expire 1
        }

        test "Active Expire - deletes hash that all its fields got expired ($type)" {
            r flushall

            set hash_sizes {1 15 16 17 31 32 33 40}
            foreach h $hash_sizes {
                for {set i 1} {$i <= $h} {incr i} {
                    # Random expiration time (Take care expired not after "mix$h")
                    r hset hrand$h f$i v$i
                    r hpexpire hrand$h [expr {70 + int(rand() * 30)}] FIELDS 1 f$i
                    assert_equal 1 [r HEXISTS hrand$h f$i]

                    # Same expiration time (Take care expired not after "mix$h")
                    r hset same$h f$i v$i
                    r hpexpire same$h 100 FIELDS 1 f$i
                    assert_equal 1 [r HEXISTS same$h f$i]

                    # same expiration time
                    r hset mix$h f$i v$i fieldWithoutExpire$i v$i
                    r hpexpire mix$h 100 FIELDS 1 f$i
                    assert_equal 1 [r HEXISTS mix$h f$i]
                }
            }

            # Wait for active expire
            wait_for_condition 50 20 { [r EXISTS same40] == 0 } else { fail "hash `same40` should be expired" }

            # Verify that all fields got expired and keys got deleted
            foreach h $hash_sizes {
                wait_for_condition 50 20 {
                    [r HLEN mix$h] == $h
                } else {
                    fail "volatile fields of hash `mix$h` should be expired"
                }

                for {set i 1} {$i <= $h} {incr i} {
                    assert_equal 0 [r HEXISTS mix$h f$i]
                }
                assert_equal 0 [r EXISTS hrand$h]
                assert_equal 0 [r EXISTS same$h]
            }
        }

        test "HPEXPIRE - Flushall deletes all pending expired fields ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2
            r hpexpire myhash 10000 NX FIELDS 1 field1
            r hpexpire myhash 10000 NX FIELDS 1 field2
            r flushall
            r del myhash
            r hset myhash field1 value1 field2 value2
            r hpexpire myhash 10000 NX FIELDS 1 field1
            r hpexpire myhash 10000 NX FIELDS 1 field2
            r flushall async
        }

        test "HTTL/HPTTL - Returns array if the key does not exist" {
            r del myhash
            assert_equal [r HTTL myhash FIELDS 1 a] [list $T_NO_FIELD]
            assert_equal [r HPTTL myhash FIELDS 2 a b] [list $T_NO_FIELD $T_NO_FIELD]
        }

        test "HTTL/HPTTL - Input validation gets failed on nonexists field or field without expire ($type)" {
            r del myhash
            r HSET myhash field1 value1 field2 value2
            r HPEXPIRE myhash 1000 NX FIELDS 1 field1

            foreach cmd {HTTL HPTTL} {
                assert_equal [r $cmd myhash FIELDS 2 field2 non_exists_field] "$T_NO_EXPIRY $T_NO_FIELD"
                # <count> not match with actual number of fields
                assert_error {*parameter must match the number*} {r $cmd myhash FIELDS 1 non_exists_field1 non_exists_field2}
                assert_error {*parameter must match the number*} {r $cmd myhash FIELDS 3 non_exists_field1 non_exists_field2}
            }
        }

        test "HTTL/HPTTL - returns time to live in seconds/msillisec ($type)" {
            r del myhash
            r HSET myhash field1 value1 field2 value2
            r HPEXPIRE myhash 2000 NX FIELDS 2 field1 field2
            set ttlArray [r HTTL myhash FIELDS 2 field1 field2]
            assert_range [lindex $ttlArray 0] 1 2
            set ttl [r HPTTL myhash FIELDS 1 field1]
            assert_range $ttl 1000 2000
        }

        test "HEXPIRETIME/HPEXPIRETIME - Returns array if the key does not exist" {
            r del myhash
            assert_equal [r HEXPIRETIME myhash FIELDS 1 a] [list $T_NO_FIELD]
            assert_equal [r HPEXPIRETIME myhash FIELDS 2 a b] [list $T_NO_FIELD $T_NO_FIELD]
        }

        test "HEXPIRETIME - returns TTL in Unix timestamp ($type)" {
            r del myhash
            r HSET myhash field1 value1
            set lo [expr {[clock seconds] + 1}]
            set hi [expr {[clock seconds] + 2}]
            r HPEXPIRE myhash 1000 NX FIELDS 1 field1
            assert_range [r HEXPIRETIME myhash FIELDS 1 field1] $lo $hi
            assert_range [r HPEXPIRETIME myhash FIELDS 1 field1] [expr $lo*1000] [expr $hi*1000]
        }

        test "HTTL/HPTTL - Verify TTL progress until expiration ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2
            r hpexpire myhash 1000 NX FIELDS 1 field1
            assert_range [r HPTTL myhash FIELDS 1 field1] 100 1000
            assert_range [r HTTL myhash FIELDS 1 field1] 0 1
            after 100
            assert_range [r HPTTL myhash FIELDS 1 field1] 1 901
            after 910
            assert_equal [r HPTTL myhash FIELDS 1 field1] $T_NO_FIELD
            assert_equal [r HTTL myhash FIELDS 1 field1] $T_NO_FIELD
        }

        test "HPEXPIRE - DEL hash with non expired fields (valgrind test) ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2
            r hpexpire myhash 10000 NX FIELDS 1 field1
            r del myhash
        }

        test "HEXPIREAT - Set time in the past ($type)" {
            r del myhash
            r hset myhash field1 value1
            assert_equal [r hexpireat myhash [expr {[clock seconds] - 1}] NX FIELDS 1 field1] $E_DELETED
            assert_equal [r hexists myhash field1] 0
        }

        test "HEXPIREAT - Set time and then get TTL ($type)" {
            r del myhash
            r hset myhash field1 value1

            r hexpireat myhash [expr {[clock seconds] + 2}] NX FIELDS 1 field1
            assert_range [r hpttl myhash FIELDS 1 field1] 500 2000
            assert_range [r httl myhash FIELDS 1 field1] 1 2

            r hexpireat myhash [expr {[clock seconds] + 5}] XX FIELDS 1 field1
            assert_range [r httl myhash FIELDS 1 field1] 4 5
        }

        test "Lazy Expire - delete hash with expired fields ($type)" {
            r del myhash
            r debug set-active-expire 0
            r hset myhash k v
            r hpexpire myhash 1 NX FIELDS 1 k
            after 5
            r del myhash
            r debug set-active-expire 1
        }

        test "Test HRANDFIELD deletes all expired fields ($type)" {
            r debug set-active-expire 0
            r flushall
            r config resetstat
            r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5
            r hpexpire myhash 1 FIELDS 2 f1 f2
            after 5
            assert_equal [lsort [r hrandfield myhash 5]] "f3 f4 f5"
            assert_equal [s expired_subkeys] 2
            r hpexpire myhash 1 FIELDS 3 f3 f4 f5
            after 5
            assert_equal [lsort [r hrandfield myhash 5]] ""
            assert_equal [r keys *] ""

            r del myhash
            r hset myhash f1 v1 f2 v2 f3 v3
            r hpexpire myhash 1 FIELDS 1 f1
            after 5
            set res [r hrandfield myhash]
            assert {$res == "f2" || $res == "f3"}
            r hpexpire myhash 1 FIELDS 1 f2
            after 5
            assert_equal [lsort [r hrandfield myhash 5]] "f3"
            r hpexpire myhash 1 FIELDS 1 f3
            after 5
            assert_equal [r hrandfield myhash] ""
            assert_equal [r keys *] ""

            r debug set-active-expire 1
        }

        test "Lazy Expire - HLEN does count expired fields ($type)" {
            # Enforce only lazy expire
            r debug set-active-expire 0

            r del h1 h4 h18 h20
            r hset h1 k1 v1
            r hpexpire h1 1 NX FIELDS 1 k1

            r hset h4 k1 v1 k2 v2 k3 v3 k4 v4
            r hpexpire h4 1 NX FIELDS 3 k1 k3 k4

            # beyond 16 fields: HFE DS (ebuckets) converts from list to rax

            r hset h18 k1 v1 k2 v2 k3 v3 k4 v4 k5 v5 k6 v6 k7 v7 k8 v8 k9 v9 k10 v10 k11 v11 k12 v12 k13 v13 k14 v14 k15 v15 k16 v16 k17 v17 k18 v18
            r hpexpire h18 1 NX FIELDS 18 k1 k2 k3 k4 k5 k6 k7 k8 k9 k10 k11 k12 k13 k14 k15 k16 k17 k18

            r hset h20 k1 v1 k2 v2 k3 v3 k4 v4 k5 v5 k6 v6 k7 v7 k8 v8 k9 v9 k10 v10 k11 v11 k12 v12 k13 v13 k14 v14 k15 v15 k16 v16 k17 v17 k18 v18 k19 v19 k20 v20
            r hpexpire h20 1 NX FIELDS 2 k1 k2

            after 10

            assert_equal [r hlen h1] 1
            assert_equal [r hlen h4] 4
            assert_equal [r hlen h18] 18
            assert_equal [r hlen h20] 20
            # Restore to support active expire
            r debug set-active-expire 1
        }

        test "Lazy Expire - HSCAN does not report expired fields ($type)" {
            # Enforce only lazy expire
            r debug set-active-expire 0

            r del h1 h20 h4 h18 h20
            r hset h1 01 01
            r hpexpire h1 1 NX FIELDS 1 01

            r hset h4 01 01 02 02 03 03 04 04
            r hpexpire h4 1 NX FIELDS 3 01 03 04

            # beyond 16 fields hash-field expiration DS (ebuckets) converts from list to rax

            r hset h18 01 01 02 02 03 03 04 04 05 05 06 06 07 07 08 08 09 09 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18
            r hpexpire h18 1 NX FIELDS 18 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18

            r hset h20 01 01 02 02 03 03 04 04 05 05 06 06 07 07 08 08 09 09 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20
            r hpexpire h20 1 NX FIELDS 2 01 02

            after 10

            # Verify SCAN does not report expired fields
            assert_equal [lsort -unique [lindex [r hscan h1 0 COUNT 10] 1]] ""
            assert_equal [lsort -unique [lindex [r hscan h4 0 COUNT 10] 1]] "02"
            assert_equal [lsort -unique [lindex [r hscan h18 0 COUNT 10] 1]] ""
            assert_equal [lsort -unique [lindex [r hscan h20 0 COUNT 100] 1]] "03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20"
            # Restore to support active expire
            r debug set-active-expire 1
        }

        test "Test HSCAN with mostly expired fields return empty result ($type)" {
            r debug set-active-expire 0

            # Create hash with 1000 fields and 999 of them will be expired
            r del myhash
            for {set i 1} {$i <= 1000} {incr i} {
                r hset myhash field$i value$i
                if {$i > 1} {
                    r hpexpire myhash 1 NX FIELDS 1 field$i
                }
            }
            after 3

            # Verify iterative HSCAN returns either empty result or only the first field
            set countEmptyResult 0
            set cur 0
            while 1 {
                set res [r hscan myhash $cur]
                set cur [lindex $res 0]
                # if the result is not empty, it should contain only the first field
                if {[llength [lindex $res 1]] > 0} {
                    assert_equal [lindex $res 1] "field1 value1"
                } else {
                    incr countEmptyResult
                }
                if {$cur == 0} break
            }
            assert {$countEmptyResult > 0}
            r debug set-active-expire 1
        }

        test "Lazy Expire - verify various HASH commands handling expired fields ($type)" {
            # Enforce only lazy expire
            r debug set-active-expire 0
            r del h1 h2 h3 h4 h5 h18
            r hset h1 01 01
            r hset h2 01 01 02 02
            r hset h3 01 01 02 02 03 03
            r hset h4 1 99 2 99 3 99 4 99
            r hset h5 1 1 2 22 3 333 4 4444 5 55555
            r hset h6 01 01 02 02 03 03 04 04 05 05 06 06
            r hset h18 01 01 02 02 03 03 04 04 05 05 06 06 07 07 08 08 09 09 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18
            r hpexpire h1 1 NX FIELDS 1 01
            r hpexpire h2 1 NX FIELDS 1 01
            r hpexpire h2 1 NX FIELDS 1 02
            r hpexpire h3 1 NX FIELDS 1 01
            r hpexpire h4 1 NX FIELDS 1 2
            r hpexpire h5 1 NX FIELDS 1 3
            r hpexpire h6 1 NX FIELDS 1 05
            r hpexpire h18 1 NX FIELDS 17 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17

            after 5

            # Verify HDEL not ignore expired field. It is too much overhead to check
            # if the field is expired before deletion.
            assert_equal [r HDEL h1 01] "1"

            # Verify HGET ignore expired field
            r config resetstat
            assert_equal [r HGET h2 01] ""
            assert_equal [s expired_subkeys] 1
            assert_equal [r HGET h2 02] ""
            assert_equal [s expired_subkeys] 2
            assert_equal [r HGET h3 01] ""
            assert_equal [r HGET h3 02] "02"
            assert_equal [r HGET h3 03] "03"
            assert_equal [s expired_subkeys] 3
            # Verify HINCRBY ignore expired field
            assert_equal [r HINCRBY h4 2 1] "1"
            assert_equal [s expired_subkeys] 4
            assert_equal [r HINCRBY h4 3 1] "100"
            # Verify HSTRLEN ignore expired field
            assert_equal [r HSTRLEN h5 3] "0"
            assert_equal [s expired_subkeys] 5
            assert_equal [r HSTRLEN h5 4] "4"
            assert_equal [lsort [r HKEYS h6]] "01 02 03 04 06"
            assert_equal [s expired_subkeys] 5
            # Verify HEXISTS ignore expired field
            assert_equal [r HEXISTS h18 07] "0"
            assert_equal [s expired_subkeys] 6
            assert_equal [r HEXISTS h18 18] "1"
            # Verify HVALS ignore expired field
            assert_equal [lsort [r HVALS h18]] "18"
            assert_equal [s expired_subkeys] 6
            # Restore to support active expire
            r debug set-active-expire 1
        }

        test "A field with TTL overridden with another value (TTL discarded) ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            r hpexpire myhash 10000 NX FIELDS 1 field1
            r hpexpire myhash 1 NX FIELDS 1 field2

            # field2 TTL will be discarded
            r hset myhash field2 value4
            after 5
            # Expected TTL will be discarded
            assert_equal [r hget myhash field2] "value4"
            assert_equal [r httl myhash FIELDS 2 field2 field3] "$T_NO_EXPIRY $T_NO_EXPIRY"
            assert_not_equal [r httl myhash FIELDS 1 field1] "$T_NO_EXPIRY"
        }

        test "Modify TTL of a field ($type)" {
            r del myhash
            r hset myhash field1 value1
            r hpexpire myhash 200000 NX FIELDS 1 field1
            r hpexpire myhash 1000000 XX FIELDS 1 field1
            after 15
            assert_equal [r hget myhash field1] "value1"
            assert_range [r hpttl myhash FIELDS 1 field1] 900000 1000000
        }

        test "Test return value of set operation ($type)" {
             r del myhash
             r hset myhash f1 v1 f2 v2
             r hexpire myhash 100000 FIELDS 1 f1
             assert_equal [r hset myhash f2 v2] 0
             assert_equal [r hset myhash f3 v3] 1
             assert_equal [r hset myhash f3 v3 f4 v4] 1
             assert_equal [r hset myhash f3 v3 f5 v5 f6 v6] 2
        }

        test "Test HGETALL not return expired fields ($type)" {
            # Test with small hash
            r debug set-active-expire 0
            r del myhash
            r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5 f6 v6
            r hpexpire myhash 1 NX FIELDS 3 f2 f4 f6
            after 10
            assert_equal [lsort [r hgetall myhash]] "f1 f3 f5 v1 v3 v5"

            # Test with large hash
            r del myhash
            for {set i 1} {$i <= 600} {incr i} {
                r hset myhash f$i v$i
                if {$i > 3} { r hpexpire myhash 1 NX FIELDS 1 f$i }
            }
            after 10
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 f2 f3 v1 v2 v3"]

            # hash that all fields are expired return empty result
            r del myhash
            r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5 f6 v6
            r hpexpire myhash 1 FIELDS 6 f1 f2 f3 f4 f5 f6
            after 10
            assert_equal [r hgetall myhash] ""
            r debug set-active-expire 1
        }

        test "Test RENAME hash with fields to be expired ($type)" {
            r debug set-active-expire 0
            r del myhash
            r hset myhash field1 value1
            r hpexpire myhash 20 NX FIELDS 1 field1
            r rename myhash myhash2
            assert_equal [r exists myhash] 0
            assert_range [r hpttl myhash2 FIELDS 1 field1] 1 20
            after 25
            # Verify the renamed key exists
            assert_equal [r exists myhash2] 1
            r debug set-active-expire 1
            # Only active expire will delete the key
            wait_for_condition 30 10 { [r exists myhash2] == 0 } else { fail "`myhash2` should be expired" }
        }

        test "Test RENAME hash that had HFEs but not during the rename ($type)" {
            r del h1
            r hset h1 f1 v1 f2 v2
            r hpexpire h1 1 FIELDS 1 f1
            after 20
            r rename h1 h1_renamed
            assert_equal [r exists h1] 0
            assert_equal [r exists h1_renamed] 1
            assert_equal [r hgetall h1_renamed] {f2 v2}
            r hpexpire h1_renamed 1 FIELDS 1 f2
            # Only active expire will delete the key
            wait_for_condition 30 10 { [r exists h1_renamed] == 0 } else { fail "`h1_renamed` should be expired" }
        }

        test "MOVE to another DB hash with fields to be expired ($type)" {
            r select 9
            r flushall
            r hset myhash field1 value1
            r hpexpire myhash 100 NX FIELDS 1 field1
            r move myhash 10
            assert_equal [r exists myhash] 0
            assert_equal [r dbsize] 0

            # Verify the key and its field exists in the target DB
            r select 10
            assert_equal [r hget myhash field1] "value1"
            assert_equal [r exists myhash] 1

            # Eventually the field will be expired and the key will be deleted
            wait_for_condition 40 10 { [r hget myhash field1] == "" } else { fail "`field1` should be expired" }
            wait_for_condition 40 10 { [r exists myhash] == 0 } else { fail "db should be empty" }
        } {} {singledb:skip}

        test "Test COPY hash with fields to be expired ($type)" {
            r flushall
            r hset h1 f1 v1 f2 v2
            r hset h2 f1 v1 f2 v2 f3 v3 f4 v4 f5 v5 f6 v6 f7 v7 f8 v8 f9 v9 f10 v10 f11 v11 f12 v12 f13 v13 f14 v14 f15 v15 f16 v16 f17 v17 f18 v18
            r hpexpire h1 100 NX FIELDS 1 f1
            r hpexpire h2 100 NX FIELDS 18 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18
            r COPY h1 h1copy
            r COPY h2 h2copy
            assert_equal [r hget h1 f1] "v1"
            assert_equal [r hget h1copy f1] "v1"
            assert_equal [r exists h2] 1
            assert_equal [r exists h2copy] 1
            after 105

            # Verify lazy expire of field in h1 and its copy
            assert_equal [r hget h1 f1] ""
            assert_equal [r hget h1copy f1] ""

            # Verify lazy expire of field in h2 and its copy. Verify the key deleted as well.
            wait_for_condition 40 10 { [r exists h2] == 0 } else { fail "`h2` should be expired" }
            wait_for_condition 40 10 { [r exists h2copy] == 0 } else { fail "`h2copy` should be expired" }

        } {} {singledb:skip}

        test "Test COPY hash that had HFEs but not during the copy ($type)" {
            r del h1
            r hset h1 f1 v1 f2 v2
            r hpexpire h1 1 FIELDS 1 f1
            after 20
            r COPY h1 h1_copy
            assert_equal [r exists h1] 1
            assert_equal [r exists h1_copy] 1
            assert_equal [r hgetall h1_copy] {f2 v2}
            r hpexpire h1_copy 1 FIELDS 1 f2
            # Only active expire will delete the key
            wait_for_condition 30 10 { [r exists h1_copy] == 0 } else { fail "`h1_copy` should be expired" }
        }

        test "Test SWAPDB hash-fields to be expired ($type)" {
            r select 9
            r flushall
            r hset myhash field1 value1
            r hpexpire myhash 50 NX FIELDS 1 field1

            r swapdb 9 10

            # Verify the key and its field doesn't exist in the source DB
            assert_equal [r exists myhash] 0
            assert_equal [r dbsize] 0

            # Verify the key and its field exists in the target DB
            r select 10
            assert_equal [r hget myhash field1] "value1"
            assert_equal [r dbsize] 1

            # Eventually the field will be expired and the key will be deleted
            wait_for_condition 20 10 { [r exists myhash] == 0 } else { fail "'myhash' should be expired" }
        } {} {singledb:skip}

        test "Test SWAPDB hash that had HFEs but not during the swap ($type)" {
            r select 9
            r flushall
            r hset myhash f1 v1 f2 v2
            r hpexpire myhash 1 NX FIELDS 1 f1
            after 10

            r swapdb 9 10

            # Verify the key and its field doesn't exist in the source DB
            assert_equal [r exists myhash] 0
            assert_equal [r dbsize] 0

            # Verify the key and its field exists in the target DB
            r select 10
            assert_equal [r hgetall myhash] {f2 v2}
            assert_equal [r dbsize] 1
            r hpexpire myhash 1 NX FIELDS 1 f2

            # Eventually the field will be expired and the key will be deleted
            wait_for_condition 20 10 { [r exists myhash] == 0 } else { fail "'myhash' should be expired" }
        } {} {singledb:skip}

        test "HMGET - returns empty entries if fields or hash expired ($type)" {
            r debug set-active-expire 0
            r del h1 h2
            r hset h1 f1 v1 f2 v2 f3 v3
            r hset h2 f1 v1 f2 v2 f3 v3
            r hpexpire h1 10000000 NX FIELDS 1 f1
            r hpexpire h1 1 NX FIELDS 2 f2 f3
            r hpexpire h2 1 NX FIELDS 3 f1 f2 f3
            after 5
            assert_equal [r hmget h1 f1 f2 f3] {v1 {} {}}
            assert_equal [r hmget h2 f1 f2 f3] {{} {} {}}
            r debug set-active-expire 1
        }

        test "HPERSIST - Returns array if the key does not exist ($type)" {
            r del myhash
            assert_equal [r HPERSIST myhash FIELDS 1 a] [list $P_NO_FIELD]
            assert_equal [r HPERSIST myhash FIELDS 2 a b] [list $P_NO_FIELD $P_NO_FIELD]
        }

        test "HPERSIST - input validation ($type)" {
            # HPERSIST key <num-fields> <field [field ...]>
            r del myhash
            r hset myhash f1 v1 f2 v2
            r hexpire myhash 1000 NX FIELDS 1 f1
            assert_error {*wrong number of arguments*} {r hpersist myhash}
            assert_error {*wrong number of arguments*} {r hpersist myhash FIELDS 1}
            assert_equal [r hpersist myhash FIELDS 2 f1 not-exists-field] "$P_OK $P_NO_FIELD"
            assert_equal [r hpersist myhash FIELDS 1 f2] "$P_NO_EXPIRY"
            # <count> not match with actual number of fields
            assert_error {*parameter must match the number*} {r hpersist myhash FIELDS 2 f1 f2 f3}
            assert_error {*parameter must match the number*} {r hpersist myhash FIELDS 4 f1 f2 f3}
        }

        test "HPERSIST - verify fields with TTL are persisted ($type)" {
            r del myhash
            r hset myhash f1 v1 f2 v2
            r hexpire myhash 20 NX FIELDS 2 f1 f2
            r hpersist myhash FIELDS 2 f1 f2
            after 25
            assert_equal [r hget myhash f1] "v1"
            assert_equal [r hget myhash f2] "v2"
            assert_equal [r HTTL myhash FIELDS 2 f1 f2] "$T_NO_EXPIRY $T_NO_EXPIRY"
        }

        test "HTTL/HPERSIST - Test expiry commands with non-volatile hash ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r httl myhash FIELDS 1 field1] $T_NO_EXPIRY
            assert_equal [r httl myhash FIELDS 1 fieldnonexist] $E_NO_FIELD

            assert_equal [r hpersist myhash FIELDS 1 field1] $P_NO_EXPIRY
            assert_equal [r hpersist myhash FIELDS 1 fieldnonexist] $P_NO_FIELD
        }

        test {DUMP / RESTORE are able to serialize / unserialize a hash} {
            r config set sanitize-dump-payload yes
            r del myhash
            r hmset myhash a 1 b 2 c 3
            r hexpireat myhash 2524600800 fields 1 a
            r hexpireat myhash 2524600801 fields 1 b
            set encoded [r dump myhash]
            r del myhash
            r restore myhash 0 $encoded
            assert_equal [lsort [r hgetall myhash]] "1 2 3 a b c"
            assert_equal [r hexpiretime myhash FIELDS 3 a b c] {2524600800 2524600801 -1}
        }

        test {RESTORE hash that had in the past HFEs but not during the dump} {
            r config set sanitize-dump-payload yes
            r del myhash
            r hmset myhash a 1 b 2 c 3
            r hpexpire myhash 1 fields 1 a
            after 10
            set encoded [r dump myhash]
            r del myhash
            r restore myhash 0 $encoded
            assert_equal [lsort [r hgetall myhash]] "2 3 b c"
            r hpexpire myhash 1 fields 2 b c
            wait_for_condition 30 10 { [r exists myhash] == 0 } else { fail "`myhash` should be expired" }
        }

        test {DUMP / RESTORE are able to serialize / unserialize a hash with TTL 0 for all fields} {
            r config set sanitize-dump-payload yes
            r del myhash
            r hmset myhash a 1 b 2 c 3
            r hexpire myhash 9999999 fields 1 a ;# make all TTLs of fields to 0
            r hpersist myhash fields 1 a
            assert_encoding $type myhash
            set encoded [r dump myhash]
            r del myhash
            r restore myhash 0 $encoded
            assert_equal [lsort [r hgetall myhash]] "1 2 3 a b c"
            assert_equal [r hexpiretime myhash FIELDS 3 a b c] {-1 -1 -1}
        }

        test {HINCRBY - discards pending expired field and reset its value} {
            r debug set-active-expire 0
            r del h1 h2
            r hset h1 f1 10 f2 2
            r hset h2 f1 10
            assert_equal [r HINCRBY h1 f1 2] 12
            assert_equal [r HINCRBY h2 f1 2] 12
            r HPEXPIRE h1 10 FIELDS 1 f1
            r HPEXPIRE h2 10 FIELDS 1 f1
            after 15
            assert_equal [r HINCRBY h1 f1 1] 1
            assert_equal [r HINCRBY h2 f1 1] 1
            r debug set-active-expire 1
        }

        test {HINCRBY - preserve expiration time of the field} {
            r del h1
            r hset h1 f1 10
            r hpexpire h1 20 FIELDS 1 f1
            assert_equal [r HINCRBY h1 f1 2] 12
            assert_range [r HPTTL h1 FIELDS 1 f1] 1 20
        }


        test {HINCRBYFLOAT - discards pending expired field and reset its value} {
            r debug set-active-expire 0
            r del h1 h2
            r hset h1 f1 10 f2 2
            r hset h2 f1 10
            assert_equal [r HINCRBYFLOAT h1 f1 2] 12
            assert_equal [r HINCRBYFLOAT h2 f1 2] 12
            r HPEXPIRE h1 10 FIELDS 1 f1
            r HPEXPIRE h2 10 FIELDS 1 f1
            after 15
            assert_equal [r HINCRBYFLOAT h1 f1 1] 1
            assert_equal [r HINCRBYFLOAT h2 f1 1] 1
            r debug set-active-expire 1
        }

        test {HINCRBYFLOAT - preserve expiration time of the field} {
            r del h1
            r hset h1 f1 10
            r hpexpire h1 20 FIELDS 1 f1
            assert_equal [r HINCRBYFLOAT h1 f1 2.5] 12.5
            assert_range [r HPTTL h1 FIELDS 1 f1] 1 20
        }

        test "HGETDEL - delete field with ttl ($type)" {
            r debug set-active-expire 0
            r del h1

            # Test deleting only field in a hash. Due to lazy expiry,
            # reply will be null and the field and the key will be deleted.
            r hsetex h1 PX 5 FIELDS 1 f1 10
            after 15
            assert_equal [r hgetdel h1 fields 1 f1] "{}"
            assert_equal [r exists h1]  0

            # Test deleting one field among many. f2 will lazily expire
            r hsetex h1 FIELDS 3 f1 10 f2 20 f3 value3
            r hpexpire h1 5 FIELDS 1 f2
            after 15
            assert_equal [r hgetdel h1 fields 2 f2 f3] "{} value3"
            assert_equal [lsort [r hgetall h1]] [lsort "f1 10"]

            # Try to delete the last field, along with non-existing fields
            assert_equal [r hgetdel h1 fields 4 f1 f2 f3 f4] "10 {} {} {}"
            r debug set-active-expire 1
        }

        test "HGETEX - input validation ($type)" {
            r del h1
            assert_error "*wrong number of arguments*" {r HGETEX}
            assert_error "*wrong number of arguments*" {r HGETEX h1}
            assert_error "*wrong number of arguments*" {r HGETEX h1 FIELDS}
            assert_error "*wrong number of arguments*" {r HGETEX h1 FIELDS 0}
            assert_error "*wrong number of arguments*" {r HGETEX h1 FIELDS 1}
            assert_error "*argument FIELDS is missing*" {r HGETEX h1 XFIELDX 1 a}
            assert_error "*argument FIELDS is missing*" {r HGETEX h1 PXAT 1 1}
            assert_error "*argument FIELDS is missing*" {r HGETEX h1 PERSIST 1 FIELDS 1 a}
            assert_error "*must match the number of arguments*" {r HGETEX h1 FIELDS 2 a}
            assert_error "*Number of fields must be a positive integer*" {r HGETEX h1 FIELDS 0 a}
            assert_error "*Number of fields must be a positive integer*" {r HGETEX h1 FIELDS -1 a}
            assert_error "*Number of fields must be a positive integer*" {r HGETEX h1 FIELDS 9223372036854775808 a}
        }

        test "HGETEX - input validation (expire time) ($type)" {
            assert_error "*value is not an integer or out of range*" {r HGETEX h1 EX bla FIELDS 1 a}
            assert_error "*value is not an integer or out of range*" {r HGETEX h1 EX 9223372036854775808 FIELDS 1 a}
            assert_error "*value is not an integer or out of range*" {r HGETEX h1 EXAT 9223372036854775808 FIELDS 1 a}
            assert_error "*invalid expire time, must be >= 0*" {r HGETEX h1 PX -1 FIELDS 1 a}
            assert_error "*invalid expire time, must be >= 0*" {r HGETEX h1 PXAT -1 FIELDS 1 a}
            assert_error "*invalid expire time*" {r HGETEX h1 EX -1 FIELDS 1 a}
            assert_error "*invalid expire time*" {r HGETEX h1 EX [expr (1<<48)] FIELDS 1 a}
            assert_error "*invalid expire time*" {r HGETEX h1 EX [expr (1<<46) - [clock seconds] + 100 ] FIELDS 1 a}
            assert_error "*invalid expire time*" {r HGETEX h1 EXAT [expr (1<<46) + 100 ] FIELDS 1 a}
            assert_error "*invalid expire time*" {r HGETEX h1 PX [expr (1<<46) - [clock milliseconds] + 100 ] FIELDS 1 a}
            assert_error "*invalid expire time*" {r HGETEX h1 PXAT [expr (1<<46) + 100 ] FIELDS 1 a}
        }

        test "HGETEX - get without setting ttl ($type)" {
            r del h1
            r hset h1 a 1 b 2 c strval
            assert_equal [r hgetex h1 fields 1 a] "1"
            assert_equal [r hgetex h1 fields 2 a b] "1 2"
            assert_equal [r hgetex h1 fields 3 a b c] "1 2 strval"
            assert_equal [r HTTL h1 FIELDS 3 a b c] "$T_NO_EXPIRY $T_NO_EXPIRY $T_NO_EXPIRY"
        }

        test "HGETEX - get and set the ttl ($type)" {
            r del h1
            r hset h1 a 1 b 2 c strval
            assert_equal [r hgetex h1 EX 10000 fields 1 a] "1"
            assert_range [r HTTL h1 FIELDS 1 a] 9000 10000
            assert_equal [r hgetex h1 EX 10000 fields 1 c] "strval"
            assert_range [r HTTL h1 FIELDS 1 c] 9000 10000
        }

        test "HGETEX - Test 'EX' flag ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hgetex myhash EX 1000 FIELDS 1 field1] [list "value1"]
            assert_range [r httl myhash FIELDS 1 field1] 1 1000
        }

        test "HGETEX - Test 'EXAT' flag ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hgetex myhash EXAT [expr [clock seconds] + 10] FIELDS 1 field2] [list "value2"]
            assert_range [r httl myhash FIELDS 1 field2] 5 10
        }

        test "HGETEX - Test 'PX' flag ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hgetex myhash PX 1000000 FIELDS 1 field3] [list "value3"]
            assert_range [r httl myhash FIELDS 1 field3] 900 1000
        }

        test "HGETEX - Test 'PXAT' flag ($type)" {
            r del myhash
            r hset myhash field1 value1 field2 value2 field3 value3
            assert_equal [r hgetex myhash PXAT [expr [clock milliseconds] + 10000] FIELDS 1 field3] [list "value3"]
            assert_range [r httl myhash FIELDS 1 field3] 5 10
        }

        test "HGETEX - Test 'PERSIST' flag ($type)" {
            r del myhash
            r debug set-active-expire 0

            r hsetex myhash PX 5000 FIELDS 3 f1 v1 f2 v2 f3 v3
            assert_not_equal [r httl myhash FIELDS 1 f1] "$T_NO_EXPIRY"
            assert_not_equal [r httl myhash FIELDS 1 f2] "$T_NO_EXPIRY"
            assert_not_equal [r httl myhash FIELDS 1 f3] "$T_NO_EXPIRY"

            # Persist f1 and verify it does not have TTL anymore
            assert_equal [r hgetex myhash PERSIST FIELDS 1 f1] "v1"
            assert_equal [r httl myhash FIELDS 1 f1] "$T_NO_EXPIRY"

            # Persist rest of the fields
            assert_equal [r hgetex myhash PERSIST FIELDS 2 f2 f3] "v2 v3"
            assert_equal [r httl myhash FIELDS 2 f2 f3]  "$T_NO_EXPIRY $T_NO_EXPIRY"

            # Redo the operation. It should be noop as fields are persisted already.
            assert_equal [r hgetex myhash PERSIST FIELDS 2 f2 f3] "v2 v3"
            assert_equal [r httl myhash FIELDS 2 f2 f3]  "$T_NO_EXPIRY $T_NO_EXPIRY"

            # Final sanity, fields exist and have no attached ttl.
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v1 f2 v2 f3 v3"]
            assert_equal [r httl myhash FIELDS 3 f1 f2 f3]  "$T_NO_EXPIRY $T_NO_EXPIRY $T_NO_EXPIRY"
            r debug set-active-expire 1
        }

        test "HGETEX - Test setting ttl in the past will delete the key ($type)" {
            r del myhash
            r hset myhash f1 v1 f2 v2 f3 v3

            # hgetex without setting ttl
            assert_equal [lsort [r hgetex myhash fields 3 f1 f2 f3]] [lsort "v1 v2 v3"]
            assert_equal [r httl myhash FIELDS 3 f1 f2 f3] "$T_NO_EXPIRY $T_NO_EXPIRY $T_NO_EXPIRY"

            # set an expired ttl and verify the key is deleted
            r hgetex myhash PXAT 1 fields 3 f1 f2 f3
            assert_equal [r exists myhash] 0
        }

        test "HGETEX - Test active expiry ($type)" {
            r del myhash
            r debug set-active-expire 0

            r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5
            assert_equal [lsort [r hgetex myhash PXAT 1 FIELDS 5 f1 f2 f3 f4 f5]] [lsort "v1 v2 v3 v4 v5"]

            r debug set-active-expire 1
            wait_for_condition 50 20 { [r EXISTS myhash] == 0 } else { fail "'myhash' should be expired" }
        }

        test "HGETEX - A field with TTL overridden with another value (TTL discarded) ($type)" {
            r del myhash
            r hset myhash f1 v1 f2 v2 f3 v3
            r hgetex myhash PX 10000 FIELDS 1 f1
            r hgetex myhash EX 100 FIELDS 1 f2

            # f2 ttl will be discarded
            r hset myhash f2 v22
            assert_equal [r hget myhash f2] "v22"
            assert_equal [r httl myhash FIELDS 2 f2 f3] "$T_NO_EXPIRY $T_NO_EXPIRY"

            # Other field is not affected (still has TTL)
            assert_not_equal [r httl myhash FIELDS 1 f1] "$T_NO_EXPIRY"
        }

        test "HGETEX - Test with lazy expiry ($type)" {
            r del myhash
            r debug set-active-expire 0

            r hsetex myhash PX 1 FIELDS 2 f1 v1 f2 v2
            after 5
            assert_equal [r hgetex myhash FIELDS 2 f1 f2] "{} {}"
            assert_equal [r exists myhash] 0

            r debug set-active-expire 1
        }

        test "HSETEX - input validation ($type)" {
            assert_error {*wrong number of arguments*} {r hsetex myhash}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields 1}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields 2 a b}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields 2 a b c}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields 2 a b c d e}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields 3 a b c d}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields 3 a b c d e}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields 3 a b c d e f g}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields 3 a b}
            assert_error {*wrong number of arguments*} {r hsetex myhash fields 1 a b unknown}
            assert_error {*unknown argument*} {r hsetex myhash nx fields 1 a b}
            assert_error {*unknown argument*} {r hsetex myhash 1 fields 1 a b}

            # Only one of FNX or FXX
            assert_error {*Only one of FXX or FNX arguments *} {r hsetex myhash fxx fxx EX 100 fields 1 a b}
            assert_error {*Only one of FXX or FNX arguments *} {r hsetex myhash fxx fnx EX 100 fields 1 a b}
            assert_error {*Only one of FXX or FNX arguments *} {r hsetex myhash fnx fxx EX 100 fields 1 a b}
            assert_error {*Only one of FXX or FNX arguments *} {r hsetex myhash fnx fnx EX 100 fields 1 a b}
            assert_error {*Only one of FXX or FNX arguments *} {r hsetex myhash fxx fnx fxx EX 100 fields 1 a b}
            assert_error {*Only one of FXX or FNX arguments *} {r hsetex myhash fnx fxx fnx EX 100 fields 1 a b}

            # Only one of EX, PX, EXAT, PXAT or KEEPTTL can be specified
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash EX 100 PX 1000 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash EX 100 EXAT 100 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash EXAT 100 EX 1000 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash EXAT 100 PX 1000 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash PX 100 EXAT 100 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash PX 100 PXAT 100 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash PXAT 100 EX 100 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash PXAT 100 EXAT 100 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash EX 100 KEEPTTL fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash KEEPTTL EX 100 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash EX 100 EX 100 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash EXAT 100 EXAT 100 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash PX 10 PX 10 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash PXAT 10 PXAT 10 fields 1 a b}
            assert_error {*Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments*} {r hsetex myhash KEEPTTL KEEPTTL fields 1 a b}

            # missing expire time
            assert_error {*not an integer or out of range*} {r hsetex myhash ex fields 1 a b}
            assert_error {*not an integer or out of range*} {r hsetex myhash px fields 1 a b}
            assert_error {*not an integer or out of range*} {r hsetex myhash exat fields 1 a b}
            assert_error {*not an integer or out of range*} {r hsetex myhash pxat fields 1 a b}

            # expire time more than 2 ^ 48
            assert_error {*invalid expire time*} {r hsetex myhash EXAT [expr (1<<48)] 1 a b}
            assert_error {*invalid expire time*} {r hsetex myhash PXAT [expr (1<<48)] 1 a b}
            assert_error {*invalid expire time*} {r hsetex myhash EX [expr (1<<48) - [clock seconds] + 1000 ] 1 a b}
            assert_error {*invalid expire time*} {r hsetex myhash PX [expr (1<<48) - [clock milliseconds] + 1000 ] 1 a b}

            # invalid expire time
            assert_error {*invalid expire time*} {r hsetex myhash EXAT -1 1 a b}
            assert_error {*not an integer or out of range*} {r hsetex myhash EXAT 9223372036854775808 1 a b}
            assert_error {*not an integer or out of range*} {r hsetex myhash EXAT x 1 a b}

            # invalid numfields arg
            assert_error {*invalid number of fields*} {r hsetex myhash fields x a b}
            assert_error {*invalid number of fields*} {r hsetex myhash fields 9223372036854775808 a b}
            assert_error {*invalid number of fields*} {r hsetex myhash fields 0 a b}
            assert_error {*invalid number of fields*} {r hsetex myhash fields -1 a b}
        }
        
        test "HSETEX - Basic test ($type)" {
            r del myhash

            # set field
            assert_equal [r hsetex myhash FIELDS 1 f1 v1] 1
            assert_equal [r hget myhash f1] "v1"

            # override
            assert_equal [r hsetex myhash FIELDS 1 f1 v11] 1
            assert_equal [r hget myhash f1] "v11"

            # set multiple
            assert_equal [r hsetex myhash FIELDS 2 f1 v1 f2 v2] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v1 f2 v2"]
            assert_equal [r hsetex myhash FIELDS 3 f1 v111 f2 v222 f3 v333] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v111 f2 v222 f3 v333"]
        }

        test "HSETEX - Test FXX flag ($type)" {
            r del myhash

            # Key is empty, command fails due to FXX
            assert_equal [r hsetex myhash FXX FIELDS 2 f1 v1 f2 v2] 0
            # Verify it did not leave the key empty
            assert_equal [r exists myhash] 0

            # Command fails and no change on fields
            r hset myhash f1 v1
            assert_equal [r hsetex myhash FXX FIELDS 2 f1 v1 f2 v2] 0
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v1"]

            # Command executed successfully
            assert_equal [r hsetex myhash FXX FIELDS 1 f1 v11] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v11"]

            # Try with multiple fields
            r hset myhash f2 v2
            assert_equal [r hsetex myhash FXX FIELDS 2 f1 v111 f2 v222] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v111 f2 v222"]

            # Try with expiry
            assert_equal [r hsetex myhash FXX EX 100 FIELDS 2 f1 v1 f2 v2] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v1 f2 v2"]
            assert_range [r httl myhash FIELDS 1 f1] 80 100
            assert_range [r httl myhash FIELDS 1 f2] 80 100

            # Try with expiry, FXX arg comes after TTL
            assert_equal [r hsetex myhash PX 5000 FXX FIELDS 2 f1 v1 f2 v2] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v1 f2 v2"]
            assert_range [r hpttl myhash FIELDS 1 f1] 4500 5000
            assert_range [r hpttl myhash FIELDS 1 f2] 4500 5000
        }

        test "HSETEX - Test FXX flag with lazy expire ($type)" {
            r del myhash
            r debug set-active-expire 0

            r hsetex myhash PX 10 FIELDS 1 f1 v1
            after 15
            assert_equal [r hsetex myhash FXX FIELDS 1 f1 v11] 0
            assert_equal [r exists myhash] 0
            r debug set-active-expire 1
        }

        test "HSETEX - Test FNX flag ($type)" {
            r del myhash

            # Command successful on an empty key
            assert_equal [r hsetex myhash FNX FIELDS 1 f1 v1] 1

            # Command fails and no change on fields
            assert_equal [r hsetex myhash FNX FIELDS 2 f1 v1 f2 v2] 0
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v1"]

            # Command executed successfully
            assert_equal [r hsetex myhash FNX FIELDS 2 f2 v2 f3 v3] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v1 f2 v2 f3 v3"]
            assert_equal [r hsetex myhash FXX FIELDS 3 f1 v11 f2 v22 f3 v33] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v11 f2 v22 f3 v33"]

            # Try with expiry
            r del myhash
            assert_equal [r hsetex myhash FNX EX 100 FIELDS 2 f1 v1 f2 v2] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v1 f2 v2"]
            assert_range [r httl myhash FIELDS 1 f1] 80 100
            assert_range [r httl myhash FIELDS 1 f2] 80 100

            # Try with expiry, FNX arg comes after TTL
            assert_equal [r hsetex myhash PX 5000 FNX FIELDS 1 f3 v3] 1
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v1 f2 v2 f3 v3"]
            assert_range [r hpttl myhash FIELDS 1 f3] 4500 5000
        }

        test "HSETEX - Test 'EX' flag ($type)" {
            r del myhash
            r hset myhash f1 v1 f2 v2
            assert_equal [r hsetex myhash EX 1000 FIELDS 1 f3 v3 ] 1
            assert_range [r httl myhash FIELDS 1 f3] 900 1000
        }

        test "HSETEX - Test 'EXAT' flag ($type)" {
            r del myhash
            r hset myhash f1 v1 f2 v2
            assert_equal [r hsetex myhash EXAT [expr [clock seconds] + 10] FIELDS 1 f3 v3] 1
            assert_range [r httl myhash FIELDS 1 f3] 5 10
        }

        test "HSETEX - Test 'PX' flag ($type)" {
            r del myhash
            assert_equal [r hsetex myhash PX 1000000 FIELDS 1 f3 v3] 1
            assert_range [r httl myhash FIELDS 1 f3] 990 1000
        }

        test "HSETEX - Test 'PXAT' flag ($type)" {
            r del myhash
            r hset myhash f1 v2 f2 v2 f3 v3
            assert_equal [r hsetex myhash PXAT [expr [clock milliseconds] + 10000] FIELDS 1 f2 v2] 1
            assert_range [r httl myhash FIELDS 1 f2] 5 10
        }

        test "HSETEX - Test 'KEEPTTL' flag ($type)" {
            r del myhash

            r hsetex myhash FIELDS 2 f1 v1 f2 v2
            r hsetex myhash PX 20000 FIELDS 1 f2 v2

            # f1 does not have ttl
            assert_equal [r httl myhash FIELDS 1 f1] "$T_NO_EXPIRY"

            # f2 has ttl
            assert_not_equal [r httl myhash FIELDS 1 f2] "$T_NO_EXPIRY"

            # Validate KEEPTTL preserves the TTL
            assert_equal [r hsetex myhash KEEPTTL FIELDS 1 f2 v22] 1
            assert_equal [r hget myhash f2] "v22"
            assert_not_equal [r httl myhash FIELDS 1 f2] "$T_NO_EXPIRY"

            # Try with multiple fields. First, set fields and TTL
            r hsetex myhash EX 10000 FIELDS 3 f1 v1 f2 v2 f3 v3

            # Update fields with KEEPTTL flag
            r hsetex myhash KEEPTTL FIELDS 3 f1 v111 f2 v222 f3 v333

            # Verify values are set, ttls are untouched
            assert_equal [lsort [r hgetall myhash]] [lsort "f1 v111 f2 v222 f3 v333"]
            assert_range [r httl myhash FIELDS 1 f1] 9000 10000
            assert_range [r httl myhash FIELDS 1 f2] 9000 10000
            assert_range [r httl myhash FIELDS 1 f3] 9000 10000
        }

        test "HSETEX - Test no expiry flag discards TTL ($type)" {
            r del myhash

            r hsetex myhash FIELDS 1 f1 v1
            r hsetex myhash PX 100000 FIELDS 1 f2 v2
            assert_range [r hpttl myhash FIELDS 1 f2] 1 100000

            assert_equal [r hsetex myhash FIELDS 2 f1 v1 f2 v2] 1
            assert_equal [r httl myhash FIELDS 2 f1 f2] "$T_NO_EXPIRY $T_NO_EXPIRY"
        }

        test "HSETEX - Test with active expiry" {
            r del myhash
            r debug set-active-expire 0

            r hsetex myhash PX 10 FIELDS 5 f1 v1 f2 v2 f3 v3 f4 v4 f5 v5
            r debug set-active-expire 1
            wait_for_condition 50 20 { [r EXISTS myhash] == 0 } else { fail "'myhash' should be expired" }
        }

        test "HSETEX - Set time in the past ($type)" {
            r del myhash

            # Try on an empty key
            assert_equal [r hsetex myhash EXAT [expr {[clock seconds] - 1}] FIELDS 2 f1 v1 f2 v2] 1
            assert_equal [r hexists myhash field1] 0

            # Try with existing fields
            r hset myhash fields 2 f1 v1 f2 v2
            assert_equal [r hsetex myhash EXAT [expr {[clock seconds] - 1}] FIELDS 2 f1 v1 f2 v2] 1
            assert_equal [r hexists myhash field1] 0
        }
    }

    test "Statistics - Hashes with HFEs ($type)" {
        r config resetstat
        r flushall

        # hash1: 5 fields, 3 with TTL. subexpiry incr +1
        r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5
        r hpexpire myhash 150 FIELDS 3 f1 f2 f3
        assert_match  [get_stat_subexpiry r] 1

        # hash2: 5 fields, 3 with TTL. subexpiry incr +1
        r hset myhash2 f1 v1 f2 v2 f3 v3 f4 v4 f5 v5
        assert_match  [get_stat_subexpiry r] 1
        r hpexpire myhash2 100 FIELDS 3 f1 f2 f3
        assert_match  [get_stat_subexpiry r] 2

        # hash3: 2 fields, 1 with TTL. HDEL field with TTL. subexpiry decr -1
        r hset myhash3 f1 v1 f2 v2
        r hpexpire myhash3 100 FIELDS 1 f2
        assert_match  [get_stat_subexpiry r] 3
        r hdel myhash3 f2
        assert_match  [get_stat_subexpiry r] 2

        # hash4: 2 fields, 1 with TTL. HGETDEL field with TTL. subexpiry decr -1
        r hset myhash4 f1 v1 f2 v2
        r hpexpire myhash4 100 FIELDS 1 f2
        assert_match [get_stat_subexpiry r] 3
        r hgetdel myhash4 FIELDS 1 f2
        assert_match [get_stat_subexpiry r] 2

        # Expired fields of hash1 and hash2. subexpiry decr -2
        wait_for_condition 50 50 {
                [get_stat_subexpiry r] == 0
        } else {
                fail "Hash field expiry statistics failed"
        }
    }

    test "HFE commands against wrong type" {
        r set wrongtype somevalue
        assert_error "WRONGTYPE Operation against a key*" {r hexpire wrongtype 10 fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r hexpireat wrongtype 10 fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r hpexpire wrongtype 10 fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r hpexpireat wrongtype 10 fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r hexpiretime wrongtype fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r hpexpiretime wrongtype fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r httl wrongtype fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r hpttl wrongtype fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r hpersist wrongtype fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r hgetex wrongtype fields 1 f1}
        assert_error "WRONGTYPE Operation against a key*" {r hsetex wrongtype fields 1 f1 v1}
    }

    r config set hash-max-listpack-entries 512
}

start_server {tags {"external:skip needs:debug"}} {

    # Tests that only applies to listpack

    test "Test listpack memory usage" {
        r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5
        r hpexpire myhash 5 FIELDS 2 f2 f4

        # Just to have code coverage for the new listpack encoding
        r memory usage myhash
    }

    test "Test listpack object encoding" {
        r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5
        r hpexpire myhash 5 FIELDS 2 f2 f4

        # Just to have code coverage for the listpackex encoding
        assert_equal [r object encoding myhash] "listpackex"
    }

    test "Test listpack debug listpack" {
        r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5

        # Just to have code coverage for the listpackex encoding
        r debug listpack myhash
    }

    test "Test listpack converts to ht and passive expiry works" {
        set prev [lindex [r config get hash-max-listpack-entries] 1]
        r config set hash-max-listpack-entries 10
        r debug set-active-expire 0

        r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5
        r hpexpire myhash 5 FIELDS 2 f2 f4

        for {set i 6} {$i < 11} {incr i} {
            r hset myhash f$i v$i
        }
        after 50
        assert_equal [lsort [r hgetall myhash]] [lsort "f1 f3 f5 f6 f7 f8 f9 f10 v1 v3 v5 v6 v7 v8 v9 v10"]
        r config set hash-max-listpack-entries $prev
        r debug set-active-expire 1
    }

    test "Test listpack converts to ht and active expiry works" {
        r del myhash
        r debug set-active-expire 0

        r hset myhash f1 v1 f2 v2 f3 v3 f4 v4 f5 v5
        r hpexpire myhash 10 FIELDS 1 f1

        for {set i 0} {$i < 2048} {incr i} {
            r hset myhash f$i v$i
        }

        for {set i 0} {$i < 2048} {incr i} {
            r hpexpire myhash 10 FIELDS 1 f$i
        }

        r debug set-active-expire 1
        wait_for_condition 50 20 { [r EXISTS myhash] == 0 } else { fail "'myhash' should be expired" }
    }

    test "Test listpack converts to ht and active expiry works" {
        r del myhash
        r debug set-active-expire 0

        # Check expiry works after listpack converts to ht
        for {set i 0} {$i < 1024} {incr i} {
            r hset myhash f1_$i v1_$i f2_$i v2_$i f3_$i v3_$i f4_$i v4_$i
            r hpexpire myhash 10 FIELDS 4 f1_$i f2_$i f3_$i f4_$i
        }

        assert_encoding hashtable myhash
        assert_equal [r hlen myhash] 4096

        r debug set-active-expire 1
        wait_for_condition 50 20 { [r EXISTS myhash] == 0 } else { fail "'myhash' should be expired" }
    }

    test "HPERSIST/HEXPIRE - Test listpack with large values" {
        r del myhash

        # Test with larger values to verify we successfully move fields in
        # listpack when we are ordering according to TTL. This config change
        # will make code to use temporary heap allocation when moving fields.
        # See listpackExUpdateExpiry() for details.
        r config set hash-max-listpack-value 2048

        set payload1 [string repeat v3 1024]
        set payload2 [string repeat v1 1024]

        # Test with single item list
        r hset myhash f1 $payload1
        r hexpire myhash 2000 FIELDS 1 f1
        assert_equal [r hget myhash f1] $payload1
        r del myhash

        # Test with multiple items
        r hset myhash f1 $payload2 f2 v2 f3 $payload1 f4 v4
        r hexpire myhash 100000 FIELDS 1 f3
        r hpersist myhash FIELDS 1 f3
        assert_equal [r hpersist myhash FIELDS 1 f3] $P_NO_EXPIRY

        r hpexpire myhash 10 FIELDS 1 f1
        after 20
        assert_equal [lsort [r hgetall myhash]] [lsort "f2 f3 f4 v2 $payload1 v4"]

        r config set hash-max-listpack-value 64
    }
}

start_server {tags {"external:skip needs:debug"}} {
    foreach type {listpack ht} {
        if {$type eq "ht"} {
            r config set hash-max-listpack-entries 0
        } else {
            r config set hash-max-listpack-entries 512
        }

        test "Test Command propagated to replica as expected ($type)" {
            start_server {overrides {appendonly {yes} appendfsync always} tags {external:skip}} {

                set aof [get_last_incr_aof_path r]
                r debug set-active-expire 0 ;# Prevent fields from being expired during data preparation

                # Time is in the past so it should propagate HDELs to replica
                # and delete the fields
                r hset h0 x1 y1 x2 y2
                r hexpireat h0 1 fields 3 x1 x2 non_exists_field

                r hset h1 f1 v1 f2 v2

                # Next command won't be propagated to replica
                # because XX condition not met or field not exists
                r hexpire h1 10 XX FIELDS 3 f1 f2 non_exists_field

                r hpexpire h1 20 FIELDS 1 f1

                # Next command will be propagate with only field 'f2'
                # because NX condition not met for field 'f1'
                r hpexpire h1 30 NX FIELDS 2 f1 f2

                # Non exists field should be ignored
                r hpexpire h1 30 FIELDS 1 non_exists_field
                r hset h2 f1 v1 f2 v2 f3 v3 f4 v4
                r hpexpire h2 40 FIELDS 2 f1 non_exists_field
                r hpexpire h2 50 FIELDS 1 f2
                r hpexpireat h2 [expr [clock seconds]*1000+100000] LT FIELDS 1 f3
                r hexpireat h2 [expr [clock seconds]+10] NX FIELDS 1 f4

                r debug set-active-expire 1
                wait_for_condition 50 100 {
                    [r hlen h2] eq 2
                } else {
                    fail "Field f2 of hash h2 wasn't deleted"
                }

                # HSETEX
                r hsetex h3 FIELDS 1 f1 v1
                r hsetex h3 FXX FIELDS 1 f1 v11
                r hsetex h3 FNX FIELDS 1 f2 v22
                r hsetex h3 KEEPTTL FIELDS 1 f2 v22

                # Next one will fail due to FNX arg and it won't be replicated
                r hsetex h3 FNX FIELDS 2 f1 v1 f2 v2

                # Commands with EX/PX/PXAT/EXAT will be replicated as PXAT
                r hsetex h3 EX 10000 FIELDS 1 f1 v111
                r hsetex h3 PX 10000 FIELDS 1 f1 v111
                r hsetex h3 PXAT [expr [clock milliseconds]+100000] FIELDS 1 f1 v111
                r hsetex h3 EXAT [expr [clock seconds]+100000] FIELDS 1 f1 v111

                # Following commands will set and then delete the fields because
                # of TTL in the past. HDELs will be propagated.
                r hsetex h3 PX 0 FIELDS 1 f1 v111
                r hsetex h3 PX 0 FIELDS 3 f1 v2 f2 v2 f3 v3

                # HGETEX
                r hsetex h4 FIELDS 3 f1 v1 f2 v2 f3 v3
                # No change on expiry, it won't be replicated.
                r hgetex h4 FIELDS 1 f1

                # Commands with EX/PX/PXAT/EXAT will be replicated as
                # HPEXPIREAT command.
                r hgetex h4 EX 10000 FIELDS 1 f1
                r hgetex h4 PX 10000 FIELDS 1 f1
                r hgetex h4 PXAT [expr [clock milliseconds]+100000] FIELDS 1 f1
                r hgetex h4 EXAT [expr [clock seconds]+100000] FIELDS 1 f1

                # Following commands will delete the fields because of TTL in
                # the past. HDELs will be propagated.
                r hgetex h4 PX 0 FIELDS 1 f1
                # HDELs will be propagated for f2 and f3 as only those exist.
                r hgetex h4 PX 0 FIELDS 3 f1 f2 f3

                # HGETEX with PERSIST flag will be replicated as HPERSIST
                r hsetex h4 EX 1000 FIELDS 1 f4 v4
                r hgetex h4 PERSIST FIELDS 1 f4

                # Nothing will be replicated as f4 is persisted already.
                r hgetex h4 PERSIST FIELDS 1 f4

                # Replicated as hdel
                r hgetdel h4 FIELDS 1 f4

                # Assert that each TTL-related command are persisted with absolute timestamps in AOF
                assert_aof_content $aof {
                    {select *}
                    {hset h0 x1 y1 x2 y2}
                    {multi}
                        {hdel h0 x1}
                        {hdel h0 x2}
                    {exec}
                    {hset h1 f1 v1 f2 v2}
                    {hpexpireat h1 * FIELDS 1 f1}
                    {hpexpireat h1 * FIELDS 1 f2}
                    {hset h2 f1 v1 f2 v2 f3 v3 f4 v4}
                    {hpexpireat h2 * FIELDS 1 f1}
                    {hpexpireat h2 * FIELDS 1 f2}
                    {hpexpireat h2 * FIELDS 1 f3}
                    {hpexpireat h2 * FIELDS 1 f4}
                    {hdel h1 f1}
                    {hdel h1 f2}
                    {hdel h2 f1}
                    {hdel h2 f2}
                    {hsetex h3 FIELDS 1 f1 v1}
                    {hsetex h3 FXX FIELDS 1 f1 v11}
                    {hsetex h3 FNX FIELDS 1 f2 v22}
                    {hsetex h3 KEEPTTL FIELDS 1 f2 v22}
                    {hsetex h3 PXAT * 1 f1 v111}
                    {hsetex h3 PXAT * 1 f1 v111}
                    {hsetex h3 PXAT * 1 f1 v111}
                    {hsetex h3 PXAT * 1 f1 v111}
                    {hdel h3 f1}
                    {multi}
                        {hdel h3 f1}
                        {hdel h3 f2}
                        {hdel h3 f3}
                    {exec}
                    {hsetex h4 FIELDS 3 f1 v1 f2 v2 f3 v3}
                    {hpexpireat h4 * FIELDS 1 f1}
                    {hpexpireat h4 * FIELDS 1 f1}
                    {hpexpireat h4 * FIELDS 1 f1}
                    {hpexpireat h4 * FIELDS 1 f1}
                    {hdel h4 f1}
                    {multi}
                        {hdel h4 f2}
                        {hdel h4 f3}
                    {exec}
                    {hsetex h4 PXAT * FIELDS 1 f4 v4}
                    {hpersist h4 FIELDS 1 f4}
                    {hdel h4 f4}
                }
            }
        } {} {needs:debug}

        test "Lazy Expire - fields are lazy deleted and propagated to replicas ($type)" {
            start_server {overrides {appendonly {yes} appendfsync always} tags {external:skip}} {
                r debug set-active-expire 0
                set aof [get_last_incr_aof_path r]

                r del myhash

                r hset myhash f1 v1 f2 v2 f3 v3
                r hpexpire myhash 1 NX FIELDS 3 f1 f2 f3
                after 5

                # Verify that still exists even if all fields are expired
                assert_equal 1 [r EXISTS myhash]

                # Verify that len counts also expired fields
                assert_equal 3 [r HLEN myhash]

                # Trying access to expired field should delete it. Len should be updated
                assert_equal 0 [r hexists myhash f1]
                assert_equal 2 [r HLEN myhash]

                # Trying access another expired field should delete it. Len should be updated
                assert_equal "" [r hget myhash f2]
                assert_equal 1 [r HLEN myhash]

                # Trying access last expired field should delete it. hash shouldn't exists afterward.
                assert_equal 0 [r hstrlen myhash f3]
                assert_equal 0 [r HLEN myhash]
                assert_equal 0 [r EXISTS myhash]

                wait_for_condition 50 100 { [r exists h1] == 0 } else { fail "hash h1 wasn't deleted" }

                # HDEL are propagated as expected
                assert_aof_content $aof {
                    {select *}
                    {hset myhash f1 v1 f2 v2 f3 v3}
                    {hpexpireat myhash * NX FIELDS 3 f1 f2 f3}
                    {hdel myhash f1}
                    {hdel myhash f2}
                    {hdel myhash f3}
                }
                r debug set-active-expire 1
            }
        }

        # Start a new server with empty data and AOF file.
        start_server {overrides {appendonly {yes} appendfsync always} tags {external:skip}} {

            # Based on test at expire.tcl: " All time-to-live(TTL) in commands are propagated as absolute ..."
            test {All TTLs in commands are propagated as absolute timestamp in milliseconds in AOF} {

                set aof [get_last_incr_aof_path r]

                r hset h1 f1 v1 f2 v2 f3 v3 f4 v4 f5 v5 f6 v6
                r hexpireat h1 [expr [clock seconds]+100] NX FIELDS 1 f1
                r hpexpireat h1 [expr [clock seconds]*1000+100000] NX FIELDS 1 f2
                r hpexpire h1 100000 NX FIELDS 3 f3 f4 f5
                r hexpire h1 100000 FIELDS 1 f6

                r hset h2 f1 v1 f2 v2
                r hpexpire h2 1 FIELDS 2 f1 f2
                after 200

                r hsetex h3 EX 100000 FIELDS 2 f1 v1 f2 v2
                r hsetex h3 EXAT [expr [clock seconds] + 1000] FIELDS 2 f1 v1 f2 v2
                r hsetex h3 PX 100000 FIELDS 2 f1 v1 f2 v2
                r hsetex h3 PXAT [expr [clock milliseconds]+100000] FIELDS 2 f1 v1 f2 v2

                r hgetex h3 EX 100000 FIELDS 2 f1 f2
                r hgetex h3 EXAT [expr [clock seconds] + 1000] FIELDS 2 f1 f2
                r hgetex h3 PX 100000 FIELDS 2 f1 f2
                r hgetex h3 PXAT [expr [clock milliseconds]+100000] FIELDS 2 f1 f2

                assert_aof_content $aof {
                    {select *}
                    {hset h1 f1 v1 f2 v2 f3 v3 f4 v4 f5 v5 f6 v6}
                    {hpexpireat h1 * FIELDS 1 f1}
                    {hpexpireat h1 * FIELDS 1 f2}
                    {hpexpireat h1 * NX FIELDS 3 f3 f4 f5}
                    {hpexpireat h1 * FIELDS 1 f6}
                    {hset h2 f1 v1 f2 v2}
                    {hpexpireat h2 * FIELDS 2 f1 f2}
                    {hdel h2 *}
                    {hdel h2 *}
                    {hsetex h3 PXAT * FIELDS 2 f1 v1 f2 v2}
                    {hsetex h3 PXAT * FIELDS 2 f1 v1 f2 v2}
                    {hsetex h3 PXAT * FIELDS 2 f1 v1 f2 v2}
                    {hsetex h3 PXAT * FIELDS 2 f1 v1 f2 v2}
                    {hpexpireat h3 * FIELDS 2 f1 f2}
                    {hpexpireat h3 * FIELDS 2 f1 f2}
                    {hpexpireat h3 * FIELDS 2 f1 f2}
                    {hpexpireat h3 * FIELDS 2 f1 f2}
                }

                array set keyAndFields1 [dumpAllHashes r]
                r debug loadaof
                array set keyAndFields2 [dumpAllHashes r]

                # Assert that absolute TTLs are the same
                assert_equal [array get keyAndFields1] [array get keyAndFields2]

            } {} {needs:debug}
        }

        # Based on test, with same name, at expire.tcl:
        test {All TTL in commands are propagated as absolute timestamp in replication stream} {
            # Make sure that both relative and absolute expire commands are propagated
            # Consider also comment of the test, with same name, at expire.tcl

            r flushall ; # Clean up keyspace to avoid interference by keys from other tests
            set repl [attach_to_replication_stream]

            # HEXPIRE/HPEXPIRE should be translated into HPEXPIREAT
            r hset h1 f1 v1
            r hexpireat h1 [expr [clock seconds]+100] NX FIELDS 1 f1
            r hset h2 f2 v2
            r hpexpireat h2 [expr [clock seconds]*1000+100000] NX FIELDS 1 f2
            r hset h3 f3 v3 f4 v4 f5 v5
            # hpersist does nothing here. Verify it is not propagated.
            r hpersist h3 FIELDS 1 f5
            r hexpire h3 100 FIELDS 3 f3 f4 non_exists_field
            r hpersist h3 FIELDS 1 f3

            assert_replication_stream $repl {
                {select *}
                {hset h1 f1 v1}
                {hpexpireat h1 * NX FIELDS 1 f1}
                {hset h2 f2 v2}
                {hpexpireat h2 * NX FIELDS 1 f2}
                {hset h3 f3 v3 f4 v4 f5 v5}
                {hpexpireat h3 * FIELDS 2 f3 f4}
                {hpersist h3 FIELDS 1 f3}
            }
            close_replication_stream $repl
        } {} {needs:repl}

        test {HRANDFIELD delete expired fields and propagate DELs to replica} {
            r debug set-active-expire 0
            r flushall
            set repl [attach_to_replication_stream]

            # HRANDFIELD delete expired fields and propagate MULTI-EXEC DELs. Reply none.
            r hset h1 f1 v1 f2 v2
            r hpexpire h1 1 FIELDS 2 f1 f2
            after 5
            assert_equal [r hrandfield h1 2] ""

            # HRANDFIELD delete expired field and propagate DEL. Reply non-expired field.
            r hset h2 f1 v1 f2 v2
            r hpexpire h2 1 FIELDS 1 f1
            after 5
            assert_equal [r hrandfield h2 2] "f2"

            # HRANDFIELD delete expired field and propagate DEL. Reply none.
            r hset h3 f1 v1
            r hpexpire h3 1 FIELDS 1 f1
            after 5
            assert_equal [r hrandfield h3 2] ""

            assert_replication_stream $repl {
                {select *}
                {hset h1 f1 v1 f2 v2}
                {hpexpireat h1 * FIELDS 2 f1 f2}
                {multi}
                {hdel h1 *}
                {hdel h1 *}
                {exec}
                {hset h2 f1 v1 f2 v2}
                {hpexpireat h2 * FIELDS 1 f1}
                {hdel h2 f1}
                {hset h3 f1 v1}
                {hpexpireat h3 * FIELDS 1 f1}
                {hdel h3 f1}
            }
            close_replication_stream $repl
            r debug set-active-expire 1
        } {OK} {needs:repl}

        # Start another server to test replication of TTLs
        start_server {tags {needs:repl external:skip}} {
            # Set the outer layer server as primary
            set primary [srv -1 client]
            set primary_host [srv -1 host]
            set primary_port [srv -1 port]
            # Set this inner layer server as replica
            set replica [srv 0 client]

            # Server should have role slave
            $replica replicaof $primary_host $primary_port
            wait_for_condition 50 100 {
                [s 0 role] eq {slave}
            } else {
                fail "Replication not started."
            }

            # Based on test, with same name, at expire.tcl
            test {For all replicated TTL-related commands, absolute expire times are identical on primary and replica} {
                # Apply each TTL-related command to a unique key on primary
                $primary flushall
                $primary hset h1 f v
                $primary hexpireat h1 [expr [clock seconds]+10000] FIELDS 1 f
                $primary hset h2 f v
                $primary hpexpireat h2 [expr [clock milliseconds]+100000] FIELDS 1 f
                $primary hset h3 f v
                $primary hexpire h3 100 NX FIELDS 1 f
                $primary hset h4 f v
                $primary hpexpire h4 100000 NX FIELDS 1 f
                $primary hset h5 f v
                $primary hpexpireat h5 [expr [clock milliseconds]-100000] FIELDS 1 f
                $primary hset h9 f v

                $primary hsetex h10 EX 100000 FIELDS 1 f v
                $primary hsetex h11 EXAT [expr [clock seconds] + 1000] FIELDS 1 f v
                $primary hsetex h12 PX 100000 FIELDS 1 f v
                $primary hsetex h13 PXAT [expr [clock milliseconds]+100000] FIELDS 1 f v
                $primary hsetex h14 PXAT 1 FIELDS 1 f v

                $primary hsetex h15 FIELDS 1 f v
                $primary hgetex h15 EX 100000 FIELDS 1 f
                $primary hsetex h16 FIELDS 1 f v
                $primary hgetex h16 EXAT [expr [clock seconds] + 1000] FIELDS 1 f
                $primary hsetex h17 FIELDS 1 f v
                $primary hgetex h17 PX 100000 FIELDS 1 f
                $primary hsetex h18 FIELDS 1 f v
                $primary hgetex h18 PXAT [expr [clock milliseconds]+100000] FIELDS 1 f
                $primary hsetex h19 FIELDS 1 f v
                $primary hgetex h19 PXAT 1 FIELDS 1 f

                # Wait for replica to get the keys and TTLs
                assert {[$primary wait 1 0] == 1}

                # Verify absolute TTLs are identical on primary and replica for all keys
                # This is because TTLs are always replicated as absolute values
                assert_equal [dumpAllHashes $primary] [dumpAllHashes $replica]
            }
        }

        test "Test HSETEX command replication" {
            r flushall
            set repl [attach_to_replication_stream]

            # Create a field and delete it in a single command due to timestamp
            # being in the past. It will be propagated as HDEL.
            r hsetex h1 PXAT 1 FIELDS 1 f1 v1

            # Following ones will be propagated with PXAT arg
            r hsetex h1 EX 100000 FIELDS 1 f1 v1
            r hsetex h1 EXAT [expr [clock seconds] + 1000] FIELDS 1 f1 v1
            r hsetex h1 PX 100000 FIELDS 1 f1 v1
            r hsetex h1 PXAT [expr [clock milliseconds]+100000] FIELDS 1 f1 v1

            # Propagate with KEEPTTL flag
            r hsetex h1 KEEPTTL FIELDS 1 f1 v1

            # Following commands will fail and won't be propagated
            r hsetex h1 FNX FIELDS 1 f1 v11
            r hsetex h1 FXX FIELDS 1 f2 v2

            # Propagate with FNX and FXX flags
            r hsetex h1 FNX FIELDS 1 f2 v2
            r hsetex h1 FXX FIELDS 1 f2 v22

            assert_replication_stream $repl {
                {select *}
                {hdel h1 f1}
                {hsetex h1 PXAT * FIELDS 1 f1 v1}
                {hsetex h1 PXAT * FIELDS 1 f1 v1}
                {hsetex h1 PXAT * FIELDS 1 f1 v1}
                {hsetex h1 PXAT * FIELDS 1 f1 v1}
                {hsetex h1 KEEPTTL FIELDS 1 f1 v1}
                {hsetex h1 FNX FIELDS 1 f2 v2}
                {hsetex h1 FXX FIELDS 1 f2 v22}
            }
            close_replication_stream $repl
        } {} {needs:repl}

        test "Test HGETEX command replication" {
            r flushall
            r debug set-active-expire 0
            set repl [attach_to_replication_stream]

            # If no fields are found, command won't be replicated
            r hgetex h1 EX 10000 FIELDS 1 f0
            r hgetex h1 PERSIST FIELDS 1 f0

            # Get without setting expiry will not be replicated
            r hsetex h1 FIELDS 1 f0 v0
            r hgetex h1 FIELDS 1 f0

            # Lazy expired field will be replicated as HDEL
            r hsetex h1 PX 10 FIELDS 1 f1 v1
            after 15
            r hgetex h1 EX 1000 FIELDS 1 f1

            # If new TTL is in the past, it will be replicated as HDEL
            r hsetex h1 EX 10000 FIELDS 1 f2 v2
            r hgetex h1 EXAT 1 FIELDS 1 f2

            # A field will expire lazily and other field will be deleted due to
            # TTL is being in the past. It'll be propagated as two HDEL's.
            r hsetex h1 PX 10 FIELDS 1 f3 v3
            after 15
            r hsetex h1 FIELDS 1 f4 v4
            r hgetex h1 EXAT 1 FIELDS 2 f3 f4

            # TTL update, it will be replicated as HPEXPIREAT
            r hsetex h1 FIELDS 1 f5 v5
            r hgetex h1 EX 10000 FIELDS 1 f5

            # If PERSIST flag is used, it will be replicated as HPERSIST
            r hsetex h1 EX 10000 FIELDS 1 f6 v6
            r hgetex h1 PERSIST FIELDS 1 f6

            assert_replication_stream $repl {
                {select *}
                {hsetex h1 FIELDS 1 f0 v0}
                {hsetex h1 PXAT * FIELDS 1 f1 v1}
                {hdel h1 f1}
                {hsetex h1 PXAT * FIELDS 1 f2 v2}
                {hdel h1 f2}
                {hsetex h1 PXAT * FIELDS 1 f3 v3}
                {hsetex h1 FIELDS 1 f4 v4}
                {multi}
                    {hdel h1 f3}
                    {hdel h1 f4}
                {exec}
                {hsetex h1 FIELDS 1 f5 v5}
                {hpexpireat h1 * FIELDS 1 f5}
                {hsetex h1 PXAT * FIELDS 1 f6 v6}
                {hpersist h1 FIELDS 1 f6}
            }
            close_replication_stream $repl
        } {} {needs:repl}
    }
}