|
| 1 | +using Microsoft.Extensions.Configuration; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Xunit; |
| 7 | +using System.Linq; |
| 8 | +using System.Diagnostics; |
| 9 | + |
| 10 | +namespace YeelightAPI.UnitTests |
| 11 | +{ |
| 12 | + public class DiscoveryTests |
| 13 | + { |
| 14 | + private readonly IConfigurationRoot _config; |
| 15 | + |
| 16 | + public DiscoveryTests() |
| 17 | + { |
| 18 | + this._config = new ConfigurationBuilder() |
| 19 | + .AddJsonFile("config.json") |
| 20 | + .Build(); |
| 21 | + } |
| 22 | + |
| 23 | + #region TESTS |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public async Task Discovery_enumerate_async_should_find_devices() |
| 27 | + { |
| 28 | + int expectedDevicesCount = GetConfig<int>("discovery_devices_expected"); |
| 29 | + int count = 0; |
| 30 | + await foreach(Device device in DeviceLocator.EnumerateDevicesAsync()) |
| 31 | + { |
| 32 | + ++count; |
| 33 | + } |
| 34 | + |
| 35 | + Assert.Equal(expectedDevicesCount, count); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task Discovery_should_find_devices() |
| 40 | + { |
| 41 | + int expectedDevicesCount = GetConfig<int>("discovery_devices_expected"); |
| 42 | + var devices = (await DeviceLocator.DiscoverAsync()).ToList(); |
| 43 | + |
| 44 | + Assert.Equal(expectedDevicesCount, devices?.Count); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task Discovery_should_find_devices_with_multiple_tries() |
| 49 | + { |
| 50 | + int expectedDevicesCount = GetConfig<int>("discovery_devices_expected"); |
| 51 | + |
| 52 | + DeviceLocator.MaxRetryCount = 3; |
| 53 | + var devices = (await DeviceLocator.DiscoverAsync()).ToList(); |
| 54 | + DeviceLocator.MaxRetryCount = 1; |
| 55 | + |
| 56 | + Assert.Equal(expectedDevicesCount, devices?.Count); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public async Task Discovery_should_throw_when_using_wrong_multicast_address() |
| 61 | + { |
| 62 | + int expectedDevicesCount = GetConfig<int>("discovery_devices_expected"); |
| 63 | + string initialMulticastAddress = DeviceLocator.DefaultMulticastIPAddress; |
| 64 | + DeviceLocator.DefaultMulticastIPAddress = "foo"; |
| 65 | + |
| 66 | + _ = await Assert.ThrowsAsync<FormatException>(async () => |
| 67 | + { |
| 68 | + _ = (await DeviceLocator.DiscoverAsync()).ToList(); |
| 69 | + }); |
| 70 | + |
| 71 | + //reset to default to ensure next tests don't crash |
| 72 | + DeviceLocator.DefaultMulticastIPAddress = initialMulticastAddress; |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public async Task Discovery_should_find_devices_on_all_multicast_addresses() |
| 77 | + { |
| 78 | + int expectedDevicesCount = GetConfig<int>("discovery_devices_expected"); |
| 79 | + DeviceLocator.UseAllAvailableMulticastAddresses = true; |
| 80 | + var devices = (await DeviceLocator.DiscoverAsync()).ToList(); |
| 81 | + |
| 82 | + Assert.Equal(expectedDevicesCount, devices?.Count); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task Discovery_should_not_last_long() |
| 87 | + { |
| 88 | + Stopwatch sw = Stopwatch.StartNew(); |
| 89 | + _ = await DeviceLocator.DiscoverAsync(); |
| 90 | + sw.Stop(); |
| 91 | + |
| 92 | + Assert.InRange(sw.ElapsedMilliseconds, 0, 1500); |
| 93 | + } |
| 94 | + |
| 95 | + #endregion TESTS |
| 96 | + |
| 97 | + #region PRIVATE METHODS |
| 98 | + |
| 99 | + private T GetConfig<T>(string key) |
| 100 | + { |
| 101 | + Type t = typeof(T); |
| 102 | + var value = _config[key]; |
| 103 | + |
| 104 | + TypeConverter converter = TypeDescriptor.GetConverter(t); |
| 105 | + try |
| 106 | + //if (value != null && converter.CanConvertTo(t) && converter.CanConvertFrom(typeof(string))) |
| 107 | + { |
| 108 | + return (T)converter.ConvertFromString(value); |
| 109 | + } |
| 110 | + catch (Exception ex) |
| 111 | + { |
| 112 | + throw new Exception($"Cannot convert '{value}' (key: {key}) to {t}", ex); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + #endregion PRIVATE METHODS |
| 117 | + } |
| 118 | +} |
0 commit comments