|
| 1 | +package com.microsoft.applicationinsights.internal.quickpulse; |
| 2 | + |
| 3 | +import com.microsoft.applicationinsights.internal.quickpulse.QuickPulseDataCollector.CountAndDuration; |
| 4 | +import com.microsoft.applicationinsights.internal.quickpulse.QuickPulseDataCollector.Counters; |
| 5 | +import com.microsoft.applicationinsights.internal.quickpulse.QuickPulseDataCollector.FinalCounters; |
| 6 | +import com.microsoft.applicationinsights.telemetry.Duration; |
| 7 | +import com.microsoft.applicationinsights.telemetry.ExceptionTelemetry; |
| 8 | +import com.microsoft.applicationinsights.telemetry.RemoteDependencyTelemetry; |
| 9 | +import com.microsoft.applicationinsights.telemetry.RequestTelemetry; |
| 10 | +import org.junit.*; |
| 11 | + |
| 12 | +import java.util.Date; |
| 13 | + |
| 14 | +import static org.junit.Assert.*; |
| 15 | + |
| 16 | +public class QuickPulseDataCollectorTests { |
| 17 | + |
| 18 | + private static final String FAKE_INSTRUMENTATION_KEY = "fake-instrumentation-key"; |
| 19 | + |
| 20 | + @Before |
| 21 | + public void setup() { |
| 22 | + QuickPulseDataCollector.INSTANCE.disable(); |
| 23 | + } |
| 24 | + |
| 25 | + @After |
| 26 | + public void tearDown() { |
| 27 | + QuickPulseDataCollector.INSTANCE.disable(); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void initialStateIsDisabled() { |
| 32 | + assertNull(QuickPulseDataCollector.INSTANCE.peek()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void emptyCountsAndDurationsAfterEnable() { |
| 37 | + QuickPulseDataCollector.INSTANCE.enable(FAKE_INSTRUMENTATION_KEY); |
| 38 | + final FinalCounters counters = QuickPulseDataCollector.INSTANCE.peek(); |
| 39 | + assertCountersReset(counters); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void nullCountersAfterDisable() { |
| 44 | + QuickPulseDataCollector.INSTANCE.enable(FAKE_INSTRUMENTATION_KEY); |
| 45 | + QuickPulseDataCollector.INSTANCE.disable(); |
| 46 | + assertNull(QuickPulseDataCollector.INSTANCE.peek()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void requestTelemetryIsCounted_DurationIsSum() { |
| 51 | + QuickPulseDataCollector.INSTANCE.enable(FAKE_INSTRUMENTATION_KEY); |
| 52 | + |
| 53 | + // add a success and peek |
| 54 | + final long duration = 112233L; |
| 55 | + RequestTelemetry rt = new RequestTelemetry("request-test", new Date(), duration, "200", true); |
| 56 | + rt.getContext().setInstrumentationKey(FAKE_INSTRUMENTATION_KEY); |
| 57 | + QuickPulseDataCollector.INSTANCE.add(rt); |
| 58 | + FinalCounters counters = QuickPulseDataCollector.INSTANCE.peek(); |
| 59 | + assertEquals(1, counters.requests); |
| 60 | + assertEquals(0, counters.unsuccessfulRequests); |
| 61 | + assertEquals((double)duration, counters.requestsDuration, Math.ulp((double)duration)); |
| 62 | + |
| 63 | + // add another success and peek |
| 64 | + final long duration2 = 65421L; |
| 65 | + rt = new RequestTelemetry("request-test-2", new Date(), duration2, "200", true); |
| 66 | + rt.getContext().setInstrumentationKey(FAKE_INSTRUMENTATION_KEY); |
| 67 | + QuickPulseDataCollector.INSTANCE.add(rt); |
| 68 | + counters = QuickPulseDataCollector.INSTANCE.peek(); |
| 69 | + double total = duration + duration2; |
| 70 | + assertEquals(2, counters.requests); |
| 71 | + assertEquals(0, counters.unsuccessfulRequests); |
| 72 | + assertEquals(total, counters.requestsDuration, Math.ulp(total)); |
| 73 | + |
| 74 | + // add a failure and get/reset |
| 75 | + final long duration3 = 9988L; |
| 76 | + rt = new RequestTelemetry("request-test-3", new Date(), duration3, "400", false); |
| 77 | + rt.getContext().setInstrumentationKey(FAKE_INSTRUMENTATION_KEY); |
| 78 | + QuickPulseDataCollector.INSTANCE.add(rt); |
| 79 | + counters = QuickPulseDataCollector.INSTANCE.getAndRestart(); |
| 80 | + total += duration3; |
| 81 | + assertEquals(3, counters.requests); |
| 82 | + assertEquals(1, counters.unsuccessfulRequests); |
| 83 | + assertEquals(total, counters.requestsDuration, Math.ulp(total)); |
| 84 | + |
| 85 | + assertCountersReset(QuickPulseDataCollector.INSTANCE.peek()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void dependencyTelemetryIsCounted_DurationIsSum() { |
| 90 | + QuickPulseDataCollector.INSTANCE.enable(FAKE_INSTRUMENTATION_KEY); |
| 91 | + |
| 92 | + // add a success and peek. |
| 93 | + final long duration = 112233L; |
| 94 | + RemoteDependencyTelemetry rdt = new RemoteDependencyTelemetry("dep-test", "dep-test-cmd", new Duration(duration), true); |
| 95 | + rdt.getContext().setInstrumentationKey(FAKE_INSTRUMENTATION_KEY); |
| 96 | + QuickPulseDataCollector.INSTANCE.add(rdt); |
| 97 | + FinalCounters counters = QuickPulseDataCollector.INSTANCE.peek(); |
| 98 | + assertEquals(1, counters.rdds); |
| 99 | + assertEquals(0, counters.unsuccessfulRdds); |
| 100 | + assertEquals((double)duration, counters.rddsDuration, Math.ulp((double)duration)); |
| 101 | + |
| 102 | + // add another success and peek. |
| 103 | + final long duration2 = 334455L; |
| 104 | + rdt = new RemoteDependencyTelemetry("dep-test-2", "dep-test-cmd-2", new Duration(duration2), true); |
| 105 | + rdt.getContext().setInstrumentationKey(FAKE_INSTRUMENTATION_KEY); |
| 106 | + QuickPulseDataCollector.INSTANCE.add(rdt); |
| 107 | + counters = QuickPulseDataCollector.INSTANCE.peek(); |
| 108 | + assertEquals(2, counters.rdds); |
| 109 | + assertEquals(0, counters.unsuccessfulRdds); |
| 110 | + double total = duration + duration2; |
| 111 | + assertEquals(total, counters.rddsDuration, Math.ulp(total)); |
| 112 | + |
| 113 | + // add a failure and get/reset. |
| 114 | + final long duration3 = 123456L; |
| 115 | + rdt = new RemoteDependencyTelemetry("dep-test-3", "dep-test-cmd-3", new Duration(duration3), false); |
| 116 | + rdt.getContext().setInstrumentationKey(FAKE_INSTRUMENTATION_KEY); |
| 117 | + QuickPulseDataCollector.INSTANCE.add(rdt); |
| 118 | + counters = QuickPulseDataCollector.INSTANCE.getAndRestart(); |
| 119 | + assertEquals(3, counters.rdds); |
| 120 | + assertEquals(1, counters.unsuccessfulRdds); |
| 121 | + total += duration3; |
| 122 | + assertEquals(total, counters.rddsDuration, Math.ulp(total)); |
| 123 | + |
| 124 | + assertCountersReset(QuickPulseDataCollector.INSTANCE.peek()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void exceptionTelemetryIsCounted() { |
| 129 | + QuickPulseDataCollector.INSTANCE.enable(FAKE_INSTRUMENTATION_KEY); |
| 130 | + |
| 131 | + ExceptionTelemetry et = new ExceptionTelemetry(new Exception()); |
| 132 | + et.getContext().setInstrumentationKey(FAKE_INSTRUMENTATION_KEY); |
| 133 | + QuickPulseDataCollector.INSTANCE.add(et); |
| 134 | + FinalCounters counters = QuickPulseDataCollector.INSTANCE.peek(); |
| 135 | + assertEquals(1, counters.exceptions, Math.ulp(1.0)); |
| 136 | + |
| 137 | + et = new ExceptionTelemetry(new Exception()); |
| 138 | + et.getContext().setInstrumentationKey(FAKE_INSTRUMENTATION_KEY); |
| 139 | + QuickPulseDataCollector.INSTANCE.add(et); |
| 140 | + counters = QuickPulseDataCollector.INSTANCE.getAndRestart(); |
| 141 | + assertEquals(2, counters.exceptions, Math.ulp(2.0)); |
| 142 | + |
| 143 | + assertCountersReset(QuickPulseDataCollector.INSTANCE.peek()); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void encodeDecodeIsIdentity() { |
| 148 | + final long count = 456L; |
| 149 | + final long duration = 112233L; |
| 150 | + final long encoded = Counters.encodeCountAndDuration(count, duration); |
| 151 | + final CountAndDuration inputs = Counters.decodeCountAndDuration(encoded); |
| 152 | + assertEquals(count, inputs.count); |
| 153 | + assertEquals(duration, inputs.duration); |
| 154 | + } |
| 155 | + |
| 156 | + private void assertCountersReset(FinalCounters counters) { |
| 157 | + assertNotNull(counters); |
| 158 | + |
| 159 | + assertEquals(0, counters.rdds); |
| 160 | + assertEquals(0.0, counters.rddsDuration, Math.ulp(0.0)); |
| 161 | + assertEquals(0, counters.unsuccessfulRdds); |
| 162 | + |
| 163 | + assertEquals(0, counters.requests); |
| 164 | + assertEquals(0.0, counters.requestsDuration, Math.ulp(0.0)); |
| 165 | + assertEquals(0, counters.unsuccessfulRequests); |
| 166 | + |
| 167 | + // FIXME exceptions is stored as a double but counted as an int; is that correct? |
| 168 | + assertEquals(0, (int) counters.exceptions); |
| 169 | + } |
| 170 | +} |
0 commit comments