diff options
author | Yang Song <songy23@users.noreply.github.com> | 2018-08-13 11:36:27 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-13 11:36:27 -0700 |
commit | 775816d699bd4f02788db5609a87386940e34aff (patch) | |
tree | 021d47fb4c67d0441a5861a9e4a69b8d168e572d | |
parent | 66ae2eed952579e6982b05c0bcebe0670afb0afe (diff) | |
download | opencensus-java-775816d699bd4f02788db5609a87386940e34aff.tar.gz |
Metrics: Implement ExportComponent, make ProducerManager abstract. (#1347)
* Metrics: Implement ExportComponent, make ProducerManager abstract.
* Metrics: Add impl and impllite.
* Resolve two TODOs.
* Fix import path, leave a TODO for getMetricRegistry.
* Make NoopMetricProducerManager private. Add missing since tag.
16 files changed, 482 insertions, 74 deletions
diff --git a/api/src/main/java/io/opencensus/common/ToDoubleFunction.java b/api/src/main/java/io/opencensus/common/ToDoubleFunction.java index 479270f1..eac85793 100644 --- a/api/src/main/java/io/opencensus/common/ToDoubleFunction.java +++ b/api/src/main/java/io/opencensus/common/ToDoubleFunction.java @@ -16,10 +16,8 @@ package io.opencensus.common; -// TODO(bdrutu): Change @code to @link when metrics is moved in the API. - /** - * Represents a function that produces a double-valued result. See {@code + * Represents a function that produces a double-valued result. See {@link * io.opencensus.metrics.MetricRegistry} for an example of its use. * * <p>Note: This class is based on the java.util.ToDoubleFunction class added in Java 1.8. We cannot diff --git a/api/src/main/java/io/opencensus/common/ToLongFunction.java b/api/src/main/java/io/opencensus/common/ToLongFunction.java index 9a6e9b19..26a27b18 100644 --- a/api/src/main/java/io/opencensus/common/ToLongFunction.java +++ b/api/src/main/java/io/opencensus/common/ToLongFunction.java @@ -16,10 +16,8 @@ package io.opencensus.common; -// TODO(bdrutu): Change @code to @link when metrics is moved in the API. - /** - * Represents a function that produces a long-valued result. See {@code + * Represents a function that produces a long-valued result. See {@link * io.opencensus.metrics.MetricRegistry} for an example of its use. * * <p>Note: This class is based on the java.util.ToLongFunction class added in Java 1.8. We cannot diff --git a/api/src/main/java/io/opencensus/metrics/export/ExportComponent.java b/api/src/main/java/io/opencensus/metrics/export/ExportComponent.java index f1511543..bab65d13 100644 --- a/api/src/main/java/io/opencensus/metrics/export/ExportComponent.java +++ b/api/src/main/java/io/opencensus/metrics/export/ExportComponent.java @@ -50,7 +50,7 @@ public abstract class ExportComponent { private static final class NoopExportComponent extends ExportComponent { private static final MetricProducerManager METRIC_PRODUCER_MANAGER = - new MetricProducerManager(); + MetricProducerManager.newNoopMetricProducerManager(); @Override public MetricProducerManager getMetricProducerManager() { diff --git a/api/src/main/java/io/opencensus/metrics/export/MetricProducerManager.java b/api/src/main/java/io/opencensus/metrics/export/MetricProducerManager.java index 9600a023..c5acf174 100644 --- a/api/src/main/java/io/opencensus/metrics/export/MetricProducerManager.java +++ b/api/src/main/java/io/opencensus/metrics/export/MetricProducerManager.java @@ -20,7 +20,6 @@ import io.opencensus.common.ExperimentalApi; import io.opencensus.internal.Utils; import io.opencensus.metrics.MetricProducer; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.Set; import javax.annotation.concurrent.ThreadSafe; @@ -32,9 +31,7 @@ import javax.annotation.concurrent.ThreadSafe; */ @ExperimentalApi @ThreadSafe -public final class MetricProducerManager { - private volatile Set<MetricProducer> metricProducers = - Collections.unmodifiableSet(new LinkedHashSet<MetricProducer>()); +public abstract class MetricProducerManager { /** * Adds the {@link MetricProducer} to the manager if it is not already present. @@ -42,34 +39,15 @@ public final class MetricProducerManager { * @param metricProducer the {@code MetricProducer} to be added to the manager. * @since 0.16 */ - public synchronized void add(MetricProducer metricProducer) { - Utils.checkNotNull(metricProducer, "metricProducer"); - // Updating the set of MetricProducers happens under a lock to avoid multiple add or remove - // operations to happen in the same time. - Set<MetricProducer> newMetricProducers = new LinkedHashSet<MetricProducer>(metricProducers); - if (!newMetricProducers.add(metricProducer)) { - // The element already present, no need to update the current set of MetricProducers. - return; - } - metricProducers = Collections.unmodifiableSet(newMetricProducers); - } + public abstract void add(MetricProducer metricProducer); /** * Removes the {@link MetricProducer} to the manager if it is present. * * @param metricProducer the {@code MetricProducer} to be removed from the manager. + * @since 0.16 */ - public synchronized void remove(MetricProducer metricProducer) { - Utils.checkNotNull(metricProducer, "metricProducer"); - // Updating the set of MetricProducers happens under a lock to avoid multiple add or remove - // operations to happen in the same time. - Set<MetricProducer> newMetricProducers = new LinkedHashSet<MetricProducer>(metricProducers); - if (!newMetricProducers.remove(metricProducer)) { - // The element not present, no need to update the current set of MetricProducers. - return; - } - metricProducers = Collections.unmodifiableSet(newMetricProducers); - } + public abstract void remove(MetricProducer metricProducer); /** * Returns all registered {@link MetricProducer}s that should be exported. @@ -78,12 +56,34 @@ public final class MetricProducerManager { * {@code MetricProducer} registered with the {@code MetricProducerManager}. * * @return all registered {@code MetricProducer}s that should be exported. + * @since 0.16 */ - public Set<MetricProducer> getAllMetricProducer() { - return metricProducers; + public abstract Set<MetricProducer> getAllMetricProducer(); + + /** + * Returns a no-op implementation for {@link MetricProducerManager}. + * + * @return a no-op implementation for {@code MetricProducerManager}. + */ + static MetricProducerManager newNoopMetricProducerManager() { + return new NoopMetricProducerManager(); } - // Package protected to allow us to possibly change this to an abstract class in the future. This - // ensures that nobody can create an instance of this class except ExportComponent. - MetricProducerManager() {} + private static final class NoopMetricProducerManager extends MetricProducerManager { + + @Override + public void add(MetricProducer metricProducer) { + Utils.checkNotNull(metricProducer, "metricProducer"); + } + + @Override + public void remove(MetricProducer metricProducer) { + Utils.checkNotNull(metricProducer, "metricProducer"); + } + + @Override + public Set<MetricProducer> getAllMetricProducer() { + return Collections.emptySet(); + } + } } diff --git a/api/src/test/java/io/opencensus/metrics/export/MetricProducerManagerTest.java b/api/src/test/java/io/opencensus/metrics/export/MetricProducerManagerTest.java index b79de213..07854927 100644 --- a/api/src/test/java/io/opencensus/metrics/export/MetricProducerManagerTest.java +++ b/api/src/test/java/io/opencensus/metrics/export/MetricProducerManagerTest.java @@ -19,7 +19,6 @@ package io.opencensus.metrics.export; import static com.google.common.truth.Truth.assertThat; import io.opencensus.metrics.MetricProducer; -import java.util.Set; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -32,9 +31,9 @@ import org.mockito.MockitoAnnotations; /** Unit tests for {@link MetricProducerManager}. */ @RunWith(JUnit4.class) public class MetricProducerManagerTest { - private final MetricProducerManager metricProducerManager = new MetricProducerManager(); + private final MetricProducerManager metricProducerManager = + MetricProducerManager.newNoopMetricProducerManager(); @Mock private MetricProducer metricProducer; - @Mock private MetricProducer metricProducerOther; @Rule public final ExpectedException thrown = ExpectedException.none(); @@ -52,34 +51,13 @@ public class MetricProducerManagerTest { @Test public void add() { metricProducerManager.add(metricProducer); - assertThat(metricProducerManager.getAllMetricProducer()).containsExactly(metricProducer); - } - - @Test - public void add_DuplicateElement() { - metricProducerManager.add(metricProducer); - Set<MetricProducer> metricProducerSet = metricProducerManager.getAllMetricProducer(); - assertThat(metricProducerSet).containsExactly(metricProducer); - metricProducerManager.add(metricProducer); - // Returns the same object. - assertThat(metricProducerManager.getAllMetricProducer()).isSameAs(metricProducerSet); - } - - @Test - public void add_MultipleElements() { - metricProducerManager.add(metricProducer); - Set<MetricProducer> metricProducerSet = metricProducerManager.getAllMetricProducer(); - assertThat(metricProducerSet).containsExactly(metricProducer); - metricProducerManager.add(metricProducerOther); - // Returns the same object. - assertThat(metricProducerManager.getAllMetricProducer()) - .containsExactly(metricProducer, metricProducerOther); + assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); } @Test public void addAndRemove() { metricProducerManager.add(metricProducer); - assertThat(metricProducerManager.getAllMetricProducer()).containsExactly(metricProducer); + assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); metricProducerManager.remove(metricProducer); assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); } @@ -97,16 +75,6 @@ public class MetricProducerManagerTest { } @Test - public void remove_NotPresent() { - metricProducerManager.add(metricProducer); - Set<MetricProducer> metricProducerSet = metricProducerManager.getAllMetricProducer(); - assertThat(metricProducerSet).containsExactly(metricProducer); - metricProducerManager.remove(metricProducerOther); - // Returns the same object. - assertThat(metricProducerManager.getAllMetricProducer()).isSameAs(metricProducerSet); - } - - @Test public void getAllMetricProducer_empty() { assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); } diff --git a/buildscripts/import-control.xml b/buildscripts/import-control.xml index 66c561a4..7c8251bb 100644 --- a/buildscripts/import-control.xml +++ b/buildscripts/import-control.xml @@ -46,6 +46,7 @@ General guidelines on imports: <subpackage name="stats"> <allow pkg="io.opencensus.common"/> <allow pkg="io.opencensus.internal"/> + <allow pkg="io.opencensus.metrics"/> <allow pkg="io.opencensus.stats"/> <allow pkg="io.opencensus.tags"/> </subpackage> @@ -178,6 +179,7 @@ General guidelines on imports: <allow pkg="io.opencensus.common"/> <allow pkg="io.opencensus.impl"/> <allow pkg="io.opencensus.implcore"/> + <allow pkg="io.opencensus.metrics"/> <allow pkg="io.opencensus.stats"/> <allow pkg="io.opencensus.tags"/> <allow pkg="io.opencensus.trace"/> @@ -186,6 +188,7 @@ General guidelines on imports: <allow pkg="io.opencensus.common"/> <allow pkg="io.opencensus.implcore"/> <allow pkg="io.opencensus.impllite"/> + <allow pkg="io.opencensus.metrics"/> <allow pkg="io.opencensus.stats"/> <allow pkg="io.opencensus.tags"/> <allow pkg="io.opencensus.trace"/> diff --git a/impl/src/main/java/io/opencensus/impl/metrics/MetricsComponentImpl.java b/impl/src/main/java/io/opencensus/impl/metrics/MetricsComponentImpl.java new file mode 100644 index 00000000..bd8b6154 --- /dev/null +++ b/impl/src/main/java/io/opencensus/impl/metrics/MetricsComponentImpl.java @@ -0,0 +1,23 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.impl.metrics; + +import io.opencensus.implcore.metrics.MetricsComponentImplBase; +import io.opencensus.metrics.MetricsComponent; + +/** Implementation of {@link MetricsComponent}. */ +public final class MetricsComponentImpl extends MetricsComponentImplBase {} diff --git a/impl/src/test/java/io/opencensus/impl/metrics/MetricsTest.java b/impl/src/test/java/io/opencensus/impl/metrics/MetricsTest.java new file mode 100644 index 00000000..d27fa83d --- /dev/null +++ b/impl/src/test/java/io/opencensus/impl/metrics/MetricsTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.impl.metrics; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.implcore.metrics.export.ExportComponentImpl; +import io.opencensus.metrics.Metrics; +import io.opencensus.metrics.MetricsComponent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Test for accessing the {@link MetricsComponent} through the {@link Metrics} class. */ +@RunWith(JUnit4.class) +public class MetricsTest { + + @Test + public void getExportComponent() { + assertThat(Metrics.getExportComponent()).isInstanceOf(ExportComponentImpl.class); + } +} diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java new file mode 100644 index 00000000..312b702e --- /dev/null +++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java @@ -0,0 +1,42 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.implcore.metrics; + +import io.opencensus.implcore.metrics.export.ExportComponentImpl; +import io.opencensus.metrics.MetricRegistry; +import io.opencensus.metrics.MetricsComponent; + +/** Implementation of {@link MetricsComponent}. */ +public class MetricsComponentImplBase extends MetricsComponent { + + private final ExportComponentImpl exportComponent; + + @Override + public ExportComponentImpl getExportComponent() { + return exportComponent; + } + + @Override + public MetricRegistry getMetricRegistry() { + // TODO: implement this method + throw new UnsupportedOperationException("not implemented"); + } + + public MetricsComponentImplBase() { + exportComponent = new ExportComponentImpl(); + } +} diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/export/ExportComponentImpl.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/export/ExportComponentImpl.java new file mode 100644 index 00000000..173c3aec --- /dev/null +++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/export/ExportComponentImpl.java @@ -0,0 +1,31 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.implcore.metrics.export; + +import io.opencensus.metrics.export.ExportComponent; +import io.opencensus.metrics.export.MetricProducerManager; + +/** Implementation of {@link ExportComponent}. */ +public final class ExportComponentImpl extends ExportComponent { + + private final MetricProducerManager metricProducerManager = new MetricProducerManagerImpl(); + + @Override + public MetricProducerManager getMetricProducerManager() { + return metricProducerManager; + } +} diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImpl.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImpl.java new file mode 100644 index 00000000..fc9db5c1 --- /dev/null +++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImpl.java @@ -0,0 +1,64 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.implcore.metrics.export; + +import com.google.common.base.Preconditions; +import io.opencensus.metrics.MetricProducer; +import io.opencensus.metrics.export.MetricProducerManager; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; +import javax.annotation.concurrent.ThreadSafe; + +/** Implementation of {@link MetricProducerManager}. */ +@ThreadSafe +public final class MetricProducerManagerImpl extends MetricProducerManager { + + private volatile Set<MetricProducer> metricProducers = + Collections.unmodifiableSet(new LinkedHashSet<MetricProducer>()); + + @Override + public synchronized void add(MetricProducer metricProducer) { + Preconditions.checkNotNull(metricProducer, "metricProducer"); + // Updating the set of MetricProducers happens under a lock to avoid multiple add or remove + // operations to happen in the same time. + Set<MetricProducer> newMetricProducers = new LinkedHashSet<MetricProducer>(metricProducers); + if (!newMetricProducers.add(metricProducer)) { + // The element already present, no need to update the current set of MetricProducers. + return; + } + metricProducers = Collections.unmodifiableSet(newMetricProducers); + } + + @Override + public synchronized void remove(MetricProducer metricProducer) { + Preconditions.checkNotNull(metricProducer, "metricProducer"); + // Updating the set of MetricProducers happens under a lock to avoid multiple add or remove + // operations to happen in the same time. + Set<MetricProducer> newMetricProducers = new LinkedHashSet<MetricProducer>(metricProducers); + if (!newMetricProducers.remove(metricProducer)) { + // The element not present, no need to update the current set of MetricProducers. + return; + } + metricProducers = Collections.unmodifiableSet(newMetricProducers); + } + + @Override + public Set<MetricProducer> getAllMetricProducer() { + return metricProducers; + } +} diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java new file mode 100644 index 00000000..a86a53d9 --- /dev/null +++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.implcore.metrics; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.implcore.metrics.export.ExportComponentImpl; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link MetricsComponentImplBase}. */ +@RunWith(JUnit4.class) +public class MetricsComponentImplBaseTest { + + @Test + public void getExportComponent() { + MetricsComponentImplBase metricsComponentImplBase = new MetricsComponentImplBase(); + assertThat(metricsComponentImplBase.getExportComponent()) + .isInstanceOf(ExportComponentImpl.class); + } +} diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/export/ExportComponentImplTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/export/ExportComponentImplTest.java new file mode 100644 index 00000000..fb91641c --- /dev/null +++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/export/ExportComponentImplTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.implcore.metrics.export; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link ExportComponentImpl}. */ +@RunWith(JUnit4.class) +public class ExportComponentImplTest { + + @Test + public void getMetricProducerManager() { + ExportComponentImpl exportComponent = new ExportComponentImpl(); + assertThat(exportComponent.getMetricProducerManager()) + .isInstanceOf(MetricProducerManagerImpl.class); + } +} diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImplTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImplTest.java new file mode 100644 index 00000000..7f2ec153 --- /dev/null +++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImplTest.java @@ -0,0 +1,115 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.implcore.metrics.export; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.metrics.MetricProducer; +import io.opencensus.metrics.export.MetricProducerManager; +import java.util.Set; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** Unit tests for {@link MetricProducerManagerImpl}. */ +@RunWith(JUnit4.class) +public class MetricProducerManagerImplTest { + + private final MetricProducerManager metricProducerManager = new MetricProducerManagerImpl(); + @Mock private MetricProducer metricProducer; + @Mock private MetricProducer metricProducerOther; + + @Rule public final ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void add_DisallowsNull() { + thrown.expect(NullPointerException.class); + metricProducerManager.add(null); + } + + @Test + public void add() { + metricProducerManager.add(metricProducer); + assertThat(metricProducerManager.getAllMetricProducer()).containsExactly(metricProducer); + } + + @Test + public void add_DuplicateElement() { + metricProducerManager.add(metricProducer); + Set<MetricProducer> metricProducerSet = metricProducerManager.getAllMetricProducer(); + assertThat(metricProducerSet).containsExactly(metricProducer); + metricProducerManager.add(metricProducer); + // Returns the same object. + assertThat(metricProducerManager.getAllMetricProducer()).isSameAs(metricProducerSet); + } + + @Test + public void add_MultipleElements() { + metricProducerManager.add(metricProducer); + Set<MetricProducer> metricProducerSet = metricProducerManager.getAllMetricProducer(); + assertThat(metricProducerSet).containsExactly(metricProducer); + metricProducerManager.add(metricProducerOther); + // Returns the same object. + assertThat(metricProducerManager.getAllMetricProducer()) + .containsExactly(metricProducer, metricProducerOther); + } + + @Test + public void addAndRemove() { + metricProducerManager.add(metricProducer); + assertThat(metricProducerManager.getAllMetricProducer()).containsExactly(metricProducer); + metricProducerManager.remove(metricProducer); + assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); + } + + @Test + public void remove_DisallowsNull() { + thrown.expect(NullPointerException.class); + metricProducerManager.remove(null); + } + + @Test + public void remove_FromEmpty() { + metricProducerManager.remove(metricProducer); + assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); + } + + @Test + public void remove_NotPresent() { + metricProducerManager.add(metricProducer); + Set<MetricProducer> metricProducerSet = metricProducerManager.getAllMetricProducer(); + assertThat(metricProducerSet).containsExactly(metricProducer); + metricProducerManager.remove(metricProducerOther); + // Returns the same object. + assertThat(metricProducerManager.getAllMetricProducer()).isSameAs(metricProducerSet); + } + + @Test + public void getAllMetricProducer_empty() { + assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); + } +} diff --git a/impl_lite/src/main/java/io/opencensus/impllite/metrics/MetricsComponentImplLite.java b/impl_lite/src/main/java/io/opencensus/impllite/metrics/MetricsComponentImplLite.java new file mode 100644 index 00000000..e755088d --- /dev/null +++ b/impl_lite/src/main/java/io/opencensus/impllite/metrics/MetricsComponentImplLite.java @@ -0,0 +1,23 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.impllite.metrics; + +import io.opencensus.implcore.metrics.MetricsComponentImplBase; +import io.opencensus.metrics.MetricsComponent; + +/** Android-compatible implementation of {@link MetricsComponent}. */ +public final class MetricsComponentImplLite extends MetricsComponentImplBase {} diff --git a/impl_lite/src/test/java/io/opencensus/impllite/metrics/MetricsTest.java b/impl_lite/src/test/java/io/opencensus/impllite/metrics/MetricsTest.java new file mode 100644 index 00000000..ab8889a9 --- /dev/null +++ b/impl_lite/src/test/java/io/opencensus/impllite/metrics/MetricsTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * 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 io.opencensus.impllite.metrics; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.implcore.metrics.export.ExportComponentImpl; +import io.opencensus.metrics.Metrics; +import io.opencensus.metrics.MetricsComponent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Test for accessing the {@link MetricsComponent} through the {@link Metrics} class. */ +@RunWith(JUnit4.class) +public class MetricsTest { + + @Test + public void getExportComponent() { + assertThat(Metrics.getExportComponent()).isInstanceOf(ExportComponentImpl.class); + } +} |