sacn 0.11.1

A Rust implementation of the ANSI E1.31 Streaming ACN protocol, tested against protocol version ANSI E1.31-2018.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
#![cfg_attr(rustfmt, rustfmt_skip)]
// Copyright 2020 sacn Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
//
// This file was created as part of a University of St Andrews Computer Science BSC Senior Honours Dissertation Project.

extern crate sacn;
extern crate uuid;
extern crate socket2;

use std::io::Read;
use std::{array, thread};
use std::thread::sleep;
use std::sync::mpsc;
use std::sync::mpsc::{Sender, SyncSender, Receiver, RecvTimeoutError};
use std::time::{Duration, Instant};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::iter;
use std::convert::TryInto; // Used for converting between u8 and u16 representations.
use std::str; // Used for converting between bytes and strings.

use sacn::source::SacnSource;
use sacn::receive::{SacnReceiver, DMXData, htp_dmx_merge};
use sacn::packet::*;
use sacn::error::errors::*;

/// UUID library used to handle the UUID's used in the CID fields.
use uuid::Uuid;

/// Socket2 used to create sockets for testing.
use socket2::{Socket, Domain, Type};

/// For some tests to work multiple instances of the protocol must be on the same network with the same port for example to test multiple simultaneous receivers, this means multiple IP's are needed.
/// This is achieved by assigning multiple static IP's to the test machine and theses IP's are specified below.
/// Theses must be changed depending on the network that the test machine is on.
pub const TEST_NETWORK_INTERFACE_IPV4: [&'static str; 3] = ["192.168.0.6", "192.168.0.7", "192.168.0.8"];


pub const TEST_DATA_PARTIAL_CAPACITY_UNIVERSE: [u8; 313] = [0,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
    ];

pub const TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE: [u8; 513] = [1,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
    ];

pub const TEST_DATA_SINGLE_UNIVERSE: [u8; 513] = [0,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
    ];

pub const TEST_DATA_MULTIPLE_ALTERNATIVE_STARTCODE_UNIVERSE: [u8; 714] = [1,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,

        3,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,
    ];

pub const TEST_DATA_MULTIPLE_UNIVERSE: [u8; 714] = [0,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,

        0,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,
    ];
pub const TEST_DATA_FULL_CAPACITY_MULTIPLE_UNIVERSE: [u8; 1026] = [0,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
        0,
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100,

        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
    ];


// Note: For this test to work the PC must be capable of connecting to the network on 2 IP's, this was done in windows by adding another static IP so the PC was connecting through
// 2 different IP's to the network. Theses IPs are manually specified in the TEST_NETWORK_INTERFACE_IPV4 constant and so to run it must be changed
// depending on the environment.
#[test]
#[ignore]
fn test_send_single_universe_multiple_receivers_multicast_ipv4(){
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread1_tx = tx.clone();
    let thread2_tx = tx.clone();

    let universe = 1;

    let rcv_thread1 = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe]).unwrap();

        thread1_tx.send(Ok(Vec::new())).unwrap();

        thread1_tx.send(dmx_recv.recv(None)).unwrap();
    });

    let rcv_thread2 = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[1].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe]).unwrap();

        thread2_tx.send(Ok(Vec::new())).unwrap();

        thread2_tx.send(dmx_recv.recv(None)).unwrap();
    });

    rx.recv().unwrap().unwrap(); // Blocks until both receivers say they are ready.
    rx.recv().unwrap().unwrap();

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);

    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    let priority = 100;

    src.register_universe(universe).unwrap();

    src.send(&[universe], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), None, None).unwrap();

    let received_result1: Result<Vec<DMXData>> = rx.recv().unwrap();
    let received_result2: Result<Vec<DMXData>> = rx.recv().unwrap();

    rcv_thread1.join().unwrap();
    rcv_thread2.join().unwrap();

    assert!(!received_result1.is_err(), "Failed: Error when receiving data");
    let received_data1: Vec<DMXData> = received_result1.unwrap();
    assert_eq!(received_data1.len(), 1); // Check only 1 universe received as expected.
    let received_universe1: DMXData = received_data1[0].clone();
    assert_eq!(received_universe1.universe, universe); // Check that the universe received is as expected.
    assert_eq!(received_universe1.values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Received payload values don't match sent!");

    assert!(!received_result2.is_err(), "Failed: Error when receiving data");
    let received_data2: Vec<DMXData> = received_result2.unwrap();
    assert_eq!(received_data2.len(), 1); // Check only 1 universe received as expected.
    let received_universe2: DMXData = received_data2[0].clone();
    assert_eq!(received_universe2.universe, universe); // Check that the universe received is as expected.
    assert_eq!(received_universe2.values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Received payload values don't match sent!");
}

#[test]
#[ignore]
fn test_send_across_universe_multiple_receivers_sync_multicast_ipv4(){
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread1_tx = tx.clone();
    let thread2_tx = tx.clone();

    let universe1 = 1;
    let universe2 = 2;

    let sync_uni = 3;

    let rcv_thread1 = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe1]).unwrap();
        dmx_recv.listen_universes(&[sync_uni]).unwrap();

        thread1_tx.send(Ok(Vec::new())).unwrap();

        thread1_tx.send(dmx_recv.recv(None)).unwrap();
    });

    let rcv_thread2 = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[1].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe2]).unwrap();
        dmx_recv.listen_universes(&[sync_uni]).unwrap();

        thread2_tx.send(Ok(Vec::new())).unwrap();

        thread2_tx.send(dmx_recv.recv(None)).unwrap();
    });

    rx.recv().unwrap().unwrap(); // Blocks until both receivers say they are ready.
    rx.recv().unwrap().unwrap();

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);

    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    let priority = 100;

    src.register_universe(universe1).unwrap();
    src.register_universe(universe2).unwrap();
    src.register_universe(sync_uni).unwrap();

    src.send(&[universe1], &TEST_DATA_MULTIPLE_UNIVERSE[..513], Some(priority), None, Some(sync_uni)).unwrap();
    src.send(&[universe2], &TEST_DATA_MULTIPLE_UNIVERSE[513..], Some(priority), None, Some(sync_uni)).unwrap();

    // Waiting to receive, if anything is received it indicates one of the receivers progressed without waiting for synchronisation.
    // This has the issue that is is possible that even though they could have progressed the receive threads may not have leading them to pass this part
    // when they shouldn't. This is difficult to avoid using this method of testing. It is also possible for the delay on the network to be so high that it
    // causes the timeout, this is also difficult to avoid. Both of these reasons should be considered if this test passes occasionally but not consistently.
    // The timeout should be large enough to make this unlikely although must be lower than the protocol's in-built timeout.
    const WAIT_RECV_TIMEOUT: u64 = 2;
    let attempt_recv = rx.recv_timeout(Duration::from_secs(WAIT_RECV_TIMEOUT));

    match attempt_recv {
        Ok(_) => {
            assert!(false, "Receivers received without waiting for sync");
        },
        Err(e) => assert_eq!(e, RecvTimeoutError::Timeout)
    }

    src.send_sync_packet(sync_uni, None).unwrap();

    let received_result1: Vec<DMXData> = rx.recv().unwrap().unwrap();
    let received_result2: Vec<DMXData> = rx.recv().unwrap().unwrap();

    rcv_thread1.join().unwrap();
    rcv_thread2.join().unwrap();

    assert_eq!(received_result1.len(), 1); // Check only 1 universe received as expected.
    assert_eq!(received_result2.len(), 1); // Check only 1 universe received as expected.

    let mut results = vec![received_result1[0].clone(), received_result2[0].clone()];
    results.sort_unstable(); // Ordering of received data is undefined, to make it easier to check sort first.

    assert_eq!(results[0].universe, universe1); // Check that the universe 1 received is as expected.
    assert_eq!(results[1].universe, universe2); // Check that the universe 2 received is as expected.

    assert_eq!(results[0].values, TEST_DATA_MULTIPLE_UNIVERSE[..513].to_vec());
    assert_eq!(results[1].values, TEST_DATA_MULTIPLE_UNIVERSE[513..].to_vec());
}

#[test]
#[ignore]
fn test_send_recv_single_universe_unicast_ipv4(){
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread_tx = tx.clone();

    let universe = 1;

    let rcv_thread = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe]).unwrap();

        thread_tx.send(Ok(Vec::new())).unwrap();

        thread_tx.send(dmx_recv.recv(None)).unwrap();
    });

    let _ = rx.recv().unwrap(); // Blocks until the receiver says it is ready.

    let ip: SocketAddr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    let priority = 100;

    src.register_universe(universe).unwrap();

    let dst_ip: SocketAddr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), ACN_SDT_MULTICAST_PORT);

    let _ = src.send(&[universe], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), Some(dst_ip), None).unwrap();

    let received_result: Result<Vec<DMXData>> = rx.recv().unwrap();

    rcv_thread.join().unwrap();

    assert!(!received_result.is_err(), "Failed: Error when receiving data");

    let received_data: Vec<DMXData> = received_result.unwrap();

    assert_eq!(received_data.len(), 1); // Check only 1 universe received as expected.

    let received_universe: DMXData = received_data[0].clone();

    assert_eq!(received_universe.universe, universe); // Check that the universe received is as expected.

    assert_eq!(received_universe.values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Received payload values don't match sent!");
}

/// A test showing a single universe of data being sent from a sender to a receiver over multicast on IPv4.
/// This test has more comments than usage as it is used as an example.
#[test]
#[ignore]
fn test_send_recv_single_universe_multicast_ipv4(){
    // The universe and priority of the data used in this test.
    const UNIVERSE: u16 = 1;
    const PRIORITY: u8 = 100;

    // Allows control of the receiver and sender so that they can be put into the correct state for the test.
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();
    let thread_tx = tx.clone();

    // A simulated receiver, this is independent from the sender (apart from the communication channel for syncing states).
    let rcv_thread = thread::spawn(move || {
        // The receiver binds to a test IP and the ACN port. This port is the ported used for this protocol so the receiver must bind to it.
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[UNIVERSE]).unwrap();

        // A control message is sent now that the receiver is ready so that the sender can progress.
        thread_tx.send(Ok(Vec::new())).unwrap();

        // The receiver then waits until it receives the data.
        let result = dmx_recv.recv(None);

        // The result of the receiver is then sent back to the original test thread using the control channel.
        // This allows the checking of the results to be done on the first test thread (having the assertions on the same thread behaves better with debug output).
        thread_tx.send(result).unwrap();
    });

    // Blocks until the receiver says it is ready. This stops the sender sending before the receiver is created meaning it would miss the data.
    rx.recv().unwrap().unwrap();

    // The sender is bound to an interface on the same network as the receiver but on a different port.
    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[1].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    // The sender registers the universe for sending and then sends some test data.
    src.register_universe(UNIVERSE).unwrap();
    src.send(&[UNIVERSE], &TEST_DATA_SINGLE_UNIVERSE, Some(PRIORITY), None, None).unwrap();

    // The data that the receiver received is sent back using the thread message passing channel.
    let received_result: Result<Vec<DMXData>> = rx.recv().unwrap();
    rcv_thread.join().unwrap();

    // Check that the receiver received the data without error.
    assert!(!received_result.is_err(), "Failed: Error when receiving data");

    // Check that the data received is as expected.
    let received_data: Vec<DMXData> = received_result.unwrap();
    assert_eq!(received_data.len(), 1); // Check only 1 universe received as expected.

    let received_universe: DMXData = received_data[0].clone();
    assert_eq!(received_universe.priority, PRIORITY, "Received priority doesn't match expected");
    assert_eq!(received_universe.universe, UNIVERSE, "Received universe doesn't match expected");
    assert_eq!(received_universe.values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Received payload values don't match sent!");
}

/// A single sender transfers 260 data packets to a single receiver.
/// Since the sequence number field is a single unsigned byte (highest value 255) this should over flow the sequence number and so therefore this
/// test checks that the implementations handle this as expected by continuing as normal.
///
#[test]
#[ignore]
fn test_send_recv_single_universe_overflow_sequence_number_multicast_ipv4(){
    const DATA_PACKETS_TO_SEND: usize = 260;

    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread_tx = tx.clone();

    let universe = 1;

    // By having the receiver be 'remote' and then send back to the sender it means the sender can check the data it has sent is correct.
    let rcv_thread = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe]).unwrap();

        thread_tx.send(Ok(Vec::new())).unwrap();

        for _ in 0 .. DATA_PACKETS_TO_SEND {
            thread_tx.send(dmx_recv.recv(None)).unwrap();
        }
    });

    rx.recv().unwrap().unwrap(); // Blocks until the receiver says it is ready.

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    src.register_universe(universe).unwrap();

    for i in 0 .. DATA_PACKETS_TO_SEND {
        src.send(&[universe], &TEST_DATA_SINGLE_UNIVERSE[0 .. i + 1], None, None, None).unwrap(); // Vary the data each packet.
        let received_data: Vec<DMXData> = rx.recv().unwrap().unwrap(); // Asserts that the data was received successfully without error.
        assert_eq!(received_data.len(), 1); // Check only 1 universe received at a time as expected.
        let received_universe: DMXData = received_data[0].clone();

        assert_eq!(received_universe.universe, universe); // Check that the universe received is as expected.

        assert_eq!(received_universe.values, TEST_DATA_SINGLE_UNIVERSE[0 .. i + 1].to_vec(), "Received payload values don't match sent!");
    }

    // Finished with the receiver.
    rcv_thread.join().unwrap();
}

/// Sends 2 packets with the same universe and synchronisation address from a sender to a receiver, the first packet has a priority of 110
/// and the second a priority of 109. The receiver should discard the second packet when received due to its higher priority as per ANSI E1.31-2018 Section 6.2.3.
/// A sync packet is then sent and the receiver output checked that the right packet was kept.
/// Tests that lower priority packets are correctly discarded.
#[test]
#[ignore]
fn test_send_recv_diff_priority_same_universe_multicast_ipv4(){
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread_tx = tx.clone();

    let universe = 1;

    let rcv_thread = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe]).unwrap();

        thread_tx.send(Ok(Vec::new())).unwrap();

        thread_tx.send(dmx_recv.recv(None)).unwrap();
    });

    rx.recv().unwrap().unwrap(); // Blocks until the receiver says it is ready.

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    let priority = 110;
    let priority_2 = 109;

    src.register_universe(universe).unwrap();

    src.send(&[universe], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), None, Some(universe)).unwrap(); // First packet with higher priority.
    src.send(&[universe], &TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE, Some(priority_2), None, Some(universe)).unwrap(); // Second packet with lower priority.
    src.send_sync_packet(universe, None).unwrap(); // Trigger the packet to be passed up on the receiver.

    let received_result: Result<Vec<DMXData>> = rx.recv().unwrap();

    rcv_thread.join().unwrap();

    assert!(!received_result.is_err(), "Failed: Error when receiving data");

    let received_data: Vec<DMXData> = received_result.unwrap();

    assert_eq!(received_data.len(), 1); // Check only 1 universe received as expected.

    let received_universe: DMXData = received_data[0].clone();

    assert_eq!(received_universe.universe, universe); // Check that the universe received is as expected.

    assert_eq!(received_universe.values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Received payload values don't match sent!");
}

/// Sends 2 packets with the same universe, priority and synchronisation address from a sender to a receiver.
/// The receiver should discard the first packet when the second arrives as per ANSI E1.31-2018 Section 6.2.3.
/// A sync packet is then sent and the receiver output checked that the right packet was kept.
/// Tests that older packet is correctly discarded.
#[test]
#[ignore]
fn test_send_recv_two_packets_same_priority_same_universe_multicast_ipv4(){
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread_tx = tx.clone();

    let universe = 1;

    let rcv_thread = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe]).unwrap();

        thread_tx.send(Ok(Vec::new())).unwrap();

        thread_tx.send(dmx_recv.recv(None)).unwrap();
    });

    rx.recv().unwrap().unwrap(); // Blocks until the receiver says it is ready.

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    let priority = 110;

    src.register_universe(universe).unwrap();

    src.send(&[universe], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), None, Some(universe)).unwrap(); // First packet
    src.send(&[universe], &TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE, Some(priority), None, Some(universe)).unwrap(); // Second packet which should override first.
    src.send_sync_packet(universe, None).unwrap(); // Trigger the packet to be passed up on the receiver.

    let received_result: Result<Vec<DMXData>> = rx.recv().unwrap();

    rcv_thread.join().unwrap();

    assert!(!received_result.is_err(), "Failed: Error when receiving data");

    let received_data: Vec<DMXData> = received_result.unwrap();

    assert_eq!(received_data.len(), 1); // Check only 1 universe received as expected.

    let received_universe: DMXData = received_data[0].clone();

    assert_eq!(received_universe.universe, universe); // Check that the universe received is as expected.

    assert_eq!(received_universe.values, TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE.to_vec(), "Received payload values don't match sent!");
}

/// Sends data 2 packets with the same universe. The first packet is a synchronised packet with a synchronisation address
/// that is > 0. The second packet isn't synchronised as it has a synchronisation address of 0. This second packet should
/// therefore override the waiting packet as per ANSI E1.31-2018 Section 6.2.4.1.
///
/// To check that the waiting data is discarded the receiver receives once to check the second packet gets through and then
/// the source sends a sync_packet and the receiver receives again, since the waiting data was discarded it is expected that the
/// sync packet should have no effect and the receiver will timeout.
#[test]
#[ignore]
fn test_send_recv_sync_then_nosync_packet_same_universe_multicast_ipv4() {
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread_tx = tx.clone();

    let universe = 1;
    const TIMEOUT: Option<Duration> = Some(Duration::from_secs(2));

    let rcv_thread = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe]).unwrap();

        thread_tx.send(Ok(Vec::new())).unwrap();

        thread_tx.send(dmx_recv.recv(None)).unwrap(); // Receive a packet, expected to be the second packet which has caused the first to be discarded.

        thread_tx.send(dmx_recv.recv(TIMEOUT)).unwrap(); // Attempt to receive a packet, expected to timeout because the other data packet was discarded.
    });

    rx.recv().unwrap().unwrap(); // Blocks until the receiver says it is ready.

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    src.register_universe(universe).unwrap();

    src.send(&[universe], &TEST_DATA_SINGLE_UNIVERSE, None, None, Some(universe)).unwrap(); // First packet, with sync.
    src.send(&[universe], &TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE, None, None, None).unwrap(); // Second packet, no sync.

    src.send_sync_packet(universe, None).unwrap(); // Send a sync packet, if the first packet isn't discarded it should now be passed up.

    let first_received_result: Result<Vec<DMXData>> = rx.recv().unwrap();
    let second_received_result: Result<Vec<DMXData>> = rx.recv().unwrap();

    rcv_thread.join().unwrap(); // Finished with receiver

    // Check that the first lot of data received (which should be the second packet) is as expected.
    assert!(!first_received_result.is_err(), "Unexpected error when receiving first lot of data");
    let received_data: Vec<DMXData> = first_received_result.unwrap();
    assert_eq!(received_data.len(), 1); // Check only 1 universe received as expected.
    let received_universe: DMXData = received_data[0].clone();
    assert_eq!(received_universe.universe, universe); // Check that the universe received is as expected.
    assert_eq!(received_universe.values, TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE.to_vec(), "Received payload values don't match sent!");

    match second_received_result {
        Err(e) => {
            match e {
                SacnError::Io(s) => {
                    match s.kind() {
                        std::io::ErrorKind::WouldBlock => {
                            // Expected to timeout.
                            // The different errors are due to windows and unix returning different errors for the same thing.
                            assert!(true, "Timed out as expected meaning waiting data was successfully discarded");
                        },
                        std::io::ErrorKind::TimedOut => {
                            assert!(true, "Timed out as expected meaning waiting data was successfully discarded");
                        },
                        _ => {
                            assert!(false, "Unexpected error returned");
                        }
                    }
                },
                _ => {
                    assert!(false, "Unexpected error returned");
                }
            }
        }
        Ok(_) => {
            assert!(false, "Second receive attempt didn't timeout as expected, indicates that the synchronised data packet wasn't discarded as expected");
        }
    }

}

#[test]
#[ignore]
fn test_send_recv_two_universe_multicast_ipv4(){
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread_tx = tx.clone();

    let universes = [1, 2];

    let rcv_thread = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&universes).unwrap();

        thread_tx.send(Ok(Vec::new())).unwrap(); // Notify the sender that the receiver is ready.

        thread_tx.send(dmx_recv.recv(None)).unwrap(); // Receive and pass on 2 lots of data, blocking.
        thread_tx.send(dmx_recv.recv(None)).unwrap();
    });

    rx.recv().unwrap().unwrap(); // Blocks until the receiver says it is ready.

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    src.register_universes(&universes).unwrap();

    // Send 2 universes of data with default priority, no synchronisation and use multicast.
    src.send(&universes, &TEST_DATA_MULTIPLE_UNIVERSE, None, None, None).unwrap();

    // Get the data that was sent to the receiver.
    let received_result: Result<Vec<DMXData>> = rx.recv().unwrap();
    let received_result_2: Result<Vec<DMXData>> = rx.recv().unwrap();

    // Receiver can be terminated.
    rcv_thread.join().unwrap();

    assert!(!received_result.is_err(), "Failed: Error when receiving 1st universe of data");
    assert!(!received_result_2.is_err(), "Failed: Error when receiving 2nd universe of data");

    let received_data: Vec<DMXData> = received_result.unwrap();
    let received_data_2: Vec<DMXData> = received_result_2.unwrap();

    assert_eq!(received_data.len(), 1);   // Check only 1 universe received from each individual recv() as expected, if this wasn't the case it would
    assert_eq!(received_data_2.len(), 1); // indicate that the data has been synchronised incorrectly or that less data than expected was received.

    assert_eq!(received_data[0].universe, universes[0]);   // Check that the universe received is as expected.
    assert_eq!(received_data_2[0].universe, universes[1]);

    assert_eq!(received_data[0].values, TEST_DATA_MULTIPLE_UNIVERSE[..513].to_vec());
    assert_eq!(received_data_2[0].values, TEST_DATA_MULTIPLE_UNIVERSE[513..].to_vec());
}

#[test]
#[ignore]
fn test_send_recv_single_universe_alternative_startcode_multicast_ipv4(){
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread_tx = tx.clone();

    let universe = 1;

    let rcv_thread = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&[universe]).unwrap();

        thread_tx.send(Ok(Vec::new())).unwrap();

        thread_tx.send(dmx_recv.recv(None)).unwrap();
    });

    rx.recv().unwrap().unwrap(); // Blocks until the receiver says it is ready.

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    let priority = 100;

    src.register_universe(universe).unwrap();

    src.send(&[universe], &TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE, Some(priority), None, None).unwrap();

    let received_result: Result<Vec<DMXData>> = rx.recv().unwrap();

    rcv_thread.join().unwrap();

    assert!(!received_result.is_err(), "Failed: Error when receiving data");

    let received_data: Vec<DMXData> = received_result.unwrap();

    assert_eq!(received_data.len(), 1); // Check only 1 universe received as expected.

    let received_universe: DMXData = received_data[0].clone();

    assert_eq!(received_universe.universe, universe); // Check that the universe received is as expected.

    assert_eq!(received_universe.values, TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE.to_vec(), "Received payload values don't match sent!");
}

/// Note: this test assumes perfect network conditions (0% reordering, loss, duplication etc.), this should be the case for
/// the loopback adapter with the low amount of data sent but this may be a possible cause if integration tests fail unexpectedly.
#[test]
#[ignore]
fn test_send_recv_across_universe_multicast_ipv4(){
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread_tx = tx.clone();

    const UNIVERSES: [u16; 2] = [2, 3];

    let rcv_thread = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(Ipv4Addr::new(0,0,0,0).into(), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&UNIVERSES).unwrap();

        thread_tx.send(Ok(Vec::new())).unwrap(); // Signal that the receiver is ready to receive.

        thread_tx.send(dmx_recv.recv(None)).unwrap(); // Receive the sync packet, the data packets shouldn't have caused .recv to return as forced to wait for sync.
    });

    let _ = rx.recv().unwrap(); // Blocks until the receiver says it is ready.

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    let priority = 100;

    src.register_universes(&UNIVERSES).unwrap();

    src.send(&UNIVERSES, &TEST_DATA_MULTIPLE_UNIVERSE, Some(priority), None, Some(UNIVERSES[0])).unwrap();
    sleep(Duration::from_millis(500)); // Small delay to allow the data packets to get through as per NSI-E1.31-2018 Appendix B.1 recommendation. See other warnings about the possibility of theses tests failing if the network isn't perfect.
    src.send_sync_packet(UNIVERSES[0], None).unwrap();

    let sync_pkt_res: Result<Vec<DMXData>> = rx.recv().unwrap();

    rcv_thread.join().unwrap();

    assert!(!sync_pkt_res.is_err(), "Failed: Error when receiving packets");

    let mut received_data: Vec<DMXData> = sync_pkt_res.unwrap();

    received_data.sort(); // No guarantee on the ordering of the received data so sort it first to allow easier checking.

    assert_eq!(received_data.len(), 2); // Check 2 universes received as expected.

    assert_eq!(received_data[0].universe, 2); // Check that the universe received is as expected.

    assert_eq!(received_data[0].sync_uni, 2); // Check that the sync universe is as expected.

    assert_eq!(received_data[0].values, TEST_DATA_MULTIPLE_UNIVERSE[..UNIVERSE_CHANNEL_CAPACITY].to_vec(), "Universe 1 received payload values don't match sent!");

    assert_eq!(received_data[1].universe, 3); // Check that the universe received is as expected.

    assert_eq!(received_data[1].sync_uni, 2); // Check that the sync universe is as expected.

    assert_eq!(received_data[1].values, TEST_DATA_MULTIPLE_UNIVERSE[UNIVERSE_CHANNEL_CAPACITY..].to_vec(), "Universe 2 received payload values don't match sent!");
}

/// Note: this test assumes perfect network conditions (0% reordering, loss, duplication etc.), this should be the case for
/// the loopback adapter with the low amount of data sent but this may be a possible cause if integration tests fail unexpectedly.
#[test]
#[ignore]
fn test_send_recv_across_universe_unicast_ipv4(){
    let (tx, rx): (Sender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::channel();

    let thread_tx = tx.clone();

    const UNIVERSES: [u16; 2] = [2, 3];

    let rcv_thread = thread::spawn(move || {
        let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(Ipv4Addr::new(127,0,0,1).into(), ACN_SDT_MULTICAST_PORT), None).unwrap();

        dmx_recv.listen_universes(&UNIVERSES).unwrap();

        thread_tx.send(Ok(Vec::new())).unwrap(); // Signal that the receiver is ready to receive.

        thread_tx.send(dmx_recv.recv(None)).unwrap(); // Receive the sync packet, the data packets shouldn't have caused .recv to return as forced to wait for sync.
    });

    let _ = rx.recv().unwrap(); // Blocks until the receiver says it is ready.

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();

    let priority = 100;

    src.register_universes(&UNIVERSES).unwrap();

    let _ = src.send(&UNIVERSES, &TEST_DATA_MULTIPLE_UNIVERSE, Some(priority), Some(SocketAddr::new(Ipv4Addr::new(127,0,0,1).into(), ACN_SDT_MULTICAST_PORT).into()), Some(UNIVERSES[0])).unwrap();
    sleep(Duration::from_millis(500)); // Small delay to allow the data packets to get through as per NSI-E1.31-2018 Appendix B.1 recommendation.
    src.send_sync_packet(UNIVERSES[0], Some(SocketAddr::new(Ipv4Addr::new(127,0,0,1).into(), ACN_SDT_MULTICAST_PORT).into())).unwrap();

    let sync_pkt_res: Result<Vec<DMXData>> = rx.recv().unwrap();

    rcv_thread.join().unwrap();

    assert!(!sync_pkt_res.is_err(), "Failed: Error when receiving packets");

    let mut received_data: Vec<DMXData> = sync_pkt_res.unwrap();

    received_data.sort(); // No guarantee on the ordering of the received data so sort it first to allow easier checking.

    assert_eq!(received_data.len(), 2); // Check 2 universes received as expected.

    assert_eq!(received_data[0].universe, 2); // Check that the universe received is as expected.

    assert_eq!(received_data[0].sync_uni, 2); // Check that the sync universe is as expected.

    assert_eq!(received_data[0].values, TEST_DATA_MULTIPLE_UNIVERSE[..UNIVERSE_CHANNEL_CAPACITY].to_vec(), "Universe 1 received payload values don't match sent!");

    assert_eq!(received_data[1].universe, 3); // Check that the universe received is as expected.

    assert_eq!(received_data[1].sync_uni, 2); // Check that the sync universe is as expected.

    assert_eq!(received_data[1].values, TEST_DATA_MULTIPLE_UNIVERSE[UNIVERSE_CHANNEL_CAPACITY..].to_vec(), "Universe 2 received payload values don't match sent!");
}

#[test]
#[ignore]
fn test_two_senders_one_recv_different_universes_multicast_ipv4(){
    let universe_1 = 1;
    let universe_2 = 2;

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(Ipv4Addr::new(0,0,0,0).into(), ACN_SDT_MULTICAST_PORT), None).unwrap();

    dmx_recv.listen_universes(&[universe_1]).unwrap();
    dmx_recv.listen_universes(&[universe_2]).unwrap();

    let snd_thread_1 = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();

        let priority = 100;

        src.register_universe(universe_1).unwrap();

        let _ = src.send(&[universe_1], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), None, None).unwrap();
    });

    let snd_thread_2 = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), ACN_SDT_MULTICAST_PORT + 2);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();

        let priority = 100;

        src.register_universe(universe_2).unwrap();

        let _ = src.send(&[universe_2], &TEST_DATA_PARTIAL_CAPACITY_UNIVERSE, Some(priority), None, None).unwrap();
    });

    let res1: Vec<DMXData> = dmx_recv.recv(None).unwrap();
    let res2: Vec<DMXData> = dmx_recv.recv(None).unwrap();

    snd_thread_1.join().unwrap();
    snd_thread_2.join().unwrap();


    assert_eq!(res1.len(), 1);
    assert_eq!(res2.len(), 1);

    let mut res = vec![res1[0].clone(), res2[0].clone()];
    res.sort_unstable();

    assert_eq!(res[0].universe, universe_1);
    assert_eq!(res[1].universe, universe_2);

    assert_eq!(res[0].values, TEST_DATA_SINGLE_UNIVERSE.to_vec());
    assert_eq!(res[1].values, TEST_DATA_PARTIAL_CAPACITY_UNIVERSE.to_vec());
}

#[test]
#[ignore]
fn test_two_senders_one_recv_same_universe_no_sync_multicast_ipv4(){
    let universe = 1;

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(Ipv4Addr::new(0,0,0,0).into(), ACN_SDT_MULTICAST_PORT), None).unwrap();

    dmx_recv.listen_universes(&[universe]).unwrap();

    let snd_thread_1 = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();

        let priority = 100;

        src.register_universe(universe).unwrap();

        let _ = src.send(&[universe], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), None, None).unwrap();
    });

    let snd_thread_2 = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), ACN_SDT_MULTICAST_PORT + 2);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();

        let priority = 100;

        src.register_universe(universe).unwrap();

        let _ = src.send(&[universe], &TEST_DATA_PARTIAL_CAPACITY_UNIVERSE, Some(priority), None, None).unwrap();
    });

    let res1: Vec<DMXData> = dmx_recv.recv(None).unwrap();
    let res2: Vec<DMXData> = dmx_recv.recv(None).unwrap();

    snd_thread_1.join().unwrap();
    snd_thread_2.join().unwrap();

    assert_eq!(res1.len(), 1);
    assert_eq!(res2.len(), 1);

    let res = vec![res1[0].clone(), res2[0].clone()];

    assert_eq!(res[0].universe, universe);
    assert_eq!(res[1].universe, universe);

    if res[0].values == TEST_DATA_SINGLE_UNIVERSE.to_vec() {
        assert_eq!(res[1].values, TEST_DATA_PARTIAL_CAPACITY_UNIVERSE.to_vec());
    } else {
        assert_eq!(res[0].values, TEST_DATA_PARTIAL_CAPACITY_UNIVERSE.to_vec());
        assert_eq!(res[1].values, TEST_DATA_SINGLE_UNIVERSE.to_vec());
    }
}

#[test]
#[ignore]
fn test_two_senders_one_recv_same_universe_custom_merge_fn_sync_multicast_ipv4(){
    let (tx, rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0); // Used for handshaking

    let snd_tx = tx.clone();

    let universe = 1;
    let sync_uni = 2;

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap(), ACN_SDT_MULTICAST_PORT), None).unwrap();

    dmx_recv.listen_universes(&[universe, sync_uni]).unwrap();

    dmx_recv.set_merge_fn(htp_dmx_merge).unwrap();

    let snd_thread_1 = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[1].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();

        let priority = 100;

        src.register_universe(universe).unwrap();
        src.register_universe(sync_uni).unwrap();

        src.send(&[universe], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), None, Some(sync_uni)).unwrap();
        snd_tx.send(()).unwrap();
    });

    let snd_thread_2 = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[2].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 2);
        let mut src = SacnSource::with_ip("Source 2", ip).unwrap();

        let priority = 100;

        src.register_universe(universe).unwrap();
        src.register_universe(sync_uni).unwrap();

        src.send(&[universe], &TEST_DATA_PARTIAL_CAPACITY_UNIVERSE, Some(priority), None, Some(sync_uni)).unwrap();
        rx.recv().unwrap(); // Must only send once both threads have sent for this test to test what happens in that situation (where there will be a merge).
        src.send_sync_packet(sync_uni, None).unwrap();
    });

    let res1: Vec<DMXData> = dmx_recv.recv(None).unwrap();

    snd_thread_1.join().unwrap();
    snd_thread_2.join().unwrap();

    assert_eq!(res1.len(), 1);
    assert_eq!(res1[0].values, htp_dmx_merge(&DMXData {
        universe: universe,
        values: TEST_DATA_SINGLE_UNIVERSE.to_vec(),
        sync_uni: sync_uni,
        priority: 100,
        src_cid: None,
        preview: false,
        recv_timestamp: Instant::now()
    },
    &DMXData {
        universe: universe,
        values: TEST_DATA_PARTIAL_CAPACITY_UNIVERSE.to_vec(),
        sync_uni: sync_uni,
        priority: 100,
        src_cid: None,
        preview: false,
        recv_timestamp: Instant::now()
    },).unwrap().values);
}

#[test]
#[ignore]
fn test_two_senders_two_recv_multicast_ipv4(){
    const SND_THREADS: usize = 2;
    const RCV_THREADS: usize = 2;
    const SND_DATA_LEN: usize = 100;

    let mut snd_data: Vec<Vec<u8>> = Vec::new();

    for i in 1 .. SND_THREADS + 1 {
        let mut d: Vec<u8> = Vec::new();
        for _k in 0 .. SND_DATA_LEN {
            d.push(i as u8);
        }
        snd_data.push(d);
    }

    let mut snd_threads = Vec::new();
    let mut rcv_threads = Vec::new();

    let (rcv_tx, rcv_rx): (SyncSender<Vec<Result<Vec<DMXData>>>>, Receiver<Vec<Result<Vec<DMXData>>>>) = mpsc::sync_channel(0);
    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0); // Used for handshaking, allows syncing the sender states.

    assert!(RCV_THREADS <= TEST_NETWORK_INTERFACE_IPV4.len(), "Number of test network interface ips less than number of recv threads!");

    const BASE_UNIVERSE: u16 = 2;

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        let data = snd_data[i].clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));
            // https://www.programming-idioms.org/idiom/153/concatenate-string-with-integer/1975/rust (11/01/2020)
            let mut src = SacnSource::with_ip(&format!("Source {}", i), ip).unwrap();

            let priority = 100;

            let universe: u16 = (i as u16) + BASE_UNIVERSE;

            src.register_universe(universe).unwrap(); // Senders all send on different universes.

            tx.send(()).unwrap(); // Forces each sender thread to wait till the controlling thread receives which stops sending before the receivers are ready.

            src.send(&[universe], &data, Some(priority), None, None).unwrap();
        }));
    }

    for i in 0 .. RCV_THREADS {
        let tx = rcv_tx.clone();

        rcv_threads.push(thread::spawn(move || {
            // Port kept the same so must use multiple IP's.
            let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[i].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

            // Receivers listen to all universes
            for i in (BASE_UNIVERSE as u16) .. ((SND_THREADS as u16) + (BASE_UNIVERSE as u16)) {
                dmx_recv.listen_universes(&[i]).unwrap();
            }

            let mut res: Vec<Result<Vec<DMXData>>> = Vec::new();

            tx.send(Vec::new()).unwrap(); // Receiver notifies controlling thread it is ready.

            for _i in 0 .. SND_THREADS { // Receiver should receive from every universe.
                res.push(dmx_recv.recv(None)); // Receiver won't complete this until it receives from the senders which are all held waiting on the controlling thread.
            }

            // Results of each receive are sent back, this allows checking that each receive was an expected universe, all universes were received and there were no errors.
            tx.send(res).unwrap();
        }));

        assert_eq!(rcv_rx.recv().unwrap().len(), 0); // Wait till the receiver has notified controlling thread it is ready.
    }

    for _i in 0 .. SND_THREADS {
        snd_rx.recv().unwrap(); // Allow each sender to progress
    }

    for _i in 0 .. RCV_THREADS {
        let res: Vec<Result<Vec<DMXData>>> = rcv_rx.recv().unwrap();

        assert_eq!(res.len(), SND_THREADS);

        let mut rcv_dmx_datas: Vec<DMXData> = Vec::new();

        for r in res {
            let data: Vec<DMXData> = r.unwrap(); // Check that there are no errors when receiving.
            assert_eq!(data.len(), 1); // Check that each universe was received separately.
            rcv_dmx_datas.push(data[0].clone());
        }

        rcv_dmx_datas.sort_unstable(); // Sorting by universe allows easier checking as order received may vary depending on network.

        for k in 0 .. SND_THREADS {
            assert_eq!(rcv_dmx_datas[k].universe, ((k as u16) + BASE_UNIVERSE)); // Check that the universe received is as expected.

            assert_eq!(rcv_dmx_datas[k].values, snd_data[k], "Received payload values don't match sent!");
        }
    }

    for s in snd_threads {
        s.join().unwrap();
    }

    for r in rcv_threads {
        r.join().unwrap();
    }
}

#[test]
#[ignore]
fn test_three_senders_two_recv_multicast_ipv4(){
    const SND_THREADS: usize = 3;
    const RCV_THREADS: usize = 2;
    const SND_DATA_LEN: usize = 100;

    let mut snd_data: Vec<Vec<u8>> = Vec::new();

    for i in 1 .. SND_THREADS + 1 {
        let mut d: Vec<u8> = Vec::new();
        for _k in 0 .. SND_DATA_LEN {
            d.push(i as u8);
        }
        snd_data.push(d);
    }

    let mut snd_threads = Vec::new();
    let mut rcv_threads = Vec::new();

    let (rcv_tx, rcv_rx): (SyncSender<Vec<Result<Vec<DMXData>>>>, Receiver<Vec<Result<Vec<DMXData>>>>) = mpsc::sync_channel(0);
    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0); // Used for handshaking, allows syncing the sender states.

    assert!(RCV_THREADS <= TEST_NETWORK_INTERFACE_IPV4.len(), "Number of test network interface ips less than number of recv threads!");

    const BASE_UNIVERSE: u16 = 2;

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        let data = snd_data[i].clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));
            // https://www.programming-idioms.org/idiom/153/concatenate-string-with-integer/1975/rust (11/01/2020)
            let mut src = SacnSource::with_ip(&format!("Source {}", i), ip).unwrap();

            let priority = 100;

            let universe: u16 = (i as u16) + BASE_UNIVERSE;

            src.register_universe(universe).unwrap(); // Senders all send on different universes.

            tx.send(()).unwrap(); // Forces each sender thread to wait till the controlling thread receives which stops sending before the receivers are ready.

            src.send(&[universe], &data, Some(priority), None, None).unwrap();
        }));
    }

    for i in 0 .. RCV_THREADS {
        let tx = rcv_tx.clone();

        rcv_threads.push(thread::spawn(move || {
            // Port kept the same so must use multiple IP's.
            let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[i].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

            // Receivers listen to all universes
            for i in (BASE_UNIVERSE as u16) .. ((SND_THREADS as u16) + (BASE_UNIVERSE as u16)) {
                dmx_recv.listen_universes(&[i]).unwrap();
            }

            let mut res: Vec<Result<Vec<DMXData>>> = Vec::new();

            tx.send(Vec::new()).unwrap(); // Receiver notifies controlling thread it is ready.

            for _i in 0 .. SND_THREADS { // Receiver should receive from every universe.
                res.push(dmx_recv.recv(None)); // Receiver won't complete this until it receives from the senders which are all held waiting on the controlling thread.
            }

            // Results of each receive are sent back, this allows checking that each receiver was an expected universe, all universes were received and there were no errors.
            tx.send(res).unwrap();
        }));

        assert_eq!(rcv_rx.recv().unwrap().len(), 0); // Wait till the receiver has notified controlling thread it is ready.
    }

    for _i in 0 .. SND_THREADS {
        snd_rx.recv().unwrap(); // Allow each sender to progress
    }

    for _i in 0 .. RCV_THREADS {
        let res: Vec<Result<Vec<DMXData>>> = rcv_rx.recv().unwrap();

        assert_eq!(res.len(), SND_THREADS);

        let mut rcv_dmx_datas: Vec<DMXData> = Vec::new();

        for r in res {
            let data: Vec<DMXData> = r.unwrap(); // Check that there are no errors when receiving.
            assert_eq!(data.len(), 1); // Check that each universe was received separately.
            rcv_dmx_datas.push(data[0].clone());
        }

        rcv_dmx_datas.sort_unstable(); // Sorting by universe allows easier checking as order received may vary depending on network.

        for k in 0 .. SND_THREADS {
            assert_eq!(rcv_dmx_datas[k].universe, ((k as u16) + BASE_UNIVERSE)); // Check that the universe received is as expected.

            assert_eq!(rcv_dmx_datas[k].values, snd_data[k], "Received payload values don't match sent!");
        }
    }

    for s in snd_threads {
        s.join().unwrap();
    }

    for r in rcv_threads {
        r.join().unwrap();
    }
}

#[test]
#[ignore]
fn test_two_senders_three_recv_multicast_ipv4(){
    const SND_THREADS: usize = 2;
    const RCV_THREADS: usize = 3;
    const SND_DATA_LEN: usize = 100;

    let mut snd_data: Vec<Vec<u8>> = Vec::new();

    for i in 1 .. SND_THREADS + 1 {
        let mut d: Vec<u8> = Vec::new();
        for _k in 0 .. SND_DATA_LEN {
            d.push(i as u8);
        }
        snd_data.push(d);
    }

    let mut snd_threads = Vec::new();
    let mut rcv_threads = Vec::new();

    let (rcv_tx, rcv_rx): (SyncSender<Vec<Result<Vec<DMXData>>>>, Receiver<Vec<Result<Vec<DMXData>>>>) = mpsc::sync_channel(0);
    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0); // Used for handshaking, allows syncing the sender states.

    assert!(RCV_THREADS <= TEST_NETWORK_INTERFACE_IPV4.len(), "Number of test network interface ips less than number of recv threads!");

    const BASE_UNIVERSE: u16 = 2;

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        let data = snd_data[i].clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));
            // https://www.programming-idioms.org/idiom/153/concatenate-string-with-integer/1975/rust (11/01/2020)
            let mut src = SacnSource::with_ip(&format!("Source {}", i), ip).unwrap();

            let priority = 100;

            let universe: u16 = (i as u16) + BASE_UNIVERSE;

            src.register_universe(universe).unwrap(); // Senders all send on different universes.

            tx.send(()).unwrap(); // Forces each sender thread to wait till the controlling thread receives which stops sending before the receivers are ready.

            src.send(&[universe], &data, Some(priority), None, None).unwrap();
        }));
    }

    for i in 0 .. RCV_THREADS {
        let tx = rcv_tx.clone();

        rcv_threads.push(thread::spawn(move || {
            // Port kept the same so must use multiple IP's.
            let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[i].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

            // Receivers listen to all universes
            for i in (BASE_UNIVERSE as u16) .. ((SND_THREADS as u16) + (BASE_UNIVERSE as u16)) {
                dmx_recv.listen_universes(&[i]).unwrap();
            }

            let mut res: Vec<Result<Vec<DMXData>>> = Vec::new();

            tx.send(Vec::new()).unwrap(); // Receiver notifies controlling thread it is ready.

            for _i in 0 .. SND_THREADS { // Receiver should receive from every universe.
                res.push(dmx_recv.recv(None)); // Receiver won't complete this until it receives from the senders which are all held waiting on the controlling thread.
            }

            // Results of each receive are sent back, this allows checking that each receive was an expected universe, all universes were received and there were no errors.
            tx.send(res).unwrap();
        }));

        assert_eq!(rcv_rx.recv().unwrap().len(), 0); // Wait till the receiver has notified controlling thread it is ready.
    }

    for _i in 0 .. SND_THREADS {
        snd_rx.recv().unwrap(); // Allow each sender to progress
    }

    for _i in 0 .. RCV_THREADS {
        let res: Vec<Result<Vec<DMXData>>> = rcv_rx.recv().unwrap();

        assert_eq!(res.len(), SND_THREADS);

        let mut rcv_dmx_datas: Vec<DMXData> = Vec::new();

        for r in res {
            let data: Vec<DMXData> = r.unwrap(); // Check that there are no errors when receiving.
            assert_eq!(data.len(), 1); // Check that each universe was received separately.
            rcv_dmx_datas.push(data[0].clone());
        }

        rcv_dmx_datas.sort_unstable(); // Sorting by universe allows easier checking as order received may vary depending on network.

        for k in 0 .. SND_THREADS {
            assert_eq!(rcv_dmx_datas[k].universe, ((k as u16) + BASE_UNIVERSE)); // Check that the universe received is as expected.

            assert_eq!(rcv_dmx_datas[k].values, snd_data[k], "Received payload values don't match sent!");
        }
    }

    for s in snd_threads {
        s.join().unwrap();
    }

    for r in rcv_threads {
        r.join().unwrap();
    }
}

#[test]
#[ignore]
fn test_three_senders_three_recv_multicast_ipv4(){
    const SND_THREADS: usize = 3;
    const RCV_THREADS: usize = 3;
    const SND_DATA_LEN: usize = 100;

    let mut snd_data: Vec<Vec<u8>> = Vec::new();

    for i in 1 .. SND_THREADS + 1 {
        let mut d: Vec<u8> = Vec::new();
        for _k in 0 .. SND_DATA_LEN {
            d.push(i as u8);
        }
        snd_data.push(d);
    }

    let mut snd_threads = Vec::new();
    let mut rcv_threads = Vec::new();

    let (rcv_tx, rcv_rx): (SyncSender<Vec<Result<Vec<DMXData>>>>, Receiver<Vec<Result<Vec<DMXData>>>>) = mpsc::sync_channel(0);
    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0); // Used for handshaking, allows syncing the sender states.

    assert!(RCV_THREADS <= TEST_NETWORK_INTERFACE_IPV4.len(), "Number of test network interface ips less than number of recv threads!");

    const BASE_UNIVERSE: u16 = 2;

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        let data = snd_data[i].clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));
            // https://www.programming-idioms.org/idiom/153/concatenate-string-with-integer/1975/rust (11/01/2020)
            let mut src = SacnSource::with_ip(&format!("Source {}", i), ip).unwrap();

            let priority = 100;

            let universe: u16 = (i as u16) + BASE_UNIVERSE;

            src.register_universe(universe).unwrap(); // Senders all send on different universes.

            tx.send(()).unwrap(); // Forces each sender thread to wait till the controlling thread receives which stops sending before the receivers are ready.

            src.send(&[universe], &data, Some(priority), None, None).unwrap();
        }));
    }

    for i in 0 .. RCV_THREADS {
        let tx = rcv_tx.clone();

        rcv_threads.push(thread::spawn(move || {
            // Port kept the same so must use multiple IP's.
            let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[i].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

            // Receivers listen to all universes
            for i in (BASE_UNIVERSE as u16) .. ((SND_THREADS as u16) + (BASE_UNIVERSE as u16)) {
                dmx_recv.listen_universes(&[i]).unwrap();
            }

            let mut res: Vec<Result<Vec<DMXData>>> = Vec::new();

            tx.send(Vec::new()).unwrap(); // Receiver notifies controlling thread it is ready.

            for _i in 0 .. SND_THREADS { // Receiver should receive from every universe.
                res.push(dmx_recv.recv(None)); // Receiver won't complete this until it receives from the senders which are all held waiting on the controlling thread.
            }

            // Results of each receive are sent back, this allows checking that each receive was an expected universe, all universes were received and there were no errors.
            tx.send(res).unwrap();
        }));

        assert_eq!(rcv_rx.recv().unwrap().len(), 0); // Wait till the receiver has notified controlling thread it is ready.
    }

    for _i in 0 .. SND_THREADS {
        snd_rx.recv().unwrap(); // Allow each sender to progress
    }

    for _i in 0 .. RCV_THREADS {
        let res: Vec<Result<Vec<DMXData>>> = rcv_rx.recv().unwrap();

        assert_eq!(res.len(), SND_THREADS);

        let mut rcv_dmx_datas: Vec<DMXData> = Vec::new();

        for r in res {
            let data: Vec<DMXData> = r.unwrap(); // Check that there are no errors when receiving.
            assert_eq!(data.len(), 1); // Check that each universe was received separately.
            rcv_dmx_datas.push(data[0].clone());
        }

        rcv_dmx_datas.sort_unstable(); // Sorting by universe allows easier checking as order received may vary depending on network.

        for k in 0 .. SND_THREADS {
            assert_eq!(rcv_dmx_datas[k].universe, ((k as u16) + BASE_UNIVERSE)); // Check that the universe received is as expected.

            assert_eq!(rcv_dmx_datas[k].values, snd_data[k], "Received payload values don't match sent!");
        }
    }

    for s in snd_threads {
        s.join().unwrap();
    }

    for r in rcv_threads {
        r.join().unwrap();
    }
}

#[test]
#[ignore]
fn test_universe_discovery_one_universe_one_source_ipv4(){
    const SND_THREADS: usize = 1;
    const BASE_UNIVERSE: u16 = 2;
    const UNIVERSE_COUNT: usize = 1;
    const SOURCE_NAMES: [&'static str; 1] = ["Source 1"];

    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let mut snd_threads = Vec::new();

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));

            let mut src = SacnSource::with_ip(SOURCE_NAMES[i], ip).unwrap();

            let mut universes: Vec<u16> = Vec::new();
            for j in 0 .. UNIVERSE_COUNT {
                universes.push(((i + j) as u16) + BASE_UNIVERSE);
            }

            src.register_universes(&universes).unwrap();

            tx.send(()).unwrap(); // Used to force the sender to wait till the receiver has received a universe discovery.
        }));
    }

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

    loop {
        let result = dmx_recv.recv(Some(Duration::from_secs(2)));
        match result {
            Err(e) => {
                match e {
                    SacnError::Io(ref s) => {
                        match s.kind() {
                            std::io::ErrorKind::WouldBlock => {
                                // Expected to timeout / would block.
                                // The different errors are due to windows and unix returning different errors for the same thing.
                            },
                            std::io::ErrorKind::TimedOut => {},
                            _ => {
                                assert!(false, "Unexpected error returned");
                            }
                        }
                    },
                    _ => {
                        assert!(false, "Unexpected error returned");
                    }
                }
            },
            Ok(_) => {
                assert!(false, "No data should have been passed up!");
            }
        }

        let discovered = dmx_recv.get_discovered_sources();

        if discovered.len() > 0 {
            assert_eq!(discovered.len(), 1);
            assert_eq!(discovered[0].name, SOURCE_NAMES[0]);
            let universes = discovered[0].get_all_universes();
            assert_eq!(universes.len(), UNIVERSE_COUNT);
            for j in 0 .. UNIVERSE_COUNT {
                assert_eq!(universes[j], (j as u16) + BASE_UNIVERSE);
            }
            break;
        }
    }

    snd_rx.recv().unwrap();

    for s in snd_threads {
        s.join().unwrap();
    }
}

/// Measures the time taken in milliseconds between 2 discovery packets to check that the interval fits with expected.
#[test]
#[ignore]
fn test_universe_discovery_interval_ipv4(){
    const SND_THREADS: usize = 1;
    const BASE_UNIVERSE: u16 = 1;
    const SOURCE_NAMES: [&'static str; 1] = ["Source 1"];
    const INTERVAL_EXPECTED_MILLIS: u128 = E131_UNIVERSE_DISCOVERY_INTERVAL.as_millis(); // Expected discovery packet interval is every 10 seconds (10000 milliseconds).
    const INTERVAL_TOLERANCE_MILLIS: u128 = 1000; // Allow up to a second either side of this interval to account for random variations.

    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let mut snd_threads = Vec::new();

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));

            tx.send(()).unwrap(); // Force the send thread to wait before creating the sender, should sync once the receiver has been created.

            let mut src = SacnSource::with_ip(SOURCE_NAMES[i], ip).unwrap();

            src.register_universes(&[BASE_UNIVERSE]).unwrap();

            tx.send(()).unwrap(); // Used to force the sender to wait till the receiver has received a universe discovery.
        }));
    }

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();
    dmx_recv.set_announce_source_discovery(true); // Make the receiver explicitly notify when it receives a universe discovery packet.

    snd_rx.recv().unwrap(); // Receiver created and ready so allow the sender to be created.

    let mut interval_start = Instant::now(); // Assignment never used.

    match dmx_recv.recv(None) {
        Err(e) => {
            match e {
                SacnError::SourceDiscovered(_) => {
                    // Measure the time between the first and second discovery packets, this removes the uncertainty in the time taken for the sender to start.
                    interval_start = Instant::now();
                }
                k => {
                    assert!(false, "Unexpected error kind, {:?}", k);
                }
            }
        }
        Ok(d) => {
            assert!(false, "No data expected, {:?}", d);
        }
    }

    match dmx_recv.recv(None) {
        Err(e) => {
            match e {
                SacnError::SourceDiscovered(_) => {
                    let interval = interval_start.elapsed();
                    let interval_millis = interval.as_millis();
                    assert!(interval_millis > (INTERVAL_EXPECTED_MILLIS - INTERVAL_TOLERANCE_MILLIS), "Discovery interval is shorter than expected, {} ms", interval_millis);
                    assert!(interval_millis < (INTERVAL_EXPECTED_MILLIS + INTERVAL_TOLERANCE_MILLIS), "Discovery interval is longer than expected, {} ms", interval_millis);
                }
                k => {
                    assert!(false, "Unexpected error kind, {:?}", k);
                }
            }
        }
        Ok(d) => {
            assert!(false, "No data expected, {:?}", d);
        }
    }

    snd_rx.recv().unwrap(); // Allow sender to finish.
}

/// Sets up a sender and a receiver, the sender then updates its sending universes multiple times within an ANSI E1.31-2018
/// E131_UNIVERSE_DISCOVERY_INTERVAL and the receiver asserts that it only receives updates on the interval as expected / compliant
/// with ANSI E1.31-2018 Section 4.3
///
#[test]
#[ignore]
fn test_universe_discovery_interval_with_updates_ipv4(){
    const SND_THREADS: usize = 1;
    const BASE_UNIVERSE: u16 = 1;
    const SOURCE_NAMES: [&'static str; 1] = ["Source 1"];
    const INTERVAL_EXPECTED_MILLIS: u128 = E131_UNIVERSE_DISCOVERY_INTERVAL.as_millis(); // Expected discovery packet interval is every 10 seconds (10000 milliseconds).
    const INTERVAL_TOLERANCE_MILLIS: u128 = 1000; // Allow up to a second either side of this interval to account for random variations.
    const SENDER_REGISTER_DELAY: Duration = Duration::from_secs(1); // The time between registering new universe on the sender.
    const UNIVERSES_TO_REGISTER: usize = 5; // The number of universes to register on the src.

    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let mut snd_threads = Vec::new();

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));

            tx.send(()).unwrap(); // Force the send thread to wait before creating the sender, should sync once the receiver has been created.

            let mut src = SacnSource::with_ip(SOURCE_NAMES[i], ip).unwrap();

            for _ in 0 .. UNIVERSES_TO_REGISTER {
                src.register_universes(&[BASE_UNIVERSE]).unwrap();
                sleep(SENDER_REGISTER_DELAY);
            }

            tx.send(()).unwrap(); // Used to force the sender to wait till the receiver has received a universe discovery.
        }));
    }

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();
    dmx_recv.set_announce_source_discovery(true); // Make the receiver explicitly notify when it receives a universe discovery packet.

    snd_rx.recv().unwrap(); // Receiver created and ready so allow the sender to be created.

    let mut interval_start = Instant::now(); // Assignment never used.

    match dmx_recv.recv(None) {
        Err(e) => {
            match e {
                SacnError::SourceDiscovered(_) => {
                    // Measure the time between the first and second discovery packets, this removes the uncertainty in the time taken for the sender to start.
                    interval_start = Instant::now();
                }
                k => {
                    assert!(false, "Unexpected error kind, {:?}", k);
                }
            }
        }
        Ok(d) => {
            assert!(false, "No data expected, {:?}", d);
        }
    }

    match dmx_recv.recv(None) {
        Err(e) => {
            match e {
                SacnError::SourceDiscovered(_) => {
                    let interval = interval_start.elapsed();
                    let interval_millis = interval.as_millis();
                    assert!(interval_millis > (INTERVAL_EXPECTED_MILLIS - INTERVAL_TOLERANCE_MILLIS), "Discovery interval is shorter than expected, {} ms", interval_millis);
                    assert!(interval_millis < (INTERVAL_EXPECTED_MILLIS + INTERVAL_TOLERANCE_MILLIS), "Discovery interval is longer than expected, {} ms", interval_millis);
                }
                k => {
                    assert!(false, "Unexpected error kind, {:?}", k);
                }
            }
        }
        Ok(d) => {
            assert!(false, "No data expected, {:?}", d);
        }
    }

    snd_rx.recv().unwrap(); // Allow sender to finish.
}

#[test]
#[ignore]
fn test_universe_discovery_multiple_universe_one_source_ipv4(){
    const SND_THREADS: usize = 1;
    const BASE_UNIVERSE: u16 = 2;
    const UNIVERSE_COUNT: usize = 5;
    const SOURCE_NAMES: [&'static str; 1] = ["Source 1"];

    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let mut snd_threads = Vec::new();

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));

            let mut src = SacnSource::with_ip(SOURCE_NAMES[i], ip).unwrap();

            let mut universes: Vec<u16> = Vec::new();
            for j in 0 .. UNIVERSE_COUNT {
                universes.push(((i + j) as u16) + BASE_UNIVERSE);
            }

            src.register_universes(&universes).unwrap();

            tx.send(()).unwrap(); // Used to force the sender to wait till the receiver has received a universe discovery.
        }));
    }

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

    loop {
        let result = dmx_recv.recv(Some(Duration::from_secs(2)));
        match result {
            Err(e) => {
                match e {
                    SacnError::Io(ref s) => {
                        match s.kind() {
                            std::io::ErrorKind::WouldBlock => {
                                // Expected to timeout / would block.
                                // The different errors are due to windows and unix returning different errors for the same thing.
                            },
                            std::io::ErrorKind::TimedOut => {},
                            _ => {
                                assert!(false, "Unexpected error returned");
                            }
                        }
                    },
                    _ => {
                        assert!(false, "Unexpected error returned");
                    }
                }
            },
            Ok(_) => {
                assert!(false, "No data should have been passed up!");
            }
        }

        let discovered = dmx_recv.get_discovered_sources();

        if discovered.len() > 0 {
            assert_eq!(discovered.len(), 1);
            assert_eq!(discovered[0].name, SOURCE_NAMES[0]);

            let universes = discovered[0].get_all_universes();
            assert_eq!(universes.len(), UNIVERSE_COUNT);
            for j in 0 .. UNIVERSE_COUNT {
                assert_eq!(universes[j], (j as u16) + BASE_UNIVERSE);
            }
            break;
        }
    }

    snd_rx.recv().unwrap();

    for s in snd_threads {
        s.join().unwrap();
    }
}

#[test]
#[ignore]
fn test_universe_discovery_multiple_pages_one_source_ipv4(){
    const SND_THREADS: usize = 1;
    const BASE_UNIVERSE: u16 = 2;
    const UNIVERSE_COUNT: usize = 600;
    const SOURCE_NAMES: [&'static str; 1] = ["Source 1"];

    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let mut snd_threads = Vec::new();

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));

            let mut src = SacnSource::with_ip(SOURCE_NAMES[i], ip).unwrap();

            src.set_is_sending_discovery(false); // To stop universe discovery packets being sent until all universes are registered.

            let mut universes: Vec<u16> = Vec::new();
            for j in 0 .. UNIVERSE_COUNT {
                universes.push(((i + j) as u16) + BASE_UNIVERSE);
            }

            src.register_universes(&universes).unwrap();

            src.set_is_sending_discovery(true);

            tx.send(()).unwrap(); // Used to force the sender to wait till the receiver has received a universe discovery.
        }));
    }

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

    loop {
        let result = dmx_recv.recv(Some(Duration::from_secs(2)));

        match result {
            Err(e) => {
                match e {
                    SacnError::Io(ref s) => {
                        match s.kind() {
                            std::io::ErrorKind::WouldBlock => {
                                // Expected to timeout / would block.
                                // The different errors are due to windows and unix returning different errors for the same thing.
                            },
                            std::io::ErrorKind::TimedOut => {},
                            _ => {
                                assert!(false, "Unexpected error returned");
                            }
                        }
                    },
                    _ => {
                        assert!(false, "Unexpected error returned");
                    }
                }
            },
            Ok(_) => {
                assert!(false, "No data should have been passed up!");
            }
        }

        let discovered = dmx_recv.get_discovered_sources();

        if discovered.len() > 0 {
            assert_eq!(discovered.len(), 1);
            assert_eq!(discovered[0].name, SOURCE_NAMES[0]);
            let universes = discovered[0].get_all_universes();
            assert_eq!(universes.len(), UNIVERSE_COUNT);
            for j in 0 .. UNIVERSE_COUNT {
                assert_eq!(universes[j], (j as u16) + BASE_UNIVERSE);
            }
            break;
        }
    }

    snd_rx.recv().unwrap();

    for s in snd_threads {
        s.join().unwrap();
    }
}

/// Creates a sender and a receiver with the sender having no registered universes.
/// Receiver waits for a discovery packet from the sender and uses it to show that the sender is transmitting
/// an empty list of universes as expected.
#[test]
#[ignore]
fn test_universe_discovery_no_universes_ipv4(){
    const SND_THREADS: usize = 1;
    const SOURCE_NAMES: [&'static str; 1] = ["Source 1"];
    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let mut snd_threads = Vec::new();

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));

            tx.send(()).unwrap(); // Force the send thread to wait before creating the sender, should sync once the receiver has been created.

            let mut src = SacnSource::with_ip(SOURCE_NAMES[i], ip).unwrap();

            // Explicitly make sure that the src is sending discovery packets (by default not).
            src.set_is_sending_discovery(true);

            // No universes registered so should transmit an empty list.

            tx.send(()).unwrap(); // Used to force the sender to wait till the receiver has received a universe discovery.
        }));
    }

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();
    dmx_recv.set_announce_source_discovery(true); // Make the receiver explicitly notify when it receives a universe discovery packet.

    snd_rx.recv().unwrap(); // Receiver created and ready so allow the sender to be created.

    match dmx_recv.recv(None) {
        Err(e) => {
            match e {
                SacnError::SourceDiscovered(src_name) => {
                    assert_eq!(src_name, SOURCE_NAMES[0], "Name of source discovered doesn't match expected");
                    let sources = dmx_recv.get_discovered_sources();
                    assert_eq!(sources.len(), 1, "Number of sources discovered doesn't match expected (1)");
                    assert_eq!(sources[0].get_all_universes(), Vec::new(), "Number of universes on source is greater than expected (0)");
                }
                k => {
                    assert!(false, "Unexpected error kind, {:?}", k);
                }
            }
        }
        Ok(d) => {
            assert!(false, "No data expected, {:?}", d);
        }
    }

    snd_rx.recv().unwrap(); // Allow sender to finish.
}

/// Creates a receiver with a source limit of 2 and then creates 3 sources to trigger a sources exceeded condition.
#[test]
#[ignore]
fn test_receiver_sources_exceeded_3() {
    const SND_THREADS: usize = 3;
    const RCV_THREADS: usize = 1;
    const SRC_LIMIT: Option<usize> = Some(2);
    const TIMEOUT: Option<Duration> = Some(Duration::from_secs(3));

    let mut snd_threads = Vec::new();

    // Separate message queues used so threads don't take messages to allow them to proceed as a message to allow finishing.
    // This is less efficient than using different message types within a single queue however as this is a test the priority is simplicity.
    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0); // Used for handshaking, allows syncing the sender states.
    let (finish_snd_tx, finish_snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0); // Used for handshaking to tell the source threads to finish.

    assert!(RCV_THREADS <= TEST_NETWORK_INTERFACE_IPV4.len(), "Number of test network interface ips less than number of recv threads!");

    const BASE_UNIVERSE: u16 = 2;

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();
        let fin_tx = finish_snd_tx.clone();

        let data = [1, 2, 3];

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));
            // https://www.programming-idioms.org/idiom/153/concatenate-string-with-integer/1975/rust (11/01/2020)
            let mut src = SacnSource::with_ip(&format!("Source {}", i), ip).unwrap();

            let priority = 100;

            let universe: u16 = (i as u16) + BASE_UNIVERSE;

            src.register_universe(universe).unwrap(); // Senders all send on different universes.

            tx.send(()).unwrap(); // Forces each sender thread to wait till the controlling thread receives which stops sending before the receivers are ready.

            src.send(&[universe], &data, Some(priority), None, None).unwrap();

            fin_tx.send(()).unwrap(); // Forces each sender to wait and not terminate.
        }));
    }

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), SRC_LIMIT).unwrap();

    // Receivers listen to all universes
    for i in (BASE_UNIVERSE as u16) .. ((SND_THREADS as u16) + (BASE_UNIVERSE as u16)) {
        dmx_recv.listen_universes(&[i]).unwrap();
    }

    for _ in 0 .. SND_THREADS {
        snd_rx.recv().unwrap(); // Allow each sender to progress
    }

    // Asserts that the first 2 recv attempts are successful.
    dmx_recv.recv(TIMEOUT).unwrap();
    dmx_recv.recv(TIMEOUT).unwrap();

    // On receiving the third time from the third source the sources exceeded error should be thrown.
    match dmx_recv.recv(TIMEOUT) {
        Err(e) => {
            match e {
                SacnError::SourcesExceededError(_) => {
                    assert!(true, "Expected error returned");
                }
                _ => {
                    assert!(false, "Unexpected error type returned");
                }
            }
        }
        Ok(_) => {
            assert!(false, "Recv was successful even though source limit was exceeded");
        }
    }

    // Allow the senders to finish / terminate.
    for _ in 0 .. SND_THREADS {
        finish_snd_rx.recv().unwrap();
    }

    for _ in 0 .. SND_THREADS {
        snd_threads.pop().unwrap().join().unwrap();
    }
}

/// Creates a receiver with a source limit of 2 and then creates 2 sources which send to the receiver.
/// This shouldn't trigger a SourcesExceededCondition
#[test]
#[ignore]
fn test_receiver_source_limit_2() {
    const SND_THREADS: usize = 2;
    const RCV_THREADS: usize = 1;
    const SRC_LIMIT: Option<usize> = Some(2);

    let mut snd_threads = Vec::new();

    let (snd_tx, snd_rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0); // Used for handshaking, allows syncing the sender states.

    assert!(RCV_THREADS <= TEST_NETWORK_INTERFACE_IPV4.len(), "Number of test network interface ips less than number of recv threads!");

    const BASE_UNIVERSE: u16 = 2;

    for i in 0 .. SND_THREADS {
        let tx = snd_tx.clone();

        let data = [1, 2, 3];

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));
            let mut src = SacnSource::with_ip(&format!("Source {}", i), ip).unwrap();

            let priority = 100;

            let universe: u16 = (i as u16) + BASE_UNIVERSE;

            src.register_universe(universe).unwrap(); // Senders all send on different universes.

            tx.send(()).unwrap(); // Forces each sender thread to wait till the controlling thread receives which stops sending before the receivers are ready.

            // Each source sends twice (meaning 4 packets total), this checks that the receiver isn't using the number of packets as the way to check for the number
            // of sources.
            src.send(&[universe], &data, Some(priority), None, None).unwrap();
            src.send(&[universe], &data, Some(priority), None, None).unwrap();
        }));
    }

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), SRC_LIMIT).unwrap();

    // Receivers listen to all universes
    for i in (BASE_UNIVERSE as u16) .. ((SND_THREADS as u16) + (BASE_UNIVERSE as u16)) {
        dmx_recv.listen_universes(&[i]).unwrap();
    }

    for _i in 0 .. SND_THREADS {
        snd_rx.recv().unwrap(); // Allow each sender to progress
    }

    // Asserts that the recv attempts are successful.
    dmx_recv.recv(None).unwrap();
    dmx_recv.recv(None).unwrap();
    dmx_recv.recv(None).unwrap();
    dmx_recv.recv(None).unwrap();
}

/// Creates a receiver with a source limit of 2 and then creates 2 sources which send to the receiver.
/// A source then terminates and another source is created.
/// At all points the total source count was less than or equal to the limit of 2 sources as specified by the receiver
/// so this should not cause a SourcesExceededCondition.
#[test]
#[ignore]
fn test_receiver_source_limit_2_termination_check() {
    const SND_THREADS: usize = 2;
    const SRC_LIMIT: Option<usize> = Some(2);
    const RECV_TIMEOUT: Option<Duration> = Some(Duration::from_secs(5));

    let mut snd_threads = Vec::new();

    let sender_channels: [(SyncSender<()>, Receiver<()>); SND_THREADS] = array::from_fn(|_| mpsc::sync_channel(0));

    const BASE_UNIVERSE: u16 = 2;

    for i in 0 .. SND_THREADS {
        // use a unique tx,rx pair per send thread to prevent race condition when using the same tx,rx between threads
        let tx = sender_channels[i].0.clone();

        let data = [1, 2, 3];

        snd_threads.push(thread::spawn(move || {
            let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (i as u16));
            let mut src = SacnSource::with_ip(&format!("Source {}", i), ip).unwrap();

            let priority = 100;

            let universe: u16 = (i as u16) + BASE_UNIVERSE;

            src.register_universe(universe).unwrap(); // Senders all send on different universes.

            tx.send(()).unwrap(); // Forces each sender thread to wait till the controlling thread receives which stops sending before the receivers are ready.

            // Each source sends twice (meaning 4 packets total), this checks that the receiver isn't using the number of packets as the way to check for the number
            // of sources.
            src.send(&[universe], &data, Some(priority), None, None).unwrap();
            src.send(&[universe], &data, Some(priority), None, None).unwrap();

            if i == 0 { // Forces the first thread not to terminate and to wait. The second thread will finish and terminate the source.
                tx.send(()).unwrap();
            }
        }));
    }

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT), SRC_LIMIT).unwrap();

    // Receivers listen to all universes
    for i in (BASE_UNIVERSE as u16) .. ((SND_THREADS as u16) + (BASE_UNIVERSE as u16)) {
        dmx_recv.listen_universes(&[i]).unwrap();
    }

    for (_, snd_rx) in &sender_channels {
        snd_rx.recv().unwrap();
    }

    // Asserts that the recv attempts are successful.
    dmx_recv.recv(RECV_TIMEOUT).expect("dmx_recv.recv() #1 failed.");
    dmx_recv.recv(RECV_TIMEOUT).expect("dmx_recv.recv() #2 failed.");
    dmx_recv.recv(RECV_TIMEOUT).expect("dmx_recv.recv() #3 failed.");
    dmx_recv.recv(RECV_TIMEOUT).expect("dmx_recv.recv() #4 failed.");


    // The first source is held back from terminating but the second source should terminate.
    let second_thread = snd_threads.remove(1);
    second_thread.join().unwrap();

    // Create a new source which sends to the receiver.
    let data = [1, 2, 3];
    let new_src_thread = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1 + (3 as u16));
        let mut src = SacnSource::with_ip(&format!("Source {}", 3), ip).unwrap();

        src.register_universe(BASE_UNIVERSE).unwrap();

        // New source now sends twice which the receiver should receive.
        src.send(&[BASE_UNIVERSE], &data, None, None, None).unwrap();

        src.send(&[BASE_UNIVERSE], &data, None, None, None).unwrap();
    });

    // Asserts that the recv attempts are successful (no source exceeded).
    dmx_recv.recv(RECV_TIMEOUT).expect("dmx_recv.recv() #5 failed.");
    dmx_recv.recv(RECV_TIMEOUT).expect("dmx_recv.recv() #6 failed.");

    // Allow the first source to progress and finish.
    sender_channels[0].1.recv().unwrap();
    let first_thread = snd_threads.remove(0);
    first_thread.join().unwrap();

    // Finish the new source.
    new_src_thread.join().unwrap();
}

/// Create 2 receivers with a single sender, one receiver listens to preview_data and the other doesn't.
/// The sender then sends data with the preview flag set and not and the receivers check they receive the data correctly.
#[test]
#[ignore]
fn test_preview_data_2_receiver_1_sender() {
    const RCV_THREADS: usize = 2;
    const UNIVERSE: u16 = 1;
    const NORMAL_DATA: [u8; 4] = [0, 1, 2, 3];
    const PREVIEW_DATA: [u8; 4] = [9, 10, 11, 12];
    const TIMEOUT: Option<Duration> = Some(Duration::from_secs(3));

    let mut rcv_threads = Vec::new();

    let (rcv_tx, rcv_rx): (SyncSender<Result<Vec<DMXData>>>, Receiver<Result<Vec<DMXData>>>) = mpsc::sync_channel(0);

    // Check that the test setup is correct.
    assert!(RCV_THREADS <= TEST_NETWORK_INTERFACE_IPV4.len(), "Number of test network interface ips less than number of recv threads!");

    for i in 0 .. RCV_THREADS {
        let tx = rcv_tx.clone();

        rcv_threads.push(thread::spawn(move || {
            // Port kept the same so must use multiple IP's.
            let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[i].parse().unwrap()), ACN_SDT_MULTICAST_PORT), None).unwrap();

            if i == 0 {
                dmx_recv.set_process_preview_data(true); // The first receiver should listen for preview data.
            }

            // Receivers listen to the same universe
            dmx_recv.listen_universes(&[UNIVERSE]).unwrap();

            tx.send(Ok(Vec::new())).unwrap(); // Receiver notifies controlling thread it is ready.

            let result = dmx_recv.recv(None).unwrap();

            assert_eq!(result.len(), 1);

            let data = &result[0];

            assert_eq!(data.universe, UNIVERSE);
            assert_eq!(data.values, NORMAL_DATA);

            assert_eq!(data.preview, false);

            if i == 0 {
                // The receiver listening to preview_data will receive twice.
                let preview_result = dmx_recv.recv(None).unwrap();
                assert_eq!(preview_result.len(), 1);

                let preview_data = &preview_result[0];

                assert_eq!(preview_data.universe, UNIVERSE);
                assert_eq!(preview_data.values, PREVIEW_DATA);
                assert_eq!(preview_data.preview, true);
            } else {
                // The other receiver should not.
                match dmx_recv.recv(TIMEOUT) {
                    Err(e) => {
                        match e {
                            SacnError::Io(ref s) => {
                                match s.kind() {
                                    std::io::ErrorKind::WouldBlock => {
                                        // Expected to timeout / would block.
                                        // The different errors are due to windows and unix returning different errors for the same thing.
                                    },
                                    std::io::ErrorKind::TimedOut => {},
                                    _ => {
                                        assert!(false, "Unexpected error returned");
                                    }
                                }
                            },
                            _ => {
                                assert!(false, "Unexpected error returned");
                            }
                        }
                    },
                    Ok(_) => {
                        assert!(false, "Non-preview receiver received preview data");
                    }
                }
            }
        }));
    }

    // Sender waits for both receivers to be ready.
    for _ in 0 .. RCV_THREADS {
        rcv_rx.recv().unwrap().unwrap();
    }

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
    let mut src = SacnSource::with_ip("Source", ip).unwrap();
    src.register_universe(UNIVERSE).unwrap();

    // Send data without the preview flag.
    src.send(&[UNIVERSE], &NORMAL_DATA, None, None, None).unwrap();

    src.set_preview_mode(true).unwrap();

    // Send data with the preview flag.
    src.send(&[UNIVERSE], &PREVIEW_DATA, None, None, None).unwrap();

    // Finish with the receive threads.
    for r in rcv_threads {
        r.join().unwrap();
    }
}

/// Creates a receiver and a sender. The sender sends a data packet to the receiver and then holds.
/// The receiver (with announce_timeout flag set to true) then waits for the timeout notification to happen.
/// This shows that the timeout mechanism for a source works.
#[test]
#[ignore]
fn test_source_1_universe_timeout(){
    // Allow the timeout notification to come up to 2.5 seconds too late compared to the expected 2.5 seconds.
    // (2.5s base as per ANSI E1.31-2018 Appendix A E131_NETWORK_DATA_LOSS_TIMEOUT, tolerance as per documentation for recv() method).
    // Both tolerances allow 50 ms for code execution time.
    let acceptable_lower_bound: Duration = E131_NETWORK_DATA_LOSS_TIMEOUT - Duration::from_millis(50);
    let acceptable_upper_bound: Duration = 2 * E131_NETWORK_DATA_LOSS_TIMEOUT + Duration::from_millis(50);

    let (tx, rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let thread_tx = tx.clone();

    let universe = 1;

    let snd_thread = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();
        let priority = 100;

        src.register_universe(universe).unwrap();

        let dst_ip: SocketAddr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), ACN_SDT_MULTICAST_PORT);

        thread_tx.send(()).unwrap(); // Sender waits till the receiver says it is ready.

        src.send(&[universe], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), Some(dst_ip), None).unwrap();

        // Sender waits till the receiver says it can terminate, this prevents the automatic stream_terminated packets being sent.
        thread_tx.send(()).unwrap();
    });

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), ACN_SDT_MULTICAST_PORT), None).unwrap();
    dmx_recv.listen_universes(&[universe]).unwrap();

    // Want to know when the source times out.
    dmx_recv.set_announce_timeout(true);

    // Receiver created successfully so allow the sender to progress.
    rx.recv().unwrap();

    // Get the packet of data and check that it is correct.
    let received_data: Vec<DMXData> = dmx_recv.recv(None).unwrap();

    assert_eq!(received_data.len(), 1); // Check only 1 universe received as expected.
    assert_eq!(received_data[0].universe, universe); // Check that the universe received is as expected.
    assert_eq!(received_data[0].values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Received payload values don't match sent!");

    let start_time: Instant = Instant::now();
    match dmx_recv.recv(Some(acceptable_upper_bound)) { // This will return a WouldBlock/Timedout error if the timeout takes too long.
        Err(e) => {
            match e {
                SacnError::UniverseTimeout(_src_cid, timedout_uni) => {
                    if start_time.elapsed() < acceptable_lower_bound{
                        assert!(false, "Timeout came quicker than expected");
                    }
                    assert_eq!(timedout_uni, universe, "Timed out universe doesn't match expected");
                    assert!(true, "Universe timed out as expected");
                }
                SacnError::Io(s) => {
                    match s.kind() {
                        std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut => {
                            assert!(false, "Timeout took too long to come through");
                        },
                        _ => {
                            assert!(false, "Unexpected error returned");
                        }
                    }
                },
                _ => {
                    assert!(false, "Unexpected error returned");
                }
            }
        }
        Ok(x) => {
            assert!(false, "Data received unexpectedly as none sent! {:?}", x);
        }
    }

    rx.recv().unwrap(); // Allow the sender to finish.
    snd_thread.join().unwrap();
}

/// Creates a receiver and a sender. The sender sends 2 data packets to the receiver on different universes and then waits a short time
/// (< E131_NETWORK_DATA_LOSS_TIMEOUT) and sends another data packet for the first universe allowing the second universe to timeout.
/// The receiver checks all 3 data packets are received correctly and that (with announce_timeout flag set to true) only the universe on which
/// a single packet was sent times out.
///
/// This shows that the timeout mechanism is per universe and not for an entire source as a single universe can timeout while other universe
/// continue as normal as per ANSI E1.31-2018 Section 6.7.1.
///
#[test]
#[ignore]
fn test_source_2_universe_1_timeout(){
    // Allow the timeout notification to come up to 2.5 seconds too late compared to the expected 2.5 seconds.
    // (2.5s base as per ANSI E1.31-2018 Appendix A E131_NETWORK_DATA_LOSS_TIMEOUT, tolerance as per documentation for recv() method).
    // Both tolerances allow 50 ms for code execution time.
    let acceptable_lower_bound: Duration = E131_NETWORK_DATA_LOSS_TIMEOUT - Duration::from_millis(50);
    let acceptable_upper_bound: Duration = 2 * E131_NETWORK_DATA_LOSS_TIMEOUT + Duration::from_millis(50);

    let (tx, rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let thread_tx = tx.clone();

    let universe_no_timeout = 1;
    let universe_timeout = 2;

    let snd_thread = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();
        let priority = 100;

        src.register_universes(&[universe_no_timeout, universe_timeout]).unwrap();

        let dst_ip: SocketAddr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), ACN_SDT_MULTICAST_PORT);

        thread_tx.send(()).unwrap(); // Sender waits till the receiver says it is ready.

        src.send(&[universe_no_timeout], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), Some(dst_ip), None).unwrap();
        src.send(&[universe_timeout], &TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE, Some(priority), Some(dst_ip), None).unwrap();

        sleep(Duration::from_secs(1)); // Wait a small amount of time.

        // Send another packet to the universe that shouldn't timeout so that it doesn't timeout.
        src.send(&[universe_no_timeout], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), Some(dst_ip), None).unwrap();

        sleep(Duration::from_secs(1)); // Wait a small amount of time.

        // Send another packet to the universe that shouldn't timeout so that it doesn't timeout.
        src.send(&[universe_no_timeout], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), Some(dst_ip), None).unwrap();

        sleep(Duration::from_secs(1)); // Wait a small amount of time.

        // Send another packet to the universe that shouldn't timeout so that it doesn't timeout.
        src.send(&[universe_no_timeout], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), Some(dst_ip), None).unwrap();

        sleep(Duration::from_secs(1)); // Wait a small amount of time.

        // Send another packet to the universe that shouldn't timeout so that it doesn't timeout.
        src.send(&[universe_no_timeout], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), Some(dst_ip), None).unwrap();

        // Sender waits till the receiver says it can terminate, this prevents the automatic stream_terminated packets being sent.
        thread_tx.send(()).unwrap();
    });

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), ACN_SDT_MULTICAST_PORT), None).unwrap();
    dmx_recv.listen_universes(&[universe_no_timeout, universe_timeout]).unwrap();

    // Want to know when the source times out.
    dmx_recv.set_announce_timeout(true);

    // Receiver created successfully so allow the sender to progress.
    rx.recv().unwrap();

    // Get the packets of data and check that they are correct.
    let received_data: Vec<DMXData> = dmx_recv.recv(None).unwrap();
    assert_eq!(received_data.len(), 1); // Check only 1 universe of data received as expected.

    if received_data[0].universe == universe_no_timeout {
        assert_eq!(received_data[0].values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Received payload values don't match sent!");

        // Get the next data packet and check it is the other packet as expected.
        let received_data: Vec<DMXData> = dmx_recv.recv(None).unwrap();
        assert_eq!(received_data.len(), 1); // Check only 1 universe received as expected.
        if received_data[0].universe == universe_timeout {
            assert_eq!(received_data[0].values, TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE.to_vec(), "Received payload values don't match sent!");
        } else {
            assert!(false, "Data packet from unexpected universe received");
        }
    } else if received_data[0].universe == universe_timeout {
        assert_eq!(received_data[0].values, TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE.to_vec(), "Received payload values don't match sent!");

        // Get the next data packet and check it is the other packet as expected.
        let received_data: Vec<DMXData> = dmx_recv.recv(None).unwrap();
        assert_eq!(received_data.len(), 1); // Check only 1 universe received as expected.
        if received_data[0].universe == universe_no_timeout {
            assert_eq!(received_data[0].values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Received payload values don't match sent!");
        } else {
            assert!(false, "Data packet from unexpected universe received");
        }
    } else {
        assert!(false, "Data packet from unexpected universe received");
    }
    // Start the expected timeout timer.
    let start_time: Instant = Instant::now();

    loop { // Loop till a timeout happens, ignoring the data packets send to the non-timeout uni.
        match dmx_recv.recv(Some(acceptable_upper_bound)) { // This will return a WouldBlock/Timedout error if the timeout takes too long.
            Err(e) => {
                match e {
                    SacnError::UniverseTimeout(_src_cid, universe) => {
                        if start_time.elapsed() < acceptable_lower_bound{
                            assert!(false, "Timeout came quicker than expected");
                        }
                        assert_eq!(universe, universe_timeout, "Unexpected universe timed out");
                        assert!(true, "Universe timed out as expected");

                        // Know that the timeout universe timed out as expected so check that the other universe hasn't timed out.
                        // Makes use of a timeout of 0 which should check the source timeouts without actually receiving any data as it times out instantly.
                        match dmx_recv.recv(Some(Duration::from_millis(0))) {
                            Err(e) => {
                                match e {
                                    SacnError::Io(s) => {
                                        match s.kind() {
                                            std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut => {
                                                assert!(true, "Other universe hasn't timedout as expected");
                                            },
                                            _ => {
                                                assert!(false, "Unexpected error returned");
                                            }
                                        }
                                    },
                                    _ => {
                                        assert!(false, "Unexpected error returned");
                                    }
                                }
                            }
                            Ok(x) => {
                                assert!(false, "Data received unexpectedly as none sent! {:?}", x);
                            }
                        }
                        break;
                    }
                    SacnError::Io(s) => {
                        match s.kind() {
                            std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut => {
                                assert!(false, "Timeout took too long to come through: {:?}", start_time.elapsed());
                            },
                            _ => {
                                assert!(false, "Unexpected error returned");
                            }
                        }
                    },
                    _ => {
                        assert!(false, "Unexpected error returned");
                    }
                }
            }
            Ok(p) => { // Check that only data from the non-timed out universe is received.
                assert_eq!(p.len(), 1, "Data packet universe count doesn't match expected");
                assert_eq!(p[0].universe, universe_no_timeout, "Data packet universe doesn't match expected");
                assert_eq!(p[0].values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Data packet values don't match expected");
            }
        }
    }

    rx.recv().unwrap(); // Allow the sender to finish.
    snd_thread.join().unwrap();
}

// A receiver listens to 2 universes. A sender then sends a packet on the multicast address for the first universe but with the packet
// being for the second universe.
// The receiver should process the packet for the second universe as normal because the multicast address used shouldn't be used to decide
// the universe of the packet.
#[test]
#[ignore]
fn test_send_recv_wrong_multicast_universe(){
    const TIMEOUT: Option<Duration> = Some(Duration::from_secs(3));

    let (tx, rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let thread_tx = tx.clone();

    let multicast_universe = 1;
    let actual_universe = 2;

    let snd_thread = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap(), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();
        let priority = 100;

        src.register_universes(&[multicast_universe, actual_universe]).unwrap();

        // The multicast address for the multicast universe as per ANSI E1.31-2018 Section 9.3.1 Table 9-10.
        let dst_ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(239, 255, 0, 1)), ACN_SDT_MULTICAST_PORT);

        // Sender waits till the receiver says it is ready.
        thread_tx.send(()).unwrap();

        // Send the second universe using the multicast address for the first universe.
        src.send(&[actual_universe], &TEST_DATA_SINGLE_UNIVERSE, Some(priority), Some(dst_ip), None).unwrap();
    });

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[1].parse().unwrap(), ACN_SDT_MULTICAST_PORT), None).unwrap();
    dmx_recv.listen_universes(&[multicast_universe, actual_universe]).unwrap();

    // Receiver created successfully so allow the sender to progress.
    rx.recv().unwrap();

    // Get the packets of data and check that they are correct.
    let received_data: Vec<DMXData> = dmx_recv.recv(TIMEOUT).unwrap();
    assert_eq!(received_data.len(), 1, "Data packet universe count doesn't match expected");

    // Particularly important that the universe is the actual universe of the data rather than the universe which corresponds to the multicast address.
    assert_eq!(received_data[0].universe, actual_universe, "Packet universe doesn't match expected");
    assert_eq!(received_data[0].values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Data packet values don't match expected");

    snd_thread.join().unwrap();
}

/// A receiver and a sender are created which both listen/register to multiple universes.
/// The sender then sends multiple data packets with different sync addresses and then follows up with the various sync packets.
/// The receiver checks that the right data packets are received in the right order based on the sync packets sent.
///
/// This shows that multiple synchronisation addresses can be used simultaneously.
///
#[test]
#[ignore]
fn test_send_recv_multiple_sync_universes(){
    const TIMEOUT: Option<Duration> = Some(Duration::from_secs(3));

    let (tx, rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let thread_tx = tx.clone();

    let universes = [1, 2, 3];

    let snd_thread = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap(), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();

        src.register_universes(&universes).unwrap();

        // Sender waits till the receiver says it is ready.
        thread_tx.send(()).unwrap();

        // Send on all 3 universes, the first universe waits for a sync packet on the second, the second on the third and the third
        // universe waits for a sync packet on its own universe.
        src.send(&[universes[0]], &TEST_DATA_SINGLE_UNIVERSE, None, None, Some(universes[1])).unwrap();
        src.send(&[universes[1]], &TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE, None, None, Some(universes[2])).unwrap();
        src.send(&[universes[2]], &TEST_DATA_PARTIAL_CAPACITY_UNIVERSE, None, None, Some(universes[2])).unwrap();

        src.send_sync_packet(universes[1], None).unwrap(); // Should trigger the first universe to be received.
        src.send_sync_packet(universes[2], None).unwrap(); // Should trigger the second and third universe to be received together.
    });

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[1].parse().unwrap(), ACN_SDT_MULTICAST_PORT), None).unwrap();
    dmx_recv.listen_universes(&universes).unwrap();

    // Receiver created successfully so allow the sender to progress.
    rx.recv().unwrap();

    // Get the packets of data and check that they are correct.

    // First set of data should be the first universe.
    let received_data: Vec<DMXData> = dmx_recv.recv(TIMEOUT).unwrap();
    assert_eq!(received_data.len(), 1, "First set of data universe count doesn't match expected");
    assert_eq!(received_data[0].universe, universes[0], "Packet universe doesn't match expected");
    assert_eq!(received_data[0].values, TEST_DATA_SINGLE_UNIVERSE.to_vec(), "Data packet values don't match expected");

    // Second set of data should be the second and third universe.
    let received_data2: Vec<DMXData> = dmx_recv.recv(TIMEOUT).unwrap();
    assert_eq!(received_data2.len(), 2, "Second set of data universe count doesn't match expected");
    if received_data2[0].universe == universes[1] { // Allow the data to be in any order as no ordering enforced within a set of data.
        assert_eq!(received_data2[0].values, TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE.to_vec(), "Second set of data part 1 packet values don't match expected");

        assert_eq!(received_data2[1].universe, universes[2], "Second set of data universes don't match expected");
        assert_eq!(received_data2[1].values, TEST_DATA_PARTIAL_CAPACITY_UNIVERSE.to_vec(), "Second set of data part 2 packet values don't match expected");
    } else if received_data2[0].universe == universes[2] {
        assert_eq!(received_data2[0].values, TEST_DATA_PARTIAL_CAPACITY_UNIVERSE.to_vec(), "Second set of data part 1 packet values don't match expected");

        assert_eq!(received_data2[1].universe, universes[1], "Second set of data universes don't match expected");
        assert_eq!(received_data2[1].values, TEST_DATA_SINGLE_ALTERNATIVE_STARTCODE_UNIVERSE.to_vec(), "Second set of data part 2 packet values don't match expected");
    } else {
        assert!(false, "Unexpected universe of data received");
    }

    snd_thread.join().unwrap();
}

/// A receiver and a sender are created which both listen to a data universe and a sync universe.
/// The sender then sends a synchronised data packet, the sender then waits for slightly longer than the E131_NETWORK_DATA_LOSS_TIMEOUT before sending
/// the corresponding sync packet. As per ANSI E1.31-2018 Section 11.1.2 this data should be discarded as universe synchronisation should stop if the
/// sync packet isn't received within the E131_NETWORK_DATA_LOSS_TIMEOUT.
///
/// This shows that this timeout mechanism to stop universe synchronisation works.
///
/// Note that this library does not attempt to implement the force_synchronisation bit behaviour and so therefore always stops universe synchronisation if the
/// sync packet is not received within the timeout.
///
#[test]
#[ignore]
fn test_send_sync_timeout(){
    const TIMEOUT: Option<Duration> = Some(Duration::from_secs(5));

    // Need to wait slightly longer than the E131_NETWORK_DATA_LOSS_TIMEOUT so that the synchronised data packet should timeout.
    let sender_wait_period: Duration = E131_NETWORK_DATA_LOSS_TIMEOUT + Duration::from_millis(100);

    let (tx, rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let thread_tx = tx.clone();

    let data_universe = 1;
    let sync_universe = 2;

    let snd_thread = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap(), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_ip("Source", ip).unwrap();

        src.register_universes(&[data_universe, sync_universe]).unwrap();

        // Sender waits till the receiver says it is ready.
        thread_tx.send(()).unwrap();

        // Sender sends a data packet synchronised to the synchronisation universe.
        src.send(&[data_universe], &TEST_DATA_SINGLE_UNIVERSE, None, None, Some(sync_universe)).unwrap();

        // Sender waits too long to send the sync packet meaning that the synchronisation should have timed out.
        sleep(sender_wait_period);

        // Since the data packet should have timed out this should have no effect on the receiver.
        src.send_sync_packet(sync_universe, None).unwrap();
    });

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[1].parse().unwrap(), ACN_SDT_MULTICAST_PORT), None).unwrap();
    dmx_recv.listen_universes(&[data_universe, sync_universe]).unwrap();

    // Receiver created successfully so allow the sender to progress.
    rx.recv().unwrap();

    // Data should never be passed up because the data packet should have timed-out before the sync packet is processed.
    match dmx_recv.recv(TIMEOUT) {
        Err(e) => {
            match e {
                SacnError::Io(s) => {
                    match s.kind() {
                        std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut => {
                            // Timeout as expected because the data packet that is awaiting a sync packet has timed out.
                            // The different errors are due to windows and unix returning different errors for the same thing.
                            assert!(true, "Timed out as expected meaning synchronised data packet timed out as expected");
                        },
                        _ => {
                            assert!(false, "Unexpected error returned");
                        }
                    }
                },
                _ => {
                    assert!(false, "Unexpected error returned");
                }
            }
        }
        Ok(p) => {
            // println!("Elapsed {:?}", p[0].recv_timestamp.elapsed());
            assert!(false, "Received data unexpectedly: {:?}", p);
        }
    }
    snd_thread.join().unwrap();
}

/// Setups and runs through the scenario as described in ANSI E1.31-2018 Appendix B.
/// This asserts that the behaviour of this implementation is exactly as outlined within that section.
/// This shows that the implementation handles universe synchronisation in the way specified by the protocol document.
/// As the force synchronisation option is not implemented as part of this library that section is ignored.
#[test]
#[ignore]
fn test_ansi_e131_appendix_b_runthrough_ipv4() {
    // The number of set of (data packets + sync packet) to send.
    const SYNC_PACKET_COUNT: usize = 5;

    // The number of data packets sent before each sync packet.
    const DATA_PACKETS_PER_SYNC_PACKET: usize = 2;

    // The 'slight pause' as specified by ANSI E1.31-2018 Section 11.2.2 between data and sync packets.
    const PAUSE_PERIOD: Duration = Duration::from_millis(100);

    let (tx, rx): (SyncSender<()>, Receiver<()>) = mpsc::sync_channel(0);

    let thread_tx = tx.clone();

    let data_universes = [1, 2];
    let sync_universe = 7962;
    let priority = 100;
    let source_name = "Source_A";
    let data = [0x00, 0xe, 0x0, 0xc, 0x1, 0x7, 0x1, 0x4, 0x8, 0x0, 0xd, 0xa, 0x7, 0xa];
    let data2 = [0x00, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xa];
    let src_cid: Uuid = Uuid::from_bytes([0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6, 0x14, 0x3e].try_into().unwrap());

    let snd_thread = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap(), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_cid_ip(source_name, src_cid, ip).unwrap();

        src.register_universes(&data_universes).unwrap();
        src.register_universe(sync_universe).unwrap();

        // Sender waits till the receiver says it is ready.
        thread_tx.send(()).unwrap();

        for _ in 0 .. SYNC_PACKET_COUNT {
            // Sender sends data packets to the 2 data universes using the same synchronisation address.
            src.send(&[data_universes[0]], &data, Some(priority), None, Some(sync_universe)).unwrap();
            src.send(&[data_universes[1]], &data2, Some(priority), None, Some(sync_universe)).unwrap();

            // Sender observes a slight pause to allow for processing delays (ANSI E1.31-2018 Section 11.2.2).
            sleep(PAUSE_PERIOD);

            // Sender sends a synchronisation packet to the sync universe.
            src.send_sync_packet(sync_universe, None).unwrap();
        }

        // Sender sends a data packet to the data universe using a zero synchronisation address indicating synchronisation is now over.
        src.send(&[data_universes[0]], &data, Some(priority), None, None).unwrap();
        src.send(&[data_universes[1]], &data2, Some(priority), None, None).unwrap();
    });

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[1].parse().unwrap(), ACN_SDT_MULTICAST_PORT), None).unwrap();

    // Receiver only listening to the data universe, the sync universe should be joined automatically when a data packet that requires it arrives.
    dmx_recv.listen_universes(&data_universes).unwrap();

    // Receiver created successfully so allow the sender to progress.
    rx.recv().unwrap();

    for _ in 0 .. SYNC_PACKET_COUNT {
        // "When the E1.31 Synchronization Packet arrives from Source A, Receiver B acts on the data."
        match dmx_recv.recv(None) {
            Ok(p) => {
                assert_eq!(p.len(), DATA_PACKETS_PER_SYNC_PACKET);
                if p[0].universe == data_universes[0] {
                    assert_eq!(p[0].values, data, "Unexpected data within first data packet of a set of synchronised packets");

                    assert_eq!(p[1].universe, data_universes[1], "Unrecognised universe as second data packet in set of synchronised packets");
                    assert_eq!(p[1].values, data2, "Unexpected data within second data packet of a set of synchronised packets");
                } else if p[0].universe == data_universes[1] {
                    assert_eq!(p[0].values, data2, "Unexpected data within first data packet of a set of synchronised packets");

                    assert_eq!(p[1].universe, data_universes[0], "Unrecognised universe as second data packet in set of synchronised packets");
                    assert_eq!(p[1].values, data, "Unexpected data within second data packet of a set of synchronised packets");
                } else {
                    assert!(false, "Unrecognised universe within data packet");
                }
            }
            Err(e) => {
                assert!(false, "Unexpected error returned: {:?}", e);
            }
        }
    }
    // "This process continues until Receiver B receives an E1.31 Data Packet with a Synchronization Address of 0."
    // "Receiver B may then unsubscribe from the synchronization multicast address" - This implementation does not automatically unsubscribe
    //        This is based on the reasoning that a synchronisation universe will be used multiple times and subscribing/un-subscribing is unneeded overhead.
    // Synchronisation is now over so should receive 2 packets individually.
    let rcv_data = dmx_recv.recv(None).unwrap();
    assert_eq!(rcv_data.len(), 1);
    assert_eq!(rcv_data[0].universe, data_universes[0]);
    assert_eq!(rcv_data[0].values, data);

    let rcv_data2 = dmx_recv.recv(None).unwrap();
    assert_eq!(rcv_data2.len(), 1);
    assert_eq!(rcv_data2[0].universe, data_universes[1]);
    assert_eq!(rcv_data2[0].values, data2);

    // "If, at any time, Receiver B receives more than one E1.31 Data Packet with the same Synchronization
    // Address in it, before receiving an E1.31 Universe Synchronization Packet, it will discard all but the most
    // recent E1.31 Data Packet. Those packets are only acted upon when the synchronization command
    // arrives."
    // This is taken to refer to a data packet within the same universe and synchronisation address not a packet with any universe
    // this assumption is based on the wording "Universe synchronization is required for applications where receivers require more than one universe to
    // be controlled, multiple receivers produce synchronized output, or unsynchronized control of receivers may
    // result in undesired visual effects." from ANSI E1.31-2018 Section 11. This wording indicates that one use case of synchronisation is to allow
    // receivers with more than one universe to be controlled however this would be impossible if the statement above (from ANSI E1.31-2018 Appendix B)
    // indicated that data packets for all but one universe should be discarded.

    // "Since the the Force_Synchronization bit in the Options field of the E1.31 Data Packet has been set to 0,
    // even if Source A times out the E131_NETWORK_DATA_LOSS_TIMEOUT, Receiver B will stay in its last
    // look until a new E1.31 Synchronization Packet arrives."
    // The implementation does not support the force synchronisation bit so always acts as if is set to 1 and times out.

    snd_thread.join().unwrap();
}

/// Sets up a single source and receiver. Like in a real-world scenario the source sends data continuously on 2 universes synchronised
/// on a third universe with a small interval between data sends.
/// The receiver starts with no knowledge of what universe the source is sending on so starts by using Universe Discovery to discover the universes
/// it then joins these universes and receives the data. The sender eventually stops sending data and terminates by sending stream termination packets.
/// The receiver receives these packets and also terminates.
/// This shows that the implementation works in a simulated scenario that makes use of multiple features / parts.
/// It also shows the receiver 'jumping into' a stream of data that has already started (meaning sequence numbers are already > 0).
#[test]
#[ignore]
fn test_discover_recv_sync_runthrough_ipv4() {
    // The number of set of (data packets + sync packet) to send.
    const SYNC_PACKET_COUNT: usize = 250;

    // The number of data packets sent before each sync packet.
    const DATA_PACKETS_PER_SYNC_PACKET: usize = 2;

    // The 'slight pause' as specified by ANSI E1.31-2018 Section 11.2.2 between data and sync packets.
    const PAUSE_PERIOD: Duration = Duration::from_millis(50);

    // The interval between sets of sync/data packets.
    const INTERVAL: Duration = Duration::from_millis(100);

    // The universes used for data.
    const DATA_UNIVERSES: [u16; 2] = [1, 2];

    // The universe used for synchronisation packets.
    const SYNC_UNIVERSE: u16 = 4;

    // The source name
    const SOURCE_NAME: &str = "Test Source";

    // The data send on the first and second universes.
    const DATA: [u8; 16] = [0x00, 0xe, 0x0, 0xc, 0x1, 0x7, 0x1, 0x4, 0x8, 0x0, 0xd, 0xa, 0x7, 0xa, 0x9, 0x8];
    const DATA2: [u8; 16] =[0x00, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0xa, 0x9, 0x8];

    // The source CID.
    let src_cid: Uuid = Uuid::from_bytes([0xef, 0x07, 0xc8, 0xdd, 0x00, 0x64, 0x44, 0x01, 0xa3, 0xa2, 0x45, 0x9e, 0xf8, 0xe6, 0x14, 0x3e].try_into().unwrap());

    let snd_thread = thread::spawn(move || {
        let ip: SocketAddr = SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap(), ACN_SDT_MULTICAST_PORT + 1);
        let mut src = SacnSource::with_cid_ip(SOURCE_NAME, src_cid, ip).unwrap();

        src.register_universes(&DATA_UNIVERSES).unwrap();
        src.register_universe(SYNC_UNIVERSE).unwrap();

        for _ in 0 .. SYNC_PACKET_COUNT {
            // Sender sends data packets to the 2 data universes using the same synchronisation address.
            src.send(&[DATA_UNIVERSES[0]], &DATA, None, None, Some(SYNC_UNIVERSE)).unwrap();
            src.send(&[DATA_UNIVERSES[1]], &DATA2, None, None, Some(SYNC_UNIVERSE)).unwrap();

            // Sender observes a slight pause to allow for processing delays (ANSI E1.31-2018 Section 11.2.2).
            sleep(PAUSE_PERIOD);

            // Sender sends a synchronisation packet to the sync universe.
            src.send_sync_packet(SYNC_UNIVERSE, None).unwrap();

            sleep(INTERVAL);
        }

        // Sender goes out of scope so will automatically send termination packets.
    });

    let mut dmx_recv = SacnReceiver::with_ip(SocketAddr::new(TEST_NETWORK_INTERFACE_IPV4[1].parse().unwrap(), ACN_SDT_MULTICAST_PORT), None).unwrap();

    // Receiver starts by not listening to any data universes (automatically listens to discovery universe).

    dmx_recv.set_announce_source_discovery(true);

    let universes: Vec<u16> = match dmx_recv.recv(None) {
        Err(e) => {
            match e {
                SacnError::SourceDiscovered(_name) => {
                    let discovered_sources = dmx_recv.get_discovered_sources();
                    assert_eq!(discovered_sources.len(), 1);

                    // Found the source so don't want to be notified about other sources.
                    dmx_recv.set_announce_source_discovery(false);

                    // Do want to be notified about stream termination in this case.
                    dmx_recv.set_announce_stream_termination(true);

                    discovered_sources[0].get_all_universes()
                }
                _ => {
                    // A real-user would want to look at using more detailed error handling as appropriate to their use case but for this test panic
                    // (which will fail the test) is suitable.
                    panic!("Unexpected error");
                }
            }
        }
        Ok(_) => {
            panic!("Unexpected data packet before any data universes registered");
        }
    };

    dmx_recv.listen_universes(&universes).unwrap(); // Assert Successful

    loop {
        match dmx_recv.recv(None) {
            Err(e) => {
                match e {
                    SacnError::UniverseTerminated(_src_cid, _universe) => {
                        // A real use-case may also want to not terminate when the source does and instead remain waiting but in this
                        // case the for the test the receiver terminates with the source.
                        break;
                    }
                    _ => {
                        assert!(false, "Unexpected error returned");
                    }
                }
            }
            Ok(rcv_data) => {
                assert_eq!(rcv_data.len(), DATA_PACKETS_PER_SYNC_PACKET);
                if rcv_data[0].universe == DATA_UNIVERSES[0] {
                    assert_eq!(rcv_data[0].values, DATA, "Unexpected data within first data packet of a set of synchronised packets");

                    assert_eq!(rcv_data[1].universe, DATA_UNIVERSES[1], "Unrecognised universe as second data packet in set of synchronised packets");
                    assert_eq!(rcv_data[1].values, DATA2, "Unexpected data within second data packet of a set of synchronised packets");
                } else if rcv_data[0].universe == DATA_UNIVERSES[1] {
                    assert_eq!(rcv_data[0].values, DATA2, "Unexpected data within first data packet of a set of synchronised packets");

                    assert_eq!(rcv_data[1].universe, DATA_UNIVERSES[0], "Unrecognised universe as second data packet in set of synchronised packets");
                    assert_eq!(rcv_data[1].values, DATA, "Unexpected data within second data packet of a set of synchronised packets");
                } else {
                    assert!(false, "Unrecognised universe within data packet");
                }
            }
        }
    }

    // Finished receiving from the sender.
    snd_thread.join().unwrap();
}

/// Generates a data packet as raw bytes with the given parameters.
/// Assert parameters are correct sizes / in-range as appropriate.
fn generate_data_packet_raw(cid: [u8; 16], universe: u16, source_name: String, priority: u8, seq_num: u8, options: u8, dmx_data: Vec<u8>) -> Vec<u8> {
    assert!(universe >= E131_MIN_MULTICAST_UNIVERSE && universe <= E131_MAX_MULTICAST_UNIVERSE, "Generated data packet universe out of range");
    assert!(priority <= E131_MAX_PRIORITY, "Generated data packet priority too high");
    assert!(dmx_data.len() <= UNIVERSE_CHANNEL_CAPACITY);
    assert_eq!(source_name.len(), 64);

    // Root ACN Layer
    let mut packet = Vec::new();

    // Preamble Size
    packet.extend("\x00\x10".bytes());

    // Post-amble Size
    packet.extend("\x00\x00".bytes());

    // ACN Packet Identifier
    packet.extend("\x41\x53\x43\x2d\x45\x31\x2e\x31\x37\x00\x00\x00".bytes());

    // Flags and Length (22 + 343)
    packet.push(0b01110001);
    packet.push(0b01101101);

    // Vector
    packet.extend("\x00\x00\x00\x04".bytes());

    // CID
    packet.extend(&cid);

    // E1.31 Framing Layer
    // Flags and Length (77 + 266)
    packet.push(0b01110001);
    packet.push(0b01010111);

    // Vector
    packet.extend("\x00\x00\x00\x02".bytes());

    // Source Name
    packet.extend(source_name.bytes());

    // Priority
    packet.push(priority);

    // Reserved
    packet.extend("\x00\x00".bytes());

    // Sequence Number
    packet.push(seq_num);

    // Options
    packet.push(options);

    // Universe, conversion to BigEndian bytes as Network Byte Order is BigEndian.
    let universe_bytes = universe.to_be_bytes();
    packet.push(universe_bytes[0]);
    packet.push(universe_bytes[1]);

    // DMP Layer
    // Flags and Length (266)
    packet.push(0b01110001);
    packet.push(0b00001010);

    // Vector
    packet.push(0x02);

    // Address Type & Data Type
    packet.push(0xa1);

    // First Property Address
    packet.extend("\x00\x00".bytes());

    // Address Increment
    packet.extend("\x00\x01".bytes());

    // Property value count = 255.
    packet.push(0b1);
    packet.push(0b00000000);

    // Property values
    packet.extend(&dmx_data);

    packet
}

/// Generates a sync packet as raw bytes with the given parameters.
fn generate_sync_packet_raw(cid: [u8; 16], sync_addr: u16, seq_num: u8) -> Vec<u8> {
    // Root ACN Layer
    let mut sync_packet = Vec::new();

    // Preamble Size
    sync_packet.extend("\x00\x10".bytes());

    // Post-amble Size
    sync_packet.extend("\x00\x00".bytes());

    // ACN Packet Identifier
    sync_packet.extend("\x41\x53\x43\x2d\x45\x31\x2e\x31\x37\x00\x00\x00".bytes());

    // Flags and Length (0x70, 33)
    sync_packet.push(0b01110000);
    sync_packet.push(0b00100001);

    // Vector, VECTOR_ROOT_E131_EXTENDED as per ANSI E1.31-2018 Section 4.2 Table 4-2.
    sync_packet.extend("\x00\x00\x00\x08".bytes());

    // CID
    sync_packet.extend(&cid);

    // E1.31 Framing Layer
    // Flags and Length (0x70, 11)
    sync_packet.push(0b01110000);
    sync_packet.push(0b00001011);

    // Vector, VECTOR_E131_EXTENDED_SYNCHRONISATION as per ANSI E1.31-2018 Appendix A.
    sync_packet.extend("\x00\x00\x00\x01".bytes());

    // Sequence Number
    sync_packet.push(seq_num);

    // Synchronisation Address, conversion to BigEndian bytes as Network Byte Order is BigEndian.
    let sync_addr_bytes = sync_addr.to_be_bytes();
    sync_packet.push(sync_addr_bytes[0]);
    sync_packet.push(sync_addr_bytes[1]);

    // Reserve bytes as 0 as per ANSI E1.31-2018 Section 6.3.4.
    sync_packet.push(0);
    sync_packet.push(0);

    sync_packet
}

/// Creates a test data packet and tests sending it to a udp socket and then checking that the output bytes match expected.
/// This shows that the SacnSource sends a data packet in the correct format.
///
/// The use of a UDP socket also shows that the protocol uses UDP at the transport layer.
///
#[test]
#[cfg_attr(rustfmt, rustfmt_skip)]
#[ignore]
fn test_data_packet_transmit_format() {
    const CID: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    const OPTIONS: u8 = 0; // Checks that the options field is transmitted as 0's.
    const PRIORITY: u8 = 150;

    let universe: u16 = 1;

    let source_name = "SourceName".to_string() +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0";

    let sequence = 0;
    let mut dmx_data: Vec<u8> = Vec::new();
    dmx_data.push(0); // Start code
    dmx_data.extend(iter::repeat(100).take(255));

    let packet = generate_data_packet_raw(CID, universe, source_name.clone(), PRIORITY, sequence, OPTIONS, dmx_data.clone());

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), ACN_SDT_MULTICAST_PORT + 1);
    let mut source = SacnSource::with_cid_ip(&source_name.clone(), Uuid::from_bytes(CID), ip).unwrap();

    source.set_preview_mode(false).unwrap();
    source.set_multicast_loop_v4(true).unwrap();

    let mut recv_socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();

    let addr: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT);

    recv_socket.bind(&addr.into()).unwrap();

    recv_socket.join_multicast_v4(&Ipv4Addr::new(239, 255, 0, 1), &Ipv4Addr::new(0, 0, 0, 0))
                .unwrap();

    let mut recv_buf = [0; 1024];

    source.register_universes(&[universe]).unwrap();

    source.send(&[universe], &dmx_data, Some(PRIORITY), None, None).unwrap();
    let amt = recv_socket.read(&mut recv_buf).unwrap();

    assert_eq!(&packet[..], &recv_buf[0..amt]);
}

/// Follows a similar process to test_data_packet_transmit_format by creating a SacnSender and then a receiving socket. The sender
/// then terminates a stream and the receive socket receives and checks that the sender sent the correct number (3) of termination packets.
#[test]
#[ignore]
fn test_terminate_packet_transmit_format() {
    let cid = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT + 1);
    let mut source = SacnSource::with_cid_ip(&"Source", Uuid::from_bytes(cid), ip).unwrap();

    source.set_multicast_loop_v4(true).unwrap();

    let mut recv_socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();

    let addr: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT);

    recv_socket.bind(&addr.into()).unwrap();

    recv_socket
        .join_multicast_v4(&Ipv4Addr::new(239, 255, 0, 1), &Ipv4Addr::new(0, 0, 0, 0))
        .unwrap();

    let mut recv_buf = [0; 1024];

    let start_code: u8 = 0;

    source.register_universes(&[1]).unwrap();

    source.terminate_stream(1, start_code).unwrap();
    for _ in 0..2 {
        recv_socket.read(&mut recv_buf).unwrap();
        assert_eq!(
            match AcnRootLayerProtocol::parse(&recv_buf).unwrap().pdu.data {
                E131RootLayerData::DataPacket(data) => data.stream_terminated,
                _ => panic!(),
            },
            true
        )
    }
}

/// Similar to test_data_packet_transmit_format, creates a SacnSender and then a receiver socket. The sender then sends
/// a synchronisation packet and the receive socket receives the packet and checks that the format of the packet is as expected.
///
/// The use of a UDP socket also shows that the protocol uses UDP at the transport layer.
///
#[test]
#[ignore]
fn test_sync_packet_transmit_format() {
    const CID: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

    const SYNC_ADDR: u16 = 1;

    // Sync packet length 49 bytes as per ANSI E1.31-2018 Section 4.2 Table 4-2.
    const E131_SYNC_PACKET_LENGTH: usize = 49;

    // Sequence number of initial synchronisation packet is expected to be 0.
    const SEQUENCE_NUM: u8 = 0;

    let sync_packet = generate_sync_packet_raw(CID, SYNC_ADDR, SEQUENCE_NUM);

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT + 1);
    let mut source = SacnSource::with_cid_ip(&"Source", Uuid::from_bytes(CID), ip).unwrap();

    source.set_multicast_loop_v4(true).unwrap();

    // Create a standard udp receive socket to receive the packet sent by the source.
    let mut recv_socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();

    let addr: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT);

    recv_socket.bind(&addr.into()).unwrap();

    recv_socket
        .join_multicast_v4(&Ipv4Addr::new(239, 255, 0, 1), &Ipv4Addr::new(0, 0, 0, 0))
        .unwrap();

    let mut recv_buf = [0; E131_SYNC_PACKET_LENGTH];

    // Send the synchronisation packet.
    source.register_universes(&[SYNC_ADDR]).unwrap();
    source.send_sync_packet(SYNC_ADDR, None).unwrap();

    // Receive the packet and compare its content to the expected.
    recv_socket.read(&mut recv_buf).unwrap();

    assert_eq!(recv_buf[..], sync_packet[..], "Sync packet sent by source doesn't match expected format");
}

/// Similar to test_data_packet_transmit_format, creates a SacnSender and then a receiver socket. The sender then sends
/// a discovery packet and the receive socket receives the packet and checks that the format of the packet is as expected.
///
/// The use of a UDP socket also shows that the protocol uses UDP at the transport layer.
///
#[test]
#[ignore]
fn test_discovery_packet_transmit_format() {
    const CID: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

    // Source name = "Controller"
    const SOURCE_NAME: [u8; 64] = [b'C', b'o', b'n', b't', b'r', b'o', b'l', b'l', b'e', b'r', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                                    0, 0, 0, 0];

    // Represents 3 16 bit universes.
    const UNIVERSES: [u8; 6] = [0x0, 0x1 , 0x0, 0x2, 0x0, 0x3];

    // Discovery packet length 49 bytes as per ANSI E1.31-2018 Section 8 Table 8-9.
    const DISCOVERY_PACKET_LENGTH_EXPECTED: usize = 120 + UNIVERSES.len();

    // As the number of universes will fit on one page expect the page number and last page number to both be 0.
    const PAGE: u8 = 0;
    const LAST_PAGE: u8 = 0;

    // Root ACN Layer
    let mut discovery_packet = Vec::new();

    // Preamble Size
    discovery_packet.extend("\x00\x10".bytes());

    // Post-amble Size
    discovery_packet.extend("\x00\x00".bytes());

    // ACN Packet Identifier
    discovery_packet.extend("\x41\x53\x43\x2d\x45\x31\x2e\x31\x37\x00\x00\x00".bytes());

    // Flags and Length (0x70, 110)
    discovery_packet.push(0b01110000);
    discovery_packet.push(0b01101110);

    // Vector, VECTOR_ROOT_E131_EXTENDED as per ANSI E1.31-2018 Section 4.3 Table 4-3 and Appendix A.
    discovery_packet.extend("\x00\x00\x00\x08".bytes());

    // CID
    discovery_packet.extend(&CID);

    // E1.31 Framing Layer
    // Flags and Length (0x70, 88)
    discovery_packet.push(0b01110000);
    discovery_packet.push(0b01011000);

    // Vector, VECTOR_E131_EXTENDED_DISCOVERY as per ANSI E1.31-2018 Section 4.3 Table 4-3 and Appendix A.
    discovery_packet.extend("\x00\x00\x00\x02".bytes());

    // Source Name
    discovery_packet.extend(SOURCE_NAME.iter());

    // Reserve bytes, should be transmitted as 0's as per ANSI E1.31-2018 Section 6.4.3.
    discovery_packet.push(0);
    discovery_packet.push(0);
    discovery_packet.push(0);
    discovery_packet.push(0);

    // Universe Discovery Layer
    // Flags and Length (0x70, 14)
    discovery_packet.push(0b01110000);
    discovery_packet.push(0b00001110);

    // Vector, VECTOR_UNIVERSE_DISCOVERY_UNIVERSE_LIST as per ANSI E1.31-2018 Section 4.3 Table 4-3 and Appendix A.
    discovery_packet.extend("\x00\x00\x00\x01".bytes());

    // Page and last page
    discovery_packet.push(PAGE);
    discovery_packet.push(LAST_PAGE);

    // The list of universes that are being advertised by the discovery packet.
    discovery_packet.extend(UNIVERSES.iter());

    assert_eq!(discovery_packet.len(), DISCOVERY_PACKET_LENGTH_EXPECTED, "Example discovery packet length doesn't match expected");

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT + 1);

    // Creates the source.
    let mut source = SacnSource::with_cid_ip(&str::from_utf8(&SOURCE_NAME).unwrap(), Uuid::from_bytes(CID), ip).unwrap();

    source.set_multicast_loop_v4(true).unwrap();

    // Create a standard udp receive socket to receive the packet sent by the source.
    let mut recv_socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();

    let addr: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT);

    recv_socket.bind(&addr.into()).unwrap();

    // Receiving on the discovery universe shows that the discovery universe is correctly used for discovery packets as per ANSI E1.31-2018 Section 6.2.7.
    let address = universe_to_ipv4_multicast_addr(E131_DISCOVERY_UNIVERSE).unwrap().as_socket_ipv4();

    recv_socket
        .join_multicast_v4(&address.unwrap().ip(), &Ipv4Addr::new(0, 0, 0, 0))
        .unwrap();

    let mut recv_buf = [0; DISCOVERY_PACKET_LENGTH_EXPECTED];

    // Register the universes, note be = BigEndian which is used as network byte order is BigEndian.
    source.register_universes(&[
        u16::from_be_bytes(UNIVERSES[0..2].try_into().unwrap()),
        u16::from_be_bytes(UNIVERSES[2..4].try_into().unwrap()),
        u16::from_be_bytes(UNIVERSES[4..6].try_into().unwrap())
        ]).unwrap();

    // The source is expected to eventually send a universe discovery packet.

    // Receive the packet and compare its content to the expected.
    recv_socket.read(&mut recv_buf).unwrap();

    assert_eq!(recv_buf[..], discovery_packet[..], "Discovery packet sent by source doesn't match expected format");
}

/// Similar to test_data_packet_transmit_format, creates a SacnSender and then a receiver socket. The sender then sends
/// a synchronisation packet and the receive socket receives the packet and checks that the format of the packet is as expected.
///
/// The use of a UDP socket also shows that the protocol uses UDP at the transport layer.
///
#[test]
#[ignore]
fn test_sync_packet_transmit_seq_numbers() {
    const CID: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

    const UNIVERSE: u16 = 1;

    // Sync packet length 49 bytes as per ANSI E1.31-2018 Section 4.2 Table 4-2.
    const E131_SYNC_PACKET_LENGTH: usize = 49;

    // Sequence number of initial synchronisation packet is expected to be 0.
    const SEQUENCE_NUM: u8 = 0;

    // Root Layer
    let mut sync_packet = Vec::new();

    // Preamble Size
    sync_packet.extend("\x00\x10".bytes());

    // Post-amble Size
    sync_packet.extend("\x00\x00".bytes());

    // ACN Packet Identifier
    sync_packet.extend("\x41\x53\x43\x2d\x45\x31\x2e\x31\x37\x00\x00\x00".bytes());

    // Flags and Length (0x70, 33)
    sync_packet.push(0b01110000);
    sync_packet.push(0b00100001);

    // Vector, VECTOR_ROOT_E131_EXTENDED as per ANSI E1.31-2018 Section 4.2 Table 4-2.
    sync_packet.extend("\x00\x00\x00\x08".bytes());

    // CID
    sync_packet.extend(&CID);

    // E1.31 Framing Layer
    // Flags and Length (0x70, 11)
    sync_packet.push(0b01110000);
    sync_packet.push(0b00001011);

    // Vector, VECTOR_E131_EXTENDED_SYNCHRONISATION as per ANSI E1.31-2018 Appendix A.
    sync_packet.extend("\x00\x00\x00\x01".bytes());

    // Sequence Number
    sync_packet.push(SEQUENCE_NUM);

    // Synchronisation Address, 1
    sync_packet.push(0);
    sync_packet.push(1);

    // Reserve bytes as 0 as per ANSI E1.31-2018 Section 6.3.4.
    sync_packet.push(0);
    sync_packet.push(0);

    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT + 1);
    let mut source = SacnSource::with_cid_ip(&"Source", Uuid::from_bytes(CID), ip).unwrap();

    source.set_multicast_loop_v4(true).unwrap();

    // Create a standard udp receive socket to receive the packet sent by the source.
    let mut recv_socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();

    let addr: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT);

    recv_socket.bind(&addr.into()).unwrap();

    recv_socket
        .join_multicast_v4(&Ipv4Addr::new(239, 255, 0, 1), &Ipv4Addr::new(0, 0, 0, 0))
        .unwrap();

    let mut recv_buf = [0; E131_SYNC_PACKET_LENGTH];

    // Send the synchronisation packet.
    source.register_universes(&[UNIVERSE]).unwrap();
    source.send_sync_packet(UNIVERSE, None).unwrap();

    // Receive the packet and compare its content to the expected.
    recv_socket.read(&mut recv_buf).unwrap();

    assert_eq!(recv_buf[..], sync_packet[..], "Sync packet sent by source doesn't match expected format");
}

/// Creates a source and a receiver socket. The source then sends data packets meant for different universes and the receiver checks
/// that the sequence numbers are incremented by 1 for each packet and are incremented independently for each universe.SockAddr
///
/// This shows the source complies with ANSI E1.31-2018 Section 6.2.5 "E1.31 Data Packet: Sequence Number".
///
#[test]
#[cfg_attr(rustfmt, rustfmt_skip)]
#[ignore]
fn test_track_data_packet_seq_numbers() {
    /* Packet parameters */
    const CID: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    const OPTIONS: u8 = 0; // Checks that the options field is transmitted as 0's.
    const PRIORITY: u8 = 150;
    let source_name = "SourceName".to_string() +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0";
    let mut dmx_data: Vec<u8> = Vec::new();
    dmx_data.push(0); // Start code
    dmx_data.extend(iter::repeat(100).take(255));

    /* The parameters above are set to arbitrary values as they aren't the focus of the test*/

    // The expected starting sequence number of data packets from the source.
    const START_SEQ_NUM: usize = 0;

    // The number of data packets to send per universe.
    const DATA_PACKETS_TO_SEND: usize = 300;

    // The universes that the data packets are sent on.
    const UNIVERSES: [u16; 5] = [1, 3, 5, 7, 9];

    // Create a source.
    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), ACN_SDT_MULTICAST_PORT + 1);
    let mut source = SacnSource::with_cid_ip(&source_name.clone(), Uuid::from_bytes(CID), ip).unwrap();
    source.set_multicast_loop_v4(true).unwrap();
    source.register_universes(&UNIVERSES).unwrap();

    // Don't want universe discovery packets to be sent which might interfer with checking data packets.
    source.set_is_sending_discovery(false);

    // Create receiver socket.
    let mut recv_socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();
    let addr: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT);
    recv_socket.bind(&addr.into()).unwrap();

    // Join the multicast groups for each of the universes.
    for u in UNIVERSES.iter() {
        let address = universe_to_ipv4_multicast_addr(*u).unwrap().as_socket_ipv4();

        recv_socket
            .join_multicast_v4(&address.unwrap().ip(), &Ipv4Addr::new(0, 0, 0, 0))
            .unwrap();
    }

    for s in START_SEQ_NUM .. START_SEQ_NUM + DATA_PACKETS_TO_SEND {
        let expected_seq_num: u8 = (s % 256).try_into().unwrap();
        for u in UNIVERSES.iter() {
            let expected_packet = generate_data_packet_raw(CID, *u, source_name.clone(), PRIORITY, expected_seq_num, OPTIONS, dmx_data.clone());
            source.send(&[*u], &dmx_data, Some(PRIORITY), None, None).unwrap();

            let mut recv_buf = [0; 1024];
            let amt = recv_socket.read(&mut recv_buf).unwrap();

            assert_eq!(&recv_buf[0..amt], &expected_packet[..]);
        }
    }
}

/// Creates a source and a receiver socket. The source then sends data packets meant for different universes and the receiver checks
/// that the sequence numbers are incremented by 1 for each packet and are incremented independently for each universe.SockAddr
///
/// This shows the source complies with ANSI E1.31-2018 Section 6.2.5 "E1.31 Data Packet: Sequence Number".
///
#[test]
#[cfg_attr(rustfmt, rustfmt_skip)]
#[ignore]
fn test_track_sync_packet_seq_numbers() {
    // Source CID and name, set to arbitrary values as not the focus of the test.
    const CID: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

    let source_name = "SourceName".to_string() +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0";

    // The expected starting sequence number of sync packets from the source.
    const START_SEQ_NUM: usize = 0;

    // The number of sync packets to send per universe. Chosen to be high enough that a sequence number wrap around due to the maximum possible value in a u8 is required.
    // This checks that the sequence numbers wrap around correctly.
    const SYNC_PACKETS_TO_SEND: usize = 300;

    // The universes that the sync packets are sent on.
    const SYNC_ADDRESSES: [u16; 5] = [1, 3, 5, 7, 9];

    // Create a source.
    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), ACN_SDT_MULTICAST_PORT + 1);
    let mut source = SacnSource::with_cid_ip(&source_name.clone(), Uuid::from_bytes(CID), ip).unwrap();
    source.set_multicast_loop_v4(true).unwrap();

    // Register the synchronisation addresses.
    source.register_universes(&SYNC_ADDRESSES).unwrap();

    // Don't want universe discovery packets to be sent which might interfer with checking sync packets.
    source.set_is_sending_discovery(false);

    // Create receiver socket.
    let mut recv_socket = Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap();
    let addr: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), ACN_SDT_MULTICAST_PORT);
    recv_socket.bind(&addr.into()).unwrap();

    // Join the multicast groups for each of the synchronisation addresses.
    for u in SYNC_ADDRESSES.iter() {
        let address = universe_to_ipv4_multicast_addr(*u).unwrap().as_socket_ipv4();

        recv_socket
            .join_multicast_v4(&address.unwrap().ip(), &Ipv4Addr::new(0, 0, 0, 0))
            .unwrap();
    }

    for s in START_SEQ_NUM .. START_SEQ_NUM + SYNC_PACKETS_TO_SEND {
        let expected_seq_num: u8 = (s % 256).try_into().unwrap();
        for a in SYNC_ADDRESSES.iter() {
            let expected_packet = generate_sync_packet_raw(CID, *a, expected_seq_num);
            source.send_sync_packet(*a, None).unwrap();

            let mut recv_buf = [0; 1024];
            let amt = recv_socket.read(&mut recv_buf).unwrap();

            assert_eq!(&recv_buf[0..amt], &expected_packet[..]);
        }
    }
}

/// Creates 5 receiver sockets each listening to a different multicast address for a specific synchronisation address.
/// Then creates a source which sends synchronisation packets meant for different synchronisation addresses.
/// The receiver sockets check that they only receive synchronisation packets meant for their synchronisation address / multicast address.
///
/// This shows that synchronisation packets are only sent to the multicast address which corresponds to the synchronisation address as per
/// ANSI E1.31-2018 Section 6.3.3.1.
///
#[test]
#[cfg_attr(rustfmt, rustfmt_skip)]
#[ignore]
/// Linux only because of the mechanism used for creating the recv sockets so that they only receive from a single multicast address.
/// This is unrelated to the actual library and is just the way the test is written.
#[cfg(target_os = "linux")]
fn test_sync_packet_multicast_address() {
    // Source CID and name, set to arbitrary values as not the focus of the test.
    const CID: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

    let source_name = "SourceName".to_string() +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0\0\0\0\0\0\0" +
                        "\0\0\0\0";

    // The expected starting sequence number of sync packets from the source.
    const START_SEQ_NUM: usize = 0;

    // The number of sync packets to send per sync_address. Chosen arbitrarily to be high enough that if there was going to be a mix up in the addressing there would be a
    // sufficient chance of it being seen.
    const SYNC_PACKETS_TO_SEND: usize = 250;

    // The universes that the sync packets are sent on.
    // Chosen to contain adjacent universes and a separate universe to check that this doesn't effect the address sending.
    const SYNC_ADDRESSES: [u16; 3] = [1, 2, 63999];

    // Create a source.
    let ip: SocketAddr = SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT + 1);
    let mut source = SacnSource::with_cid_ip(&source_name.clone(), Uuid::from_bytes(CID), ip).unwrap();
    source.set_multicast_loop_v4(true).unwrap();

    // Register the synchronisation addresses.
    source.register_universes(&SYNC_ADDRESSES).unwrap();

    // Don't want universe discovery packets to be sent which might interfer with checking sync packets.
    source.set_is_sending_discovery(false);

    // Create receiver sockets.
    let mut recv_sockets: Vec<Socket> = Vec::new();

    let mut i = 0;
    for sync_addr in SYNC_ADDRESSES.iter() {
        recv_sockets.push(Socket::new(Domain::IPV4, Type::DGRAM, None).unwrap());

        // Join only the multicast address corresponding to the synchronisation address.
        let multicast_addr = universe_to_ipv4_multicast_addr(*sync_addr).unwrap();
        recv_sockets[i].bind(&multicast_addr).unwrap();
        recv_sockets[i]
            .join_multicast_v4(&multicast_addr.as_socket_ipv4().unwrap().ip(), &TEST_NETWORK_INTERFACE_IPV4[i].parse().unwrap())
            .unwrap();

        i = i + 1;
    }

    for s in START_SEQ_NUM .. START_SEQ_NUM + SYNC_PACKETS_TO_SEND {
        let expected_seq_num: u8 = (s % 256).try_into().unwrap();

        let mut i = 0;
        for sync_addr in SYNC_ADDRESSES.iter() {
            let expected_packet = generate_sync_packet_raw(CID, *sync_addr, expected_seq_num);
            source.send_sync_packet(*sync_addr, None).unwrap();

            let mut recv_buf = [0; 1024];

            // Receive only from the corresponding socket for that sync address.
            // This means that the sync address must have been sent to the correct multicast address.
            // If it was also sent to other addresses then this will be caught the next time the other sockets
            // receive as they will receive the wrong packet.
            let amt = recv_sockets[i].read(&mut recv_buf).unwrap();

            assert_eq!(&recv_buf[0..amt], &expected_packet[..]);

            i = i + 1;
        }
    }
}

#[test]
#[ignore]
fn test_register_terminate_universe() {
    let mut src = SacnSource::with_cid_ip("Test name", Uuid::new_v4(), SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT)).unwrap();

    let universe = 1;

    src.register_universe(universe).unwrap();

    assert_eq!(src.universes().unwrap(), vec!(1), "Universe not registered correctly");

    src.terminate_stream(universe, 0).unwrap();

    assert_eq!(src.universes().unwrap(), Vec::new(), "Universe not registered correctly");
}

#[test]
#[ignore]
fn test_terminate_universe_no_register() {
    let mut src = SacnSource::with_cid_ip("Test name", Uuid::new_v4(), SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT)).unwrap();

    let universe = 1;

    match src.terminate_stream(universe, 0) {
        Err(e) => {
            match e {
                SacnError::UniverseNotRegistered(_) => {
                    assert!(true, "Expected error returned");
                },
                _ => {
                    assert!(false, "Unexpected error returned");
                }
            }
        }
        _ => {
            assert!(false, "Src terminated stream that wasn't registered!");
        }
    }
}

#[test]
#[ignore]
fn test_send_empty() {
    const UNIVERSE: u16 = 1;

    let mut src = SacnSource::with_cid_ip("Test name", Uuid::new_v4(), SocketAddr::new(IpAddr::V4(TEST_NETWORK_INTERFACE_IPV4[0].parse().unwrap()), ACN_SDT_MULTICAST_PORT)).unwrap();

    src.register_universe(UNIVERSE).unwrap();

    match src.send(&[UNIVERSE], &[], None, None, None) {
        Err(e) => {
            match e {
                SacnError::DataArrayEmpty() => {
                            assert!(true, "Expected error returned");
                },
                _ => {
                    assert!(false, "Unexpected error returned");
                }
            }
        }
        _ => {
            assert!(false, "Empty data accepted to send incorrectly");
        }
    }
}