azure_iot_operations_protocol 1.0.0

Utilities for using the Azure IoT Operations Protocol over MQTT
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::{collections::HashMap, marker::PhantomData, time::Duration};

use azure_iot_operations_mqtt::{
    aio::cloud_event as aio_cloud_event,
    session::{SessionManagedClient, SessionPubReceiver},
};
use azure_iot_operations_mqtt::{
    control_packet::{PublishProperties, QoS, TopicFilter, TopicName},
    token::AckToken,
};
use bytes::Bytes;
use chrono::{DateTime, Utc};
use tokio::sync::oneshot;
use tokio::time::{Instant, timeout};
use tokio_util::sync::{CancellationToken, DropGuard};

use crate::{
    ProtocolVersion,
    application::{ApplicationContext, ApplicationHybridLogicalClock},
    common::{
        aio_protocol_error::{AIOProtocolError, Value},
        cloud_event as protocol_cloud_event,
        hybrid_logical_clock::{HLCErrorKind, HybridLogicalClock},
        is_invalid_utf8,
        payload_serialize::{
            DeserializationError, FormatIndicator, PayloadSerialize, SerializedPayload,
        },
        topic_processor::{TopicPattern, contains_invalid_char, is_valid_replacement},
        user_properties::{PARTITION_KEY, UserProperty, validate_user_properties},
    },
    rpc_command::{
        DEFAULT_RPC_COMMAND_PROTOCOL_VERSION, DEFAULT_RPC_RESPONSE_CLOUD_EVENT_EVENT_TYPE,
        RPC_COMMAND_PROTOCOL_VERSION, StatusCode,
    },
    supported_protocol_major_versions_to_string,
};

/// Default message expiry interval only for when the message expiry interval is not present
const DEFAULT_MESSAGE_EXPIRY_INTERVAL_SECONDS: u32 = 10;

/// Additional time in seconds to extend cache entry expiration beyond the command expiration time
const CACHE_EXPIRY_BUFFER_SECONDS: u64 = 60;

/// Message for when expiration time is unable to be calculated, internal logic error
const INTERNAL_LOGIC_EXPIRATION_ERROR: &str =
    "Internal logic error, unable to calculate command expiration time";

const SUPPORTED_PROTOCOL_VERSIONS: &[u16] = &[1];

/// Struct to hold response arguments
struct ResponseArguments {
    command_name: String,
    response_topic: TopicName,
    correlation_data: Option<Bytes>,
    status_code: StatusCode,
    status_message: Option<String>,
    is_application_error: bool,
    invalid_property_name: Option<String>,
    invalid_property_value: Option<String>,
    command_expiration_time: Option<Instant>,
    message_expiry_interval: Option<u32>,
    supported_protocol_major_versions: Option<Vec<u16>>,
    request_protocol_version: Option<String>,
    cached_key: Option<CacheKey>,
    cache_lookup_result: CacheLookupResult,
}

/// Command Executor Request struct.
/// Used by the [`Executor`]
///
/// If dropped, executor will send an error response to the invoker
pub struct Request<TReq, TResp>
where
    TReq: PayloadSerialize,
    TResp: PayloadSerialize,
{
    /// Payload of the command request.
    pub payload: TReq,
    /// Content Type of the command request.
    pub content_type: Option<String>,
    /// Format Indicator of the command request.
    pub format_indicator: FormatIndicator,
    /// Custom user data set as custom MQTT User Properties on the request message.
    pub custom_user_data: Vec<(String, String)>,
    /// Timestamp of the command request.
    pub timestamp: Option<HybridLogicalClock>,
    /// If present, contains the client ID of the invoker of the command.
    pub invoker_id: Option<String>,
    /// Resolved static and dynamic topic tokens from the incoming request's topic.
    pub topic_tokens: HashMap<String, String>,
    // Internal fields
    command_name: String,
    response_tx: oneshot::Sender<Response<TResp>>,
    publish_completion_rx: oneshot::Receiver<Result<(), AIOProtocolError>>,
}

impl<TReq, TResp> Request<TReq, TResp>
where
    TReq: PayloadSerialize,
    TResp: PayloadSerialize,
{
    /// Consumes the command request and reports the response to the executor. An attempt is made to
    /// send the response to the invoker.
    ///
    /// Returns Ok(()) on success, otherwise returns [`AIOProtocolError`].
    ///
    /// # Arguments
    /// * `response` - The [`Response`] to send.
    ///
    /// # Errors
    ///
    /// [`AIOProtocolError`] of kind [`Timeout`](crate::common::aio_protocol_error::AIOProtocolErrorKind::Timeout) if the command request
    /// has expired.
    ///
    /// [`AIOProtocolError`] of kind [`ClientError`](crate::common::aio_protocol_error::AIOProtocolErrorKind::ClientError) if the response
    /// acknowledgement returns an error.
    ///
    /// [`AIOProtocolError`] of kind [`Cancellation`](crate::common::aio_protocol_error::AIOProtocolErrorKind::Cancellation) if the
    /// executor is dropped.
    ///
    /// [`AIOProtocolError`] of kind [`InternalLogicError`](crate::common::aio_protocol_error::AIOProtocolErrorKind::InternalLogicError)
    /// if the response publish completion fails. This should not happen.
    pub async fn complete(self, response: Response<TResp>) -> Result<(), AIOProtocolError> {
        // We can ignore the error here. If the receiver of the response is dropped it may be
        // because the executor is shutting down in which case the receive below will fail.
        // If the executor is not shutting down, the receive below will succeed and we'll receive a
        // timeout error since that is the only possible error at this point.
        let _ = self.response_tx.send(response);

        self.publish_completion_rx
            .await
            .map_err(|_| Self::create_cancellation_error(self.command_name))?
    }

    fn create_cancellation_error(command_name: String) -> AIOProtocolError {
        AIOProtocolError::new_cancellation_error(
            false,
            None,
            Some(
                "Command Executor has been shutdown and can no longer respond to commands"
                    .to_string(),
            ),
            Some(command_name),
        )
    }

    /// Check if the command response is no longer expected.
    ///
    /// Returns true if the response is no longer expected, otherwise returns false.
    pub fn is_cancelled(&self) -> bool {
        self.response_tx.is_closed()
    }
}

/// Cloud Event struct derived from the Command Request.
pub type RequestCloudEvent = aio_cloud_event::CloudEvent;
/// Error when parsing a Cloud Event from a Request
pub type RequestCloudEventParseError = aio_cloud_event::CloudEventParseError;

/// Parse a [`RequestCloudEvent`] from a [`Request`].
/// Note that this will return an error if the [`Request`] does not contain the required fields for a [`RequestCloudEvent`].
///
/// # Errors
/// [`RequestCloudEventParseError`] if
/// - the [`Request`] does not contain the required fields for a [`RequestCloudEvent`].
/// - any of the field values are not valid for a [`RequestCloudEvent`].
pub fn cloud_event_from_request<TReq: PayloadSerialize, TResp: PayloadSerialize>(
    request: &Request<TReq, TResp>,
) -> Result<RequestCloudEvent, RequestCloudEventParseError> {
    RequestCloudEvent::try_from((&request.custom_user_data, request.content_type.as_deref()))
}

/// Command Executor Response struct.
/// Used by the [`Executor`]
#[derive(Builder, Clone, Debug)]
#[builder(setter(into), build_fn(validate = "Self::validate"))]
pub struct Response<TResp>
where
    TResp: PayloadSerialize,
{
    /// Payload of the command response.
    #[builder(setter(custom))]
    serialized_payload: SerializedPayload,
    /// Strongly link `Response` with type `TResp`
    #[builder(private)]
    payload_type: PhantomData<TResp>,
    /// Custom user data set as custom MQTT User Properties on the response message.
    /// Used to pass additional metadata to the invoker.
    /// Default is an empty vector.
    #[builder(default)]
    custom_user_data: Vec<(String, String)>,
    /// Cloud event of the response.
    #[builder(default = "None")]
    cloud_event: Option<ResponseCloudEvent>,
}

/// Cloud Event struct used for the Command Response.
///
/// Implements the Cloud Events spec 1.0 for the command executor.
/// See [CloudEvents Spec](https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md).
#[derive(Clone, Debug)]
pub struct ResponseCloudEvent(protocol_cloud_event::CloudEvent);

/// Builder for [`ResponseCloudEvent`].
#[derive(Clone)]
pub struct ResponseCloudEventBuilder(protocol_cloud_event::CloudEventBuilder);

/// Error type for [`ResponseCloudEventBuilder`]
#[derive(Debug)]
#[non_exhaustive]
pub enum ResponseCloudEventBuilderError {
    /// Uninitialized field
    UninitializedField(&'static str),
    /// Custom validation error
    ValidationError(String),
}

impl std::error::Error for ResponseCloudEventBuilderError {}
impl std::fmt::Display for ResponseCloudEventBuilderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ResponseCloudEventBuilderError::UninitializedField(field_name) => {
                write!(f, "Uninitialized field: {field_name}")
            }
            ResponseCloudEventBuilderError::ValidationError(err_msg) => {
                write!(f, "Validation error: {err_msg}")
            }
        }
    }
}

impl From<protocol_cloud_event::CloudEventBuilderError> for ResponseCloudEventBuilderError {
    fn from(value: protocol_cloud_event::CloudEventBuilderError) -> Self {
        match value {
            protocol_cloud_event::CloudEventBuilderError::UninitializedField(field_name) => {
                ResponseCloudEventBuilderError::UninitializedField(field_name)
            }
            protocol_cloud_event::CloudEventBuilderError::ValidationError(err_msg) => {
                ResponseCloudEventBuilderError::ValidationError(err_msg)
            }
        }
    }
}
impl Default for ResponseCloudEventBuilder {
    fn default() -> Self {
        Self(protocol_cloud_event::CloudEventBuilder::new(
            DEFAULT_RPC_RESPONSE_CLOUD_EVENT_EVENT_TYPE.to_string(),
        ))
    }
}

impl ResponseCloudEventBuilder {
    /// Builds a new [`ResponseCloudEvent`].
    /// # Errors
    /// If a required field has not been initialized.
    pub fn build(&self) -> Result<ResponseCloudEvent, ResponseCloudEventBuilderError> {
        Ok(ResponseCloudEvent(
            protocol_cloud_event::CloudEventBuilder::build(&self.0)?,
        ))
    }
    /// Identifies the context in which an event happened. Often this will include information such
    /// as the type of the event source, the organization publishing the event or the process that
    /// produced the event. The exact syntax and semantics behind the data encoded in the URI is
    /// defined by the event producer.
    pub fn source<VALUE: Into<String>>(&mut self, value: VALUE) -> &mut Self {
        self.0.source(value);
        self
    }
    /// The version of the cloud events specification which the event uses. This enables the
    /// interpretation of the context. Compliant event producers MUST use a value of 1.0 when
    /// referring to this version of the specification.
    pub fn spec_version<VALUE: Into<String>>(&mut self, value: VALUE) -> &mut Self {
        self.0.spec_version(value);
        self
    }
    /// Contains a value describing the type of event related to the originating occurrence. Often
    /// this attribute is used for routing, observability, policy enforcement, etc. The format of
    /// this is producer defined and might include information such as the version of the type.
    pub fn event_type<VALUE: Into<String>>(&mut self, value: VALUE) -> &mut Self {
        self.0.event_type(value);
        self
    }
    /// Identifies the schema that data adheres to. Incompatible changes to the schema SHOULD be
    /// reflected by a different URI.
    pub fn data_schema<VALUE: Into<Option<String>>>(&mut self, value: VALUE) -> &mut Self {
        self.0.data_schema(value);
        self
    }
    /// Identifies the event. Producers MUST ensure that source + id is unique for each distinct
    /// event. If a duplicate event is re-sent (e.g. due to a network error) it MAY have the same
    /// id. Consumers MAY assume that Events with identical source and id are duplicates.
    pub fn id<VALUE: Into<String>>(&mut self, value: VALUE) -> &mut Self {
        self.0.id(value);
        self
    }
    /// Timestamp of when the occurrence happened. If the time of the occurrence cannot be
    /// determined then this attribute MAY be set to some other time (such as the current time) by
    /// the cloud event producer, however all producers for the same source MUST be consistent in
    /// this respect. In other words, either they all use the actual time of the occurrence or they
    /// all use the same algorithm to determine the value used.
    pub fn time<VALUE: Into<Option<DateTime<Utc>>>>(&mut self, value: VALUE) -> &mut Self {
        self.0.time(value);
        self
    }
    /// Identifies the subject of the event in the context of the event producer (identified by
    /// source). In publish-subscribe scenarios, a subscriber will typically subscribe to events
    /// emitted by a source, but the source identifier alone might not be sufficient as a qualifier
    /// for any specific event if the source context has internal sub-structure.
    pub fn subject<VALUE: Into<protocol_cloud_event::CloudEventSubject>>(
        &mut self,
        value: VALUE,
    ) -> &mut Self {
        self.0.subject(value);
        self
    }
}

impl<TResp: PayloadSerialize> ResponseBuilder<TResp> {
    /// Add a payload to the command response. Validates successful serialization of the payload.
    ///
    /// # Errors
    /// [`AIOProtocolError`] of kind [`PayloadInvalid`](crate::common::aio_protocol_error::AIOProtocolErrorKind::PayloadInvalid) if serialization of the payload fails
    ///
    /// [`AIOProtocolError`] of kind [`ConfigurationInvalid`](crate::common::aio_protocol_error::AIOProtocolErrorKind::ConfigurationInvalid) if the content type is not valid utf-8
    pub fn payload(&mut self, payload: TResp) -> Result<&mut Self, AIOProtocolError> {
        match payload.serialize() {
            Err(e) => Err(AIOProtocolError::new_payload_invalid_error(
                true,
                false,
                Some(e.into()),
                Some("Payload serialization error".to_string()),
                None,
            )),
            Ok(serialized_payload) => {
                // Validate content type of command response is valid UTF-8
                if is_invalid_utf8(&serialized_payload.content_type) {
                    return Err(AIOProtocolError::new_configuration_invalid_error(
                        None,
                        "content_type",
                        Value::String(serialized_payload.content_type.clone()),
                        Some(format!(
                            "Content type '{}' of command response is not valid UTF-8",
                            serialized_payload.content_type
                        )),
                        None,
                    ));
                }
                self.serialized_payload = Some(serialized_payload);
                self.payload_type = Some(PhantomData);
                Ok(self)
            }
        }
    }

    /// Validate the command response.
    ///
    /// # Errors
    /// Returns a `String` describing the error if any of `custom_user_data`'s keys or values are invalid utf-8
    /// or the reserved [`PARTITION_KEY`] key is used.
    fn validate(&self) -> Result<(), String> {
        if let Some(custom_user_data) = &self.custom_user_data {
            for (key, _) in custom_user_data {
                if aio_cloud_event::CloudEventFields::from_str(key).is_ok() {
                    return Err(format!(
                        "Invalid user data property '{key}' is a reserved Cloud Event key"
                    ));
                }
            }
            validate_user_properties(custom_user_data)?;
        }
        // If there's a cloud event, make sure the content type is valid for the cloud event spec version
        if let Some(Some(cloud_event)) = &self.cloud_event
            && let Some(serialized_payload) = &self.serialized_payload
        {
            aio_cloud_event::CloudEventFields::DataContentType.validate(
                &serialized_payload.content_type,
                &cloud_event.0.spec_version,
            )?;
        }
        Ok(())
    }
}

/// Helper function to add user-defined application error headers to a [`Vec<(String, String)>`] to be used as the `custom_user_data` on the [`Response`].
///
/// `custom_user_data` can be an empty Vec or an existing Vec of custom user data. This function will add the application error headers to this Vec.
/// `application_error_code` required to be a non-empty `String`.
/// `application_error_payload` is optional and can be an empty `String`, in which case it is ignored and not added to `custom_user_data`. It is conventionally, but not necessarily, a stringified JSON object/value/array.
///
/// Returns `Ok(())` if the properties are added to `custom_user_data`. If an error is returned, `custom_user_data` won't get modified.
///
/// # Errors
/// Returns an Error with the `String` "`application_error_code` cannot be empty" if `application_error_code` is an empty string.
pub fn application_error_headers(
    custom_user_data: &mut Vec<(String, String)>,
    application_error_code: String,
    application_error_payload: String,
) -> Result<(), String> {
    const APPLICATION_ERROR_CODE_HEADER: &str = "AppErrCode";
    const APPLICATION_ERROR_PAYLOAD_HEADER: &str = "AppErrPayload";

    if application_error_code.trim().is_empty() {
        return Err("application_error_code cannot be empty".into());
    }

    custom_user_data.push((APPLICATION_ERROR_CODE_HEADER.into(), application_error_code));

    if !application_error_payload.trim().is_empty() {
        custom_user_data.push((
            APPLICATION_ERROR_PAYLOAD_HEADER.into(),
            application_error_payload,
        ));
    }

    Ok(())
}

/// Command Executor Cache Key struct.
///
/// Used to uniquely identify a command request.
#[derive(Eq, Hash, PartialEq, Clone)]
struct CacheKey {
    response_topic: TopicName,
    correlation_data: Bytes,
}

/// Command Executor Cache Entry struct.
#[derive(Clone, Debug)]
#[allow(clippy::large_enum_variant)]
enum CacheEntry {
    /// Indicates that the response is completed and cached
    Cached {
        serialized_payload: SerializedPayload,
        properties: PublishProperties,
        expiration_time: Instant,
    },
    /// Indicates that the request is in progress
    InProgress {
        /// Used for deduped requests to await the completion of the original request in order to ack
        processing_cancellation_token: CancellationToken,
    },
}

/// Command Executor Cache Lookup Result enum
///
/// Used to indicate the status of a cache entry.
#[derive(Debug)]
enum CacheLookupResult {
    /// The cache entry is cached and has not expired
    Cached {
        serialized_payload: SerializedPayload,
        properties: PublishProperties,
        response_message_expiry_interval: u32,
    },
    /// The cache entry is in progress
    InProgress(CancellationToken),
    /// The cache entry is not found
    NotFound,
}

/// The Command Executor Cache struct.
///
/// Used to cache command responses and determine if a command request is a duplicate.
#[derive(Clone)]
struct Cache(Arc<Mutex<HashMap<CacheKey, CacheEntry>>>);

impl Cache {
    /// Get the status of a cache entry from the [`Cache`].
    ///
    /// # Arguments
    /// `key` - The cache key to look for.
    ///
    /// Returns a [`CacheLookupResult`] indicating the result of the get.
    fn get(&self, key: &CacheKey) -> CacheLookupResult {
        let cache = self.0.lock().unwrap();

        match cache.get(key) {
            Some(entry) => {
                match entry {
                    CacheEntry::Cached {
                        serialized_payload,
                        properties,
                        expiration_time,
                    } => {
                        let response_message_expiry_interval =
                            get_response_message_expiry_interval(*expiration_time);

                        // Check if the entry has expired, return the entry if not
                        if let Some(response_message_expiry_interval) =
                            response_message_expiry_interval
                        {
                            CacheLookupResult::Cached {
                                serialized_payload: serialized_payload.clone(),
                                properties: properties.clone(),
                                response_message_expiry_interval,
                            }
                        } else {
                            // Entry has expired, return not found
                            CacheLookupResult::NotFound
                        }
                    }
                    CacheEntry::InProgress {
                        processing_cancellation_token,
                    } => {
                        // If the entry is in progress, it means it has not been completed yet
                        // any duplicate requests should await the cancellation token to complete
                        // their acknowledgement
                        CacheLookupResult::InProgress(processing_cancellation_token.clone())
                    }
                }
            }
            None => CacheLookupResult::NotFound,
        }
    }

    /// Set a cache entry in the cache. Also removes expired cache entries.
    ///
    /// # Arguments
    /// `key` - The cache key to set the cache entry for.
    /// `entry` - The cache entry to set.
    fn set(&self, key: CacheKey, entry: CacheEntry) {
        let mut cache = self.0.lock().unwrap();
        cache.retain(|_, entry| {
            match entry {
                CacheEntry::Cached {
                    expiration_time, ..
                } => {
                    // Retain only non-expired entries
                    expiration_time.elapsed().is_zero()
                }
                CacheEntry::InProgress {
                    processing_cancellation_token,
                } => {
                    // If an entry is in progress and its processing cancellation token is cancelled
                    // it means it timed out or the application dropped it, so it can be safely
                    // removed. If it didn't time out it would have been converted to a Cached entry.
                    !processing_cancellation_token.is_cancelled()
                }
            }
        });
        cache.insert(key, entry);
    }
}

/// Command Executor Options struct
#[allow(unused)]
#[derive(Builder, Clone)]
#[builder(setter(into, strip_option))]
pub struct Options {
    /// Topic pattern for the command request.
    /// Must align with [topic-structure.md](https://github.com/Azure/iot-operations-sdks/blob/main/doc/reference/topic-structure.md)
    request_topic_pattern: String,
    /// Command name
    command_name: String,
    /// Optional Topic namespace to be prepended to the topic pattern
    #[builder(default = "None")]
    topic_namespace: Option<String>,
    /// Topic token keys/values to be permanently replaced in the topic pattern
    #[builder(default)]
    topic_token_map: HashMap<String, String>,
    /// Denotes if commands are idempotent
    #[builder(default = "false")]
    is_idempotent: bool,
    /// Service group ID
    #[builder(default = "None")]
    service_group_id: Option<String>,
}

/// Command Executor struct
/// # Example
/// ```
/// # use std::{collections::HashMap, time::Duration};
/// # use tokio_test::block_on;
/// # use azure_iot_operations_mqtt::aio::connection_settings::MqttConnectionSettingsBuilder;
/// # use azure_iot_operations_mqtt::session::{Session, SessionOptionsBuilder};
/// # use azure_iot_operations_protocol::rpc_command;
/// # use azure_iot_operations_protocol::application::ApplicationContextBuilder;
/// # let mut connection_settings = MqttConnectionSettingsBuilder::default()
/// #     .client_id("test_server")
/// #     .hostname("localhost")
/// #     .tcp_port(1883u16)
/// #     .build().unwrap();
/// # let mut session_options = SessionOptionsBuilder::default()
/// #     .connection_settings(connection_settings)
/// #     .build().unwrap();
/// # let mqtt_session = Session::new(session_options).unwrap();
/// # let application_context = ApplicationContextBuilder::default().build().unwrap();;
/// let executor_options = rpc_command::executor::OptionsBuilder::default()
///   .command_name("test_command")
///   .request_topic_pattern("test/request")
///   .build().unwrap();
/// # tokio_test::block_on(async {
/// let mut executor: rpc_command::Executor<Vec<u8>, Vec<u8>> = rpc_command::Executor::new(application_context, mqtt_session.create_managed_client(), executor_options).unwrap();
/// // let request = executor.recv().await.unwrap();
/// // let response = rpc_command::executor::ResponseBuilder::default()
///  // .payload(Vec::new()).unwrap()
///  // .build().unwrap();
/// // let request.complete(response).await.unwrap();
/// # });
/// ```
#[allow(unused)]
pub struct Executor<TReq, TResp>
where
    TReq: PayloadSerialize + Send + 'static,
    TResp: PayloadSerialize + Send + 'static,
{
    // Static properties of the executor
    application_hlc: Arc<ApplicationHybridLogicalClock>,
    mqtt_client: SessionManagedClient,
    mqtt_receiver: SessionPubReceiver,
    is_idempotent: bool,
    request_topic_pattern: TopicPattern,
    request_topic_filter: TopicFilter,
    command_name: String,
    request_payload_type: PhantomData<TReq>,
    response_payload_type: PhantomData<TResp>,
    cache: Cache,
    // Describes state
    state: State,
    // Information to manage state
    cancellation_token: CancellationToken,
}

/// Describes state of executor
#[derive(PartialEq)]
enum State {
    New,
    Subscribed,
    ShutdownSuccessful,
}

/// Implementation of Command Executor.
impl<TReq, TResp> Executor<TReq, TResp>
where
    TReq: PayloadSerialize + Send + 'static,
    TResp: PayloadSerialize + Send + 'static,
{
    /// Create a new [`Executor`].
    ///
    /// # Arguments
    /// * `application_context` - [`ApplicationContext`] that the command executor is part of.
    /// * `client` - The MQTT client to use for communication.
    /// * `executor_options` - Configuration options.
    ///
    /// Returns Ok([`Executor`]) on success, otherwise returns [`AIOProtocolError`].
    ///
    /// # Errors
    /// [`AIOProtocolError`] of kind [`ConfigurationInvalid`](crate::common::aio_protocol_error::AIOProtocolErrorKind::ConfigurationInvalid) if:
    /// - [`command_name`](OptionsBuilder::command_name) is empty, whitespace or invalid
    /// - [`request_topic_pattern`](OptionsBuilder::request_topic_pattern),
    ///   [`topic_namespace`](OptionsBuilder::topic_namespace)
    ///   are Some and invalid or contain a token with no valid replacement
    /// - [`topic_token_map`](OptionsBuilder::topic_token_map) is not empty and contains invalid key(s) and/or token(s)
    pub fn new(
        application_context: ApplicationContext,
        client: SessionManagedClient,
        executor_options: Options,
    ) -> Result<Self, AIOProtocolError> {
        // Validate function parameters, validation for topic pattern and related options done in
        // TopicPattern::new
        if executor_options.command_name.is_empty()
            || contains_invalid_char(&executor_options.command_name)
        {
            return Err(AIOProtocolError::new_configuration_invalid_error(
                None,
                "command_name",
                Value::String(executor_options.command_name.clone()),
                None,
                Some(executor_options.command_name),
            ));
        }

        // Create a new Command Pattern, validates topic pattern and options
        let request_topic_pattern = TopicPattern::new(
            &executor_options.request_topic_pattern,
            executor_options.service_group_id,
            executor_options.topic_namespace.as_deref(),
            &executor_options.topic_token_map,
        )
        .map_err(|e| {
            AIOProtocolError::config_invalid_from_topic_pattern_error(
                e,
                "executor_options.request_topic_pattern",
            )
        })?;

        let request_topic_filter = request_topic_pattern.as_subscribe_topic().map_err(|e| {
            AIOProtocolError::config_invalid_from_topic_pattern_error(
                e,
                "executor_options.request_topic_pattern",
            )
        })?;

        // Get pub sub and receiver from the mqtt session
        let mqtt_receiver = client.create_filtered_pub_receiver(request_topic_filter.clone());

        // Create Command executor
        Ok(Executor {
            application_hlc: application_context.application_hlc,
            mqtt_client: client,
            mqtt_receiver,
            is_idempotent: executor_options.is_idempotent,
            request_topic_pattern,
            request_topic_filter,
            command_name: executor_options.command_name,
            request_payload_type: PhantomData,
            response_payload_type: PhantomData,
            cache: Cache(Arc::new(Mutex::new(HashMap::new()))),
            state: State::New,
            cancellation_token: CancellationToken::new(),
        })
    }

    /// Shutdown the [`Executor`]. Unsubscribes from the request topic.
    ///
    /// Note: If this method is called, the [`Executor`] will no longer receive commands
    /// from the MQTT client, any command requests that have not been processed can still be received
    /// by the executor. If the method returns an error, it may be called again to attempt the unsubscribe again.
    ///
    /// Returns Ok(()) on success, otherwise returns [`AIOProtocolError`].
    /// # Errors
    /// [`AIOProtocolError`] of kind [`ClientError`](crate::common::aio_protocol_error::AIOProtocolErrorKind::ClientError) if the unsubscribe fails or if the unsuback reason code doesn't indicate success.
    pub async fn shutdown(&mut self) -> Result<(), AIOProtocolError> {
        // Close the receiver, no longer receive messages
        self.mqtt_receiver.close();

        match self.state {
            State::New | State::ShutdownSuccessful => {
                // If subscribe has not been called or shutdown was successful, do not unsubscribe
                self.state = State::ShutdownSuccessful;
            }
            State::Subscribed => {
                let unsubscribe_result = self
                    .mqtt_client
                    .unsubscribe(
                        self.request_topic_filter.clone(),
                        azure_iot_operations_mqtt::control_packet::UnsubscribeProperties::default(),
                    )
                    .await;

                match unsubscribe_result {
                    Ok(unsub_ct) => match unsub_ct.await {
                        Ok(unsuback) => match unsuback.as_result() {
                            Ok(()) => {
                                self.state = State::ShutdownSuccessful;
                            }
                            Err(e) => {
                                log::error!(
                                    "[{}] Executor nsuback error: {unsuback:?}",
                                    self.command_name
                                );
                                return Err(AIOProtocolError::new_mqtt_error(
                                    Some("MQTT error on command executor unsuback".to_string()),
                                    Box::new(e),
                                    Some(self.command_name.clone()),
                                ));
                            }
                        },
                        Err(e) => {
                            log::error!(
                                "[{}] Executor unsubscribe completion error: {e}",
                                self.command_name
                            );
                            return Err(AIOProtocolError::new_mqtt_error(
                                Some("MQTT error on command executor unsubscribe".to_string()),
                                Box::new(e),
                                Some(self.command_name.clone()),
                            ));
                        }
                    },
                    Err(e) => {
                        log::error!(
                            "[{}] Client error while unsubscribing in Executor: {e}",
                            self.command_name
                        );
                        return Err(AIOProtocolError::new_mqtt_error(
                            Some("Client error on command executor unsubscribe".to_string()),
                            Box::new(e),
                            Some(self.command_name.clone()),
                        ));
                    }
                }
            }
        }
        log::info!("[{}] Executor Shutdown", self.command_name);
        Ok(())
    }

    /// Subscribe to the request topic.
    ///
    /// Returns Ok(()) on success, otherwise returns [`AIOProtocolError`].
    /// # Errors
    /// [`AIOProtocolError`] of kind [`ClientError`](crate::common::aio_protocol_error::AIOProtocolErrorKind::ClientError) if the subscribe fails or if the suback reason code doesn't indicate success.
    async fn try_subscribe(&mut self) -> Result<(), AIOProtocolError> {
        let subscribe_result = self
            .mqtt_client
            .subscribe(
                self.request_topic_filter.clone(),
                QoS::AtLeastOnce,
                false,
                azure_iot_operations_mqtt::control_packet::RetainOptions::default(),
                azure_iot_operations_mqtt::control_packet::SubscribeProperties::default(),
            )
            .await;

        match subscribe_result {
            Ok(sub_ct) => match sub_ct.await {
                Ok(suback) => {
                    suback.as_result().map_err(|e| {
                        log::error!("[{}] Executor suback error: {suback:?}", self.command_name);
                        AIOProtocolError::new_mqtt_error(
                            Some("MQTT error on command executor suback".to_string()),
                            Box::new(e),
                            Some(self.command_name.clone()),
                        )
                    })?;
                }
                Err(e) => {
                    log::error!(
                        "[{}] Executor subscribe completion error: {e}",
                        self.command_name
                    );
                    return Err(AIOProtocolError::new_mqtt_error(
                        Some("MQTT error on command executor subscribe".to_string()),
                        Box::new(e),
                        Some(self.command_name.clone()),
                    ));
                }
            },
            Err(e) => {
                log::error!(
                    "[{}] Client error while subscribing in Executor: {e}",
                    self.command_name
                );
                return Err(AIOProtocolError::new_mqtt_error(
                    Some("Client error on command executor subscribe".to_string()),
                    Box::new(e),
                    Some(self.command_name.clone()),
                ));
            }
        }
        Ok(())
    }

    /// Receive a command request or [`None`] if there will be no more requests.
    ///
    /// If there are messages:
    /// - Returns Ok([`Request`]) on success
    /// - Returns [`AIOProtocolError`] on error.
    ///
    /// Will also subscribe to the request topic if not already subscribed.
    ///
    /// # Errors
    /// [`AIOProtocolError`] of kind [`UnknownError`](crate::common::aio_protocol_error::AIOProtocolErrorKind::UnknownError) if an error occurs while receiving the message.
    ///
    /// [`AIOProtocolError`] of kind [`ClientError`](crate::common::aio_protocol_error::AIOProtocolErrorKind::ClientError) if the subscribe fails or if the suback reason code doesn't indicate success.
    ///
    /// [`AIOProtocolError`] of kind [`InternalLogicError`](crate::common::aio_protocol_error::AIOProtocolErrorKind::InternalLogicError) if the command expiration time cannot be calculated.
    pub async fn recv(&mut self) -> Option<Result<Request<TReq, TResp>, AIOProtocolError>> {
        // Subscribe to the request topic if not already subscribed
        if State::New == self.state {
            if let Err(e) = self.try_subscribe().await {
                return Some(Err(e));
            }
            self.state = State::Subscribed;
        }

        loop {
            match self.mqtt_receiver.recv_manual_ack().await {
                Some((m, ack_token)) => {
                    let Some(ack_token) = ack_token else {
                        // No ack token, ignore the message. This should never happen as the executor
                        // should always receive QoS 1 messages that have an ack token.
                        log::warn!(
                            "[{}] Received command request without ack token",
                            self.command_name
                        );
                        continue;
                    };
                    let pkid = match m.qos {
                        azure_iot_operations_mqtt::control_packet::DeliveryQoS::AtMostOnce
                        | azure_iot_operations_mqtt::control_packet::DeliveryQoS::ExactlyOnce(_) => {
                            // This should never happen as the executor should always receive QoS 1 messages
                            log::warn!(
                                "[{}] Received non QoS 1 command request",
                                self.command_name
                            );
                            continue;
                        }
                        azure_iot_operations_mqtt::control_packet::DeliveryQoS::AtLeastOnce(
                            delivery_info,
                        ) => delivery_info.packet_identifier.get(),
                    };
                    // Process the request
                    log::debug!("[{}][pkid: {}] Received request", self.command_name, pkid);
                    let message_received_time = Instant::now();

                    // Create a cancellation token to be used to track cache entry state, once the
                    // entry has gone from InProgress to Cached or removed, this token will be cancelled
                    let processing_cancellation_token = CancellationToken::new();
                    let processing_cancellation_token_clone = processing_cancellation_token.clone();
                    // This drop guard will stay alive until the request processing is complete,
                    // whether a timeout occurs, or we have an ok or error response.
                    let processing_drop_guard = processing_cancellation_token.drop_guard();

                    // Clone properties
                    let properties = m.properties;

                    // Get response topic
                    let response_topic = if let Some(rt) = properties.response_topic {
                        if !is_valid_replacement(rt.as_str()) {
                            log::warn!(
                                "[{}][pkid: {}] Response topic invalid, command response will not be published",
                                self.command_name,
                                pkid
                            );
                            tokio::task::spawn({
                                let executor_cancellation_token_clone =
                                    self.cancellation_token.clone();
                                async move {
                                    handle_ack(ack_token, executor_cancellation_token_clone, pkid)
                                        .await;
                                }
                            });
                            continue;
                        }
                        rt
                    } else {
                        log::warn!(
                            "[{}][pkid: {}] Response topic missing, command response will not be published",
                            self.command_name,
                            pkid
                        );
                        tokio::task::spawn({
                            let executor_cancellation_token_clone = self.cancellation_token.clone();
                            async move {
                                handle_ack(ack_token, executor_cancellation_token_clone, pkid)
                                    .await;
                            }
                        });
                        continue;
                    };

                    let mut command_expiration_time_calculated = false;
                    let mut response_arguments = ResponseArguments {
                        command_name: self.command_name.clone(),
                        response_topic,
                        correlation_data: None,
                        status_code: StatusCode::Ok,
                        status_message: None,
                        is_application_error: false,
                        invalid_property_name: None,
                        invalid_property_value: None,
                        message_expiry_interval: None,
                        command_expiration_time: None,
                        supported_protocol_major_versions: None,
                        request_protocol_version: None,
                        cached_key: None,
                        cache_lookup_result: CacheLookupResult::NotFound,
                    };

                    // Get message expiry interval
                    let command_expiration_time = match properties.message_expiry_interval {
                        Some(ct) => {
                            response_arguments.message_expiry_interval = Some(ct);
                            message_received_time.checked_add(Duration::from_secs(ct.into()))
                        }
                        _ => message_received_time.checked_add(Duration::from_secs(u64::from(
                            DEFAULT_MESSAGE_EXPIRY_INTERVAL_SECONDS,
                        ))),
                    };

                    // Check if there was an error calculating the command expiration time
                    // if not, set the command expiration time
                    if let Some(command_expiration_time) = command_expiration_time {
                        response_arguments.command_expiration_time = Some(command_expiration_time);
                        command_expiration_time_calculated = true;
                    }

                    // Get correlation data
                    if let Some(correlation_data) = properties.correlation_data {
                        if correlation_data.len() == 16 {
                            response_arguments.correlation_data = Some(correlation_data.clone());
                            response_arguments.cached_key = Some(CacheKey {
                                response_topic: response_arguments.response_topic.clone(),
                                correlation_data,
                            });
                        } else {
                            response_arguments.status_code = StatusCode::BadRequest;
                            response_arguments.status_message =
                                Some("Correlation data bytes do not conform to a GUID".to_string());
                            response_arguments.invalid_property_name =
                                Some("Correlation Data".to_string());
                            if let Ok(correlation_data_str) =
                                String::from_utf8(correlation_data.to_vec())
                            {
                                response_arguments.invalid_property_value =
                                    Some(correlation_data_str);
                            } else { /* Ignore */
                            }
                            response_arguments.correlation_data = Some(correlation_data);
                        }
                    } else {
                        response_arguments.status_code = StatusCode::BadRequest;
                        response_arguments.status_message =
                            Some("Correlation data missing".to_string());
                        response_arguments.invalid_property_name =
                            Some("Correlation Data".to_string());
                    }

                    'process_request: {
                        // If the cache key was not created it means the correlation data was invalid
                        let Some(cache_key) = &response_arguments.cached_key else {
                            break 'process_request;
                        };

                        // Checking if command expiration time was calculated after correlation
                        // to provide a more accurate response to the invoker.
                        let Some(command_expiration_time) = command_expiration_time else {
                            response_arguments.status_code = StatusCode::InternalServerError;
                            response_arguments.status_message =
                                Some(INTERNAL_LOGIC_EXPIRATION_ERROR.to_string());
                            break 'process_request;
                        };

                        // Check if message expiry interval is present
                        if properties.message_expiry_interval.is_none() {
                            response_arguments.status_code = StatusCode::BadRequest;
                            response_arguments.status_message =
                                Some("Message expiry interval missing".to_string());
                            response_arguments.invalid_property_name =
                                Some("Message Expiry".to_string());
                            break 'process_request;
                        }

                        // Check cache
                        response_arguments.cache_lookup_result = self.cache.get(cache_key);

                        if !matches!(
                            response_arguments.cache_lookup_result,
                            CacheLookupResult::NotFound
                        ) {
                            // This means there is an entry for this correlation ID so either we
                            // have a cached response or the request is already being processed.
                            break 'process_request;
                        }

                        // If there is no entry for this correlation ID we register it as in progress
                        self.cache.set(
                            cache_key.clone(),
                            CacheEntry::InProgress {
                                processing_cancellation_token: processing_cancellation_token_clone,
                            },
                        );

                        // unused beyond validation, but may be used in the future to determine how to handle other fields. Can be moved higher in the future if needed.
                        let mut request_protocol_version = DEFAULT_RPC_COMMAND_PROTOCOL_VERSION; // assume default version if none is provided
                        if let Some((_, protocol_version)) =
                            properties.user_properties.iter().find(|(key, _)| {
                                UserProperty::from_str(key) == Ok(UserProperty::ProtocolVersion)
                            })
                        {
                            if let Some(request_version) =
                                ProtocolVersion::parse_protocol_version(protocol_version)
                            {
                                request_protocol_version = request_version;
                            } else {
                                response_arguments.status_code = StatusCode::VersionNotSupported;
                                response_arguments.status_message = Some(format!(
                                    "Unparsable protocol version value provided: {protocol_version}."
                                ));
                                response_arguments.supported_protocol_major_versions =
                                    Some(SUPPORTED_PROTOCOL_VERSIONS.to_vec());
                                response_arguments.request_protocol_version =
                                    Some(protocol_version.clone());
                                break 'process_request;
                            }
                        }
                        // Check that the version (or the default version if one isn't provided) is supported
                        if !request_protocol_version.is_supported(SUPPORTED_PROTOCOL_VERSIONS) {
                            response_arguments.status_code = StatusCode::VersionNotSupported;
                            response_arguments.status_message = Some(format!(
                                "The command executor that received the request only supports major protocol versions '{SUPPORTED_PROTOCOL_VERSIONS:?}', but '{request_protocol_version}' was sent on the request."
                            ));
                            response_arguments.supported_protocol_major_versions =
                                Some(SUPPORTED_PROTOCOL_VERSIONS.to_vec());
                            response_arguments.request_protocol_version =
                                Some(request_protocol_version.to_string());
                            break 'process_request;
                        }

                        let mut user_data = Vec::new();
                        let mut timestamp = None;
                        let mut invoker_id = None;
                        for (key, value) in properties.user_properties {
                            match UserProperty::from_str(&key) {
                                Ok(UserProperty::Timestamp) => {
                                    match HybridLogicalClock::from_str(&value) {
                                        Ok(ts) => {
                                            // Update application HLC against received __ts
                                            if let Err(e) = self.application_hlc.update(&ts) {
                                                response_arguments.status_message = Some(format!(
                                                    "Failure updating application HLC against {value}: {e}"
                                                ));
                                                response_arguments.invalid_property_name =
                                                    Some(UserProperty::Timestamp.to_string());
                                                response_arguments.invalid_property_value =
                                                    Some(value);
                                                match e.kind() {
                                                    HLCErrorKind::ClockDrift => {
                                                        response_arguments.status_code =
                                                            StatusCode::ServiceUnavailable;
                                                    }
                                                    HLCErrorKind::OverflowWarning => {
                                                        response_arguments.status_code =
                                                            StatusCode::InternalServerError;
                                                    }
                                                }
                                                break 'process_request;
                                            }
                                            timestamp = Some(ts);
                                        }
                                        Err(e) => {
                                            response_arguments.status_code = StatusCode::BadRequest;
                                            response_arguments.status_message =
                                                Some(format!("Timestamp invalid: {e}"));
                                            response_arguments.invalid_property_name =
                                                Some(UserProperty::Timestamp.to_string());
                                            response_arguments.invalid_property_value = Some(value);
                                            break 'process_request;
                                        }
                                    }
                                }
                                Ok(UserProperty::SourceId) => {
                                    invoker_id = Some(value);
                                }
                                Ok(UserProperty::ProtocolVersion) => {
                                    // skip, already processed
                                }
                                Err(()) => {
                                    if key == PARTITION_KEY {
                                        // Ignore partition key, it is meant for the broker
                                        continue;
                                    }
                                    user_data.push((key, value));
                                }
                                _ => {
                                    /* UserProperty::Status, UserProperty::StatusMessage, UserProperty::IsApplicationError, UserProperty::InvalidPropertyName, UserProperty::InvalidPropertyValue */
                                    // Don't return error, although above properties shouldn't be in the request
                                    log::warn!(
                                        "[{}] Command request should not contain MQTT user property {key}. Value is {value}",
                                        self.command_name
                                    );
                                    user_data.push((key, value));
                                }
                            }
                        }

                        let topic_tokens = self
                            .request_topic_pattern
                            .parse_tokens(m.topic_name.as_str());

                        // Deserialize payload
                        let format_indicator = properties.payload_format_indicator.into();
                        let payload = match TReq::deserialize(
                            &m.payload,
                            properties.content_type.as_ref(),
                            &format_indicator,
                        ) {
                            Ok(payload) => payload,
                            Err(e) => match e {
                                DeserializationError::InvalidPayload(deserialization_e) => {
                                    response_arguments.status_code = StatusCode::BadRequest;
                                    response_arguments.status_message = Some(format!(
                                        "Error deserializing payload: {deserialization_e:?}"
                                    ));
                                    break 'process_request;
                                }
                                DeserializationError::UnsupportedContentType(message) => {
                                    response_arguments.status_code =
                                        StatusCode::UnsupportedMediaType;
                                    response_arguments.status_message = Some(message);
                                    response_arguments.invalid_property_name =
                                        Some("Content Type".to_string());
                                    response_arguments.invalid_property_value =
                                        Some(properties.content_type.unwrap_or("None".to_string()));
                                    break 'process_request;
                                }
                            },
                        };

                        let (response_tx, response_rx) = oneshot::channel();
                        let (publish_completion_tx, publish_completion_rx) = oneshot::channel();

                        let command_request = Request {
                            payload,
                            content_type: properties.content_type,
                            format_indicator,
                            custom_user_data: user_data,
                            timestamp,
                            invoker_id,
                            topic_tokens,
                            command_name: self.command_name.clone(),
                            response_tx,
                            publish_completion_rx,
                        };

                        // Check the command has not expired, if it has, we do not respond to the invoker.
                        if command_expiration_time.elapsed().is_zero() {
                            // Elapsed returns zero if the time has not passed
                            tokio::task::spawn({
                                let app_hlc_clone = self.application_hlc.clone();
                                let client_clone = self.mqtt_client.clone();
                                let cache_clone = self.cache.clone();
                                let executor_cancellation_token_clone =
                                    self.cancellation_token.clone();
                                async move {
                                    tokio::select! {
                                        () = executor_cancellation_token_clone.cancelled() => { /* executor dropped */},
                                        () = Self::process_command(
                                            app_hlc_clone,
                                            client_clone,
                                            pkid,
                                            response_arguments,
                                            (Some(response_rx), Some(publish_completion_tx)),
                                            cache_clone,
                                            processing_drop_guard,
                                        ) => {
                                            // Finished processing command
                                            handle_ack(ack_token, executor_cancellation_token_clone, pkid).await;
                                        },
                                    }
                                }
                            });
                            return Some(Ok(command_request));
                        }
                    }

                    // Checking that command expiration time was calculated, if it has not we do not
                    // respond to the invoker.
                    let Some(command_expiration_time) = command_expiration_time else {
                        continue;
                    };

                    match response_arguments.cache_lookup_result {
                        CacheLookupResult::Cached {
                            serialized_payload,
                            properties,
                            response_message_expiry_interval,
                        } => {
                            // Process the duplicate command
                            tokio::task::spawn({
                                let client_clone = self.mqtt_client.clone();
                                let executor_cancellation_token_clone =
                                    self.cancellation_token.clone();
                                async move {
                                    tokio::select! {
                                        () = executor_cancellation_token_clone.cancelled() => { /* executor dropped */},
                                        () = Self::process_duplicate_command(
                                            client_clone,
                                            response_arguments.response_topic,
                                            serialized_payload,
                                            properties,
                                            response_message_expiry_interval,
                                            response_arguments.command_name,
                                            pkid,
                                        ) => {
                                            // Finished processing duplicate command
                                            handle_ack(ack_token, executor_cancellation_token_clone, pkid).await;
                                        },
                                    }
                                }
                            });
                        }
                        CacheLookupResult::InProgress(cancellation_token) => {
                            // If the command is currently in progress that means this message
                            // is a dupe and we need to wait for the original to complete before
                            // acking.
                            tokio::task::spawn(handle_in_progress_duplicate_ack(
                                ack_token,
                                cancellation_token.clone(),
                                self.cancellation_token.clone(),
                                pkid,
                            ));
                        }
                        CacheLookupResult::NotFound => {
                            // Indicates the command should be processed as an error

                            // Check the command has not expired, if it has, we do not respond to the invoker.
                            if command_expiration_time.elapsed().is_zero() {
                                tokio::task::spawn({
                                    let app_hlc_clone = self.application_hlc.clone();
                                    let client_clone = self.mqtt_client.clone();
                                    let cache_clone = self.cache.clone();
                                    let executor_cancellation_token_clone =
                                        self.cancellation_token.clone();
                                    async move {
                                        tokio::select! {
                                            () = executor_cancellation_token_clone.cancelled() => { /* executor dropped */},
                                            () = Self::process_command(
                                                app_hlc_clone,
                                                client_clone,
                                                pkid,
                                                response_arguments,
                                                (None, None),
                                                cache_clone,
                                                processing_drop_guard,
                                            ) => {
                                                // Finished processing command
                                                handle_ack(ack_token, executor_cancellation_token_clone, pkid).await;
                                            },
                                        }
                                    }
                                });
                            }
                        }
                    }

                    if !command_expiration_time_calculated {
                        return Some(Err(AIOProtocolError::new_internal_logic_error(
                            true,
                            false,
                            None,
                            "command_expiration_time",
                            None,
                            Some(INTERNAL_LOGIC_EXPIRATION_ERROR.to_string()),
                            Some(self.command_name.clone()),
                        )));
                    }
                }
                _ => {
                    // There will be no more requests
                    return None;
                }
            }
        }
    }

    /// Process a duplicate command by sending the cached response.
    async fn process_duplicate_command(
        client: SessionManagedClient,
        response_topic: TopicName,
        serialized_payload: SerializedPayload,
        mut publish_properties: PublishProperties,
        response_message_expiry_interval: u32,
        command_name: String,
        pkid: u16,
    ) {
        log::debug!(
            "[{command_name}][pkid: {pkid}] Duplicate request, responding with cached response"
        );

        publish_properties.message_expiry_interval = Some(response_message_expiry_interval);

        match client
            .publish_qos1(
                response_topic,
                false,
                serialized_payload.payload,
                publish_properties,
            )
            .await
        {
            Ok(publish_completion_token) => {
                // Wait and handle puback
                match publish_completion_token.await {
                    Ok(puback) => {
                        if !puback.is_success() {
                            log::warn!(
                                "[{command_name}][pkid: {pkid}] Puback reported failure for cached command response: {puback:?}"
                            );
                        }
                    }
                    Err(e) => {
                        log::warn!(
                            "[{command_name}][pkid: {pkid}] Publish completion error for cached command response: {e}"
                        );
                    }
                }
            }
            Err(e) => {
                log::warn!(
                    "[{command_name}][pkid: {pkid}] Client error on cached command response publish: {e}"
                );
            }
        }
    }

    /// Process a command request, finish building the response and send it.
    #[allow(clippy::type_complexity)]
    async fn process_command(
        application_hlc: Arc<ApplicationHybridLogicalClock>,
        client: SessionManagedClient,
        pkid: u16,
        mut response_arguments: ResponseArguments,
        application_channels: (
            Option<oneshot::Receiver<Response<TResp>>>,
            Option<oneshot::Sender<Result<(), AIOProtocolError>>>,
        ), // TODO: Once simplified, remove this complex type
        cache: Cache,
        _processing_drop_guard: DropGuard,
    ) {
        let (response_rx, completion_tx) = application_channels;
        let mut serialized_payload = SerializedPayload::default();
        let mut publish_properties = PublishProperties::default();

        let mut user_properties: Vec<(String, String)> = Vec::new();
        'process_response: {
            let Some(command_expiration_time) = response_arguments.command_expiration_time else {
                break 'process_response;
            };
            if let Some(response_rx) = response_rx {
                // Wait for response
                let response = if let Ok(response_timer) = timeout(
                    command_expiration_time.duration_since(Instant::now()),
                    response_rx,
                )
                .await
                {
                    if let Ok(response_app) = response_timer {
                        response_app
                    } else {
                        // Happens when the sender is dropped by the application.
                        response_arguments.status_code = StatusCode::InternalServerError;
                        response_arguments.status_message =
                            Some("Request has been dropped by the application".to_string());
                        response_arguments.is_application_error = true;
                        break 'process_response;
                    }
                } else {
                    log::warn!(
                        "[{}][pkid: {}] Command request timed out",
                        response_arguments.command_name,
                        pkid
                    );
                    // Notify the application that a timeout occurred
                    if let Some(completion_tx) = completion_tx {
                        let _ = completion_tx.send(Err(AIOProtocolError::new_timeout_error(
                            false,
                            None,
                            &response_arguments.command_name,
                            Duration::from_secs(
                                response_arguments
                                    .message_expiry_interval
                                    .unwrap_or_default()
                                    .into(),
                            ),
                            None,
                            Some(response_arguments.command_name.clone()),
                        )));
                    }
                    return;
                };

                user_properties = response.custom_user_data;

                // Cloud Events headers
                if let Some(cloud_event) = response.cloud_event {
                    let cloud_event_headers = cloud_event
                        .0
                        .into_headers(response_arguments.response_topic.as_str());
                    for (key, value) in cloud_event_headers {
                        user_properties.push((key, value));
                    }
                }

                // Serialize payload
                serialized_payload = response.serialized_payload;

                if serialized_payload.payload.is_empty() {
                    response_arguments.status_code = StatusCode::NoContent;
                }
            } else { /* Error */
            }
        }

        if response_arguments.status_code != StatusCode::Ok
            || response_arguments.status_code != StatusCode::NoContent
        {
            user_properties.push((
                UserProperty::IsApplicationError.to_string(),
                response_arguments.is_application_error.to_string(),
            ));
        }

        user_properties.push((
            UserProperty::Status.to_string(),
            (response_arguments.status_code as u16).to_string(),
        ));

        user_properties.push((
            UserProperty::ProtocolVersion.to_string(),
            RPC_COMMAND_PROTOCOL_VERSION.to_string(),
        ));

        user_properties.push((
            UserProperty::SourceId.to_string(),
            client.client_id().to_string(),
        ));

        // Update HLC and use as the timestamp.
        // If there are errors updating the HLC (unlikely when updating against now),
        // the timestamp will not be added.
        if let Ok(timestamp_str) = application_hlc.update_now() {
            user_properties.push((UserProperty::Timestamp.to_string(), timestamp_str));
        }

        if let Some(status_message) = response_arguments.status_message {
            log::warn!(
                "[{}][pkid: {}] sending error reponse to invoker: {}",
                response_arguments.command_name,
                pkid,
                status_message
            );
            user_properties.push((UserProperty::StatusMessage.to_string(), status_message));
        }

        if let Some(name) = response_arguments.invalid_property_name {
            user_properties.push((UserProperty::InvalidPropertyName.to_string(), name));
        }

        if let Some(value) = response_arguments.invalid_property_value {
            user_properties.push((UserProperty::InvalidPropertyValue.to_string(), value));
        }

        if let Some(supported_protocol_major_versions) =
            response_arguments.supported_protocol_major_versions
        {
            user_properties.push((
                UserProperty::SupportedMajorVersions.to_string(),
                supported_protocol_major_versions_to_string(&supported_protocol_major_versions),
            ));
        }

        if let Some(request_protocol_version) = response_arguments.request_protocol_version {
            user_properties.push((
                UserProperty::RequestProtocolVersion.to_string(),
                request_protocol_version,
            ));
        }

        // Create publish properties
        publish_properties.payload_format_indicator = serialized_payload.format_indicator.into();
        publish_properties.topic_alias = None;
        publish_properties.response_topic = None;
        publish_properties.correlation_data = response_arguments.correlation_data;
        publish_properties.user_properties = user_properties;
        publish_properties.subscription_identifiers = Vec::new();
        publish_properties.content_type = Some(serialized_payload.content_type.clone());

        match response_arguments.command_expiration_time {
            Some(command_expiration_time) => {
                let response_message_expiry_interval =
                    get_response_message_expiry_interval(command_expiration_time);

                // Check if the entry has expired
                if let Some(response_message_expiry_interval) = response_message_expiry_interval {
                    publish_properties.message_expiry_interval =
                        Some(response_message_expiry_interval);
                } else {
                    log::warn!(
                        "[{}][pkid: {}] Command request timed out",
                        response_arguments.command_name,
                        pkid
                    );
                    // Notify the application that a timeout occurred
                    if let Some(completion_tx) = completion_tx {
                        let _ = completion_tx.send(Err(AIOProtocolError::new_timeout_error(
                            false,
                            None,
                            &response_arguments.command_name,
                            Duration::from_secs(
                                response_arguments
                                    .message_expiry_interval
                                    .unwrap_or_default()
                                    .into(),
                            ),
                            None,
                            Some(response_arguments.command_name.clone()),
                        )));
                    }
                    return;
                }

                // Store cache, even if the response is an error
                if let Some(cached_key) = response_arguments.cached_key {
                    let cache_entry = CacheEntry::Cached {
                        serialized_payload: serialized_payload.clone(),
                        properties: publish_properties.clone(),
                        expiration_time: command_expiration_time
                            + Duration::from_secs(CACHE_EXPIRY_BUFFER_SECONDS),
                    };
                    log::debug!(
                        "[{}][pkid: {}] Caching response",
                        response_arguments.command_name,
                        pkid
                    );
                    cache.set(cached_key, cache_entry);
                }
            }
            _ => {
                // Happens when the command expiration time was not able to be calculated.
                // We don't cache the response in this case. Note that we did not set a in progress
                // entry for this case since it requires a valid expiration time.
                publish_properties.message_expiry_interval =
                    Some(DEFAULT_MESSAGE_EXPIRY_INTERVAL_SECONDS);
            }
        }

        // Try to publish
        match client
            .publish_qos1(
                response_arguments.response_topic,
                false,
                serialized_payload.payload,
                publish_properties,
            )
            .await
        {
            Ok(publish_completion_token) => {
                // Wait and handle puback
                match publish_completion_token.await {
                    Ok(puback) => {
                        match puback.as_result() {
                            Ok(()) => {
                                if let Some(completion_tx) = completion_tx {
                                    // We ignore the error as the receiver may have been dropped indicating that the
                                    // application is not interested in the completion of the publish.
                                    let _ = completion_tx.send(Ok(()));
                                }
                            }
                            Err(e) => {
                                log::error!(
                                    "[{}][pkid: {}] Command response Puback error: {puback:?}",
                                    response_arguments.command_name,
                                    pkid
                                );
                                if let Some(completion_tx) = completion_tx {
                                    // Ignore error as receiver may have been dropped
                                    let _ =
                                        completion_tx.send(Err(AIOProtocolError::new_mqtt_error(
                                            Some(
                                                "MQTT error on command executor response puback"
                                                    .to_string(),
                                            ),
                                            Box::new(e),
                                            Some(response_arguments.command_name.clone()),
                                        )));
                                }
                            }
                        }
                    }
                    Err(e) => {
                        log::error!(
                            "[{}][pkid: {}] Command response Publish completion error: {e}",
                            response_arguments.command_name,
                            pkid
                        );
                        if let Some(completion_tx) = completion_tx {
                            // Ignore error as receiver may have been dropped
                            let _ = completion_tx.send(Err(AIOProtocolError::new_mqtt_error(
                                Some("MQTT error on command executor response publish".to_string()),
                                Box::new(e),
                                Some(response_arguments.command_name.clone()),
                            )));
                        }
                    }
                }
            }
            Err(e) => {
                log::error!(
                    "[{}][pkid: {}] Client error on command executor response publish: {e}",
                    response_arguments.command_name,
                    pkid
                );
                // Notify error publishing
                if let Some(completion_tx) = completion_tx {
                    // Ignore error as receiver may have been dropped
                    let _ = completion_tx.send(Err(AIOProtocolError::new_mqtt_error(
                        Some("MQTT error on command executor response publish".to_string()),
                        Box::new(e),
                        Some(response_arguments.command_name.clone()),
                    )));
                }
            }
        }
    }
}

impl<TReq, TResp> Drop for Executor<TReq, TResp>
where
    TReq: PayloadSerialize + Send + 'static,
    TResp: PayloadSerialize + Send + 'static,
{
    fn drop(&mut self) {
        // Cancel all tasks awaiting responses
        self.cancellation_token.cancel();
        // Close the receiver, once dropped all remaining messages are automatically ack'd
        self.mqtt_receiver.close();

        // If the executor has not been unsubscribed, attempt to unsubscribe
        if State::Subscribed == self.state {
            tokio::spawn({
                let request_topic = self.request_topic_filter.clone();
                let mqtt_client = self.mqtt_client.clone();
                async move {
                    match mqtt_client
                        .unsubscribe(
                            request_topic.clone(),
                            azure_iot_operations_mqtt::control_packet::UnsubscribeProperties::default(),
                        )
                        .await
                    {
                        Ok(_) => {
                            log::debug!(
                                "Executor Unsubscribe sent on topic {request_topic}. Unsuback may still be pending."
                            );
                        }
                        Err(e) => {
                            log::warn!("Executor Unsubscribe error on topic {request_topic}: {e}");
                        }
                    }
                }
            });
        }

        log::info!("[{}] Command Executor has been dropped", self.command_name);
    }
}

fn get_response_message_expiry_interval(command_expiration_time: Instant) -> Option<u32> {
    // Calculate the remaining time until the command expires
    let response_message_expiry_interval =
        command_expiration_time.saturating_duration_since(Instant::now());

    // Check if the entry has expired
    if response_message_expiry_interval.is_zero() {
        // Don't return zero as returning a message expiry interval of zero means the message
        // never expires.
        None
    } else {
        // Rounding remaining expiration time up to the nearest second
        let response_message_expiry_interval =
            if response_message_expiry_interval.subsec_nanos() != 0 {
                // NOTE: We should always be able to add 1 since the seconds portion of the
                // response_message_expiry_interval is always at least one less than its initial
                // value when received in this block.
                // NOTE: Rounding up to the nearest second to ensure the invoker will time out
                // at or before the response expires.
                response_message_expiry_interval.as_secs().saturating_add(1)
            } else {
                response_message_expiry_interval.as_secs()
            };

        match response_message_expiry_interval.try_into() {
            Ok(interval) => Some(interval),
            Err(_) => unreachable!(), // Unreachable, will be less than or equal to u32::MAX, see unit test "test_get_response_message_expiry_interval_at_limit"
        }
    }
}

/// Wait on an [`AckToken`] ack to complete, if the [`CancellationToken`] is cancelled, the ack is dropped.
/// # Arguments
/// * `ack_token` - [`AckToken`] ack to wait on
/// * `executor_cancellation_token` - Cancellation token to check if the ack should be dropped
/// * `pkid` - Packet identifier of the message
async fn handle_ack(
    ack_token: AckToken,
    executor_cancellation_token: CancellationToken,
    pkid: u16,
) {
    tokio::select! {
        () = executor_cancellation_token.cancelled() => { /* executor dropped */ },
        ack_res = ack_token.ack() => {
            match ack_res {
                Ok(ack_ct) => {
                    match ack_ct.await {
                        Ok(()) => log::debug!("[pkid: {pkid}] Command Request Acknowledged"),
                        Err(e) => {
                            match e {
                                azure_iot_operations_mqtt::error::CompletionError::Detached => {
                                    log::warn!("[pkid: {pkid}] Command Request Ack error: {e}");
                                },
                                azure_iot_operations_mqtt::error::CompletionError::Canceled(_) => {
                                    // This means the executor will receive a future ack from the
                                    // session once the dupe comes in.
                                    log::warn!("[pkid: {pkid}] Command Request ack cancelled due to disconnect, request will be redelivered");
                                },
                            }
                         }
                    }
                },
                Err(e) => {
                    log::warn!("[pkid: {pkid}] Command Request Ack error: {e}");
                }
            }
        }
    }
}

async fn handle_in_progress_duplicate_ack(
    ack_token: AckToken,
    in_progress_cancellation_token: CancellationToken,
    executor_cancellation_token: CancellationToken,
    pkid: u16,
) {
    tokio::select! {
        () = executor_cancellation_token.cancelled() => { /* executor dropped */ },
        () = in_progress_cancellation_token.cancelled() => {
            // This means the in progress command has finished processing, we can now ack the
            // duplicate ack
            handle_ack(ack_token, executor_cancellation_token, pkid).await;
        }
    }
}

#[cfg(test)]
mod tests {
    use azure_iot_operations_mqtt::session::{Session, SessionOptionsBuilder};
    use test_case::test_case;
    // TODO: This dependency on MqttConnectionSettingsBuilder should be removed in lieu of using a true mock
    use azure_iot_operations_mqtt::aio::connection_settings::MqttConnectionSettingsBuilder;

    use super::*;
    use crate::application::ApplicationContextBuilder;
    use crate::common::{aio_protocol_error::AIOProtocolErrorKind, payload_serialize::MockPayload};

    // TODO: This should return a mock ManagedClient instead.
    // Until that's possible, need to return a Session so that the Session doesn't go out of
    // scope and render the ManagedClient unable to to be used correctly.
    fn create_session() -> Session {
        let connection_settings = MqttConnectionSettingsBuilder::default()
            .hostname("localhost")
            .client_id("test_server")
            .build()
            .unwrap();
        let session_options = SessionOptionsBuilder::default()
            .connection_settings(connection_settings)
            .build()
            .unwrap();
        Session::new(session_options).unwrap()
    }

    fn create_topic_tokens() -> HashMap<String, String> {
        HashMap::from([
            ("executorId".to_string(), "test_executor_id".to_string()),
            ("commandName".to_string(), "test_command_name".to_string()),
        ])
    }

    #[tokio::test]
    async fn test_new_defaults() {
        let session = create_session();
        let managed_client = session.create_managed_client();
        let executor_options = OptionsBuilder::default()
            .request_topic_pattern("test/{commandName}/{executorId}/request")
            .command_name("test_command_name")
            .topic_token_map(create_topic_tokens())
            .build()
            .unwrap();

        let executor: Executor<MockPayload, MockPayload> = Executor::new(
            ApplicationContextBuilder::default().build().unwrap(),
            managed_client,
            executor_options,
        )
        .unwrap();

        assert_eq!(
            executor
                .request_topic_pattern
                .as_subscribe_topic()
                .unwrap()
                .as_str(),
            "test/test_command_name/test_executor_id/request"
        );

        assert!(!executor.is_idempotent);
    }

    #[tokio::test]
    async fn test_new_override_defaults() {
        let session = create_session();
        let managed_client = session.create_managed_client();
        let executor_options = OptionsBuilder::default()
            .request_topic_pattern("test/{commandName}/{executorId}/request")
            .command_name("test_command_name")
            .topic_namespace("test_namespace")
            .topic_token_map(create_topic_tokens())
            .is_idempotent(true)
            .build()
            .unwrap();

        let executor: Executor<MockPayload, MockPayload> = Executor::new(
            ApplicationContextBuilder::default().build().unwrap(),
            managed_client,
            executor_options,
        )
        .unwrap();

        assert_eq!(
            executor
                .request_topic_pattern
                .as_subscribe_topic()
                .unwrap()
                .as_str(),
            "test_namespace/test/test_command_name/test_executor_id/request"
        );

        assert!(executor.is_idempotent);
    }

    #[test_case(""; "empty command name")]
    #[test_case(" "; "whitespace command name")]
    #[tokio::test]
    async fn test_new_empty_and_whitespace_command_name(command_name: &str) {
        let session = create_session();
        let managed_client = session.create_managed_client();

        let executor_options = OptionsBuilder::default()
            .request_topic_pattern("test/{commandName}/request")
            .command_name(command_name.to_string())
            .topic_token_map(create_topic_tokens())
            .build()
            .unwrap();

        let executor: Result<Executor<MockPayload, MockPayload>, AIOProtocolError> = Executor::new(
            ApplicationContextBuilder::default().build().unwrap(),
            managed_client,
            executor_options,
        );

        match executor {
            Err(e) => {
                assert_eq!(e.kind, AIOProtocolErrorKind::ConfigurationInvalid);
                assert!(e.is_shallow);
                assert!(!e.is_remote);
                assert_eq!(e.property_name, Some("command_name".to_string()));
                assert!(e.property_value == Some(Value::String(command_name.to_string())));
            }
            Ok(_) => {
                panic!("Expected error");
            }
        }
    }

    #[test_case(""; "empty request topic pattern")]
    #[test_case(" "; "whitespace request topic pattern")]
    #[test_case("test/{commandName}/\u{0}/request"; "invalid request topic pattern")]
    #[tokio::test]
    async fn test_invalid_request_topic_string(request_topic: &str) {
        let session = create_session();
        let managed_client = session.create_managed_client();

        let executor_options = OptionsBuilder::default()
            .request_topic_pattern(request_topic.to_string())
            .command_name("test_command_name")
            .topic_token_map(create_topic_tokens())
            .build()
            .unwrap();

        let executor: Result<Executor<MockPayload, MockPayload>, AIOProtocolError> = Executor::new(
            ApplicationContextBuilder::default().build().unwrap(),
            managed_client,
            executor_options,
        );

        match executor {
            Err(e) => {
                assert_eq!(e.kind, AIOProtocolErrorKind::ConfigurationInvalid);
                assert!(e.is_shallow);
                assert!(!e.is_remote);
                assert_eq!(
                    e.property_name,
                    Some("executor_options.request_topic_pattern".to_string())
                );
                assert!(e.property_value == Some(Value::String(request_topic.to_string())));
            }
            Ok(_) => {
                panic!("Expected error");
            }
        }
    }

    #[test_case(""; "empty topic namespace")]
    #[test_case(" "; "whitespace topic namespace")]
    #[test_case("test/\u{0}"; "invalid topic namespace")]
    #[tokio::test]
    async fn test_invalid_topic_namespace(topic_namespace: &str) {
        let session = create_session();
        let managed_client = session.create_managed_client();
        let executor_options = OptionsBuilder::default()
            .request_topic_pattern("test/{commandName}/request")
            .command_name("test_command_name")
            .topic_namespace(topic_namespace.to_string())
            .topic_token_map(create_topic_tokens())
            .build()
            .unwrap();

        let executor: Result<Executor<MockPayload, MockPayload>, AIOProtocolError> = Executor::new(
            ApplicationContextBuilder::default().build().unwrap(),
            managed_client,
            executor_options,
        );
        match executor {
            Err(e) => {
                assert_eq!(e.kind, AIOProtocolErrorKind::ConfigurationInvalid);
                assert!(e.is_shallow);
                assert!(!e.is_remote);
                assert_eq!(e.property_name, Some("topic_namespace".to_string()));
                assert!(e.property_value == Some(Value::String(topic_namespace.to_string())));
            }
            Ok(_) => {
                panic!("Expected error");
            }
        }
    }

    #[tokio::test]
    async fn test_shutdown_without_subscribe() {
        let session = create_session();
        let executor_options = OptionsBuilder::default()
            .request_topic_pattern("test/request")
            .command_name("test_command_name")
            .build()
            .unwrap();
        let mut executor: Executor<MockPayload, MockPayload> = Executor::new(
            ApplicationContextBuilder::default().build().unwrap(),
            session.create_managed_client(),
            executor_options,
        )
        .unwrap();
        assert!(executor.shutdown().await.is_ok());
    }

    // Command Response tests
    #[test]
    fn test_response_serialization_error() {
        let mut mock_response_payload = MockPayload::new();
        mock_response_payload
            .expect_serialize()
            .returning(|| Err("dummy error".to_string()))
            .times(1);

        let mut binding = ResponseBuilder::default();
        let resp_builder = binding.payload(mock_response_payload);
        match resp_builder {
            Err(e) => {
                assert_eq!(e.kind, AIOProtocolErrorKind::PayloadInvalid);
            }
            Ok(_) => {
                panic!("Expected error");
            }
        }
    }
    #[test]
    fn test_response_serialization_bad_content_type_error() {
        let mut mock_response_payload = MockPayload::new();
        mock_response_payload
            .expect_serialize()
            .returning(|| {
                Ok(SerializedPayload {
                    payload: Vec::new(),
                    content_type: "application/json\u{0000}".to_string(),
                    format_indicator: FormatIndicator::Utf8EncodedCharacterData,
                })
            })
            .times(1);

        let mut binding = ResponseBuilder::default();
        let resp_builder = binding.payload(mock_response_payload);
        match resp_builder {
            Err(e) => {
                assert_eq!(e.kind, AIOProtocolErrorKind::ConfigurationInvalid);
                assert!(e.is_shallow);
                assert!(!e.is_remote);
                assert_eq!(e.property_name, Some("content_type".to_string()));
                assert!(
                    e.property_value == Some(Value::String("application/json\u{0000}".to_string()))
                );
            }
            Ok(_) => {
                panic!("Expected error");
            }
        }
    }

    #[test]
    fn test_response_invalid_custom_user_data_cloud_event_header() {
        let mut mock_response_payload = MockPayload::new();
        mock_response_payload
            .expect_serialize()
            .returning(|| {
                Ok(SerializedPayload {
                    payload: Vec::new(),
                    content_type: "application/json".to_string(),
                    format_indicator: FormatIndicator::Utf8EncodedCharacterData,
                })
            })
            .times(1);

        let response_builder_result = ResponseBuilder::default()
            .payload(mock_response_payload)
            .unwrap()
            .custom_user_data(vec![("source".to_string(), "test".to_string())])
            .build();

        assert!(response_builder_result.is_err());
    }

    #[test]
    fn test_response_defaults() {
        let mut mock_response_payload = MockPayload::new();
        mock_response_payload
            .expect_serialize()
            .returning(|| {
                Ok(SerializedPayload {
                    payload: Vec::new(),
                    content_type: "application/json".to_string(),
                    format_indicator: FormatIndicator::Utf8EncodedCharacterData,
                })
            })
            .times(1);

        let response_builder_result = ResponseBuilder::default()
            .payload(mock_response_payload)
            .unwrap()
            .build();

        let r = response_builder_result.unwrap();

        assert!(r.custom_user_data.is_empty());
        assert!(r.cloud_event.is_none());
        assert!(r.serialized_payload.payload.is_empty());
    }

    #[tokio::test]
    async fn test_cache_not_found() {
        let cache = Cache(Arc::new(Mutex::new(HashMap::new())));
        let key = CacheKey {
            response_topic: TopicName::new("test_response_topic").unwrap(),
            correlation_data: Bytes::from("test_correlation_data"),
        };
        let status = cache.get(&key);
        assert!(matches!(status, CacheLookupResult::NotFound));
    }

    #[test]
    fn test_cache_found_complete() {
        let cache = Cache(Arc::new(Mutex::new(HashMap::new())));
        let key = CacheKey {
            response_topic: TopicName::new("test_response_topic").unwrap(),
            correlation_data: Bytes::from("test_correlation_data"),
        };
        let entered_serialized_payload = SerializedPayload {
            payload: Bytes::from("test_payload").to_vec(),
            content_type: "application/json".to_string(),
            format_indicator: FormatIndicator::Utf8EncodedCharacterData,
        };
        let entry = CacheEntry::Cached {
            serialized_payload: entered_serialized_payload.clone(),
            properties: PublishProperties::default(),
            expiration_time: Instant::now() + Duration::from_secs(60),
        };
        cache.set(key.clone(), entry.clone());
        let status = cache.get(&key);
        match status {
            CacheLookupResult::Cached {
                serialized_payload,
                properties,
                response_message_expiry_interval,
            } => {
                assert_eq!(serialized_payload, entered_serialized_payload);
                assert_eq!(properties, PublishProperties::default());
                // The expiry interval should be between 1 and 60 seconds, 60 seconds should not pass
                // before this check is made and we will always round up to the nearest second.
                let range = 1..=60;
                assert!(range.contains(&response_message_expiry_interval));
            }
            _ => {
                panic!("Expected cached entry");
            }
        }
    }

    #[test]
    fn test_cache_found_in_progress() {
        let cache = Cache(Arc::new(Mutex::new(HashMap::new())));
        let key = CacheKey {
            response_topic: TopicName::new("test_response_topic").unwrap(),
            correlation_data: Bytes::from("test_correlation_data"),
        };
        let entry = CacheEntry::InProgress {
            processing_cancellation_token: CancellationToken::new(),
        };
        cache.set(key.clone(), entry.clone());

        match cache.get(&key) {
            CacheLookupResult::InProgress(_) => { /* Success */ }
            _ => {
                panic!("Expected in progress entry");
            }
        }
    }

    #[test]
    fn test_cache_expired_entry_not_found() {
        let cache = Cache(Arc::new(Mutex::new(HashMap::new())));
        let key = CacheKey {
            response_topic: TopicName::new("test_response_topic").unwrap(),
            correlation_data: Bytes::from("test_correlation_data"),
        };
        let entry = CacheEntry::Cached {
            serialized_payload: SerializedPayload {
                payload: Bytes::from("test_payload").to_vec(),
                content_type: "application/json".to_string(),
                format_indicator: FormatIndicator::Utf8EncodedCharacterData,
            },
            properties: PublishProperties::default(),
            expiration_time: Instant::now() - Duration::from_secs(60),
        };
        cache.set(key.clone(), entry);
        let status = cache.get(&key);
        assert!(matches!(status, CacheLookupResult::NotFound));

        // Set a new entry and check if the expired entry is deleted
        let new_serialized_payload = SerializedPayload {
            payload: Bytes::from("new_test_payload").to_vec(),
            content_type: "application/json".to_string(),
            format_indicator: FormatIndicator::Utf8EncodedCharacterData,
        };
        let new_entry = CacheEntry::Cached {
            serialized_payload: new_serialized_payload.clone(),
            properties: PublishProperties::default(),
            expiration_time: Instant::now() + Duration::from_secs(60),
        };
        // The cache should never see another entry with the same key, this is for testing purposes only.
        cache.set(key.clone(), new_entry.clone());

        let new_status = cache.get(&key);
        match new_status {
            CacheLookupResult::Cached {
                serialized_payload,
                properties,
                response_message_expiry_interval,
            } => {
                assert_eq!(serialized_payload, new_serialized_payload);
                assert_eq!(properties, PublishProperties::default());
                // The expiry interval should be between 1 and 60 seconds, 60 seconds should not pass
                // before this check is made and we will always round up to the nearest second.
                let range = 1..=60;
                assert!(range.contains(&response_message_expiry_interval));
            }
            _ => {
                panic!("Expected cached entry");
            }
        }
    }

    #[test]
    fn test_cache_expired_entry_not_found_with_different_key_set() {
        let cache = Cache(Arc::new(Mutex::new(HashMap::new())));
        let old_key = CacheKey {
            response_topic: TopicName::new("test_response_topic").unwrap(),
            correlation_data: Bytes::from("test_correlation_data"),
        };
        let old_entry = CacheEntry::Cached {
            serialized_payload: SerializedPayload {
                payload: Bytes::from("test_payload").to_vec(),
                content_type: "application/json".to_string(),
                format_indicator: FormatIndicator::Utf8EncodedCharacterData,
            },
            properties: PublishProperties::default(),
            expiration_time: Instant::now() - Duration::from_secs(60),
        };
        cache.set(old_key.clone(), old_entry);
        let status = cache.get(&old_key);
        assert!(matches!(status, CacheLookupResult::NotFound));

        // Set a new entry with a different key and check if the expired entry is deleted
        let new_key = CacheKey {
            response_topic: TopicName::new("new_test_response_topic").unwrap(),
            correlation_data: Bytes::from("new_test_correlation_data"),
        };
        let new_serialized_payload = SerializedPayload {
            payload: Bytes::from("new_test_payload").to_vec(),
            content_type: "application/json".to_string(),
            format_indicator: FormatIndicator::Utf8EncodedCharacterData,
        };
        let new_entry = CacheEntry::Cached {
            serialized_payload: new_serialized_payload.clone(),
            properties: PublishProperties::default(),
            expiration_time: Instant::now() + Duration::from_secs(60),
        };
        cache.set(new_key.clone(), new_entry.clone());

        let old_status = cache.get(&old_key);
        assert!(matches!(old_status, CacheLookupResult::NotFound));
        let new_status = cache.get(&new_key);

        match new_status {
            CacheLookupResult::Cached {
                serialized_payload,
                properties,
                response_message_expiry_interval,
            } => {
                assert_eq!(serialized_payload, new_serialized_payload);
                assert_eq!(properties, PublishProperties::default());
                // The expiry interval should be between 1 and 60 seconds, 60 seconds should not pass
                // before this check is made and we will always round up to the nearest second.
                let range = 1..=60;
                assert!(range.contains(&response_message_expiry_interval));
            }
            _ => {
                panic!("Expected cached entry");
            }
        }
    }

    #[test]
    fn test_cache_in_progress_found_with_different_key_set() {
        let cache = Cache(Arc::new(Mutex::new(HashMap::new())));
        let old_key = CacheKey {
            response_topic: TopicName::new("test_response_topic").unwrap(),
            correlation_data: Bytes::from("test_correlation_data"),
        };
        let old_entry = CacheEntry::InProgress {
            processing_cancellation_token: CancellationToken::new(),
        };
        cache.set(old_key.clone(), old_entry);
        let status = cache.get(&old_key);
        // While in progress, the entry can't expire.
        assert!(matches!(status, CacheLookupResult::InProgress(..)));

        // Set a new entry with a different key and check if the in progress entry is still present
        let new_key = CacheKey {
            response_topic: TopicName::new("new_test_response_topic").unwrap(),
            correlation_data: Bytes::from("new_test_correlation_data"),
        };
        let new_serialized_payload = SerializedPayload {
            payload: Bytes::from("new_test_payload").to_vec(),
            content_type: "application/json".to_string(),
            format_indicator: FormatIndicator::Utf8EncodedCharacterData,
        };
        let new_entry = CacheEntry::Cached {
            serialized_payload: new_serialized_payload.clone(),
            properties: PublishProperties::default(),
            expiration_time: Instant::now() + Duration::from_secs(60),
        };
        cache.set(new_key.clone(), new_entry.clone());

        let old_status = cache.get(&old_key);
        assert!(matches!(old_status, CacheLookupResult::InProgress(..)));
        let new_status = cache.get(&new_key);

        match new_status {
            CacheLookupResult::Cached {
                serialized_payload,
                properties,
                response_message_expiry_interval,
            } => {
                assert_eq!(serialized_payload, new_serialized_payload);
                assert_eq!(properties, PublishProperties::default());
                // The expiry interval should be between 1 and 60 seconds, 60 seconds should not pass
                // before this check is made and we will always round up to the nearest second.
                let range = 1..=60;
                assert!(range.contains(&response_message_expiry_interval));
            }
            _ => {
                panic!("Expected cached entry");
            }
        }
    }

    #[test]
    fn test_cache_in_progress_notified_completion() {
        // This tests the verified flow of registering to completion in case a dupe comes in
        let cache = Cache(Arc::new(Mutex::new(HashMap::new())));
        let processing_cancellation_token = CancellationToken::new();
        let key = CacheKey {
            response_topic: TopicName::new("test_response_topic").unwrap(),
            correlation_data: Bytes::from("test_correlation_data"),
        };
        let entry = CacheEntry::InProgress {
            processing_cancellation_token: processing_cancellation_token.clone(),
        };
        cache.set(key.clone(), entry.clone());

        {
            // This simulates processing the command
            let _processing_drop_guard = processing_cancellation_token.drop_guard();

            match cache.get(&key) {
                CacheLookupResult::InProgress(_) => { /* Success */ }
                _ => {
                    panic!("Expected in progress entry");
                }
            }
        }

        // If a dupe comes in, and checks the cancellation token it should know it can ack
        match cache.get(&key) {
            CacheLookupResult::InProgress(cancellation_token) => {
                // Since the command processing simulation ended, the cancellation token should be cancelled
                assert!(cancellation_token.is_cancelled());
            }
            _ => {
                panic!("Expected in progress entry");
            }
        }
    }

    #[test]
    fn test_response_add_empty_error_payload_success() {
        let mut mock_response_payload = MockPayload::new();
        mock_response_payload
            .expect_serialize()
            .returning(|| {
                Ok(SerializedPayload {
                    payload: Vec::new(),
                    content_type: "application/json".to_string(),
                    format_indicator: FormatIndicator::Utf8EncodedCharacterData,
                })
            })
            .times(1);

        let mut custom_user_data = Vec::new();
        assert!(
            application_error_headers(&mut custom_user_data, "500".into(), "  ".into()).is_ok()
        );

        let response = ResponseBuilder::default()
            .custom_user_data(custom_user_data)
            .payload(mock_response_payload)
            .unwrap()
            .build()
            .unwrap();

        assert_eq!(response.custom_user_data.len(), 1);

        let mut app_error_code_header_found = false;
        let mut app_error_payload_header_found = false;
        for (key, value) in response.custom_user_data {
            if key == "AppErrCode" {
                app_error_code_header_found = true;
                assert_eq!(value, "500");
            }
            if key == "AppErrPayload" {
                app_error_payload_header_found = true;
            }
        }

        assert!(app_error_code_header_found);
        assert!(!app_error_payload_header_found);
    }

    #[test]
    fn test_response_add_empty_error_code_error() {
        let mut custom_user_data = Vec::new();

        assert!(
            application_error_headers(&mut custom_user_data, " ".into(), "Some error".into())
                .is_err()
        );

        assert_eq!(custom_user_data.len(), 0);
    }

    #[test]
    fn test_get_response_message_expiry_interval_not_expired() {
        let response_message_expiry_interval =
            get_response_message_expiry_interval(Instant::now() + Duration::from_secs(10));

        let range = 1..=10;
        // Should be between 1 and 10 seconds
        assert!(range.contains(&response_message_expiry_interval.unwrap()));
    }

    #[test]
    fn test_get_response_message_expiry_inteval_expired() {
        let response_message_expiry_interval =
            get_response_message_expiry_interval(Instant::now() - Duration::from_secs(10));

        assert!(response_message_expiry_interval.is_none());
    }

    #[test]
    fn test_get_response_message_expiry_interval_at_limit() {
        // When the message_expiry_interval property is received it is bounded to a u32 meaning that the
        // maximum value is u32::MAX seconds. We test that the function correctly handles this upper limit.
        let response_message_expiry_interval = get_response_message_expiry_interval(
            Instant::now() + Duration::from_secs(u64::from(u32::MAX)),
        );

        let range = 1..=u32::MAX; // note, range is memory efficient
        assert!(range.contains(&response_message_expiry_interval.unwrap()));
    }
}

// Test cases for subscribe
// Tests success:
//   start() is called and successfully receives suback
//   stop() is called and successfully receives unsuback
// Tests failure:
//   start() is called and receives a suback with a bad rc
//   stop() is called and receives an unsuback with a bad rc
//   start() is called and the subscribe call fails (invalid filter or failure on sending outbound sub async)
//   stop() is called and the unsubscribe call fails (invalid filter or failure on sending outbound unsub async)
//
// Test cases for recv request
// Tests success:
//   recv() is called and successfully sends a command request to the application
//   response topic, correlation data, invoker id, and payload are valid and successfully received
//   if payload format indicator, content type, and timestamp are present, they are validated successfully
//
// Tests failure:
//   if an error response is published, the original request is acked
//   response topic is invalid and command response is not published and original request is acked
//   correlation data, invoker id, or payload are missing and error response is published and original request is acked
//   if payload format indicator, content type, and timestamp are present and invalid, error response is published and original request is acked
//
// Test cases for response processing
// Tests success:
//    a command response is received and successfully published, the original request is acked
//    response payload is serialized and published
//    an empty response payload has a status code of NoContent
//
// Tests failure:
//    an error occurs while processing the command response, an error response is sent and the original request is acked
//    response payload is not serialized and an error response is sent and the original request is acked
//
// Test cases for timeout
// Tests success:
//   a command request is received and a response is published before the command expiration time, the original request is acked
//   a command request is received and a response is not published after the command expiration time, the original request is acked
// Tests failure:
//   a command request is received and the command expiration time cannot be calculated, an error response is sent to the invoker and executor application and the original request is acked