SensorCollectorTest.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Linq;
  2. using System.Runtime.InteropServices;
  3. using LibreHardwareMonitor.Hardware;
  4. using Xunit;
  5. namespace OhmGraphite.Test
  6. {
  7. public class SensorCollectorTest
  8. {
  9. [Fact]
  10. public void SensorsAddedWhenHardwareAdded()
  11. {
  12. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  13. {
  14. return;
  15. }
  16. var computer = new Computer();
  17. using var collector = new SensorCollector(computer, MetricConfig.ParseAppSettings(new BlankConfig()));
  18. collector.Open();
  19. var unused = collector.ReadAllSensors().Count();
  20. computer.IsCpuEnabled = true;
  21. computer.IsMotherboardEnabled = true;
  22. computer.IsStorageEnabled = true;
  23. computer.IsMemoryEnabled = true;
  24. var addedCount = collector.ReadAllSensors().Count();
  25. // On CI platforms there may be no detected hardware
  26. if (addedCount <= 0)
  27. {
  28. return;
  29. }
  30. computer.IsCpuEnabled = false;
  31. computer.IsMotherboardEnabled = false;
  32. computer.IsStorageEnabled = false;
  33. computer.IsMemoryEnabled = false;
  34. var removedCount = collector.ReadAllSensors().Count();
  35. Assert.True(addedCount > removedCount, "addedCount > removedCount");
  36. }
  37. [Theory]
  38. [InlineData("/amdcpu/0", "0")]
  39. [InlineData("/nvme/1", "1")]
  40. [InlineData("/ram", "ram")]
  41. [InlineData("/nct6792d/0", "0")]
  42. [InlineData("/nic/%7BDBC40827-A257-41FA-84F5-ACBB6A148017%7D", "DBC40827-A257-41FA-84F5-ACBB6A148017")]
  43. public void ExtractHardwareInstance_ReturnsCorrectValue(string input, string expected)
  44. {
  45. var actual = SensorCollector.ExtractHardwareInstance(input);
  46. Assert.Equal(expected, actual);
  47. }
  48. }
  49. }