GraphiteTest.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading;
  4. using Xunit;
  5. namespace OhmGraphite.Test
  6. {
  7. public class GraphiteTest
  8. {
  9. [Fact, Trait("Category", "integration")]
  10. public async void InsertGraphiteTest()
  11. {
  12. using (var writer = new GraphiteWriter("graphite", 2003, "my-pc", tags: false))
  13. using (var client = new HttpClient())
  14. {
  15. for (int attempts = 0; ; attempts++)
  16. {
  17. try
  18. {
  19. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  20. var resp = await client.GetAsync(
  21. "http://graphite/render?format=csv&target=ohm.my-pc.intelcpu.0.temperature.cpucore.1");
  22. var content = await resp.Content.ReadAsStringAsync();
  23. Assert.Contains("ohm.my-pc.intelcpu.0.temperature.cpucore.1", content);
  24. break;
  25. }
  26. catch (Exception)
  27. {
  28. if (attempts >= 10)
  29. {
  30. throw;
  31. }
  32. Thread.Sleep(TimeSpan.FromSeconds(1));
  33. }
  34. }
  35. }
  36. }
  37. [Fact, Trait("Category", "integration")]
  38. public async void InsertTagGraphiteTest()
  39. {
  40. // Let the tag engine time to breathe
  41. Thread.Sleep(TimeSpan.FromSeconds(2));
  42. using (var writer = new GraphiteWriter("graphite", 2003, "my-pc", tags: true))
  43. using (var client = new HttpClient())
  44. {
  45. for (int attempts = 0; ; attempts++)
  46. {
  47. try
  48. {
  49. await writer.ReportMetrics(DateTime.Now, TestSensorCreator.Values());
  50. var resp = await client.GetAsync("http://graphite/render?format=csv&target=seriesByTag('sensor_type=Temperature','hardware_type=CPU')");
  51. var content = await resp.Content.ReadAsStringAsync();
  52. Assert.Contains("host=my-pc", content);
  53. Assert.Contains("app=ohm", content);
  54. Assert.Contains("sensor_type=Temperature", content);
  55. break;
  56. }
  57. catch (Exception)
  58. {
  59. if (attempts >= 10)
  60. {
  61. throw;
  62. }
  63. Thread.Sleep(TimeSpan.FromSeconds(1));
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }