ApplianceShuffleCounters { public void importCounters(String[] counterNames, String[] counterKinds, long[] counterDeltas) { final int length = counterNames.length; if (counterKinds.length != length || counterDeltas.length != length) { throw new AssertionError("array lengths do not match"); } for (int i = 0; i < length; ++i) { final CounterName name = CounterName.named(counterPrefix + counterNames[i]); final String kind = counterKinds[i]; final long delta = counterDeltas[i]; switch (kind) { case "sum": counterFactory.longSum(name).addValue(delta); break; case "max": counterFactory.longMax(name).addValue(delta); break; case "min": counterFactory.longMin(name).addValue(delta); break; default: throw new IllegalArgumentException("unsupported counter kind: " + kind); } } } ApplianceShuffleCounters(
      CounterFactory counterFactory, NameContext nameContext, String datasetId); void importCounters(String[] counterNames, String[] counterKinds, long[] counterDeltas);  }
@Test public void testSingleCounter() throws Exception { String[] names = {"sum_counter"}; String[] kinds = {"sum"}; long[] deltas = {122}; counters.importCounters(names, kinds, deltas); counterSet.extractUpdates(false, mockUpdateExtractor); verify(mockUpdateExtractor) .longSum(named("stageName-systemName-dataset-sum_counter"), false, 122L); verifyNoMoreInteractions(mockUpdateExtractor); }
@Test public void testSingleCounterMultipleDeltas() throws Exception { String[] names = {"sum_counter", "sum_counter"}; String[] kinds = {"sum", "sum"}; long[] deltas = {122, 78}; counters.importCounters(names, kinds, deltas); counterSet.extractUpdates(false, mockUpdateExtractor); verify(mockUpdateExtractor) .longSum(named("stageName-systemName-dataset-sum_counter"), false, 200L); verifyNoMoreInteractions(mockUpdateExtractor); }
@Test public void testMultipleCounters() throws Exception { String[] names = {"sum_counter", "max_counter", "min_counter"}; String[] kinds = {"sum", "max", "min"}; long[] deltas = {100, 200, 300}; counters.importCounters(names, kinds, deltas); Counter<Long, Long> sumCounter = getCounter("sum_counter"); assertNotNull(sumCounter); counterSet.extractUpdates(false, mockUpdateExtractor); verify(mockUpdateExtractor) .longSum(named("stageName-systemName-dataset-sum_counter"), false, 100L); verify(mockUpdateExtractor) .longMax(named("stageName-systemName-dataset-max_counter"), false, 200L); verify(mockUpdateExtractor) .longMin(named("stageName-systemName-dataset-min_counter"), false, 300L); verifyNoMoreInteractions(mockUpdateExtractor); }
@Test public void testSinglePreexistingCounter() throws Exception { Counter<Long, Long> sumCounter = counterSet.longSum(CounterName.named("stageName-systemName-dataset-sum_counter")); sumCounter.addValue(1000L); String[] names = {"sum_counter"}; String[] kinds = {"sum"}; long[] deltas = {122}; counters.importCounters(names, kinds, deltas); counterSet.extractUpdates(false, mockUpdateExtractor); verify(mockUpdateExtractor) .longSum(named("stageName-systemName-dataset-sum_counter"), false, 1122L); verifyNoMoreInteractions(mockUpdateExtractor); }
@Test public void testArrayLengthMismatch() throws Exception { String[] names = {"sum_counter"}; String[] kinds = {"sum", "max"}; long[] deltas = {122}; try { counters.importCounters(names, kinds, deltas); } catch (AssertionError e) { } }
@Test public void testUnsupportedKind() throws Exception { String[] names = {"sum_counter"}; String[] kinds = {"sum_int"}; long[] deltas = {122}; thrown.expect(IllegalArgumentException.class); thrown.expectMessage("sum_int"); counters.importCounters(names, kinds, deltas); }