http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/8524ed95/sdks/java/core/src/test/java/org/apache/beam/sdk/metrics/MetricsMapTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/metrics/MetricsMapTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/metrics/MetricsMapTest.java new file mode 100644 index 0000000..4104f8d --- /dev/null +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/metrics/MetricsMapTest.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.beam.sdk.metrics; + +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.sameInstance; +import static org.junit.Assert.assertThat; + +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicLong; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +/** + * Tests for {@link MetricsMap}. + */ +@RunWith(JUnit4.class) +public class MetricsMapTest { + + public MetricsMap<String, AtomicLong> metricsMap = + new MetricsMap<>(new MetricsMap.Factory<String, AtomicLong>() { + @Override + public AtomicLong createInstance(String unusedKey) { + return new AtomicLong(); + } + }); + + @Test + public void testCreateSeparateInstances() { + AtomicLong foo = metricsMap.get("foo"); + AtomicLong bar = metricsMap.get("bar"); + + assertThat(foo, not(sameInstance(bar))); + } + + @Test + public void testReuseInstances() { + AtomicLong foo1 = metricsMap.get("foo"); + AtomicLong foo2 = metricsMap.get("foo"); + + assertThat(foo1, sameInstance(foo2)); + } + + @Test + public void testGet() { + assertThat(metricsMap.tryGet("foo"), nullValue(AtomicLong.class)); + + AtomicLong foo = metricsMap.get("foo"); + assertThat(metricsMap.tryGet("foo"), sameInstance(foo)); + } + + @Test + public void testGetEntries() { + AtomicLong foo = metricsMap.get("foo"); + AtomicLong bar = metricsMap.get("bar"); + assertThat(metricsMap.entries(), containsInAnyOrder( + hasEntry("foo", foo), + hasEntry("bar", bar))); + } + + private static Matcher<Map.Entry<String, AtomicLong>> hasEntry( + final String key, final AtomicLong value) { + return new TypeSafeMatcher<Entry<String, AtomicLong>>() { + + @Override + public void describeTo(Description description) { + description + .appendText("Map.Entry{key=").appendValue(key) + .appendText(", value=").appendValue(value) + .appendText("}"); + } + + @Override + protected boolean matchesSafely(Entry<String, AtomicLong> item) { + return Objects.equals(key, item.getKey()) + && Objects.equals(value, item.getValue()); + } + }; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/8524ed95/sdks/java/core/src/test/java/org/apache/beam/sdk/metrics/MetricsTest.java ---------------------------------------------------------------------- diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/metrics/MetricsTest.java b/sdks/java/core/src/test/java/org/apache/beam/sdk/metrics/MetricsTest.java new file mode 100644 index 0000000..d11b44d --- /dev/null +++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/metrics/MetricsTest.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.beam.sdk.metrics; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; + +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Test; + +/** + * Tests for {@link Metrics}. + */ +public class MetricsTest { + + private static final String NS = "test"; + private static final String NAME = "name"; + private static final MetricName METRIC_NAME = MetricName.named(NS, NAME); + + @After + public void tearDown() { + MetricsEnvironment.unsetMetricsContainer(); + } + + @Test + public void distributionWithoutContainer() { + assertNull(MetricsEnvironment.getCurrentContainer()); + // Should not fail even though there is no metrics container. + Metrics.distribution(NS, NAME).update(5L); + } + + @Test + public void counterWithoutContainer() { + assertNull(MetricsEnvironment.getCurrentContainer()); + // Should not fail even though there is no metrics container. + Counter counter = Metrics.counter(NS, NAME); + counter.inc(); + counter.inc(5L); + counter.dec(); + counter.dec(5L); + } + + @Test + public void distributionToCell() { + MetricsContainer container = new MetricsContainer("step"); + MetricsEnvironment.setMetricsContainer(container); + + Distribution distribution = Metrics.distribution(NS, NAME); + + distribution.update(5L); + + DistributionCell cell = container.getDistribution(METRIC_NAME); + assertThat(cell.getCumulative(), equalTo(DistributionData.create(5, 1, 5, 5))); + + distribution.update(36L); + assertThat(cell.getCumulative(), equalTo(DistributionData.create(41, 2, 5, 36))); + + distribution.update(1L); + assertThat(cell.getCumulative(), equalTo(DistributionData.create(42, 3, 1, 36))); + } + + @Test + public void counterToCell() { + MetricsContainer container = new MetricsContainer("step"); + MetricsEnvironment.setMetricsContainer(container); + Counter counter = Metrics.counter(NS, NAME); + CounterCell cell = container.getCounter(METRIC_NAME); + counter.inc(); + assertThat(cell.getCumulative(), CoreMatchers.equalTo(1L)); + + counter.inc(47L); + assertThat(cell.getCumulative(), CoreMatchers.equalTo(48L)); + + counter.dec(5L); + assertThat(cell.getCumulative(), CoreMatchers.equalTo(43L)); + + counter.dec(); + assertThat(cell.getCumulative(), CoreMatchers.equalTo(42L)); + } +}