Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DiagnosticsTest.cs 8.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using System.Runtime.CompilerServices;
  6. using DotNetCore.CAP.Diagnostics;
  7. using DotNetCore.CAP.Internal;
  8. using Xunit;
  9. namespace DotNetCore.CAP.Test
  10. {
  11. public class DiagnosticsTest
  12. {
  13. private static readonly DiagnosticListener s_diagnosticListener =
  14. new DiagnosticListener(CapDiagnosticListenerExtensions.DiagnosticListenerName);
  15. [Fact]
  16. public void WritePublishBeforeTest()
  17. {
  18. Guid operationId = Guid.NewGuid();
  19. DiagnosticsWapper(() =>
  20. {
  21. var eventData = new BrokerPublishEventData(operationId, "", "", "", "", DateTimeOffset.UtcNow);
  22. s_diagnosticListener.WritePublishBefore(eventData);
  23. }, kvp =>
  24. {
  25. if (kvp.Key.Equals(CapDiagnosticListenerExtensions.CapBeforePublish))
  26. {
  27. Assert.NotNull(kvp.Value);
  28. Assert.IsType<BrokerPublishEventData>(kvp.Value);
  29. Assert.Equal(operationId, ((BrokerPublishEventData)kvp.Value).OperationId);
  30. }
  31. });
  32. }
  33. [Fact]
  34. public void WritePublishAfterTest()
  35. {
  36. Guid operationId = Guid.NewGuid();
  37. DiagnosticsWapper(() =>
  38. {
  39. var eventData = new BrokerPublishEndEventData(operationId, "", "", "", "", DateTimeOffset.UtcNow, TimeSpan.FromMinutes(1));
  40. s_diagnosticListener.WritePublishAfter(eventData);
  41. }, kvp =>
  42. {
  43. if (kvp.Key.Equals(CapDiagnosticListenerExtensions.CapAfterPublish))
  44. {
  45. Assert.NotNull(kvp.Value);
  46. Assert.IsType<BrokerPublishEndEventData>(kvp.Value);
  47. Assert.Equal(operationId, ((BrokerPublishEndEventData)kvp.Value).OperationId);
  48. Assert.Equal(TimeSpan.FromMinutes(1), ((BrokerPublishEndEventData)kvp.Value).Duration);
  49. }
  50. });
  51. }
  52. [Fact]
  53. public void WritePublishErrorTest()
  54. {
  55. Guid operationId = Guid.NewGuid();
  56. var ex = new Exception("WritePublishErrorTest");
  57. DiagnosticsWapper(() =>
  58. {
  59. var eventData = new BrokerPublishErrorEventData(operationId, "", "", "", "", ex, DateTimeOffset.UtcNow, default(TimeSpan));
  60. s_diagnosticListener.WritePublishError(eventData);
  61. }, kvp =>
  62. {
  63. if (kvp.Key.Equals(CapDiagnosticListenerExtensions.CapErrorPublish))
  64. {
  65. Assert.NotNull(kvp.Value);
  66. Assert.IsType<BrokerPublishErrorEventData>(kvp.Value);
  67. Assert.Equal(operationId, ((BrokerPublishErrorEventData)kvp.Value).OperationId);
  68. Assert.Equal(ex, ((BrokerPublishErrorEventData)kvp.Value).Exception);
  69. }
  70. });
  71. }
  72. [Fact]
  73. public void WriteConsumeBeforeTest()
  74. {
  75. Guid operationId = Guid.NewGuid();
  76. DiagnosticsWapper(() =>
  77. {
  78. var eventData = new BrokerConsumeEventData(operationId, "", "", "", "", DateTimeOffset.UtcNow);
  79. s_diagnosticListener.WriteConsumeBefore(eventData);
  80. }, kvp =>
  81. {
  82. if (kvp.Key.Equals(CapDiagnosticListenerExtensions.CapBeforeConsume))
  83. {
  84. Assert.NotNull(kvp.Value);
  85. Assert.IsType<BrokerConsumeEventData>(kvp.Value);
  86. Assert.Equal(operationId, ((BrokerConsumeEventData)kvp.Value).OperationId);
  87. }
  88. });
  89. }
  90. [Fact]
  91. public void WriteConsumeAfterTest()
  92. {
  93. Guid operationId = Guid.NewGuid();
  94. DiagnosticsWapper(() =>
  95. {
  96. var eventData = new BrokerConsumeEndEventData(operationId, "", "", "", "", DateTimeOffset.UtcNow, TimeSpan.FromMinutes(1));
  97. s_diagnosticListener.WriteConsumeAfter(eventData);
  98. }, kvp =>
  99. {
  100. if (kvp.Key.Equals(CapDiagnosticListenerExtensions.CapAfterConsume))
  101. {
  102. Assert.NotNull(kvp.Value);
  103. Assert.IsType<BrokerConsumeEndEventData>(kvp.Value);
  104. Assert.Equal(operationId, ((BrokerConsumeEndEventData)kvp.Value).OperationId);
  105. Assert.Equal(TimeSpan.FromMinutes(1), ((BrokerConsumeEndEventData)kvp.Value).Duration);
  106. }
  107. });
  108. }
  109. [Fact]
  110. public void WriteConsumeErrorTest()
  111. {
  112. Guid operationId = Guid.NewGuid();
  113. var ex = new Exception("WriteConsumeErrorTest");
  114. DiagnosticsWapper(() =>
  115. {
  116. var eventData = new BrokerConsumeErrorEventData(operationId, "", "", "", "", ex, DateTimeOffset.UtcNow, default(TimeSpan));
  117. s_diagnosticListener.WriteConsumeError(eventData);
  118. }, kvp =>
  119. {
  120. if (kvp.Key.Equals(CapDiagnosticListenerExtensions.CapErrorPublish))
  121. {
  122. Assert.NotNull(kvp.Value);
  123. Assert.IsType<BrokerConsumeErrorEventData>(kvp.Value);
  124. Assert.Equal(operationId, ((BrokerConsumeErrorEventData)kvp.Value).OperationId);
  125. Assert.Equal(ex, ((BrokerConsumeErrorEventData)kvp.Value).Exception);
  126. }
  127. });
  128. }
  129. [Fact]
  130. public void WriteSubscriberInvokeBeforeTest()
  131. {
  132. DiagnosticsWapper(() =>
  133. {
  134. s_diagnosticListener.WriteSubscriberInvokeBefore(FackConsumerContext());
  135. }, kvp =>
  136. {
  137. if (kvp.Key.Equals(CapDiagnosticListenerExtensions.CapBeforeSubscriberInvoke))
  138. {
  139. Assert.NotNull(kvp.Value);
  140. Assert.IsType<SubscriberInvokeEventData>(kvp.Value);
  141. }
  142. });
  143. }
  144. [Fact]
  145. public void WriteSubscriberInvokeAfterTest()
  146. {
  147. Guid operationId = Guid.NewGuid();
  148. DiagnosticsWapper(() =>
  149. {
  150. s_diagnosticListener.WriteSubscriberInvokeAfter(operationId, FackConsumerContext(), DateTimeOffset.Now, TimeSpan.FromMinutes(1));
  151. }, kvp =>
  152. {
  153. if (kvp.Key.Equals(CapDiagnosticListenerExtensions.CapAfterSubscriberInvoke))
  154. {
  155. Assert.NotNull(kvp.Value);
  156. Assert.IsType<SubscriberInvokeEndEventData>(kvp.Value);
  157. Assert.Equal(operationId, ((SubscriberInvokeEndEventData)kvp.Value).OperationId);
  158. }
  159. });
  160. }
  161. [Fact]
  162. public void WriteSubscriberInvokeErrorTest()
  163. {
  164. Guid operationId = Guid.NewGuid();
  165. var ex = new Exception("WriteConsumeErrorTest");
  166. DiagnosticsWapper(() =>
  167. {
  168. s_diagnosticListener.WriteSubscriberInvokeError(operationId, FackConsumerContext(), ex,
  169. DateTimeOffset.Now, TimeSpan.MaxValue);
  170. }, kvp =>
  171. {
  172. if (kvp.Key.Equals(CapDiagnosticListenerExtensions.CapErrorSubscriberInvoke))
  173. {
  174. Assert.NotNull(kvp.Value);
  175. Assert.IsType<SubscriberInvokeErrorEventData>(kvp.Value);
  176. Assert.Equal(operationId, ((SubscriberInvokeErrorEventData)kvp.Value).OperationId);
  177. Assert.Equal(ex, ((SubscriberInvokeErrorEventData)kvp.Value).Exception);
  178. }
  179. });
  180. }
  181. private ConsumerContext FackConsumerContext()
  182. {
  183. //Mock description
  184. var description = new ConsumerExecutorDescriptor
  185. {
  186. MethodInfo = GetType().GetMethod("WriteSubscriberInvokeAfterTest"),
  187. Attribute = new CapSubscribeAttribute("xxx"),
  188. ImplTypeInfo = GetType().GetTypeInfo()
  189. };
  190. //Mock messageContext
  191. var messageContext = new MessageContext
  192. {
  193. Name= "Name",
  194. Group= "Group",
  195. Content = "Content"
  196. };
  197. return new ConsumerContext(description, messageContext);
  198. }
  199. private void DiagnosticsWapper(Action operation, Action<KeyValuePair<string, object>> assert, [CallerMemberName]string methodName = "")
  200. {
  201. FakeDiagnosticListenerObserver diagnosticListenerObserver = new FakeDiagnosticListenerObserver(assert);
  202. diagnosticListenerObserver.Enable();
  203. using (DiagnosticListener.AllListeners.Subscribe(diagnosticListenerObserver))
  204. {
  205. Console.WriteLine(string.Format("Test: {0} Enabled Listeners", methodName));
  206. operation();
  207. }
  208. }
  209. }
  210. }