|
@@ -23,9 +23,11 @@ import java.util.List;
|
|
|
|
|
|
import org.apache.commons.configuration.SubsetConfiguration;
|
|
import org.apache.commons.configuration.SubsetConfiguration;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
|
|
+
|
|
import static org.junit.Assert.*;
|
|
import static org.junit.Assert.*;
|
|
import static org.mockito.Mockito.*;
|
|
import static org.mockito.Mockito.*;
|
|
|
|
|
|
|
|
+import org.apache.hadoop.metrics2.MetricsFilter;
|
|
import org.apache.hadoop.metrics2.MetricsRecord;
|
|
import org.apache.hadoop.metrics2.MetricsRecord;
|
|
import org.apache.hadoop.metrics2.MetricsTag;
|
|
import org.apache.hadoop.metrics2.MetricsTag;
|
|
import org.apache.hadoop.metrics2.impl.ConfigBuilder;
|
|
import org.apache.hadoop.metrics2.impl.ConfigBuilder;
|
|
@@ -53,7 +55,7 @@ public class TestPatternFilter {
|
|
.add("p.include.tags", "foo:f").subset("p");
|
|
.add("p.include.tags", "foo:f").subset("p");
|
|
shouldAccept(wl, "foo");
|
|
shouldAccept(wl, "foo");
|
|
shouldAccept(wl, Arrays.asList(tag("bar", "", ""),
|
|
shouldAccept(wl, Arrays.asList(tag("bar", "", ""),
|
|
- tag("foo", "", "f")));
|
|
|
|
|
|
+ tag("foo", "", "f")), new boolean[] {false, true});
|
|
shouldAccept(wl, mockMetricsRecord("foo", Arrays.asList(
|
|
shouldAccept(wl, mockMetricsRecord("foo", Arrays.asList(
|
|
tag("bar", "", ""), tag("foo", "", "f"))));
|
|
tag("bar", "", ""), tag("foo", "", "f"))));
|
|
shouldReject(wl, "bar");
|
|
shouldReject(wl, "bar");
|
|
@@ -78,7 +80,7 @@ public class TestPatternFilter {
|
|
tag("bar", "", ""))));
|
|
tag("bar", "", ""))));
|
|
shouldReject(bl, "foo");
|
|
shouldReject(bl, "foo");
|
|
shouldReject(bl, Arrays.asList(tag("bar", "", ""),
|
|
shouldReject(bl, Arrays.asList(tag("bar", "", ""),
|
|
- tag("foo", "", "f")));
|
|
|
|
|
|
+ tag("foo", "", "f")), new boolean[] {true, false});
|
|
shouldReject(bl, mockMetricsRecord("foo", Arrays.asList(
|
|
shouldReject(bl, mockMetricsRecord("foo", Arrays.asList(
|
|
tag("bar", "", ""))));
|
|
tag("bar", "", ""))));
|
|
shouldReject(bl, mockMetricsRecord("bar", Arrays.asList(
|
|
shouldReject(bl, mockMetricsRecord("bar", Arrays.asList(
|
|
@@ -125,15 +127,61 @@ public class TestPatternFilter {
|
|
shouldAccept(c, mockMetricsRecord("foo", Arrays.asList(
|
|
shouldAccept(c, mockMetricsRecord("foo", Arrays.asList(
|
|
tag("foo", "", "f"))));
|
|
tag("foo", "", "f"))));
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
static void shouldAccept(SubsetConfiguration conf, String s) {
|
|
static void shouldAccept(SubsetConfiguration conf, String s) {
|
|
assertTrue("accepts "+ s, newGlobFilter(conf).accepts(s));
|
|
assertTrue("accepts "+ s, newGlobFilter(conf).accepts(s));
|
|
assertTrue("accepts "+ s, newRegexFilter(conf).accepts(s));
|
|
assertTrue("accepts "+ s, newRegexFilter(conf).accepts(s));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Version for one tag:
|
|
static void shouldAccept(SubsetConfiguration conf, List<MetricsTag> tags) {
|
|
static void shouldAccept(SubsetConfiguration conf, List<MetricsTag> tags) {
|
|
- assertTrue("accepts "+ tags, newGlobFilter(conf).accepts(tags));
|
|
|
|
- assertTrue("accepts "+ tags, newRegexFilter(conf).accepts(tags));
|
|
|
|
|
|
+ shouldAcceptImpl(true, conf, tags, new boolean[] {true});
|
|
|
|
+ }
|
|
|
|
+ // Version for multiple tags:
|
|
|
|
+ static void shouldAccept(SubsetConfiguration conf, List<MetricsTag> tags,
|
|
|
|
+ boolean[] expectedAcceptedSpec) {
|
|
|
|
+ shouldAcceptImpl(true, conf, tags, expectedAcceptedSpec);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Version for one tag:
|
|
|
|
+ static void shouldReject(SubsetConfiguration conf, List<MetricsTag> tags) {
|
|
|
|
+ shouldAcceptImpl(false, conf, tags, new boolean[] {false});
|
|
|
|
+ }
|
|
|
|
+ // Version for multiple tags:
|
|
|
|
+ static void shouldReject(SubsetConfiguration conf, List<MetricsTag> tags,
|
|
|
|
+ boolean[] expectedAcceptedSpec) {
|
|
|
|
+ shouldAcceptImpl(false, conf, tags, expectedAcceptedSpec);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void shouldAcceptImpl(final boolean expectAcceptList,
|
|
|
|
+ SubsetConfiguration conf, List<MetricsTag> tags, boolean[] expectedAcceptedSpec) {
|
|
|
|
+ final MetricsFilter globFilter = newGlobFilter(conf);
|
|
|
|
+ final MetricsFilter regexFilter = newRegexFilter(conf);
|
|
|
|
+
|
|
|
|
+ // Test acceptance of the tag list:
|
|
|
|
+ assertEquals("accepts "+ tags, expectAcceptList, globFilter.accepts(tags));
|
|
|
|
+ assertEquals("accepts "+ tags, expectAcceptList, regexFilter.accepts(tags));
|
|
|
|
+
|
|
|
|
+ // Test results on each of the individual tags:
|
|
|
|
+ int acceptedCount = 0;
|
|
|
|
+ for (int i=0; i<tags.size(); i++) {
|
|
|
|
+ MetricsTag tag = tags.get(i);
|
|
|
|
+ boolean actGlob = globFilter.accepts(tag);
|
|
|
|
+ boolean actRegex = regexFilter.accepts(tag);
|
|
|
|
+ assertEquals("accepts "+tag, expectedAcceptedSpec[i], actGlob);
|
|
|
|
+ // Both the filters should give the same result:
|
|
|
|
+ assertEquals(actGlob, actRegex);
|
|
|
|
+ if (actGlob) {
|
|
|
|
+ acceptedCount++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (expectAcceptList) {
|
|
|
|
+ // At least one individual tag should be accepted:
|
|
|
|
+ assertTrue("No tag of the following accepted: " + tags, acceptedCount > 0);
|
|
|
|
+ } else {
|
|
|
|
+ // At least one individual tag should be rejected:
|
|
|
|
+ assertTrue("No tag of the following rejected: " + tags, acceptedCount < tags.size());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -152,11 +200,6 @@ public class TestPatternFilter {
|
|
assertTrue("rejects "+ s, !newRegexFilter(conf).accepts(s));
|
|
assertTrue("rejects "+ s, !newRegexFilter(conf).accepts(s));
|
|
}
|
|
}
|
|
|
|
|
|
- static void shouldReject(SubsetConfiguration conf, List<MetricsTag> tags) {
|
|
|
|
- assertTrue("rejects "+ tags, !newGlobFilter(conf).accepts(tags));
|
|
|
|
- assertTrue("rejects "+ tags, !newRegexFilter(conf).accepts(tags));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Asserts that filters with the given configuration reject the given record.
|
|
* Asserts that filters with the given configuration reject the given record.
|
|
*
|
|
*
|