PageRenderTime 50ms CodeModel.GetById 31ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

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

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