PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/stat/src/test/java/com/google/sitebricks/stat/StatsIntegrationTest.java

https://github.com/juven/sitebricks
Java | 234 lines | 180 code | 43 blank | 11 comment | 3 complexity | 28abcc4aec05ec3b1b39766de98a0674 MD5 | raw file
  1. package com.google.sitebricks.stat;
  2. import com.google.common.collect.ImmutableMap;
  3. import com.google.inject.AbstractModule;
  4. import com.google.inject.Guice;
  5. import com.google.inject.Injector;
  6. import com.google.sitebricks.stat.testservices.ChildDummyService;
  7. import com.google.sitebricks.stat.testservices.DummyService;
  8. import com.google.sitebricks.stat.testservices.StatExposerTestingService;
  9. import com.google.sitebricks.stat.testservices.StaticDummyService;
  10. import org.testng.annotations.BeforeMethod;
  11. import org.testng.annotations.Test;
  12. import java.util.List;
  13. import java.util.Map.Entry;
  14. import java.util.concurrent.atomic.AtomicInteger;
  15. import static org.testng.Assert.assertEquals;
  16. /**
  17. * @author dhanji@gmail.com (Dhanji R. Prasanna)
  18. */
  19. public class StatsIntegrationTest {
  20. Injector injector;
  21. @BeforeMethod
  22. public final void before() {
  23. injector = Guice.createInjector(new AbstractModule() {
  24. @Override
  25. protected void configure() {
  26. install(new StatModule("/stat"));
  27. bind(DummyService.class);
  28. bind(ChildDummyService.class);
  29. bind(StatExposerTestingService.class);
  30. }
  31. });
  32. }
  33. @Test
  34. public final void testPublishingStatsInDummyService() {
  35. DummyService service = injector.getInstance(DummyService.class);
  36. service.call();
  37. service.call();
  38. service.call();
  39. Stats stats = injector.getInstance(Stats.class);
  40. ImmutableMap<StatDescriptor, Object> snapshot = stats.snapshot();
  41. assertEquals(snapshot.size(), 2);
  42. // Here we check the value of the field, NUMBER_OF_CALLS
  43. StatDescriptor numberOfCallsDescriptor =
  44. getByName(DummyService.NUMBER_OF_CALLS, snapshot);
  45. String numberOfCallsValue = (String) snapshot.get(numberOfCallsDescriptor);
  46. assertEquals(String.valueOf(service.getCalls()), numberOfCallsValue);
  47. // Here we check the value of the method, CALL_LATENCY_NS
  48. StatDescriptor callLatencyNsDescriptor =
  49. getByName(DummyService.CALL_LATENCY_NS, snapshot);
  50. String callLatencyValue = (String) snapshot.get(callLatencyNsDescriptor);
  51. assertEquals(service.getCallLatencyMs().toString(), callLatencyValue);
  52. }
  53. /**
  54. * This test illustrates how stats are published from parent classes when
  55. * a child class is published.
  56. */
  57. @Test public void testPublishingStatsInChildService() {
  58. ChildDummyService service = injector.getInstance(ChildDummyService.class);
  59. service.call();
  60. service.call();
  61. Stats stats = injector.getInstance(Stats.class);
  62. ImmutableMap<StatDescriptor, Object> snapshot = stats.snapshot();
  63. // We expect 1 stat from the child class and 2 from its parent
  64. assertEquals(snapshot.size(), 3);
  65. StatDescriptor numberOfChildCallsDescriptor =
  66. getByName(ChildDummyService.NUMBER_OF_CHILD_CALLS, snapshot);
  67. String numberOfChildCallsValue =
  68. (String) snapshot.get(numberOfChildCallsDescriptor);
  69. assertEquals(
  70. String.valueOf(service.getChildCalls()), numberOfChildCallsValue);
  71. // Below we check the value of the stats on the parent class
  72. StatDescriptor numberOfCallsDescriptor =
  73. getByName(DummyService.NUMBER_OF_CALLS, snapshot);
  74. String numberOfCallsValue = (String) snapshot.get(numberOfCallsDescriptor);
  75. assertEquals(String.valueOf(service.getCalls()), numberOfCallsValue);
  76. StatDescriptor callLatencyNsDescriptor =
  77. getByName(DummyService.CALL_LATENCY_NS, snapshot);
  78. String callLatencyValue = (String) snapshot.get(callLatencyNsDescriptor);
  79. assertEquals(service.getCallLatencyMs().toString(), callLatencyValue);
  80. }
  81. @Test
  82. public void testPublishingStatsAsStaticMember() {
  83. StaticDummyService.reset();
  84. StaticDummyService service1 = injector.getInstance(StaticDummyService.class);
  85. StaticDummyService service2 = injector.getInstance(StaticDummyService.class);
  86. service1.call();
  87. service2.call();
  88. Stats stats = injector.getInstance(Stats.class);
  89. ImmutableMap<StatDescriptor, Object> snapshot = stats.snapshot();
  90. assertEquals(snapshot.size(), 1);
  91. StatDescriptor staticCallsDescriptor =
  92. getByName(StaticDummyService.STATIC_CALLS, snapshot);
  93. String numberOfStaticCallsValue =
  94. (String) snapshot.get(staticCallsDescriptor);
  95. assertEquals(
  96. String.valueOf(StaticDummyService.getNumberOfStaticCalls()),
  97. numberOfStaticCallsValue);
  98. }
  99. @Test
  100. public final void testPublishingDuplicatedStat() {
  101. DummyService service1 = injector.getInstance(DummyService.class);
  102. DummyService service2 = injector.getInstance(DummyService.class);
  103. service1.call();
  104. service2.call();
  105. Stats stats = injector.getInstance(Stats.class);
  106. ImmutableMap<StatDescriptor, Object> snapshot = stats.snapshot();
  107. assertEquals(2, snapshot.size(), snapshot.toString());
  108. for (Entry<StatDescriptor, Object> entry : snapshot.entrySet()) {
  109. assertEquals(Stats.DUPLICATED_STAT_VALUE, entry.getValue(),
  110. "Unexpected value for " + entry.getKey());
  111. }
  112. }
  113. @SuppressWarnings("unchecked")
  114. @Test
  115. public final void testPublishingUsingDifferentExposers() {
  116. StatExposerTestingService service =
  117. injector.getInstance(StatExposerTestingService.class);
  118. service.call();
  119. service.call();
  120. Stats stats = injector.getInstance(Stats.class);
  121. ImmutableMap<StatDescriptor, Object> snapshot = stats.snapshot();
  122. assertEquals(snapshot.size(), 8, "Snapshot has unexpected size: " + snapshot);
  123. AtomicInteger atomicIntegerCallCount = service.getCallCount();
  124. String stringCallCount = String.valueOf(atomicIntegerCallCount);
  125. StatDescriptor callsDefaultExposerDescriptor = getByName(
  126. StatExposerTestingService.CALLS_WITH_DEFAULT_EXPOSER, snapshot);
  127. String callsDefaultExposerValue =
  128. (String) snapshot.get(callsDefaultExposerDescriptor);
  129. assertEquals(stringCallCount, callsDefaultExposerValue);
  130. StatDescriptor callsIdentityExposerDescriptor = getByName(
  131. StatExposerTestingService.CALLS_WITH_IDENTITY_EXPOSER, snapshot);
  132. AtomicInteger callsIdentityExposerValue =
  133. (AtomicInteger) snapshot.get(callsIdentityExposerDescriptor);
  134. assertEquals(atomicIntegerCallCount.get(), callsIdentityExposerValue.get());
  135. StatDescriptor callsInferenceExposerDescriptor = getByName(
  136. StatExposerTestingService.CALLS_WITH_INFERENCE_EXPOSER, snapshot);
  137. String callsInferenceExposerValue =
  138. (String) snapshot.get(callsInferenceExposerDescriptor);
  139. assertEquals(stringCallCount, callsInferenceExposerValue);
  140. StatDescriptor callsToStringExposerDescriptor = getByName(
  141. StatExposerTestingService.CALLS_WITH_TO_STRING_EXPOSER, snapshot);
  142. String callsToStringExposerValue =
  143. (String) snapshot.get(callsToStringExposerDescriptor);
  144. assertEquals(stringCallCount, callsToStringExposerValue);
  145. List<Integer> callsList = service.getCallsList();
  146. String callsListAsString = String.valueOf(callsList);
  147. StatDescriptor listDefaultExposerDescriptor = getByName(
  148. StatExposerTestingService.LIST_WITH_DEFAULT_EXPOSER, snapshot);
  149. List<Integer> listDefaultExposerValue =
  150. (List<Integer>) snapshot.get(listDefaultExposerDescriptor);
  151. assertEquals(callsList, listDefaultExposerValue);
  152. StatDescriptor listIdentityExposerDescriptor = getByName(
  153. StatExposerTestingService.LIST_WITH_IDENTITY_EXPOSER, snapshot);
  154. List<Integer> listIdentityExposerValue =
  155. (List<Integer>) snapshot.get(listIdentityExposerDescriptor);
  156. assertEquals(callsList, listIdentityExposerValue);
  157. StatDescriptor listInferenceExposerDescriptor = getByName(
  158. StatExposerTestingService.LIST_WITH_INFERENCE_EXPOSER, snapshot);
  159. List<Integer> listInferenceExposerValue =
  160. (List<Integer>) snapshot.get(listInferenceExposerDescriptor);
  161. assertEquals(callsList, listInferenceExposerValue);
  162. StatDescriptor listToStringExposerDescriptor = getByName(
  163. StatExposerTestingService.LIST_WITH_TO_STRING_EXPOSER, snapshot);
  164. String listToStringExposerValue =
  165. (String) snapshot.get(listToStringExposerDescriptor);
  166. assertEquals(callsListAsString, listToStringExposerValue);
  167. }
  168. @Test
  169. public final void testPublishingStandaloneStat() {
  170. StatRegistrar statRegistrar = injector.getInstance(StatRegistrar.class);
  171. AtomicInteger statValue = new AtomicInteger(0);
  172. statRegistrar.registerSingleStat("single-stat", "", statValue);
  173. Stats stats = injector.getInstance(Stats.class);
  174. ImmutableMap<StatDescriptor, Object> snapshot = stats.snapshot();
  175. StatDescriptor numberOfChildCallsDescriptor =
  176. getByName("single-stat", snapshot);
  177. String snapshottedValue =
  178. (String) snapshot.get(numberOfChildCallsDescriptor);
  179. assertEquals(String.valueOf(statValue.intValue()), snapshottedValue);
  180. }
  181. StatDescriptor getByName(
  182. String name, ImmutableMap<StatDescriptor, Object> snapshot) {
  183. for (StatDescriptor key : snapshot.keySet()) {
  184. if (key.getName().equals(name)) {
  185. return key;
  186. }
  187. }
  188. throw new RuntimeException(
  189. "No entry found for " + name + " within " + snapshot);
  190. }
  191. }