-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding TestScope interface and factory method
- Loading branch information
Showing
4 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package com.uber.m3.tally; | ||
|
||
import com.uber.m3.util.Duration; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* TestScope is a metrics collector that has no reporting, ensuring that | ||
* all emitted values have a given prefix or set of tags. | ||
*/ | ||
interface TestScope extends Scope { | ||
|
||
/** | ||
* Creates a new TestScope that adds the ability to take snapshots of | ||
* metrics emitted to it. | ||
*/ | ||
static TestScope create() { | ||
return (TestScope) new RootScopeBuilder() | ||
.reporter(new NullStatsReporter()) | ||
.reportEvery(Duration.ZERO); | ||
} | ||
|
||
/** | ||
* Creates a new TestScope with given prefix/tags that adds the ability to | ||
* take snapshots of metrics emitted to it. | ||
*/ | ||
static TestScope create(String prefix, Map<String, String> tags) { | ||
return (TestScope) new RootScopeBuilder() | ||
.prefix(prefix) | ||
.tags(tags) | ||
.reporter(new NullStatsReporter()) | ||
.reportEvery(Duration.ZERO); | ||
} | ||
|
||
/** | ||
* Snapshot returns a copy of all values since the last report execution | ||
* This is an expensive operation and should only be used for testing purposes. | ||
*/ | ||
Snapshot snapshot(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package com.uber.m3.tally; | ||
|
||
import com.uber.m3.util.ImmutableMap; | ||
import org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.hamcrest.CoreMatchers.instanceOf; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class TestScopeTest { | ||
|
||
@Test | ||
public void getInstance() { | ||
TestScope testScope = TestScope.create(); | ||
assertNotNull(testScope); | ||
assertThat(testScope, instanceOf(Scope.class)); | ||
assertThat(testScope, instanceOf(ScopeImpl.class)); | ||
|
||
assertNotNull(testScope.capabilities()); | ||
assertFalse(testScope.capabilities().reporting()); | ||
assertFalse(testScope.capabilities().tagging()); | ||
} | ||
|
||
@Test | ||
public void prefixTags() { | ||
Map<String, String> tags = ImmutableMap.of("key", "value"); | ||
TestScope testScope = TestScope.create("prefix", tags); | ||
testScope.counter("counter").inc(1); | ||
|
||
Snapshot snapshot = testScope.snapshot(); | ||
assertNotNull(snapshot); | ||
|
||
Map<String, CounterSnapshot> counters = snapshot.counters(); | ||
assertNotNull(counters); | ||
assertEquals(1, counters.size()); | ||
assertNotNull(counters.get("prefix.counter+key=value")); | ||
assertEquals("prefix.counter", counters.get("prefix.counter+key=value").name()); | ||
assertEquals(tags, counters.get("prefix.counter+key=value").tags()); | ||
assertEquals(1, counters.get("prefix.counter+key=value").value()); | ||
} | ||
} |