http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/objectsize/ObjectSizeCalculatorTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/objectsize/ObjectSizeCalculatorTest.java b/commons/src/test/java/org/apache/aurora/common/objectsize/ObjectSizeCalculatorTest.java deleted file mode 100644 index b912844..0000000 --- a/commons/src/test/java/org/apache/aurora/common/objectsize/ObjectSizeCalculatorTest.java +++ /dev/null @@ -1,211 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.objectsize; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class ObjectSizeCalculatorTest { - - private int A; - private int O; - private int R; - private int S; - private ObjectSizeCalculator objectSizeCalculator; - - @Before - public void setUp() { - ObjectSizeCalculator.MemoryLayoutSpecification memoryLayoutSpecification = - new ObjectSizeCalculator.MemoryLayoutSpecification() { - @Override public int getArrayHeaderSize() { - return 16; - } - @Override public int getObjectHeaderSize() { - return 12; - } - @Override public int getObjectPadding() { - return 8; - } - @Override public int getReferenceSize() { - return 4; - } - @Override public int getSuperclassFieldPadding() { - return 4; - } - }; - - A = memoryLayoutSpecification.getArrayHeaderSize(); - O = memoryLayoutSpecification.getObjectHeaderSize(); - R = memoryLayoutSpecification.getReferenceSize(); - S = memoryLayoutSpecification.getSuperclassFieldPadding(); - - objectSizeCalculator = new ObjectSizeCalculator(memoryLayoutSpecification); - } - - @Test - public void testRounding() { - assertEquals(0, roundTo(0, 8)); - assertEquals(8, roundTo(1, 8)); - assertEquals(8, roundTo(7, 8)); - assertEquals(8, roundTo(8, 8)); - assertEquals(16, roundTo(9, 8)); - assertEquals(16, roundTo(15, 8)); - assertEquals(16, roundTo(16, 8)); - assertEquals(24, roundTo(17, 8)); - } - - @Test - public void testObjectSize() { - assertSizeIs(O, new Object()); - } - - static class ObjectWithFields { - int length; - int offset; - int hashcode; - char[] data = {}; - } - - @Test - public void testObjectWithFields() { - assertSizeIs(O + 3 * 4 + R + A, new ObjectWithFields()); - } - - public static class Class1 { - private boolean b1; - } - - @Test - public void testOneBooleanSize() { - assertSizeIs(O + 1, new Class1()); - } - - public static class Class2 extends Class1 { - private int i1; - } - - @Test - public void testSimpleSubclassSize() { - assertSizeIs(O + roundTo(1, S) + 4, new Class2()); - } - - @Test - public void testZeroLengthArray() { - assertSizeIs(A, new byte[0]); - assertSizeIs(A, new int[0]); - assertSizeIs(A, new long[0]); - assertSizeIs(A, new Object[0]); - } - - @Test - public void testByteArrays() { - assertSizeIs(A + 1, new byte[1]); - assertSizeIs(A + 8, new byte[8]); - assertSizeIs(A + 9, new byte[9]); - } - - @Test - public void testCharArrays() { - assertSizeIs(A + 2 * 1, new char[1]); - assertSizeIs(A + 2 * 4, new char[4]); - assertSizeIs(A + 2 * 5, new char[5]); - } - - @Test - public void testIntArrays() { - assertSizeIs(A + 4 * 1, new int[1]); - assertSizeIs(A + 4 * 2, new int[2]); - assertSizeIs(A + 4 * 3, new int[3]); - } - - @Test - public void testLongArrays() { - assertSizeIs(A + 8 * 1, new long[1]); - assertSizeIs(A + 8 * 2, new long[2]); - assertSizeIs(A + 8 * 3, new long[3]); - } - - @Test - public void testObjectArrays() { - assertSizeIs(A + R * 1, new Object[1]); - assertSizeIs(A + R * 2, new Object[2]); - assertSizeIs(A + R * 3, new Object[3]); - assertSizeIs(A + R * 1, new String[1]); - assertSizeIs(A + R * 2, new String[2]); - assertSizeIs(A + R * 3, new String[3]); - } - - public static class Circular { - Circular c; - } - - @Test - public void testCircular() { - Circular c1 = new Circular(); - long size = objectSizeCalculator.calculateObjectSize(c1); - c1.c = c1; - assertEquals(size, objectSizeCalculator.calculateObjectSize(c1)); - } - - static class ComplexObject<T> { - static class Node<T> { - final T value; - Node<T> previous; - Node<T> next; - - Node(T value) { - this.value = value; - } - } - - private Node<T> first; - private Node<T> last; - - void add(T item) { - Node<T> node = new Node<T>(item); - if (first == null) { - first = node; - } else { - last.next = node; - node.previous = last; - } - last = node; - } - } - - @Test - public void testComplexObject() { - ComplexObject<Object> l = new ComplexObject<Object>(); - l.add(new Object()); - l.add(new Object()); - l.add(new Object()); - - long expectedSize = 0; - expectedSize += roundTo(O + 2 * R, 8); // The complex object itself plus first and last refs. - expectedSize += roundTo(O + 3 * R, 8) * 3; // 3 Nodes - each with 3 object references. - expectedSize += roundTo(O, 8) * 3; // 3 vanilla objects contained in the node values. - - assertSizeIs(expectedSize, l); - } - - private void assertSizeIs(long size, Object o) { - assertEquals(roundTo(size, 8), objectSizeCalculator.calculateObjectSize(o)); - } - - private static long roundTo(long x, int multiple) { - return ObjectSizeCalculator.roundTo(x, multiple); - } -}
http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/ApproximateHistogramTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/ApproximateHistogramTest.java b/commons/src/test/java/org/apache/aurora/common/stats/ApproximateHistogramTest.java deleted file mode 100644 index 62b8846..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/ApproximateHistogramTest.java +++ /dev/null @@ -1,321 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import java.util.Arrays; -import java.util.List; - -import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; -import com.google.common.collect.ImmutableList; - -import org.junit.Test; - -import org.apache.aurora.common.objectsize.ObjectSizeCalculator; -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Data; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class ApproximateHistogramTest { - final int b = 10; - final int h = 3; - - @Test - public void testCollapse() { - ApproximateHistogram hist = new ApproximateHistogram(); - long[] buf1 = {2,5,7}; - long[] buf2 = {3,8,9}; - long[] expected = {3,7,9}; - long[] result = new long[3]; - // [2,5,7] weight 2 and [3,8,9] weight 3 - // weight x array + concat = [2,2,5,5,7,7,3,3,3,8,8,8,9,9,9] - // sort = [2,2,3,3,3,5,5,7,7,8,8,8,9,9,9] - // select every nth elems = [3,7,9] (n = sum weight / 2, ie. 5/3 = 2) - // [2,2,3,3,3,5,5,7,7,8,8,8,9,9,9] - // . . ^ . . . . ^ . . . . ^ . . - // [-------] [-------] [-------] we make 3 packets of 5 elements and take the middle - - ApproximateHistogram.collapse(buf1, 2, buf2, 3, result); - assertArrayEquals(result, expected); - - long[] buf3 = {2, 5, 7, 9}; - long[] buf4 = {3, 8, 9, 12}; - long[] expected2 = {3, 7, 9, 12}; - long[] result2 = new long[4]; - ApproximateHistogram.collapse(buf3, 2, buf4, 2, result2); - assertArrayEquals(expected2, result2); - } - - @Test - public void testRecCollapse() { - long[] empty = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - long[] full = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - ApproximateHistogram hist = new ApproximateHistogram(b, h); - assertArrayEquals(empty, hist.buffer[0]); - assertArrayEquals(empty, hist.buffer[1]); - - initializeValues(hist, b, Suppliers.ofInstance(1L)); - assertArrayEquals(full, hist.buffer[0]); - assertArrayEquals(empty, hist.buffer[1]); - - initializeValues(hist, b, Suppliers.ofInstance(1L)); - assertArrayEquals(full, hist.buffer[0]); - assertArrayEquals(full, hist.buffer[1]); - - hist.add(1); - assertEquals(2, hist.currentTop); - // Buffers are not cleared so we can't check that! - assertArrayEquals(full, hist.buffer[2]); - - initializeValues(hist, 2 * b, Suppliers.ofInstance(1L)); - assertEquals(3, hist.currentTop); - assertArrayEquals(full, hist.buffer[3]); - } - - @Test - public void testReachingMaxDepth() { - ApproximateHistogram hist = new ApproximateHistogram(b, h); - - initializeValues(hist, 8 * b, Suppliers.ofInstance(1L)); - assertEquals(3, hist.currentTop); - - hist.add(1); - assertEquals(3, hist.currentTop); - } - - @Test - public void testMem() { - for (int b = 10; b < 100; b += 10) { - for (int h = 4; h < 16; h += 4) { - ApproximateHistogram hist = new ApproximateHistogram(b, h); - long actualSize = ObjectSizeCalculator.getObjectSize(hist); - long estimatedSize = ApproximateHistogram.memoryUsage(b, h); - assertTrue("Consume less memory than the constraint", actualSize < estimatedSize); - } - } - } - - @Test - public void testMemConstraint() { - ImmutableList.Builder<Amount<Long, Data>> builder = ImmutableList.builder(); - builder.add(Amount.of(1L, Data.KB)); - builder.add(Amount.of(4L, Data.KB)); - builder.add(Amount.of(8L, Data.KB)); - builder.add(Amount.of(16L, Data.KB)); - builder.add(Amount.of(32L, Data.KB)); - builder.add(Amount.of(64L, Data.KB)); - builder.add(Amount.of(256L, Data.KB)); - builder.add(Amount.of(1L, Data.MB)); - builder.add(Amount.of(16L, Data.MB)); - builder.add(Amount.of(32L, Data.MB)); - List<Amount<Long, Data>> sizes = builder.build(); - - for (Amount<Long, Data> maxSize: sizes) { - ApproximateHistogram hist = new ApproximateHistogram(maxSize); - for (long i = 0; i < 1000 * 1000; i++) { hist.add(i); } - long size = ObjectSizeCalculator.getObjectSize(hist); - assertTrue(size < maxSize.as(Data.BYTES)); - } - } - - @Test - public void testLowMemoryPrecision() { - double e = ApproximateHistogram.DEFAULT_PRECISION.getEpsilon(); - int n = ApproximateHistogram.DEFAULT_PRECISION.getN(); - int defaultDepth = ApproximateHistogram.computeDepth(e, n); - int defaultBufferSize = ApproximateHistogram.computeBufferSize(defaultDepth, n); - - ApproximateHistogram hist = new ApproximateHistogram(Amount.of(1L, Data.KB)); - int depth = hist.buffer.length - 1; - int bufferSize = hist.buffer[0].length; - - assertTrue(depth > defaultDepth); - assertTrue(bufferSize < defaultBufferSize); - } - - @Test - public void testHighMemoryPrecision() { - double e = ApproximateHistogram.DEFAULT_PRECISION.getEpsilon(); - int n = ApproximateHistogram.DEFAULT_PRECISION.getN(); - int defaultDepth = ApproximateHistogram.computeDepth(e, n); - int defaultBufferSize = ApproximateHistogram.computeBufferSize(defaultDepth, n); - - ApproximateHistogram hist = new ApproximateHistogram(Amount.of(1L, Data.MB)); - int depth = hist.buffer.length - 1; - int bufferSize = hist.buffer[0].length; - - assertTrue(depth < defaultDepth); - assertTrue(bufferSize > defaultBufferSize); - } - - private void initIndexArray(ApproximateHistogram hist, int b) { - Arrays.fill(hist.indices, b - 1); - int buf0Size = Math.min(b, hist.leafCount); - int buf1Size = Math.max(0, hist.leafCount - buf0Size); - hist.indices[0] = buf0Size - 1; - hist.indices[1] = buf1Size - 1; - } - - private long getBiggest(ApproximateHistogram hist) { - int j = hist.biggest(hist.indices); - int idx = hist.indices[j]; - hist.indices[j] -= 1; - return hist.buffer[j][idx]; - } - - @Test - public void testBiggestIndexFinder() { - ApproximateHistogram hist = new ApproximateHistogram(b, h); - int n = 3; - for (int i=1; i <= n; i++) { - hist.add(i); - } - - initIndexArray(hist, b); - for (int i=1; i <= n; i++) { - assertEquals(n - i + 1, getBiggest(hist)); - } - - n = 2 * b; - for (int i=4; i <= n; i++) { - hist.add(i); - } - - initIndexArray(hist, b); - for (int i=1; i <= n; i++) { - assertEquals(n - i + 1, getBiggest(hist)); - } - - hist.add(2*b + 1); - n += 1; - - initIndexArray(hist, b); - assertEquals(n, getBiggest(hist)); - - for (int i=2; i <= n; i += 2) { - assertEquals(n - i + 1, getBiggest(hist)); - } - } - - @Test - public void testIsBufferEmpty() { - ApproximateHistogram hist = new ApproximateHistogram(b, h); - - for (int i=0; i < 3*b; i++) { - hist.add(i); - } - assertEquals(false, hist.isBufferEmpty(2)); - assertEquals(true, hist.isBufferEmpty(3)); - - for (int i=0; i < 2*b; i++) { - hist.add(i); - } - assertEquals(true, hist.isBufferEmpty(2)); - assertEquals(false, hist.isBufferEmpty(3)); - } - - @Test - public void testHistogramWithNegative() { - ApproximateHistogram hist = new ApproximateHistogram(); - hist.add(-1L); - assertEquals(-1L, hist.getQuantile(0.0)); - assertEquals(-1L, hist.getQuantile(0.5)); - assertEquals(-1L, hist.getQuantile(1.0)); - } - - @Test - public void testHistogramWithEdgeCases() { - ApproximateHistogram hist = new ApproximateHistogram(); - hist.add(Long.MIN_VALUE); - assertEquals(Long.MIN_VALUE, hist.getQuantile(0.0)); - assertEquals(Long.MIN_VALUE, hist.getQuantile(1.0)); - - hist.add(Long.MAX_VALUE); - assertEquals(Long.MIN_VALUE, hist.getQuantile(0.0)); - assertEquals(Long.MAX_VALUE, hist.getQuantile(1.0)); - } - - @Test - public void testQueryZerothQuantile() { - // Tests that querying the zeroth quantile does not throw an exception - ApproximateHistogram hist = new ApproximateHistogram(b, h); - initializeValues(hist, 10, Suppliers.ofInstance(1L)); - assertEquals(1L, hist.getQuantile(0.0)); - } - - @Test - public void testSmallDataCase() { - // Tests that querying the zeroth quantile does not throw an exception - ApproximateHistogram hist = new ApproximateHistogram(b, h); - initializeValues(hist, 1, Suppliers.ofInstance(1L)); - assertEquals(1L, hist.getQuantile(0.5)); - } - - @Test - public void testSimpleCase() { - ApproximateHistogram hist = new ApproximateHistogram(); - int n = 10; - initializeValues(hist, n, monotonic()); - for (int i = 1; i <= n; i++) { - double q = i / 10.0; - assertEquals(i, hist.getQuantile(q), 1.0); - } - } - - @Test - public void testGetQuantiles() { - ApproximateHistogram hist = new ApproximateHistogram(); - int n = 10; - initializeValues(hist, n, monotonic()); - double[] quantiles = new double[n]; - for (int i = 0; i < n; i++) { - quantiles[i] = (i + 1) / 10.0; - } - long[] results = hist.getQuantiles(quantiles); - for (int i = 0; i < n; i++) { - long res = results[i]; - double q = quantiles[i]; - assertEquals(hist.getQuantile(q), res); - } - } - - @Test - public void testYetAnotherGetQuantiles() { - // this test originates from issue CSL-586 - ApproximateHistogram hist = new ApproximateHistogram(); - hist.add(0); - hist.add(4); - hist.add(9); - hist.add(8); - double[] quantiles = new double[]{0.5, 0.9, 0.99}; - long[] expected = new long[]{8,9,9}; - assertArrayEquals(hist.getQuantiles(quantiles), expected); - } - - private static void initializeValues(ApproximateHistogram hist, int n, Supplier<Long> what) { - for (int i=0; i<n ; i++) { - hist.add(what.get()); - } - } - - private static Supplier<Long> monotonic() { - return new Supplier<Long>() { - long i = 0; - @Override public Long get() { return ++i; } - }; - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/ElapsedTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/ElapsedTest.java b/commons/src/test/java/org/apache/aurora/common/stats/ElapsedTest.java deleted file mode 100644 index 3343f4f..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/ElapsedTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import org.junit.Before; -import org.junit.Test; - -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; -import org.apache.aurora.common.util.testing.FakeTicker; - -import static org.junit.Assert.assertEquals; - -/** - * @author William Farner - */ -public class ElapsedTest { - - private static final Amount<Long, Time> ONE_SECOND = Amount.of(1L, Time.SECONDS); - - private static final String NAME = "elapsed"; - - private FakeTicker ticker; - - @Before - public void setUp() { - ticker = new FakeTicker(); - Stats.flush(); - } - - private Elapsed elapsed(Time granularity) { - return new Elapsed(NAME, granularity, ticker); - } - - @Test - public void testTimeSince() { - Elapsed elapsed = elapsed(Time.MILLISECONDS); - checkValue(0); - ticker.advance(ONE_SECOND); - checkValue(1000); - - elapsed.reset(); - checkValue(0); - - elapsed.reset(); - ticker.advance(ONE_SECOND); - checkValue(1000); - ticker.advance(ONE_SECOND); - checkValue(2000); - ticker.advance(ONE_SECOND); - checkValue(3000); - ticker.advance(ONE_SECOND); - checkValue(4000); - } - - @Test - public void testGranularity() { - Elapsed elapsed = elapsed(Time.HOURS); - checkValue(0); - ticker.advance(Amount.of(1L, Time.DAYS)); - checkValue(24); - - elapsed.reset(); - ticker.advance(Amount.of(1L, Time.MINUTES)); - checkValue(0); - } - - private void checkValue(long expected) { - long actual = (Long) Stats.getVariable(NAME).read(); - assertEquals(expected, actual); - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/EntropyTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/EntropyTest.java b/commons/src/test/java/org/apache/aurora/common/stats/EntropyTest.java deleted file mode 100644 index 0807986..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/EntropyTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import com.google.common.collect.Lists; -import junit.framework.TestCase; -import org.junit.Test; - -import java.util.List; - -/** - * Tests Entropy. - * - * @author Gilad Mishne - */ -public class EntropyTest extends TestCase { - - private void assertEqualsWithDeviation(double expected, double predicted, double deviation) { - assertTrue(String.format("%2.4f not within %2.4f distance of %2.4f", - predicted, deviation, expected), - Math.abs(expected - predicted) <= deviation); - } - - @Test - public void test() throws Exception { - List<Integer> numbers = Lists.newArrayList(); - double deviation = 0.01; - - assertEqualsWithDeviation(new Entropy<Integer>(numbers).entropy(), 0, deviation); - - numbers.add(1); - assertEqualsWithDeviation(new Entropy<Integer>(numbers).entropy(), 0, deviation); - - numbers.add(2); - assertEqualsWithDeviation(new Entropy<Integer>(numbers).entropy(), 1, deviation); - - numbers.addAll(Lists.newArrayList(1, 2)); - assertEqualsWithDeviation(new Entropy<Integer>(numbers).entropy(), 1, deviation); - assertEqualsWithDeviation(new Entropy<Integer>(numbers).perplexity(), 2, deviation); - - numbers.addAll(Lists.newArrayList(2, 2, 3, 4)); - assertEqualsWithDeviation(new Entropy<Integer>(numbers).entropy(), 1.75, deviation); - - numbers.addAll(Lists.newArrayList(1, 1, 1, 1)); - assertEqualsWithDeviation(new Entropy<Integer>(numbers).entropy(), 1.625, deviation); - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/MergedHistogramTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/MergedHistogramTest.java b/commons/src/test/java/org/apache/aurora/common/stats/MergedHistogramTest.java deleted file mode 100644 index e71a34f..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/MergedHistogramTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import org.junit.Test; - -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Data; - -import static org.junit.Assert.assertEquals; - -public class MergedHistogramTest { - - @Test - public void testEmptyMergedHistogram() { - ApproximateHistogram empty[] = new ApproximateHistogram[0]; - Histogram mergedHistogram = ApproximateHistogram.merge(empty); - - assertEquals(0L, mergedHistogram.getQuantile(0.5)); - } - - @Test - public void testMergedSimilarHistogram() { - int n = 10; - ApproximateHistogram histograms[] = new ApproximateHistogram[n]; - for (int i = 0; i < n; i++) { - ApproximateHistogram h = new ApproximateHistogram(); - h.add(i); - histograms[i] = h; - } - - Histogram mergedHistogram = ApproximateHistogram.merge(histograms); - assertEquals(0L, mergedHistogram.getQuantile(0.0)); - assertEquals(1L, mergedHistogram.getQuantile(0.1)); - assertEquals(5L, mergedHistogram.getQuantile(0.5)); - assertEquals(9L, mergedHistogram.getQuantile(0.9)); - assertEquals(9L, mergedHistogram.getQuantile(0.99)); - } - - @Test - public void testMergedDifferentHistogram() { - int n = 10; - ApproximateHistogram histograms[] = new ApproximateHistogram[n]; - for (int i = 0; i < n; i++) { - ApproximateHistogram h = new ApproximateHistogram(Amount.of(2L + 4*i, Data.KB)); - h.add(i); - histograms[i] = h; - } - - Histogram mergedHistogram = ApproximateHistogram.merge(histograms); - assertEquals(0L, mergedHistogram.getQuantile(0.0)); - assertEquals(1L, mergedHistogram.getQuantile(0.1)); - assertEquals(5L, mergedHistogram.getQuantile(0.5)); - assertEquals(9L, mergedHistogram.getQuantile(0.9)); - assertEquals(9L, mergedHistogram.getQuantile(0.99)); - } - - @Test - public void testMergedBigHistogram() { - int n = 10; - int m = 5000; - ApproximateHistogram histograms[] = new ApproximateHistogram[n]; - int x = 0; - for (int i = 0; i < n; i++) { - ApproximateHistogram h = new ApproximateHistogram(); - while(x < m * (i + 1)) { - h.add(x); - x += 1; - } - histograms[i] = h; - } - long sum = m * n; - - double maxError = ApproximateHistogram.DEFAULT_PRECISION.getEpsilon() * - ApproximateHistogram.DEFAULT_PRECISION.getN(); - Histogram mergedHistogram = ApproximateHistogram.merge(histograms); - for (int i = 1; i < 10; i++) { - double q = i / 10.0; - double expected = q * sum; - assertEquals(expected, mergedHistogram.getQuantile(q), maxError); - } - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/MovingAverageTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/MovingAverageTest.java b/commons/src/test/java/org/apache/aurora/common/stats/MovingAverageTest.java deleted file mode 100644 index cce2f69..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/MovingAverageTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import java.util.List; - -import com.google.common.collect.Lists; - -import org.junit.Before; -import org.junit.Test; - -import org.apache.aurora.common.testing.easymock.EasyMockTest; - -import static org.easymock.EasyMock.expect; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -/** - * Test for MovingAverage. - * - * @author William Farner - */ -public class MovingAverageTest extends EasyMockTest { - - private Stat<Integer> input; - - @Before - public void setUp() { - input = createMock(new Clazz<Stat<Integer>>() {}); - } - - @Test - public void testEmptySeries() { - runTest(Lists.<Integer>newArrayList(), Lists.<Double>newArrayList()); - } - - @Test - public void testConstantValues() { - runTest( - Lists.newArrayList( 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5), - Lists.newArrayList(5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d, 5d)); - } - - @Test - public void testLinear() { - runTest( - Lists.newArrayList( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13), - Lists.newArrayList(1d, 1.5d, 2d, 2.5d, 3d, 3.5d, 4d, 4.5d, 5d, 5.5d, 6.5d, 7.5d, 8.5d)); - } - - @Test - public void testStep() { - runTest( - Lists.newArrayList( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10), - Lists.newArrayList(0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 0d, 1d, 2d, 3d, 4d, 5d)); - } - - private void runTest(List<Integer> inputs, List<Double> expectedOutputs) { - expect(input.getName()).andReturn("test").atLeastOnce(); - for (int value : inputs) { - expect(input.read()).andReturn(value); - } - - control.replay(); - - MovingAverage<Integer> movingAvg = MovingAverage.of(input, 10 /* window size */); - - for (double output : expectedOutputs) { - assertThat(movingAvg.sample(), is(output)); - } - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/MovingWindowDeltaTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/MovingWindowDeltaTest.java b/commons/src/test/java/org/apache/aurora/common/stats/MovingWindowDeltaTest.java deleted file mode 100644 index 857916c..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/MovingWindowDeltaTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import org.junit.Test; - -import static org.junit.Assert.*; - -import java.util.concurrent.atomic.AtomicLong; - -import com.google.common.base.Supplier; - -/** - * Test for MovingWindowDelta. - * - * @author Feng Zhuge - */ -public class MovingWindowDeltaTest { - private static final int DEFAULT_WINDOW_SIZE = 5; - - private AtomicLong externalValue = new AtomicLong(); - - public Supplier<AtomicLong> getSupplier() { - return new Supplier<AtomicLong>() { - @Override - public AtomicLong get() { - return externalValue; - } - }; - } - - @Test - public void testOneSample() { - MovingWindowDelta<AtomicLong> movingWindowDelta = MovingWindowDelta.of( - "test", getSupplier(), DEFAULT_WINDOW_SIZE); - - externalValue.getAndSet(7l); - externalValue.getAndSet(11l); - - assertEquals(11l, movingWindowDelta.doSample().longValue()); - } - - @Test - public void testMultipleSamples() { - MovingWindowDelta<AtomicLong> movingWindowDelta = MovingWindowDelta.of( - "test", getSupplier(), DEFAULT_WINDOW_SIZE); - - externalValue.getAndSet(3l); - assertEquals(3l, movingWindowDelta.doSample().longValue()); - externalValue.getAndSet(8l); - assertEquals(8l, movingWindowDelta.doSample().longValue()); - } - - @Test - public void TestExpiringCounts() { - MovingWindowDelta<AtomicLong> movingWindowDelta = MovingWindowDelta.of( - "test", getSupplier(), DEFAULT_WINDOW_SIZE); - - long expectedDelta; - for (long i = 0; i < 100; ++i) { - expectedDelta = i < DEFAULT_WINDOW_SIZE ? i + 1 : DEFAULT_WINDOW_SIZE; - - externalValue.getAndSet(i + 1); - assertEquals(expectedDelta, movingWindowDelta.doSample().longValue()); - } - } - - @Test - public void TestDifferentValueExpiring() { - MovingWindowDelta<AtomicLong> movingWindowDelta = - MovingWindowDelta.of("test", getSupplier(), 5); - - long ret = 0l; - for (long i = 0; i < 10; ++i) { - externalValue.getAndSet(i * i); - ret = movingWindowDelta.doSample(); - } - assertEquals(65l, ret); - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/NumericStatExporterTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/NumericStatExporterTest.java b/commons/src/test/java/org/apache/aurora/common/stats/NumericStatExporterTest.java deleted file mode 100644 index 23a767d..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/NumericStatExporterTest.java +++ /dev/null @@ -1,144 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import java.util.Map; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Matchers; -import org.mockito.Mock; - -import org.apache.aurora.common.application.ShutdownRegistry; -import org.apache.aurora.common.base.Closure; -import org.apache.aurora.common.base.Command; -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; -import org.apache.aurora.common.testing.mockito.MockitoTest; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.anyLong; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -/** - * Unit tests for {@code NumericStatExporter} - */ -public class NumericStatExporterTest extends MockitoTest { - private static final Amount<Long, Time> TEST_EXPORT_INTERVAL = Amount.of(1L, Time.MINUTES); - private static final String MOCK_STAT_NAME = "NumericStatExporterTest_mock_stat"; - private static final int MOCK_STAT_READ_VALUE = 0; - private static final int MOCK_STAT_SAMPLED_VALUE = 1; - - @Mock - private Closure<Map<String, ? extends Number>> mockExportSink; - @Mock - private ScheduledExecutorService mockExecutor; - @Mock - private ShutdownRegistry mockShutdownRegistry; - @Mock - private RecordingStat<Integer> mockRecordingStat; - - @Captor - private ArgumentCaptor<Runnable> runnableCaptor; - @Captor - private ArgumentCaptor<Command> commandCaptor; - @Captor - private ArgumentCaptor<Map<String, ? extends Number>> statReadValueMapCaptor; - - - private NumericStatExporter numericStatExporter; - - @Before - public void setUp() { - when(mockRecordingStat.getName()).thenReturn(MOCK_STAT_NAME); - when(mockRecordingStat.read()).thenReturn(MOCK_STAT_READ_VALUE); - when(mockRecordingStat.sample()).thenReturn(MOCK_STAT_SAMPLED_VALUE); - Stats.export(mockRecordingStat); - - numericStatExporter - = new NumericStatExporter(mockExportSink, mockExecutor, TEST_EXPORT_INTERVAL); - } - - @Test - public void testStartMethodScheduleExport() { - numericStatExporter.start(mockShutdownRegistry); - - // Verify the executor is scheduled properly. - verify(mockExecutor).scheduleAtFixedRate(runnableCaptor.capture(), - anyLong(), anyLong(), Matchers.<TimeUnit>anyObject()); - // Verify the behavior of the schedule runnable. - runnableCaptor.getValue().run(); - verify(mockExportSink).execute(statReadValueMapCaptor.capture()); - // Verify stat reading behavior. - assertEquals(MOCK_STAT_READ_VALUE, statReadValueMapCaptor.getValue().get(MOCK_STAT_NAME)); - } - - @Test - public void testStartMethodShutdownRegistryFinalSampleAndExport() { - numericStatExporter.start(mockShutdownRegistry); - - // Verify the shutdown registry is called. - verify(mockShutdownRegistry).addAction(commandCaptor.capture()); - // Verify the behavior of the shutdown registry command. - commandCaptor.getValue().execute(); - - // The shutdown command calls stop(), which we'll test separately. - - // Now verifies the final sample and export behavior. - verify(mockExportSink).execute(statReadValueMapCaptor.capture()); - // Verify stat sampling and reading behavior. - assertEquals(MOCK_STAT_SAMPLED_VALUE, statReadValueMapCaptor.getValue().get(MOCK_STAT_NAME)); - } - - @Test - public void testStopMethodAwaitTerminationReturnsFast() throws Exception { - when(mockExecutor.awaitTermination(anyLong(), Matchers.<TimeUnit>anyObject())) - .thenReturn(true); - numericStatExporter.stop(); - verify(mockExecutor).awaitTermination(eq(1L), eq(TimeUnit.SECONDS)); - verifyNoMoreInteractions(mockExecutor); - } - - @Test - public void testStopMethodAwaitTerminationReturnsSlowly() throws Exception { - when(mockExecutor.awaitTermination(anyLong(), Matchers.<TimeUnit>anyObject())) - .thenReturn(false); - numericStatExporter.stop(); - verify(mockExecutor, times(2)).awaitTermination(eq(1L), eq(TimeUnit.SECONDS)); - verify(mockExecutor).shutdownNow(); - verifyNoMoreInteractions(mockExecutor); - } - - @Test - public void testStopMethodAwaitTerminationInterrupted() throws Exception { - when(mockExecutor.awaitTermination(anyLong(), Matchers.<TimeUnit>anyObject())) - .thenThrow(new InterruptedException("mock failure")); - numericStatExporter.stop(); - verify(mockExecutor).awaitTermination(eq(1L), eq(TimeUnit.SECONDS)); - verify(mockExecutor).shutdownNow(); - verifyNoMoreInteractions(mockExecutor); - // We need to reset the thread's interrupt flag so other tests who uses certain - // concurrent calls like latches and various waits wouldn't fail. - Thread.currentThread().interrupted(); - } -} - http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/PipelineStatsTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/PipelineStatsTest.java b/commons/src/test/java/org/apache/aurora/common/stats/PipelineStatsTest.java deleted file mode 100644 index b090c64..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/PipelineStatsTest.java +++ /dev/null @@ -1,147 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import com.google.common.collect.Sets; -import org.apache.aurora.common.quantity.Time; -import org.apache.aurora.common.util.Clock; -import org.apache.aurora.common.util.testing.FakeClock; -import org.junit.Before; -import org.junit.Test; - -import java.util.concurrent.atomic.AtomicLong; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -/** - * Tests the PipelineStats class. - * - * @author William Farner - */ -public class PipelineStatsTest { - - private Clock clock = new FakeClock(); - private PipelineStats stats; - - @Before - public void setUp() { - stats = new PipelineStats("test", Sets.newHashSet("a", "b", "c"), clock, Time.MILLISECONDS); - } - - @Test - public void testEmptyFlow() { - PipelineStats.Snapshot pipeline = stats.newSnapshot(); - pipeline.end(); - - checkSample("a", 0, 0); - checkSample("b", 0, 0); - checkSample("c", 0, 0); - checkSample("full", 1, 0); - } - - @Test - public void testSimpleFlow() throws Exception { - PipelineStats.Snapshot pipeline = stats.newSnapshot(); - pipeline.start("a"); - clock.waitFor(10); - pipeline.start("b"); - clock.waitFor(20); - pipeline.start("c"); - clock.waitFor(30); - pipeline.end(); - - checkSample("a", 1, 10); - checkSample("b", 1, 20); - checkSample("c", 1, 30); - checkSample("full", 1, 60); - } - - @Test - public void testEarlyExit() throws Exception { - PipelineStats.Snapshot pipeline = stats.newSnapshot(); - pipeline.start("a"); - clock.waitFor(10); - pipeline.start("b"); - clock.waitFor(20); - pipeline.end(); - - checkSample("a", 1, 10); - checkSample("b", 1, 20); - checkSample("full", 1, 30); - } - - @Test - public void testDuplicatedStages() throws Exception { - PipelineStats.Snapshot pipeline = stats.newSnapshot(); - pipeline.start("a"); - clock.waitFor(10); - pipeline.start("b"); - clock.waitFor(20); - pipeline.start("b"); - clock.waitFor(10); - pipeline.start("b"); - clock.waitFor(50); - pipeline.start("c"); - clock.waitFor(30); - pipeline.start("c"); - clock.waitFor(70); - pipeline.end(); - - checkSample("a", 1, 10); - checkSample("b", 3, 80); - checkSample("c", 2, 100); - checkSample("full", 1, 190); - } - - @Test - public void testSimultaneousSnapshots() throws Exception { - PipelineStats.Snapshot pipeline1 = stats.newSnapshot(); - PipelineStats.Snapshot pipeline2 = stats.newSnapshot(); - pipeline1.start("a"); - clock.waitFor(10); - pipeline2.start("a"); - pipeline1.start("b"); - clock.waitFor(20); - pipeline2.start("b"); - clock.waitFor(10); - pipeline2.start("c"); - clock.waitFor(10); - pipeline2.end(); - - // Only pipeline2 was recorded, so we should not see pipeline1 in the time series yet. - checkSample("a", 1, 20); - checkSample("b", 1, 10); - checkSample("c", 1, 10); - checkSample("full", 1, 40); - - pipeline1.start("c"); - clock.waitFor(30); - pipeline1.end(); - - // The current sample will now be the sum of pipeline1 and pipeline2. - checkSample("a", 2, 30); - checkSample("b", 2, 50); - checkSample("c", 2, 40); - checkSample("full", 2, 120); - } - - private void checkSample(String stage, long events, long latency) { - AtomicLong eventsCounter = stats.getStatsForStage(stage).getEventCounter(); - AtomicLong latencyCounter = stats.getStatsForStage(stage).getTotalCounter(); - - assertThat(eventsCounter.get(), is(events)); - assertThat(latencyCounter.get(), is(latency)); - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/PrintableHistogramTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/PrintableHistogramTest.java b/commons/src/test/java/org/apache/aurora/common/stats/PrintableHistogramTest.java deleted file mode 100644 index d2aa019..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/PrintableHistogramTest.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import junit.framework.TestCase; - -public class PrintableHistogramTest extends TestCase { - - public void testPrintHistogram() { - PrintableHistogram hist = new PrintableHistogram(10, 20, 30, 40, 50, 60, 70, 80, 90, 100); - for (int i = 10; i > 0; i--) { - hist.addValue(i * 10, 10 - i); - } - System.out.println(hist); - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/ReservoirSamplerTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/ReservoirSamplerTest.java b/commons/src/test/java/org/apache/aurora/common/stats/ReservoirSamplerTest.java deleted file mode 100644 index 4d5a7e9..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/ReservoirSamplerTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import java.util.List; - -import org.junit.Before; -import org.junit.Test; - -import com.google.common.collect.ImmutableList; - -import org.apache.aurora.common.testing.easymock.EasyMockTest; -import org.apache.aurora.common.util.Random; - -import static org.easymock.EasyMock.expect; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertEquals; - -/** - * Tests the Reservoir Sampler code - * - * @author Delip Rao - */ -public class ReservoirSamplerTest extends EasyMockTest { - - private Random random; - - @Before - public void setUp() throws Exception { - random = createMock(Random.class); - } - - @Test - public void testSampling() throws Exception { - int mockValues[] = {3, 4, 5, 6, 7}; - for (int value : mockValues) { - expect(random.nextInt(value + 1)).andReturn(value); - } - control.replay(); - - ReservoirSampler<Integer> sampler = new ReservoirSampler<Integer>(3, random); - List<Integer> stream = ImmutableList.of(1, 2, 3, 4, 5, 6, 7, 8); - for (int i : stream) { - sampler.sample(i); - } - List<Integer> expectedSamples = ImmutableList.of(1, 2, 3); - assertEquals("The samples should be 1, 2, 3", expectedSamples, - ImmutableList.copyOf(sampler.getSamples())); - } - - @Test - public void testNoSampling() throws Exception { - // no calls to random.nextInt should happen in this test - control.replay(); - List<Integer> stream = ImmutableList.of(1, 2, 3); - // reservoir is larger than the stream. No sampling should happen here. - ReservoirSampler<Integer> sampler = new ReservoirSampler<Integer>(20); - for (int i : stream) { - sampler.sample(i); - } - assertEquals("The samples should be same as the stream", stream, - ImmutableList.copyOf(sampler.getSamples())); - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/StatisticsTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/StatisticsTest.java b/commons/src/test/java/org/apache/aurora/common/stats/StatisticsTest.java deleted file mode 100644 index 35c076b..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/StatisticsTest.java +++ /dev/null @@ -1,116 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import java.util.Arrays; -import java.util.List; - -import junit.framework.TestCase; - -import com.google.common.collect.ImmutableList; - -/** - * Tests the functionality of the Statistics class. - * - * @author William Farner - */ -public class StatisticsTest extends TestCase { - private static final double ERROR_THRESHOLD = 1e-10; - - private static final List<Integer> EMPTY_SET = ImmutableList.of(); - - private static final List<Integer> TEST_SET_A = Arrays.asList(76117373, 76167137, 75870125, 75880508, 78099974, - 77810738, 75763975, 78042301, 76109165, 77816921, - 76115544, 76075750, 75391297, 75597249, 77793835, - 76001118, 77752542, 78413670, 60351776, 75607235, - 76057629, 80011920, 24067379, 75767484, 80052983, - 79278613, 75600277); - - private Statistics createAndLoad(List<Integer> values) { - Statistics stats = new Statistics(); - for (long value : values) { - stats.accumulate(value); - } - - return stats; - } - - private void checkWithinThreshold(double expected, double actual) { - assertTrue(Math.abs(expected - actual) < ERROR_THRESHOLD); - } - - public void testMin() { - // min is undefined for an empty set, but it should not fail. - Statistics stats = createAndLoad(EMPTY_SET); - stats.min(); - - stats = createAndLoad(TEST_SET_A); - assertEquals(24067379, stats.min()); - } - - public void testMax() { - // max is undefined for an empty set, but it should not fail. - Statistics stats = createAndLoad(EMPTY_SET); - stats.max(); - - stats = createAndLoad(TEST_SET_A); - assertEquals(80052983, stats.max()); - } - - public void testMean() { - // mean is undefined for an empty set, but it should not fail. - Statistics stats = createAndLoad(EMPTY_SET); - stats.mean(); - - stats = createAndLoad(TEST_SET_A); - checkWithinThreshold(7.435609325925925E7, stats.mean()); - } - - public void testVariance() { - Statistics stats = createAndLoad(EMPTY_SET); - assertEquals(Double.NaN, stats.variance()); - - stats = createAndLoad(TEST_SET_A); - checkWithinThreshold(1.089077613763465E14, stats.variance()); - } - - public void testStandardDeviation() { - Statistics stats = createAndLoad(EMPTY_SET); - assertEquals(Double.NaN, stats.standardDeviation()); - - stats = createAndLoad(TEST_SET_A); - checkWithinThreshold(1.0435888145066835E7, stats.standardDeviation()); - } - - public void testPopulationSize() { - Statistics stats = createAndLoad(EMPTY_SET); - assertEquals(0L, stats.populationSize()); - - stats = createAndLoad(TEST_SET_A); - assertEquals(TEST_SET_A.size(), stats.populationSize()); - } - - public void testSum() { - Statistics stats = createAndLoad(EMPTY_SET); - assertEquals(0L, stats.sum()); - - stats = createAndLoad(TEST_SET_A); - long expectedSum = 0; - for (long x: TEST_SET_A) { - expectedSum += x; - } - assertEquals(expectedSum, stats.sum()); - } - -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/WindowedHistogramTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/WindowedHistogramTest.java b/commons/src/test/java/org/apache/aurora/common/stats/WindowedHistogramTest.java deleted file mode 100644 index c93801a..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/WindowedHistogramTest.java +++ /dev/null @@ -1,234 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import java.util.List; - -import com.google.common.collect.ImmutableList; - -import org.junit.Test; - -import org.apache.aurora.common.objectsize.ObjectSizeCalculator; -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Data; -import org.apache.aurora.common.quantity.Time; -import org.apache.aurora.common.stats.testing.RealHistogram; -import org.apache.aurora.common.util.testing.FakeClock; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import static org.apache.aurora.common.stats.WindowedApproxHistogram.DEFAULT_MAX_MEMORY; - -/** - * Tests WindowedHistogram. - */ -public class WindowedHistogramTest { - - @Test - public void testEmptyWinHistogram() { - WindowedApproxHistogram wh = new WindowedApproxHistogram(); - assertEquals(0L, wh.getQuantile(0.0)); - } - - @Test - public void testWinHistogramWithEdgeCases() { - FakeClock clock = new FakeClock(); - Amount<Long, Time> window = Amount.of(100L, Time.MILLISECONDS); - int slices = 10; - long sliceDuration = window.as(Time.NANOSECONDS) / slices; - WindowedApproxHistogram h = - new WindowedApproxHistogram(window, slices, DEFAULT_MAX_MEMORY, clock); - - h.add(Long.MIN_VALUE); - clock.advance(Amount.of(2 * sliceDuration, Time.NANOSECONDS)); - assertEquals(Long.MIN_VALUE, h.getQuantile(0.0)); - assertEquals(Long.MIN_VALUE, h.getQuantile(0.5)); - assertEquals(Long.MIN_VALUE, h.getQuantile(1.0)); - - h.add(Long.MAX_VALUE); - clock.advance(Amount.of(2 * sliceDuration, Time.NANOSECONDS)); - assertEquals(Long.MIN_VALUE, h.getQuantile(0.0)); - assertEquals(Long.MIN_VALUE, h.getQuantile(0.25)); - assertEquals(Long.MAX_VALUE, h.getQuantile(0.75)); - assertEquals(Long.MAX_VALUE, h.getQuantile(1.0)); - } - - @Test - public void testClearedWinHistogram() { - FakeClock clock = new FakeClock(); - Amount<Long, Time> window = Amount.of(100L, Time.MILLISECONDS); - int slices = 10; - Amount<Long, Time> sliceDuration = Amount.of( - window.as(Time.NANOSECONDS) / slices, Time.NANOSECONDS); - WindowedHistogram<?> h = createFullHistogram(window, slices, clock); - long p0 = h.getQuantile(0.1); - long p50 = h.getQuantile(0.5); - long p90 = h.getQuantile(0.9); - assertFalse(0 == p0); - assertFalse(0 == p50); - assertFalse(0 == p90); - - h.clear(); - - assertEquals(0, h.getQuantile(0.1)); - assertEquals(0, h.getQuantile(0.5)); - assertEquals(0, h.getQuantile(0.9)); - - // reload the histogram with the exact same values than before - fillHistogram(h, sliceDuration, slices, clock); - - assertEquals(p0, h.getQuantile(0.1)); - assertEquals(p50, h.getQuantile(0.5)); - assertEquals(p90, h.getQuantile(0.9)); - } - - @Test - public void testSimpleWinHistogram() { - FakeClock clock = new FakeClock(); - Amount<Long, Time> window = Amount.of(100L, Time.MILLISECONDS); - int slices = 10; - WindowedHistogram<?> wh = createFullHistogram(window, slices, clock); - - // check that the global distribution is the aggregation of all underlying histograms - for (int i = 1; i <= slices; i++) { - double q = (double) i / slices; - assertEquals(i, wh.getQuantile(q), 1.0); - } - - // advance in time an forget about old values - long sliceDuration = window.as(Time.NANOSECONDS) / slices; - clock.advance(Amount.of(sliceDuration, Time.NANOSECONDS)); - for (int j = 0; j < 1000; j++) { - wh.add(11); - } - assertEquals(2, wh.getQuantile(0.05), 1.0); - assertEquals(11, wh.getQuantile(0.99), 1.0); - } - - @Test - public void testWinHistogramWithGap() { - FakeClock clock = new FakeClock(); - Amount<Long, Time> window = Amount.of(100L, Time.MILLISECONDS); - int slices = 10; - WindowedHistogram<?> wh = createFullHistogram(window, slices, clock); - // wh is a WindowedHistogram of 10 slices + the empty current with values from 1 to 10 - // [1][2][3][4][5][6][7][8][9][10][.] - // ^ - - for (int j = 0; j < 1000; j++) { - wh.add(100); - } - // [1][2][3][4][5][6][7][8][9][10][100] - // ^ - // quantiles are computed based on [1] -> [10] - - clock.advance(Amount.of((slices - 1) * 100L / slices, Time.MILLISECONDS)); - for (int j = 0; j < 1000; j++) { - wh.add(200); - } - // [1][2][3][4][5][6][7][8][200][10][100] - // ^ - // quantiles are computed based on [10][100][1][2][3][4][5][6][7][8] - // and removing old ones [10][100][.][.][.][.][.][.][.][.] - // all the histograms between 100 and 200 are old and shouldn't matter in the computation of - // quantiles. - assertEquals(10L, wh.getQuantile(0.25), 1.0); - assertEquals(100L, wh.getQuantile(0.75), 1.0); - - clock.advance(Amount.of(100L / slices, Time.MILLISECONDS)); - // [1][2][3][4][5][6][7][8][200][10][100] - // ^ - // quantiles are computed based on [100][1][2][3][4][5][6][7][8][200] - // and removing old ones [100][.][.][.][.][.][.][.][.][200] - - assertEquals(100L, wh.getQuantile(0.25), 1.0); - assertEquals(200L, wh.getQuantile(0.75), 1.0); - - // advance a lot in time, everything should be "forgotten" - clock.advance(Amount.of(500L, Time.MILLISECONDS)); - assertEquals(0L, wh.getQuantile(0.5), 1.0); - } - - @Test - public void testWinHistogramMemory() { - ImmutableList.Builder<Amount<Long, Data>> builder = ImmutableList.builder(); - builder.add(Amount.of(8L, Data.KB)); - builder.add(Amount.of(12L, Data.KB)); - builder.add(Amount.of(16L, Data.KB)); - builder.add(Amount.of(20L, Data.KB)); - builder.add(Amount.of(24L, Data.KB)); - builder.add(Amount.of(32L, Data.KB)); - builder.add(Amount.of(64L, Data.KB)); - builder.add(Amount.of(256L, Data.KB)); - builder.add(Amount.of(1L, Data.MB)); - builder.add(Amount.of(16L, Data.MB)); - builder.add(Amount.of(32L, Data.MB)); - List<Amount<Long, Data>> sizes = builder.build(); - - // large estimation of the memory used outside of buffers - long fixSize = Amount.of(4, Data.KB).as(Data.BYTES); - - for (Amount<Long, Data> maxSize: sizes) { - WindowedApproxHistogram hist = new WindowedApproxHistogram( - Amount.of(60L, Time.SECONDS), 6, maxSize); - hist.add(1L); - hist.getQuantile(0.5); - long size = ObjectSizeCalculator.getObjectSize(hist); - // reverting CI JVM seems to have different memory consumption than mine - //assertTrue(size < fixSize + maxSize.as(Data.BYTES)); - } - } - - @Test - public void testWinHistogramAccuracy() { - FakeClock ticker = new FakeClock(); - Amount<Long, Time> window = Amount.of(100L, Time.MILLISECONDS); - int slices = 10; - Amount<Long, Time> sliceDuration = Amount.of( - window.as(Time.NANOSECONDS) / slices, Time.NANOSECONDS); - WindowedHistogram<?> wh = createFullHistogram(window, slices, ticker); - RealHistogram rh = fillHistogram(new RealHistogram(), sliceDuration, slices, new FakeClock()); - - assertEquals(wh.getQuantile(0.5), rh.getQuantile(0.5)); - assertEquals(wh.getQuantile(0.75), rh.getQuantile(0.75)); - assertEquals(wh.getQuantile(0.9), rh.getQuantile(0.9)); - assertEquals(wh.getQuantile(0.99), rh.getQuantile(0.99)); - } - - /** - * @return a WindowedHistogram with different value in each underlying Histogram - */ - private WindowedHistogram<?> createFullHistogram( - Amount<Long, Time> duration, int slices, FakeClock clock) { - long sliceDuration = duration.as(Time.NANOSECONDS) / slices; - WindowedApproxHistogram wh = new WindowedApproxHistogram(duration, slices, - DEFAULT_MAX_MEMORY, clock); - clock.advance(Amount.of(1L, Time.NANOSECONDS)); - - return fillHistogram(wh, Amount.of(sliceDuration, Time.NANOSECONDS), slices, clock); - } - - private <H extends Histogram> H fillHistogram(H h, - Amount<Long, Time> sliceDuration, int slices, FakeClock clock) { - for (int i = 1; i <= slices; i++) { - for (int j = 0; j < 1000; j++) { - h.add(i); - } - clock.advance(sliceDuration); - } - return h; - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/WindowedStatsTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/WindowedStatsTest.java b/commons/src/test/java/org/apache/aurora/common/stats/WindowedStatsTest.java deleted file mode 100644 index c143174..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/WindowedStatsTest.java +++ /dev/null @@ -1,202 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import org.junit.Test; - -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; - -import org.apache.aurora.common.util.testing.FakeClock; - -import static org.junit.Assert.assertEquals; - -public class WindowedStatsTest { - private Amount<Long, Time> window = Amount.of(1L, Time.MINUTES); - private int slices = 3; - private long sliceDuration = window.as(Time.NANOSECONDS) / slices; - - @Test - public void testEmptyStats() { - FakeClock clock = new FakeClock(); - WindowedStatistics ws = new WindowedStatistics(window, slices, clock); - - assertEmpty(ws); - } - - @Test - public void testStatsCorrectness() { - FakeClock clock = new FakeClock(); - Statistics reference = new Statistics(); - WindowedStatistics ws = new WindowedStatistics(window, slices, clock); - - for (int i=0; i<1000; i++) { - reference.accumulate(i); - ws.accumulate(i); - } - clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - - assertEquals(reference.max(), ws.max()); - assertEquals(reference.min(), ws.min()); - assertEquals(reference.populationSize(), ws.populationSize()); - assertEquals(reference.sum(), ws.sum()); - assertEquals(reference.range(), ws.range()); - assertEquals(reference.mean(), ws.mean(), 0.01); - assertEquals(reference.variance(), ws.variance(), 0.01); - assertEquals(reference.standardDeviation(), ws.standardDeviation(), 0.01); - - for (int i=0; i<1000; i++) { - long x = i + 500; - reference.accumulate(x); - ws.accumulate(x); - } - clock.advance(Amount.of(sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - - assertEquals(reference.max(), ws.max()); - assertEquals(reference.min(), ws.min()); - assertEquals(reference.populationSize(), ws.populationSize()); - assertEquals(reference.sum(), ws.sum()); - assertEquals(reference.range(), ws.range()); - assertEquals(reference.mean(), ws.mean(), 0.01); - assertEquals(reference.variance(), ws.variance(), 0.01); - assertEquals(reference.standardDeviation(), ws.standardDeviation(), 0.01); - - for (int i=0; i<1000; i++) { - long x = i * i; - reference.accumulate(x); - ws.accumulate(x); - } - clock.advance(Amount.of(sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - - assertEquals(reference.max(), ws.max()); - assertEquals(reference.min(), ws.min()); - assertEquals(reference.populationSize(), ws.populationSize()); - assertEquals(reference.sum(), ws.sum()); - assertEquals(reference.range(), ws.range()); - assertEquals(reference.mean(), ws.mean(), 0.01); - assertEquals(reference.variance(), ws.variance(), 0.01); - assertEquals(reference.standardDeviation(), ws.standardDeviation(), 0.01); - } - - @Test - public void testWindowStats() { - FakeClock clock = new FakeClock(); - WindowedStatistics ws = new WindowedStatistics(window, slices, clock); - ws.accumulate(1L); - assertEmpty(ws); - - clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - - assertEquals(1L, ws.max()); - assertEquals(1L, ws.min()); - assertEquals(1L, ws.populationSize()); - assertEquals(1L, ws.sum()); - assertEquals(1.0, ws.mean(), 0.01); - assertEquals(0.0, ws.standardDeviation(), 0.01); - - clock.advance(Amount.of(slices * sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - assertEmpty(ws); - } - - @Test - public void testCleaningOfExpiredWindows() { - FakeClock clock = new FakeClock(); - WindowedStatistics ws = new WindowedStatistics(window, slices, clock); - - long n = 1000L; - for (int i=0; i<n; i++) { - ws.accumulate(i); - } - assertEmpty(ws); - - clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - assertEquals(n, ws.populationSize()); // this window is not empty - - clock.advance(Amount.of(100 * sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - assertEmpty(ws); // this window has been cleaned - } - - @Test - public void testAddNewValueToFullWS() { - FakeClock clock = new FakeClock(); - WindowedStatistics ws = new WindowedStatistics(window, slices, clock); - - // AAAAA - // BBBBB - // CCCCC - // DDDDD - // | | | | - //---------------------------------> t - // t=0 t=1 t=2 t=3 - - // t=0 fill {D} - long n = 1000L; - for (int i=0; i<n; i++) { - ws.accumulate(i); - } - // read {A,B,C}, which should be empty - assertEmpty(ws); - - clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - // t=1, read {B,C,D} which shouldn't be empty - - assertEquals(n - 1L, ws.max()); - assertEquals(0L, ws.min()); - assertEquals(n, ws.populationSize()); - assertEquals(n * (n - 1) / 2, ws.sum()); - assertEquals((n - 1) / 2.0, ws.mean(), 0.01); - - clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - // t=2, read {C,D,A} which shouldn't be empty as well - - assertEquals(n - 1L, ws.max()); - assertEquals(0L, ws.min()); - assertEquals(n, ws.populationSize()); - assertEquals(n * (n - 1) / 2, ws.sum()); - assertEquals((n - 1) / 2.0, ws.mean(), 0.01); - - clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - // t=3, read {D,A,B} which shouldn't be empty as well - - assertEquals(n - 1L, ws.max()); - assertEquals(0L, ws.min()); - assertEquals(n, ws.populationSize()); - assertEquals(n * (n - 1) / 2, ws.sum()); - assertEquals((n - 1) / 2.0, ws.mean(), 0.01); - - clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS)); - ws.refresh(); - // t=4, read {A,B,C} which must be empty (cleaned by the Windowed class) - assertEmpty(ws); - } - - private void assertEmpty(WindowedStatistics ws) { - assertEquals(Long.MIN_VALUE, ws.max()); - assertEquals(Long.MAX_VALUE, ws.min()); - assertEquals(0L, ws.populationSize()); - assertEquals(0L, ws.sum()); - assertEquals(0.0, ws.mean(), 0.01); - assertEquals(0.0, ws.standardDeviation(), 0.01); - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/stats/WindowedTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/stats/WindowedTest.java b/commons/src/test/java/org/apache/aurora/common/stats/WindowedTest.java deleted file mode 100644 index d0f46dd..0000000 --- a/commons/src/test/java/org/apache/aurora/common/stats/WindowedTest.java +++ /dev/null @@ -1,110 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.stats; - -import com.google.common.base.Function; -import com.google.common.base.Supplier; - -import org.junit.Test; - -import org.apache.aurora.common.quantity.Amount; -import org.apache.aurora.common.quantity.Time; -import org.apache.aurora.common.util.Clock; -import org.apache.aurora.common.util.testing.FakeClock; - -import junit.framework.Assert; - -/** - * Test the Windowed abstract class by making a very simple implementation. - */ -public class WindowedTest { - - private class WindowedBox extends Windowed<Integer[]> { - WindowedBox(Amount<Long, Time > window, int slices, Clock clock) { - super(Integer[].class, window, slices, - new Supplier<Integer[]>() { - @Override public Integer[] get() { - Integer[] box = new Integer[1]; - box[0] = 0; - return box; - } - }, - new Function<Integer[], Integer[]>() { - @Override public Integer[] apply(Integer[] xs) { - xs[0] = 0; - return xs; - } - }, clock); - } - - void increment() { - getCurrent()[0] += 1; - } - - int sum() { - int s = 0; - for (Integer[] box : getTenured()) { - s += box[0]; - } - return s; - } - } - - @Test - public void testWindowed() { - Amount<Long, Time > window = Amount.of(1L, Time.MINUTES); - int slices = 3; - Amount<Long, Time > delta = Amount.of( - Amount.of(1L, Time.MINUTES).as(Time.NANOSECONDS) / 3, Time.NANOSECONDS); - FakeClock clock = new FakeClock(); - WindowedBox win = new WindowedBox(window, slices, clock); - // [0][0][0][0] - clock.advance(Amount.of(1L, Time.NANOSECONDS)); - - win.increment(); - // [0][0][0][1] - Assert.assertEquals(0, win.sum()); - - clock.advance(delta); - win.increment(); - win.increment(); - Assert.assertEquals(1, win.sum()); - // [0][0][1][2] - - clock.advance(delta); - win.increment(); - win.increment(); - win.increment(); - Assert.assertEquals(3, win.sum()); - // [0][1][2][3] - - clock.advance(delta); - win.increment(); - win.increment(); - win.increment(); - win.increment(); - Assert.assertEquals(6, win.sum()); - // [1][2][3][4] - - clock.advance(delta); - win.increment(); - win.increment(); - win.increment(); - win.increment(); - win.increment(); - Assert.assertEquals(9, win.sum()); - // [2][3][4][5] - } - -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/testing/TearDownRegistryTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/testing/TearDownRegistryTest.java b/commons/src/test/java/org/apache/aurora/common/testing/TearDownRegistryTest.java deleted file mode 100644 index a9157ec..0000000 --- a/commons/src/test/java/org/apache/aurora/common/testing/TearDownRegistryTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.testing; - -import java.util.concurrent.atomic.AtomicBoolean; - -import com.google.common.testing.junit4.TearDownTestCase; - -import org.junit.Test; - -import org.apache.aurora.common.base.Command; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * @author John Sirois - */ -public class TearDownRegistryTest extends TearDownTestCase { - - @Test - public void testTearDown() { - TearDownRegistry tearDownRegistry = new TearDownRegistry(this); - final AtomicBoolean actionExecuted = new AtomicBoolean(false); - tearDownRegistry.addAction(new Command() { - @Override public void execute() { - actionExecuted.set(true); - } - }); - - assertFalse(actionExecuted.get()); - tearDown(); - assertTrue(actionExecuted.get()); - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/thrift/ThriftConnectionFactoryTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/thrift/ThriftConnectionFactoryTest.java b/commons/src/test/java/org/apache/aurora/common/thrift/ThriftConnectionFactoryTest.java deleted file mode 100644 index 7876264..0000000 --- a/commons/src/test/java/org/apache/aurora/common/thrift/ThriftConnectionFactoryTest.java +++ /dev/null @@ -1,140 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.thrift; - -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.CoreMatchers.sameInstance; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.IOException; -import java.net.InetSocketAddress; - -import org.apache.thrift.transport.TTransport; -import org.apache.thrift.transport.TTransportException; -import org.hamcrest.Matcher; -import org.junit.Test; - -import org.apache.aurora.common.net.pool.Connection; -import org.apache.aurora.common.net.pool.ObjectPool; -import org.apache.aurora.common.thrift.testing.MockTSocket; - -/** - * @author John Sirois - */ -public class ThriftConnectionFactoryTest { - - @Test - public void testPreconditions() { - try { - new ThriftConnectionFactory(null, 1, 1); - fail("a non-null host should be required"); - } catch (NullPointerException e) { - // expected - } - - try { - new ThriftConnectionFactory(" ", 1, 1); - fail("a non-blank host should be required"); - } catch (IllegalArgumentException e) { - // expected - } - - try { - new ThriftConnectionFactory("localhost", 0, 1); - fail("a valid concrete remote port should be required"); - } catch (IllegalArgumentException e) { - // expected - } - - try { - new ThriftConnectionFactory("localhost", 65536, 1); - fail("a valid port should be required"); - } catch (IllegalArgumentException e) { - // expected - } - - try { - new ThriftConnectionFactory("localhost", 65535, 0); - fail("a non-zero value for maxConnections should be required"); - } catch (IllegalArgumentException e) { - // expected - } - } - - @Test - public void testMaxConnections() throws TTransportException, IOException { - ThriftConnectionFactory thriftConnectionFactory = createConnectionFactory(2); - - Connection<TTransport, InetSocketAddress> connection1 = - thriftConnectionFactory.create(ObjectPool.NO_TIMEOUT); - assertOpenConnection(connection1); - - Connection<TTransport, InetSocketAddress> connection2 = - thriftConnectionFactory.create(ObjectPool.NO_TIMEOUT); - assertOpenConnection(connection2); - assertThat(connection1, not(sameInstance(connection2))); - - assertNull("Should've reached maximum connections", - thriftConnectionFactory.create(ObjectPool.NO_TIMEOUT)); - - thriftConnectionFactory.destroy(connection1); - assertClosedConnection(connection1); - - Connection<TTransport, InetSocketAddress> connection3 = - thriftConnectionFactory.create(ObjectPool.NO_TIMEOUT); - assertOpenConnection(connection3); - @SuppressWarnings("unchecked") // Needed because type information lost in vargs. - Matcher<Connection<TTransport, InetSocketAddress>> matcher = - allOf(not(sameInstance(connection1)), not(sameInstance(connection2))); - assertThat(connection3, matcher); - } - - @Test(expected = IllegalArgumentException.class) - public void testInactiveConnectionReturn() { - createConnectionFactory(1).destroy(new TTransportConnection(new MockTSocket(), - InetSocketAddress.createUnresolved(MockTSocket.HOST, MockTSocket.PORT))); - } - - @Test(expected = IllegalArgumentException.class) - public void testNullConnectionReturn() { - createConnectionFactory(1).destroy(null); - } - - private void assertOpenConnection(Connection<TTransport, InetSocketAddress> connection) { - assertNotNull(connection); - assertTrue(connection.isValid()); - assertTrue(connection.get().isOpen()); - } - - private void assertClosedConnection(Connection<TTransport, InetSocketAddress> connection) { - assertFalse(connection.isValid()); - assertFalse(connection.get().isOpen()); - } - - private ThriftConnectionFactory createConnectionFactory(int maxConnections) { - return new ThriftConnectionFactory("foo", 1234, maxConnections) { - @Override TTransport createTransport(int timeoutMillis) throws TTransportException { - TTransport transport = new MockTSocket(); - transport.open(); - return transport; - } - }; - } -} http://git-wip-us.apache.org/repos/asf/aurora/blob/356eeac9/commons/src/test/java/org/apache/aurora/common/thrift/ThriftFactoryTest.java ---------------------------------------------------------------------- diff --git a/commons/src/test/java/org/apache/aurora/common/thrift/ThriftFactoryTest.java b/commons/src/test/java/org/apache/aurora/common/thrift/ThriftFactoryTest.java deleted file mode 100644 index 802caa2..0000000 --- a/commons/src/test/java/org/apache/aurora/common/thrift/ThriftFactoryTest.java +++ /dev/null @@ -1,242 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.aurora.common.thrift; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.net.Socket; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.google.common.collect.ImmutableSet; -import com.google.common.testing.TearDown; -import com.google.common.testing.junit4.TearDownTestCase; - -import org.apache.thrift.async.AsyncMethodCallback; -import org.apache.thrift.async.TAsyncClient; -import org.apache.thrift.async.TAsyncClientManager; -import org.apache.thrift.protocol.TProtocol; -import org.apache.thrift.protocol.TProtocolFactory; -import org.apache.thrift.transport.TNonblockingTransport; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; -import org.junit.Before; -import org.junit.Test; - -import org.apache.aurora.common.net.pool.DynamicHostSet; -import org.apache.aurora.common.thrift.ThriftFactoryTest.GoodService.AsyncIface; -import org.apache.aurora.common.thrift.ServiceInstance; - -/** - * @author John Sirois - */ -public class ThriftFactoryTest extends TearDownTestCase { - - private static final Logger LOG = Logger.getLogger(ThriftFactoryTest.class.getName()); - private IMocksControl control; - - static class GoodService { - public interface Iface { - String doWork() throws TResourceExhaustedException; - } - - public interface AsyncIface { - void doWork(AsyncMethodCallback<String> callback); - } - - public static final String DONE = "done"; - - public static class Client implements Iface { - public Client(TProtocol protocol) { - assertNotNull(protocol); - } - - @Override public String doWork() throws TResourceExhaustedException { - return DONE; - } - } - - public static class AsyncClient extends TAsyncClient implements AsyncIface { - public AsyncClient(TProtocolFactory factory, TAsyncClientManager manager, - TNonblockingTransport transport) { - super(factory, manager, transport); - assertNotNull(factory); - assertNotNull(manager); - assertNotNull(transport); - } - - @Override public void doWork(AsyncMethodCallback<String> callback) { - callback.onComplete(DONE); - } - } - } - - static class BadService { - public interface Iface { - void doWork(); - } - public interface AsyncIface { - void doWork(AsyncMethodCallback<Void> callback); - } - - public static class Client implements Iface { - @Override public void doWork() { - throw new UnsupportedOperationException(); - } - } - } - - private ImmutableSet<InetSocketAddress> endpoints; - - @Before - public void setUp() throws Exception { - control = EasyMock.createControl(); - endpoints = ImmutableSet.of(new InetSocketAddress(5555)); - } - - @Test(expected = NullPointerException.class) - public void testNullServiceInterface() { - ThriftFactory.create(null); - } - - @Test(expected = IllegalArgumentException.class) - public void testBadServiceInterface() { - ThriftFactory.create(GoodService.class); - } - - @Test(expected = IllegalArgumentException.class) - public void testBadServiceImpl() throws ThriftFactory.ThriftFactoryException { - ThriftFactory.<BadService.Iface>create(BadService.Iface.class) - .build(endpoints); - } - - @Test(expected = IllegalArgumentException.class) - public void testBadAsyncServiceImpl() throws ThriftFactory.ThriftFactoryException { - ThriftFactory.<BadService.AsyncIface>create(BadService.AsyncIface.class) - .useFramedTransport(true) - .buildAsync(endpoints); - } - - @Test(expected = IllegalArgumentException.class) - public void testNoBackends() { - ThriftFactory.create(GoodService.Iface.class) - .build(ImmutableSet.<InetSocketAddress>of()); - } - - @Test - public void testCreate() throws Exception { - final AtomicReference<Socket> clientConnection = new AtomicReference<Socket>(); - final CountDownLatch connected = new CountDownLatch(1); - final ServerSocket server = new ServerSocket(0); - Thread service = new Thread(new Runnable() { - @Override public void run() { - try { - clientConnection.set(server.accept()); - } catch (IOException e) { - LOG.log(Level.WARNING, "Problem accepting a connection to thrift server", e); - } finally { - connected.countDown(); - } - } - }); - service.setDaemon(true); - service.start(); - - try { - final Thrift<GoodService.Iface> thrift = ThriftFactory.create(GoodService.Iface.class) - .withMaxConnectionsPerEndpoint(1) - .build(ImmutableSet.of(new InetSocketAddress(server.getLocalPort()))); - addTearDown(new TearDown() { - @Override public void tearDown() { - thrift.close(); - } - }); - - GoodService.Iface client = thrift.create(); - - assertEquals(GoodService.DONE, client.doWork()); - } finally { - connected.await(); - server.close(); - } - - Socket socket = clientConnection.get(); - assertNotNull(socket); - socket.close(); - } - - @Test(expected = TResourceExhaustedException.class) - public void testCreateEmpty() throws Exception { - @SuppressWarnings("unchecked") - DynamicHostSet<ServiceInstance> emptyHostSet = control.createMock(DynamicHostSet.class); - final Thrift<GoodService.Iface> thrift = ThriftFactory.create(GoodService.Iface.class) - .withMaxConnectionsPerEndpoint(1) - .build(emptyHostSet); - addTearDown(new TearDown() { - @Override public void tearDown() { - thrift.close(); - } - }); - GoodService.Iface client = thrift.create(); - - // This should throw a TResourceExhaustedException - client.doWork(); - } - - @Test - public void testCreateAsync() - throws IOException, InterruptedException, ThriftFactory.ThriftFactoryException { - final String responseHolder[] = new String[] {null}; - final CountDownLatch done = new CountDownLatch(1); - AsyncMethodCallback<String> callback = new AsyncMethodCallback<String>() { - @Override - public void onComplete(String response) { - responseHolder[0] = response; - done.countDown(); - } - - @Override - public void onError(Exception throwable) { - responseHolder[0] = throwable.toString(); - done.countDown(); - } - }; - - final Thrift<AsyncIface> thrift = ThriftFactory.create(GoodService.AsyncIface.class) - .withMaxConnectionsPerEndpoint(1) - .useFramedTransport(true) - .buildAsync(ImmutableSet.of(new InetSocketAddress(1234))); - addTearDown(new TearDown() { - @Override public void tearDown() { - thrift.close(); - } - }); - GoodService.AsyncIface client = thrift.builder() - .blocking() - .create(); - - client.doWork(callback); - assertTrue("wasn't called back in time, callback got " + responseHolder[0], - done.await(5000, TimeUnit.MILLISECONDS)); - assertEquals(GoodService.DONE, responseHolder[0]); - } -}
