YARN-8943. Upgrade JUnit from 4 to 5 in hadoop-yarn-api.
Signed-off-by: Takanobu Asanuma <tasanuma@apache.org>
This commit is contained in:
parent
b1120d27ab
commit
c65218ee8e
|
@ -72,6 +72,27 @@
|
|||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Only required to run tests in an IDE that bundles an older version -->
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-launcher</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
|
|
|
@ -17,27 +17,29 @@
|
|||
*/
|
||||
package org.apache.hadoop.yarn.api.records;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* The class to test {@link Resource}.
|
||||
*/
|
||||
public class TestResource {
|
||||
class TestResource {
|
||||
|
||||
@Test
|
||||
public void testCastToIntSafely() {
|
||||
void testCastToIntSafely() {
|
||||
assertEquals(0, Resource.castToIntSafely(0));
|
||||
assertEquals(1, Resource.castToIntSafely(1));
|
||||
assertEquals(Integer.MAX_VALUE,
|
||||
Resource.castToIntSafely(Integer.MAX_VALUE));
|
||||
|
||||
assertEquals("Cast to Integer.MAX_VALUE if the long is greater than "
|
||||
+ "Integer.MAX_VALUE", Integer.MAX_VALUE,
|
||||
Resource.castToIntSafely(Integer.MAX_VALUE + 1L));
|
||||
assertEquals("Cast to Integer.MAX_VALUE if the long is greater than "
|
||||
+ "Integer.MAX_VALUE", Integer.MAX_VALUE,
|
||||
Resource.castToIntSafely(Long.MAX_VALUE));
|
||||
assertEquals(Integer.MAX_VALUE,
|
||||
Resource.castToIntSafely(Integer.MAX_VALUE + 1L),
|
||||
"Cast to Integer.MAX_VALUE if the long is greater than "
|
||||
+ "Integer.MAX_VALUE");
|
||||
assertEquals(Integer.MAX_VALUE,
|
||||
Resource.castToIntSafely(Long.MAX_VALUE),
|
||||
"Cast to Integer.MAX_VALUE if the long is greater than "
|
||||
+ "Integer.MAX_VALUE");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,19 +17,19 @@
|
|||
*/
|
||||
package org.apache.hadoop.yarn.api.records;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.apache.hadoop.conf.Configuration;
|
||||
import org.apache.hadoop.fs.Path;
|
||||
import org.apache.hadoop.yarn.conf.YarnConfiguration;
|
||||
import org.apache.hadoop.yarn.factories.RecordFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Test for the URL class. */
|
||||
public class TestURL {
|
||||
class TestURL {
|
||||
|
||||
@Test
|
||||
public void testConversion() throws Exception {
|
||||
void testConversion() throws Exception {
|
||||
Configuration conf = new Configuration();
|
||||
conf.set(YarnConfiguration.IPC_RECORD_FACTORY_CLASS,
|
||||
RecordFactoryForTest.class.getName());
|
||||
|
|
|
@ -18,18 +18,18 @@
|
|||
|
||||
package org.apache.hadoop.yarn.api.records.timelineservice;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Various tests for the ApplicationEntity class.
|
||||
*
|
||||
*/
|
||||
public class TestApplicationEntity {
|
||||
class TestApplicationEntity {
|
||||
|
||||
@Test
|
||||
public void testIsApplicationEntity() {
|
||||
void testIsApplicationEntity() {
|
||||
TimelineEntity te = new TimelineEntity();
|
||||
te.setType(TimelineEntityType.YARN_APPLICATION.toString());
|
||||
assertTrue(ApplicationEntity.isApplicationEntity(te));
|
||||
|
@ -43,7 +43,7 @@ public class TestApplicationEntity {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testGetApplicationEvent() {
|
||||
void testGetApplicationEvent() {
|
||||
TimelineEntity te = null;
|
||||
TimelineEvent tEvent = ApplicationEntity.getApplicationEvent(te,
|
||||
"no event");
|
||||
|
|
|
@ -17,20 +17,20 @@
|
|||
*/
|
||||
package org.apache.hadoop.yarn.api.records.timelineservice;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.hadoop.yarn.api.records.timelineservice.TimelineMetric.Type;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TestTimelineMetric {
|
||||
class TestTimelineMetric {
|
||||
|
||||
@Test
|
||||
public void testTimelineMetricAggregation() {
|
||||
void testTimelineMetricAggregation() {
|
||||
long ts = System.currentTimeMillis();
|
||||
// single_value metric add against null metric
|
||||
TimelineMetric m1 = getSingleValueMetric("MEGA_BYTES_MILLIS",
|
||||
|
|
|
@ -49,17 +49,19 @@ import static org.apache.hadoop.yarn.api.resource.PlacementConstraints.targetIn;
|
|||
import static org.apache.hadoop.yarn.api.resource.PlacementConstraints.targetNodeAttribute;
|
||||
import static org.apache.hadoop.yarn.api.resource.PlacementConstraints.targetNotIn;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Class to test placement constraint parser.
|
||||
*/
|
||||
public class TestPlacementConstraintParser {
|
||||
class TestPlacementConstraintParser {
|
||||
|
||||
@Test
|
||||
public void testTargetExpressionParser()
|
||||
throws PlacementConstraintParseException {
|
||||
void testTargetExpressionParser() throws PlacementConstraintParseException {
|
||||
String expressionStr;
|
||||
ConstraintParser parser;
|
||||
AbstractConstraint constraint;
|
||||
|
@ -70,22 +72,22 @@ public class TestPlacementConstraintParser {
|
|||
expressionStr = "NOTIN, NODE, foo";
|
||||
parser = new TargetConstraintParser(expressionStr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof SingleConstraint);
|
||||
assertTrue(constraint instanceof SingleConstraint);
|
||||
single = (SingleConstraint) constraint;
|
||||
Assert.assertEquals("node", single.getScope());
|
||||
Assert.assertEquals(0, single.getMinCardinality());
|
||||
Assert.assertEquals(0, single.getMaxCardinality());
|
||||
assertEquals("node", single.getScope());
|
||||
assertEquals(0, single.getMinCardinality());
|
||||
assertEquals(0, single.getMaxCardinality());
|
||||
verifyConstraintToString(expressionStr, constraint);
|
||||
|
||||
// lower cases is also valid
|
||||
expressionStr = "notin, node, foo";
|
||||
parser = new TargetConstraintParser(expressionStr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof SingleConstraint);
|
||||
assertTrue(constraint instanceof SingleConstraint);
|
||||
single = (SingleConstraint) constraint;
|
||||
Assert.assertEquals("node", single.getScope());
|
||||
Assert.assertEquals(0, single.getMinCardinality());
|
||||
Assert.assertEquals(0, single.getMaxCardinality());
|
||||
assertEquals("node", single.getScope());
|
||||
assertEquals(0, single.getMinCardinality());
|
||||
assertEquals(0, single.getMaxCardinality());
|
||||
verifyConstraintToString(expressionStr, constraint);
|
||||
|
||||
// Affinity with single target tag
|
||||
|
@ -93,11 +95,11 @@ public class TestPlacementConstraintParser {
|
|||
expressionStr = "IN, NODE, foo";
|
||||
parser = new TargetConstraintParser(expressionStr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof SingleConstraint);
|
||||
assertTrue(constraint instanceof SingleConstraint);
|
||||
single = (SingleConstraint) constraint;
|
||||
Assert.assertEquals("node", single.getScope());
|
||||
Assert.assertEquals(1, single.getMinCardinality());
|
||||
Assert.assertEquals(Integer.MAX_VALUE, single.getMaxCardinality());
|
||||
assertEquals("node", single.getScope());
|
||||
assertEquals(1, single.getMinCardinality());
|
||||
assertEquals(Integer.MAX_VALUE, single.getMaxCardinality());
|
||||
verifyConstraintToString(expressionStr, constraint);
|
||||
|
||||
// Anti-affinity with multiple target tags
|
||||
|
@ -105,17 +107,17 @@ public class TestPlacementConstraintParser {
|
|||
expressionStr = "NOTIN, NODE, foo, bar, exp";
|
||||
parser = new TargetConstraintParser(expressionStr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof SingleConstraint);
|
||||
assertTrue(constraint instanceof SingleConstraint);
|
||||
single = (SingleConstraint) constraint;
|
||||
Assert.assertEquals("node", single.getScope());
|
||||
Assert.assertEquals(0, single.getMinCardinality());
|
||||
Assert.assertEquals(0, single.getMaxCardinality());
|
||||
Assert.assertEquals(3, single.getTargetExpressions().size());
|
||||
assertEquals("node", single.getScope());
|
||||
assertEquals(0, single.getMinCardinality());
|
||||
assertEquals(0, single.getMaxCardinality());
|
||||
assertEquals(3, single.getTargetExpressions().size());
|
||||
Set<TargetExpression> expectedTargetExpressions = Sets.newHashSet(
|
||||
PlacementTargets.allocationTag("foo"),
|
||||
PlacementTargets.allocationTag("bar"),
|
||||
PlacementTargets.allocationTag("exp"));
|
||||
Assert.assertTrue(Sets.difference(expectedTargetExpressions,
|
||||
assertTrue(Sets.difference(expectedTargetExpressions,
|
||||
single.getTargetExpressions()).isEmpty());
|
||||
verifyConstraintToString(expressionStr, constraint);
|
||||
|
||||
|
@ -124,13 +126,13 @@ public class TestPlacementConstraintParser {
|
|||
try {
|
||||
parser.parse();
|
||||
} catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof PlacementConstraintParseException);
|
||||
Assert.assertTrue(e.getMessage().contains("expecting in or notin"));
|
||||
assertTrue(e instanceof PlacementConstraintParseException);
|
||||
assertTrue(e.getMessage().contains("expecting in or notin"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCardinalityConstraintParser()
|
||||
void testCardinalityConstraintParser()
|
||||
throws PlacementConstraintParseException {
|
||||
String expressionExpr;
|
||||
ConstraintParser parser;
|
||||
|
@ -141,34 +143,34 @@ public class TestPlacementConstraintParser {
|
|||
expressionExpr = "cardinality, NODE, foo, 0, 1";
|
||||
parser = new CardinalityConstraintParser(expressionExpr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof SingleConstraint);
|
||||
assertTrue(constraint instanceof SingleConstraint);
|
||||
single = (SingleConstraint) constraint;
|
||||
Assert.assertEquals("node", single.getScope());
|
||||
Assert.assertEquals(0, single.getMinCardinality());
|
||||
Assert.assertEquals(1, single.getMaxCardinality());
|
||||
Assert.assertEquals(1, single.getTargetExpressions().size());
|
||||
assertEquals("node", single.getScope());
|
||||
assertEquals(0, single.getMinCardinality());
|
||||
assertEquals(1, single.getMaxCardinality());
|
||||
assertEquals(1, single.getTargetExpressions().size());
|
||||
TargetExpression exp =
|
||||
single.getTargetExpressions().iterator().next();
|
||||
Assert.assertEquals("ALLOCATION_TAG", exp.getTargetType().toString());
|
||||
Assert.assertEquals(1, exp.getTargetValues().size());
|
||||
Assert.assertEquals("foo", exp.getTargetValues().iterator().next());
|
||||
assertEquals("ALLOCATION_TAG", exp.getTargetType().toString());
|
||||
assertEquals(1, exp.getTargetValues().size());
|
||||
assertEquals("foo", exp.getTargetValues().iterator().next());
|
||||
verifyConstraintToString(expressionExpr, constraint);
|
||||
|
||||
// cardinality,NODE,foo,bar,moo,0,1
|
||||
expressionExpr = "cardinality,RACK,foo,bar,moo,0,1";
|
||||
parser = new CardinalityConstraintParser(expressionExpr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof SingleConstraint);
|
||||
assertTrue(constraint instanceof SingleConstraint);
|
||||
single = (SingleConstraint) constraint;
|
||||
Assert.assertEquals("rack", single.getScope());
|
||||
Assert.assertEquals(0, single.getMinCardinality());
|
||||
Assert.assertEquals(1, single.getMaxCardinality());
|
||||
Assert.assertEquals(3, single.getTargetExpressions().size());
|
||||
assertEquals("rack", single.getScope());
|
||||
assertEquals(0, single.getMinCardinality());
|
||||
assertEquals(1, single.getMaxCardinality());
|
||||
assertEquals(3, single.getTargetExpressions().size());
|
||||
Set<TargetExpression> expectedTargetExpressions = Sets.newHashSet(
|
||||
PlacementTargets.allocationTag("foo"),
|
||||
PlacementTargets.allocationTag("bar"),
|
||||
PlacementTargets.allocationTag("moo"));
|
||||
Assert.assertTrue(Sets.difference(expectedTargetExpressions,
|
||||
assertTrue(Sets.difference(expectedTargetExpressions,
|
||||
single.getTargetExpressions()).isEmpty());
|
||||
verifyConstraintToString(expressionExpr, constraint);
|
||||
|
||||
|
@ -177,9 +179,9 @@ public class TestPlacementConstraintParser {
|
|||
parser = new CardinalityConstraintParser(
|
||||
"cardinality,NOWHERE,foo,bar,moo,0,1");
|
||||
parser.parse();
|
||||
Assert.fail("Expecting a parsing failure!");
|
||||
fail("Expecting a parsing failure!");
|
||||
} catch (PlacementConstraintParseException e) {
|
||||
Assert.assertTrue(e.getMessage()
|
||||
assertTrue(e.getMessage()
|
||||
.contains("expecting scope to node or rack, but met NOWHERE"));
|
||||
}
|
||||
|
||||
|
@ -188,15 +190,15 @@ public class TestPlacementConstraintParser {
|
|||
parser = new CardinalityConstraintParser(
|
||||
"cardinality,NODE,0,1");
|
||||
parser.parse();
|
||||
Assert.fail("Expecting a parsing failure!");
|
||||
fail("Expecting a parsing failure!");
|
||||
} catch (PlacementConstraintParseException e) {
|
||||
Assert.assertTrue(e.getMessage()
|
||||
assertTrue(e.getMessage()
|
||||
.contains("at least 5 elements, but only 4 is given"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAndConstraintParser()
|
||||
void testAndConstraintParser()
|
||||
throws PlacementConstraintParseException {
|
||||
String expressionExpr;
|
||||
ConstraintParser parser;
|
||||
|
@ -206,33 +208,33 @@ public class TestPlacementConstraintParser {
|
|||
expressionExpr = "AND(NOTIN,NODE,foo:NOTIN,NODE,bar)";
|
||||
parser = new ConjunctionConstraintParser(expressionExpr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof And);
|
||||
assertTrue(constraint instanceof And);
|
||||
and = (And) constraint;
|
||||
Assert.assertEquals(2, and.getChildren().size());
|
||||
assertEquals(2, and.getChildren().size());
|
||||
verifyConstraintToString(expressionExpr, constraint);
|
||||
|
||||
expressionExpr = "AND(NOTIN,NODE,foo:cardinality,NODE,foo,0,1)";
|
||||
parser = new ConjunctionConstraintParser(expressionExpr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof And);
|
||||
Assert.assertEquals(2, and.getChildren().size());
|
||||
assertTrue(constraint instanceof And);
|
||||
assertEquals(2, and.getChildren().size());
|
||||
verifyConstraintToString(expressionExpr, constraint);
|
||||
|
||||
expressionExpr =
|
||||
"AND(NOTIN,NODE,foo:AND(NOTIN,NODE,foo:cardinality,NODE,foo,0,1))";
|
||||
parser = new ConjunctionConstraintParser(expressionExpr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof And);
|
||||
assertTrue(constraint instanceof And);
|
||||
and = (And) constraint;
|
||||
Assert.assertTrue(and.getChildren().get(0) instanceof SingleConstraint);
|
||||
Assert.assertTrue(and.getChildren().get(1) instanceof And);
|
||||
assertTrue(and.getChildren().get(0) instanceof SingleConstraint);
|
||||
assertTrue(and.getChildren().get(1) instanceof And);
|
||||
and = (And) and.getChildren().get(1);
|
||||
Assert.assertEquals(2, and.getChildren().size());
|
||||
assertEquals(2, and.getChildren().size());
|
||||
verifyConstraintToString(expressionExpr, constraint);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrConstraintParser()
|
||||
void testOrConstraintParser()
|
||||
throws PlacementConstraintParseException {
|
||||
String expressionExpr;
|
||||
ConstraintParser parser;
|
||||
|
@ -242,33 +244,33 @@ public class TestPlacementConstraintParser {
|
|||
expressionExpr = "OR(NOTIN,NODE,foo:NOTIN,NODE,bar)";
|
||||
parser = new ConjunctionConstraintParser(expressionExpr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof Or);
|
||||
assertTrue(constraint instanceof Or);
|
||||
or = (Or) constraint;
|
||||
Assert.assertEquals(2, or.getChildren().size());
|
||||
assertEquals(2, or.getChildren().size());
|
||||
verifyConstraintToString(expressionExpr, constraint);
|
||||
|
||||
expressionExpr = "OR(NOTIN,NODE,foo:cardinality,NODE,foo,0,1)";
|
||||
parser = new ConjunctionConstraintParser(expressionExpr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof Or);
|
||||
Assert.assertEquals(2, or.getChildren().size());
|
||||
assertTrue(constraint instanceof Or);
|
||||
assertEquals(2, or.getChildren().size());
|
||||
verifyConstraintToString(expressionExpr, constraint);
|
||||
|
||||
expressionExpr =
|
||||
"OR(NOTIN,NODE,foo:OR(NOTIN,NODE,foo:cardinality,NODE,foo,0,1))";
|
||||
parser = new ConjunctionConstraintParser(expressionExpr);
|
||||
constraint = parser.parse();
|
||||
Assert.assertTrue(constraint instanceof Or);
|
||||
assertTrue(constraint instanceof Or);
|
||||
or = (Or) constraint;
|
||||
Assert.assertTrue(or.getChildren().get(0) instanceof SingleConstraint);
|
||||
Assert.assertTrue(or.getChildren().get(1) instanceof Or);
|
||||
assertTrue(or.getChildren().get(0) instanceof SingleConstraint);
|
||||
assertTrue(or.getChildren().get(1) instanceof Or);
|
||||
or = (Or) or.getChildren().get(1);
|
||||
Assert.assertEquals(2, or.getChildren().size());
|
||||
assertEquals(2, or.getChildren().size());
|
||||
verifyConstraintToString(expressionExpr, constraint);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleConstraintsTokenizer()
|
||||
void testMultipleConstraintsTokenizer()
|
||||
throws PlacementConstraintParseException {
|
||||
MultipleConstraintsTokenizer ct;
|
||||
SourceTagsTokenizer st;
|
||||
|
@ -310,9 +312,9 @@ public class TestPlacementConstraintParser {
|
|||
st = new SourceTagsTokenizer("A(B)");
|
||||
mp = new TokenizerTester(st, "A", "B");
|
||||
mp.verify();
|
||||
Assert.fail("Expecting a parsing failure");
|
||||
fail("Expecting a parsing failure");
|
||||
} catch (PlacementConstraintParseException e) {
|
||||
Assert.assertTrue(e.getMessage()
|
||||
assertTrue(e.getMessage()
|
||||
.contains("Value of the expression must be an integer"));
|
||||
}
|
||||
}
|
||||
|
@ -334,15 +336,15 @@ public class TestPlacementConstraintParser {
|
|||
int i = 0;
|
||||
while (tokenizer.hasMoreElements()) {
|
||||
String current = tokenizer.nextElement();
|
||||
Assert.assertTrue(i < expectedExtractions.length);
|
||||
Assert.assertEquals(expectedExtractions[i], current);
|
||||
assertTrue(i < expectedExtractions.length);
|
||||
assertEquals(expectedExtractions[i], current);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParsePlacementSpec()
|
||||
void testParsePlacementSpec()
|
||||
throws PlacementConstraintParseException {
|
||||
Map<SourceTags, PlacementConstraint> result;
|
||||
PlacementConstraint expectedPc1, expectedPc2;
|
||||
|
@ -352,92 +354,92 @@ public class TestPlacementConstraintParser {
|
|||
// Only Source Tag without constraint
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("foo(3)");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
tag1 = result.keySet().iterator().next();
|
||||
Assert.assertEquals("foo", tag1.getTag());
|
||||
Assert.assertEquals(3, tag1.getNumOfAllocations());
|
||||
assertEquals("foo", tag1.getTag());
|
||||
assertEquals(3, tag1.getNumOfAllocations());
|
||||
expectedPc1 = null;
|
||||
actualPc1 = result.values().iterator().next();
|
||||
Assert.assertEquals(expectedPc1, actualPc1);
|
||||
assertEquals(expectedPc1, actualPc1);
|
||||
|
||||
// A single anti-affinity constraint
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("foo(3),notin,node,foo");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
tag1 = result.keySet().iterator().next();
|
||||
Assert.assertEquals("foo", tag1.getTag());
|
||||
Assert.assertEquals(3, tag1.getNumOfAllocations());
|
||||
assertEquals("foo", tag1.getTag());
|
||||
assertEquals(3, tag1.getNumOfAllocations());
|
||||
expectedPc1 = targetNotIn("node", allocationTag("foo")).build();
|
||||
actualPc1 = result.values().iterator().next();
|
||||
Assert.assertEquals(expectedPc1, actualPc1);
|
||||
assertEquals(expectedPc1, actualPc1);
|
||||
|
||||
// Upper case
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("foo(3),NOTIN,NODE,foo");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
tag1 = result.keySet().iterator().next();
|
||||
Assert.assertEquals("foo", tag1.getTag());
|
||||
Assert.assertEquals(3, tag1.getNumOfAllocations());
|
||||
assertEquals("foo", tag1.getTag());
|
||||
assertEquals(3, tag1.getNumOfAllocations());
|
||||
expectedPc1 = targetNotIn("node", allocationTag("foo")).build();
|
||||
actualPc1 = result.values().iterator().next();
|
||||
Assert.assertEquals(expectedPc1, actualPc1);
|
||||
assertEquals(expectedPc1, actualPc1);
|
||||
|
||||
// A single cardinality constraint
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("foo(10),cardinality,node,foo,bar,0,100");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
tag1 = result.keySet().iterator().next();
|
||||
Assert.assertEquals("foo", tag1.getTag());
|
||||
Assert.assertEquals(10, tag1.getNumOfAllocations());
|
||||
assertEquals("foo", tag1.getTag());
|
||||
assertEquals(10, tag1.getNumOfAllocations());
|
||||
TargetExpression[] targetExpressions = new TargetExpression[] {
|
||||
PlacementTargets.allocationTag("foo"),
|
||||
PlacementTargets.allocationTag("bar")};
|
||||
expectedPc1 = PlacementConstraints.targetCardinality("node", 0,
|
||||
100, targetExpressions).build();
|
||||
Assert.assertEquals(expectedPc1, result.values().iterator().next());
|
||||
assertEquals(expectedPc1, result.values().iterator().next());
|
||||
|
||||
// Two constraint expressions
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("foo(3),notin,node,foo:bar(2),in,node,foo");
|
||||
Assert.assertEquals(2, result.size());
|
||||
assertEquals(2, result.size());
|
||||
Iterator<SourceTags> keyIt = result.keySet().iterator();
|
||||
tag1 = keyIt.next();
|
||||
Assert.assertEquals("foo", tag1.getTag());
|
||||
Assert.assertEquals(3, tag1.getNumOfAllocations());
|
||||
assertEquals("foo", tag1.getTag());
|
||||
assertEquals(3, tag1.getNumOfAllocations());
|
||||
tag2 = keyIt.next();
|
||||
Assert.assertEquals("bar", tag2.getTag());
|
||||
Assert.assertEquals(2, tag2.getNumOfAllocations());
|
||||
assertEquals("bar", tag2.getTag());
|
||||
assertEquals(2, tag2.getNumOfAllocations());
|
||||
Iterator<PlacementConstraint> valueIt = result.values().iterator();
|
||||
expectedPc1 = targetNotIn("node", allocationTag("foo")).build();
|
||||
expectedPc2 = targetIn("node", allocationTag("foo")).build();
|
||||
Assert.assertEquals(expectedPc1, valueIt.next());
|
||||
Assert.assertEquals(expectedPc2, valueIt.next());
|
||||
assertEquals(expectedPc1, valueIt.next());
|
||||
assertEquals(expectedPc2, valueIt.next());
|
||||
|
||||
// And constraint
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("foo(1000),and(notin,node,bar:in,node,foo)");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
keyIt = result.keySet().iterator();
|
||||
tag1 = keyIt.next();
|
||||
Assert.assertEquals("foo", tag1.getTag());
|
||||
Assert.assertEquals(1000, tag1.getNumOfAllocations());
|
||||
assertEquals("foo", tag1.getTag());
|
||||
assertEquals(1000, tag1.getNumOfAllocations());
|
||||
actualPc1 = result.values().iterator().next();
|
||||
expectedPc1 = and(targetNotIn("node", allocationTag("bar")),
|
||||
targetIn("node", allocationTag("foo"))).build();
|
||||
Assert.assertEquals(expectedPc1, actualPc1);
|
||||
assertEquals(expectedPc1, actualPc1);
|
||||
|
||||
// Multiple constraints with nested forms.
|
||||
result = PlacementConstraintParser.parsePlacementSpec(
|
||||
"foo(1000),and(notin,node,bar:or(in,node,foo:in,node,moo))"
|
||||
+ ":bar(200),notin,node,foo");
|
||||
Assert.assertEquals(2, result.size());
|
||||
assertEquals(2, result.size());
|
||||
keyIt = result.keySet().iterator();
|
||||
tag1 = keyIt.next();
|
||||
tag2 = keyIt.next();
|
||||
Assert.assertEquals("foo", tag1.getTag());
|
||||
Assert.assertEquals(1000, tag1.getNumOfAllocations());
|
||||
Assert.assertEquals("bar", tag2.getTag());
|
||||
Assert.assertEquals(200, tag2.getNumOfAllocations());
|
||||
assertEquals("foo", tag1.getTag());
|
||||
assertEquals(1000, tag1.getNumOfAllocations());
|
||||
assertEquals("bar", tag2.getTag());
|
||||
assertEquals(200, tag2.getNumOfAllocations());
|
||||
valueIt = result.values().iterator();
|
||||
actualPc1 = valueIt.next();
|
||||
actualPc2 = valueIt.next();
|
||||
|
@ -445,18 +447,18 @@ public class TestPlacementConstraintParser {
|
|||
expectedPc1 = and(targetNotIn("node", allocationTag("bar")),
|
||||
or(targetIn("node", allocationTag("foo")),
|
||||
targetIn("node", allocationTag("moo")))).build();
|
||||
Assert.assertEquals(actualPc1, expectedPc1);
|
||||
assertEquals(actualPc1, expectedPc1);
|
||||
expectedPc2 = targetNotIn("node", allocationTag("foo")).build();
|
||||
Assert.assertEquals(expectedPc2, actualPc2);
|
||||
assertEquals(expectedPc2, actualPc2);
|
||||
|
||||
// Failure Cases
|
||||
String[] invalidSpecs = {"foo(3", "foo),bar", "foobar", "),java=1.7,1.8"};
|
||||
for (String spec : invalidSpecs) {
|
||||
try {
|
||||
result = PlacementConstraintParser.parsePlacementSpec(spec);
|
||||
Assert.fail("Expected a failure!");
|
||||
fail("Expected a failure!");
|
||||
} catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof PlacementConstraintParseException);
|
||||
assertTrue(e instanceof PlacementConstraintParseException);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -475,13 +477,13 @@ public class TestPlacementConstraintParser {
|
|||
try {
|
||||
PlacementConstraintParser.parseExpression(constrainExpr);
|
||||
} catch (PlacementConstraintParseException e) {
|
||||
Assert.fail("The parser is unable to parse the expression: "
|
||||
fail("The parser is unable to parse the expression: "
|
||||
+ constrainExpr + ", caused by: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseNodeAttributeSpec()
|
||||
void testParseNodeAttributeSpec()
|
||||
throws PlacementConstraintParseException {
|
||||
Map<SourceTags, PlacementConstraint> result;
|
||||
PlacementConstraint.AbstractConstraint expectedPc1, expectedPc2;
|
||||
|
@ -490,33 +492,33 @@ public class TestPlacementConstraintParser {
|
|||
// A single node attribute constraint
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("xyz(4),rm.yarn.io/foo=true");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
TargetExpression target = PlacementTargets
|
||||
.nodeAttribute("rm.yarn.io/foo", "true");
|
||||
expectedPc1 = targetNodeAttribute("node", NodeAttributeOpCode.EQ, target);
|
||||
|
||||
actualPc1 = result.values().iterator().next();
|
||||
Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
|
||||
// A single node attribute constraint
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("xyz(3),rm.yarn.io/foo!=abc");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
target = PlacementTargets
|
||||
.nodeAttribute("rm.yarn.io/foo", "abc");
|
||||
expectedPc1 = targetNodeAttribute("node", NodeAttributeOpCode.NE, target);
|
||||
|
||||
actualPc1 = result.values().iterator().next();
|
||||
Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
|
||||
actualPc1 = result.values().iterator().next();
|
||||
Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
|
||||
// A single node attribute constraint
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec(
|
||||
"xyz(1),rm.yarn.io/foo!=abc:zxy(1),rm.yarn.io/bar=true");
|
||||
Assert.assertEquals(2, result.size());
|
||||
assertEquals(2, result.size());
|
||||
target = PlacementTargets
|
||||
.nodeAttribute("rm.yarn.io/foo", "abc");
|
||||
expectedPc1 = targetNodeAttribute("node", NodeAttributeOpCode.NE, target);
|
||||
|
@ -527,25 +529,25 @@ public class TestPlacementConstraintParser {
|
|||
Iterator<PlacementConstraint> valueIt = result.values().iterator();
|
||||
actualPc1 = valueIt.next();
|
||||
actualPc2 = valueIt.next();
|
||||
Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
Assert.assertEquals(expectedPc2, actualPc2.getConstraintExpr());
|
||||
assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
assertEquals(expectedPc2, actualPc2.getConstraintExpr());
|
||||
|
||||
// A single node attribute constraint w/o source tags
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("rm.yarn.io/foo=true");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
target = PlacementTargets.nodeAttribute("rm.yarn.io/foo", "true");
|
||||
expectedPc1 = targetNodeAttribute("node", NodeAttributeOpCode.EQ, target);
|
||||
|
||||
SourceTags actualSourceTags = result.keySet().iterator().next();
|
||||
Assert.assertTrue(actualSourceTags.isEmpty());
|
||||
assertTrue(actualSourceTags.isEmpty());
|
||||
actualPc1 = result.values().iterator().next();
|
||||
Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
|
||||
// Node Attribute Constraint With Multiple Values
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("java=1.7,1.8");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
|
||||
Set<String> constraintEntities = new TreeSet<>();
|
||||
constraintEntities.add("1.7");
|
||||
|
@ -554,52 +556,52 @@ public class TestPlacementConstraintParser {
|
|||
constraintEntities.toArray(new String[constraintEntities.size()]));
|
||||
expectedPc1 = targetNodeAttribute("node", NodeAttributeOpCode.EQ, target);
|
||||
actualSourceTags = result.keySet().iterator().next();
|
||||
Assert.assertTrue(actualSourceTags.isEmpty());
|
||||
assertTrue(actualSourceTags.isEmpty());
|
||||
actualPc1 = result.values().iterator().next();
|
||||
Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
assertEquals(expectedPc1, actualPc1.getConstraintExpr());
|
||||
|
||||
// If source tags is not specified for a node-attribute constraint,
|
||||
// then this expression must be single constraint expression.
|
||||
try {
|
||||
PlacementConstraintParser
|
||||
.parsePlacementSpec("rm.yarn.io/foo=true:xyz(1),notin,node,xyz");
|
||||
Assert.fail("Expected a failure!");
|
||||
fail("Expected a failure!");
|
||||
} catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof PlacementConstraintParseException);
|
||||
assertTrue(e instanceof PlacementConstraintParseException);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseAllocationTagNameSpace()
|
||||
void testParseAllocationTagNameSpace()
|
||||
throws PlacementConstraintParseException {
|
||||
Map<SourceTags, PlacementConstraint> result;
|
||||
|
||||
// Constraint with Two Different NameSpaces
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("foo(2),notin,node,not-self/bar,all/moo");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
Set<TargetExpression> expectedTargetExpressions = Sets.newHashSet(
|
||||
PlacementTargets.allocationTagWithNamespace("not-self", "bar"),
|
||||
PlacementTargets.allocationTagWithNamespace("all", "moo"));
|
||||
AbstractConstraint constraint = result.values().iterator().next().
|
||||
getConstraintExpr();
|
||||
Assert.assertTrue(constraint instanceof SingleConstraint);
|
||||
assertTrue(constraint instanceof SingleConstraint);
|
||||
SingleConstraint single = (SingleConstraint) constraint;
|
||||
Assert.assertEquals(2, single.getTargetExpressions().size());
|
||||
Assert.assertTrue(Sets.difference(expectedTargetExpressions,
|
||||
assertEquals(2, single.getTargetExpressions().size());
|
||||
assertTrue(Sets.difference(expectedTargetExpressions,
|
||||
single.getTargetExpressions()).isEmpty());
|
||||
|
||||
// Constraint With Default NameSpace SELF
|
||||
result = PlacementConstraintParser
|
||||
.parsePlacementSpec("foo(2),notin,node,moo");
|
||||
Assert.assertEquals(1, result.size());
|
||||
assertEquals(1, result.size());
|
||||
TargetExpression expectedTargetExpression = PlacementTargets.
|
||||
allocationTagWithNamespace("self", "moo");
|
||||
constraint = result.values().iterator().next().getConstraintExpr();
|
||||
Assert.assertTrue(constraint instanceof SingleConstraint);
|
||||
assertTrue(constraint instanceof SingleConstraint);
|
||||
single = (SingleConstraint) constraint;
|
||||
Assert.assertEquals(1, single.getTargetExpressions().size());
|
||||
Assert.assertEquals(expectedTargetExpression,
|
||||
assertEquals(1, single.getTargetExpressions().size());
|
||||
assertEquals(expectedTargetExpression,
|
||||
single.getTargetExpressions().iterator().next());
|
||||
|
||||
// Constraint With Invalid NameSpace
|
||||
|
@ -610,8 +612,8 @@ public class TestPlacementConstraintParser {
|
|||
} catch(PlacementConstraintParseException e) {
|
||||
caughtException = true;
|
||||
}
|
||||
Assert.assertTrue("PlacementConstraintParseException is expected",
|
||||
caughtException);
|
||||
assertTrue(caughtException,
|
||||
"PlacementConstraintParseException is expected");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,6 +27,8 @@ import static org.apache.hadoop.yarn.api.resource.PlacementConstraints.targetIn;
|
|||
import static org.apache.hadoop.yarn.api.resource.PlacementConstraints.targetNotIn;
|
||||
import static org.apache.hadoop.yarn.api.resource.PlacementConstraints.PlacementTargets.allocationTag;
|
||||
import static org.apache.hadoop.yarn.api.resource.PlacementConstraints.PlacementTargets.nodeAttribute;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.apache.hadoop.yarn.api.records.AllocationTagNamespaceType;
|
||||
import org.apache.hadoop.yarn.api.resource.PlacementConstraint.AbstractConstraint;
|
||||
|
@ -34,36 +36,35 @@ import org.apache.hadoop.yarn.api.resource.PlacementConstraint.And;
|
|||
import org.apache.hadoop.yarn.api.resource.PlacementConstraint.SingleConstraint;
|
||||
import org.apache.hadoop.yarn.api.resource.PlacementConstraint.TargetExpression;
|
||||
import org.apache.hadoop.yarn.api.resource.PlacementConstraint.TargetExpression.TargetType;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test class for the various static methods in
|
||||
* {@link org.apache.hadoop.yarn.api.resource.PlacementConstraints}.
|
||||
*/
|
||||
public class TestPlacementConstraints {
|
||||
class TestPlacementConstraints {
|
||||
|
||||
@Test
|
||||
public void testNodeAffinityToTag() {
|
||||
void testNodeAffinityToTag() {
|
||||
AbstractConstraint constraintExpr =
|
||||
targetIn(NODE, allocationTag("hbase-m"));
|
||||
|
||||
SingleConstraint sConstraint = (SingleConstraint) constraintExpr;
|
||||
Assert.assertEquals(NODE, sConstraint.getScope());
|
||||
Assert.assertEquals(1, sConstraint.getMinCardinality());
|
||||
Assert.assertEquals(Integer.MAX_VALUE, sConstraint.getMaxCardinality());
|
||||
assertEquals(NODE, sConstraint.getScope());
|
||||
assertEquals(1, sConstraint.getMinCardinality());
|
||||
assertEquals(Integer.MAX_VALUE, sConstraint.getMaxCardinality());
|
||||
|
||||
Assert.assertEquals(1, sConstraint.getTargetExpressions().size());
|
||||
assertEquals(1, sConstraint.getTargetExpressions().size());
|
||||
TargetExpression tExpr =
|
||||
sConstraint.getTargetExpressions().iterator().next();
|
||||
Assert.assertEquals(AllocationTagNamespaceType.SELF.toString(),
|
||||
assertEquals(AllocationTagNamespaceType.SELF.toString(),
|
||||
tExpr.getTargetKey());
|
||||
Assert.assertEquals(TargetType.ALLOCATION_TAG, tExpr.getTargetType());
|
||||
Assert.assertEquals(1, tExpr.getTargetValues().size());
|
||||
Assert.assertEquals("hbase-m", tExpr.getTargetValues().iterator().next());
|
||||
assertEquals(TargetType.ALLOCATION_TAG, tExpr.getTargetType());
|
||||
assertEquals(1, tExpr.getTargetValues().size());
|
||||
assertEquals("hbase-m", tExpr.getTargetValues().iterator().next());
|
||||
|
||||
PlacementConstraint constraint = PlacementConstraints.build(constraintExpr);
|
||||
Assert.assertNotNull(constraint.getConstraintExpr());
|
||||
assertNotNull(constraint.getConstraintExpr());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -72,17 +73,17 @@ public class TestPlacementConstraints {
|
|||
targetNotIn(NODE, nodeAttribute("java", "1.8"));
|
||||
|
||||
SingleConstraint sConstraint = (SingleConstraint) constraintExpr;
|
||||
Assert.assertEquals(NODE, sConstraint.getScope());
|
||||
Assert.assertEquals(0, sConstraint.getMinCardinality());
|
||||
Assert.assertEquals(0, sConstraint.getMaxCardinality());
|
||||
assertEquals(NODE, sConstraint.getScope());
|
||||
assertEquals(0, sConstraint.getMinCardinality());
|
||||
assertEquals(0, sConstraint.getMaxCardinality());
|
||||
|
||||
Assert.assertEquals(1, sConstraint.getTargetExpressions().size());
|
||||
assertEquals(1, sConstraint.getTargetExpressions().size());
|
||||
TargetExpression tExpr =
|
||||
sConstraint.getTargetExpressions().iterator().next();
|
||||
Assert.assertEquals("java", tExpr.getTargetKey());
|
||||
Assert.assertEquals(TargetType.NODE_ATTRIBUTE, tExpr.getTargetType());
|
||||
Assert.assertEquals(1, tExpr.getTargetValues().size());
|
||||
Assert.assertEquals("1.8", tExpr.getTargetValues().iterator().next());
|
||||
assertEquals("java", tExpr.getTargetKey());
|
||||
assertEquals(TargetType.NODE_ATTRIBUTE, tExpr.getTargetType());
|
||||
assertEquals(1, tExpr.getTargetValues().size());
|
||||
assertEquals("1.8", tExpr.getTargetValues().iterator().next());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -93,17 +94,17 @@ public class TestPlacementConstraints {
|
|||
targetCardinality(RACK, 2, 10, allocationTag("zk")));
|
||||
|
||||
And andExpr = (And) constraintExpr;
|
||||
Assert.assertEquals(3, andExpr.getChildren().size());
|
||||
assertEquals(3, andExpr.getChildren().size());
|
||||
SingleConstraint sConstr = (SingleConstraint) andExpr.getChildren().get(0);
|
||||
TargetExpression tExpr = sConstr.getTargetExpressions().iterator().next();
|
||||
Assert.assertEquals("spark", tExpr.getTargetValues().iterator().next());
|
||||
assertEquals("spark", tExpr.getTargetValues().iterator().next());
|
||||
|
||||
sConstr = (SingleConstraint) andExpr.getChildren().get(1);
|
||||
Assert.assertEquals(0, sConstr.getMinCardinality());
|
||||
Assert.assertEquals(3, sConstr.getMaxCardinality());
|
||||
assertEquals(0, sConstr.getMinCardinality());
|
||||
assertEquals(3, sConstr.getMaxCardinality());
|
||||
|
||||
sConstr = (SingleConstraint) andExpr.getChildren().get(2);
|
||||
Assert.assertEquals(2, sConstr.getMinCardinality());
|
||||
Assert.assertEquals(10, sConstr.getMaxCardinality());
|
||||
assertEquals(2, sConstr.getMinCardinality());
|
||||
assertEquals(10, sConstr.getMaxCardinality());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,74 +22,77 @@ import com.google.common.collect.ImmutableMap;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import org.apache.hadoop.yarn.api.protocolrecords.ResourceTypes;
|
||||
import org.apache.hadoop.yarn.api.records.ResourceInformation;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Test class to verify various resource informations in a given resource.
|
||||
*/
|
||||
public class TestResourceInformation {
|
||||
class TestResourceInformation {
|
||||
|
||||
@Test
|
||||
public void testName() {
|
||||
void testName() {
|
||||
String name = "yarn.io/test";
|
||||
ResourceInformation ri = ResourceInformation.newInstance(name);
|
||||
Assert.assertEquals("Resource name incorrect", name, ri.getName());
|
||||
assertEquals(name, ri.getName(), "Resource name incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnits() {
|
||||
void testUnits() {
|
||||
String name = "yarn.io/test";
|
||||
String units = "m";
|
||||
ResourceInformation ri = ResourceInformation.newInstance(name, units);
|
||||
Assert.assertEquals("Resource name incorrect", name, ri.getName());
|
||||
Assert.assertEquals("Resource units incorrect", units, ri.getUnits());
|
||||
assertEquals(name, ri.getName(), "Resource name incorrect");
|
||||
assertEquals(units, ri.getUnits(), "Resource units incorrect");
|
||||
units = "z";
|
||||
try {
|
||||
ResourceInformation.newInstance(name, units).setUnits(units);
|
||||
Assert.fail(units + "is not a valid unit");
|
||||
fail(units + "is not a valid unit");
|
||||
} catch (IllegalArgumentException ie) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValue() {
|
||||
void testValue() {
|
||||
String name = "yarn.io/test";
|
||||
long value = 1L;
|
||||
ResourceInformation ri = ResourceInformation.newInstance(name, value);
|
||||
Assert.assertEquals("Resource name incorrect", name, ri.getName());
|
||||
Assert.assertEquals("Resource value incorrect", value, ri.getValue());
|
||||
assertEquals(name, ri.getName(), "Resource name incorrect");
|
||||
assertEquals(value, ri.getValue(), "Resource value incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResourceInformation() {
|
||||
void testResourceInformation() {
|
||||
String name = "yarn.io/test";
|
||||
long value = 1L;
|
||||
String units = "m";
|
||||
ResourceInformation ri =
|
||||
ResourceInformation.newInstance(name, units, value);
|
||||
Assert.assertEquals("Resource name incorrect", name, ri.getName());
|
||||
Assert.assertEquals("Resource value incorrect", value, ri.getValue());
|
||||
Assert.assertEquals("Resource units incorrect", units, ri.getUnits());
|
||||
assertEquals(name, ri.getName(), "Resource name incorrect");
|
||||
assertEquals(value, ri.getValue(), "Resource value incorrect");
|
||||
assertEquals(units, ri.getUnits(), "Resource units incorrect");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEqualsWithTagsAndAttributes() {
|
||||
void testEqualsWithTagsAndAttributes() {
|
||||
// Same tags but different order
|
||||
ResourceInformation ri01 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
ResourceTypes.COUNTABLE, 0, 100,
|
||||
ImmutableSet.of("A", "B"), null);
|
||||
ResourceInformation ri02 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
ResourceTypes.COUNTABLE, 0, 100, ImmutableSet.of("B", "A"), null);
|
||||
Assert.assertEquals(ri01, ri02);
|
||||
assertEquals(ri01, ri02);
|
||||
|
||||
// Different tags
|
||||
ResourceInformation ri11 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
ResourceTypes.COUNTABLE, 0, 100, null, null);
|
||||
ResourceInformation ri12 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
ResourceTypes.COUNTABLE, 0, 100, ImmutableSet.of("B", "A"), null);
|
||||
Assert.assertNotEquals(ri11, ri12);
|
||||
assertNotEquals(ri11, ri12);
|
||||
|
||||
// Different attributes
|
||||
ResourceInformation ri21 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
|
@ -98,20 +101,20 @@ public class TestResourceInformation {
|
|||
ResourceInformation ri22 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
ResourceTypes.COUNTABLE, 0, 100, null,
|
||||
ImmutableMap.of("A", "A1", "B", "B2"));
|
||||
Assert.assertNotEquals(ri21, ri22);
|
||||
assertNotEquals(ri21, ri22);
|
||||
|
||||
// No tags or attributes
|
||||
ResourceInformation ri31 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
ResourceTypes.COUNTABLE, 0, 100, null, null);
|
||||
ResourceInformation ri32 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
ResourceTypes.COUNTABLE, 0, 100, null, null);
|
||||
Assert.assertEquals(ri31, ri32);
|
||||
assertEquals(ri31, ri32);
|
||||
|
||||
// Null tags/attributes same as empty ones
|
||||
ResourceInformation ri41 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
ResourceTypes.COUNTABLE, 0, 100, ImmutableSet.of(), null);
|
||||
ResourceInformation ri42 = ResourceInformation.newInstance("r1", "M", 100,
|
||||
ResourceTypes.COUNTABLE, 0, 100, null, ImmutableMap.of());
|
||||
Assert.assertEquals(ri41, ri42);
|
||||
assertEquals(ri41, ri42);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,121 +18,132 @@
|
|||
|
||||
package org.apache.hadoop.yarn.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Test class to handle all test cases needed to verify basic unit conversion
|
||||
* scenarios.
|
||||
*/
|
||||
public class TestUnitsConversionUtil {
|
||||
class TestUnitsConversionUtil {
|
||||
|
||||
@Test
|
||||
public void testUnitsConversion() {
|
||||
void testUnitsConversion() {
|
||||
int value = 5;
|
||||
String fromUnit = "";
|
||||
long test = value;
|
||||
Assert.assertEquals("pico test failed",
|
||||
value * 1000L * 1000L * 1000L * 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "p", test));
|
||||
Assert.assertEquals("nano test failed",
|
||||
value * 1000L * 1000L * 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "n", test));
|
||||
Assert
|
||||
.assertEquals("micro test failed", value * 1000L * 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "u", test));
|
||||
Assert.assertEquals("milli test failed", value * 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "m", test));
|
||||
assertEquals(value * 1000L * 1000L * 1000L * 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "p", test),
|
||||
"pico test failed");
|
||||
assertEquals(value * 1000L * 1000L * 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "n", test),
|
||||
"nano test failed");
|
||||
assertEquals(value * 1000L * 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "u", test),
|
||||
"micro test failed");
|
||||
assertEquals(value * 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "m", test),
|
||||
"milli test failed");
|
||||
|
||||
test = value * 1000L * 1000L * 1000L * 1000L * 1000L;
|
||||
fromUnit = "";
|
||||
Assert.assertEquals("kilo test failed", test / 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "k", test));
|
||||
assertEquals(test / 1000L,
|
||||
UnitsConversionUtil.convert(fromUnit, "k", test),
|
||||
"kilo test failed");
|
||||
|
||||
Assert
|
||||
.assertEquals("mega test failed", test / (1000L * 1000L),
|
||||
UnitsConversionUtil.convert(fromUnit, "M", test));
|
||||
Assert.assertEquals("giga test failed",
|
||||
test / (1000L * 1000L * 1000L),
|
||||
UnitsConversionUtil.convert(fromUnit, "G", test));
|
||||
Assert.assertEquals("tera test failed",
|
||||
test / (1000L * 1000L * 1000L * 1000L),
|
||||
UnitsConversionUtil.convert(fromUnit, "T", test));
|
||||
Assert.assertEquals("peta test failed",
|
||||
test / (1000L * 1000L * 1000L * 1000L * 1000L),
|
||||
UnitsConversionUtil.convert(fromUnit, "P", test));
|
||||
assertEquals(test / (1000L * 1000L),
|
||||
UnitsConversionUtil.convert(fromUnit, "M", test),
|
||||
"mega test failed");
|
||||
assertEquals(test / (1000L * 1000L * 1000L),
|
||||
UnitsConversionUtil.convert(fromUnit, "G", test),
|
||||
"giga test failed");
|
||||
assertEquals(test / (1000L * 1000L * 1000L * 1000L),
|
||||
UnitsConversionUtil.convert(fromUnit, "T", test),
|
||||
"tera test failed");
|
||||
assertEquals(test / (1000L * 1000L * 1000L * 1000L * 1000L),
|
||||
UnitsConversionUtil.convert(fromUnit, "P", test),
|
||||
"peta test failed");
|
||||
|
||||
Assert.assertEquals("nano to pico test failed", value * 1000L,
|
||||
UnitsConversionUtil.convert("n", "p", value));
|
||||
assertEquals(value * 1000L,
|
||||
UnitsConversionUtil.convert("n", "p", value),
|
||||
"nano to pico test failed");
|
||||
|
||||
Assert.assertEquals("mega to giga test failed", value,
|
||||
UnitsConversionUtil.convert("M", "G", value * 1000L));
|
||||
assertEquals(value,
|
||||
UnitsConversionUtil.convert("M", "G", value * 1000L),
|
||||
"mega to giga test failed");
|
||||
|
||||
Assert.assertEquals("Mi to Gi test failed", value,
|
||||
UnitsConversionUtil.convert("Mi", "Gi", value * 1024L));
|
||||
assertEquals(value,
|
||||
UnitsConversionUtil.convert("Mi", "Gi", value * 1024L),
|
||||
"Mi to Gi test failed");
|
||||
|
||||
Assert.assertEquals("Mi to Ki test failed", value * 1024,
|
||||
UnitsConversionUtil.convert("Mi", "Ki", value));
|
||||
assertEquals(value * 1024,
|
||||
UnitsConversionUtil.convert("Mi", "Ki", value),
|
||||
"Mi to Ki test failed");
|
||||
|
||||
Assert.assertEquals("Ki to base units test failed", 5 * 1024,
|
||||
UnitsConversionUtil.convert("Ki", "", 5));
|
||||
assertEquals(5 * 1024,
|
||||
UnitsConversionUtil.convert("Ki", "", 5),
|
||||
"Ki to base units test failed");
|
||||
|
||||
Assert.assertEquals("Mi to k test failed", 1073741,
|
||||
UnitsConversionUtil.convert("Mi", "k", 1024));
|
||||
assertEquals(1073741,
|
||||
UnitsConversionUtil.convert("Mi", "k", 1024),
|
||||
"Mi to k test failed");
|
||||
|
||||
Assert.assertEquals("M to Mi test failed", 953,
|
||||
UnitsConversionUtil.convert("M", "Mi", 1000));
|
||||
assertEquals(953,
|
||||
UnitsConversionUtil.convert("M", "Mi", 1000),
|
||||
"M to Mi test failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverflow() {
|
||||
void testOverflow() {
|
||||
long test = 5 * 1000L * 1000L * 1000L * 1000L * 1000L;
|
||||
try {
|
||||
UnitsConversionUtil.convert("P", "p", test);
|
||||
Assert.fail("this operation should result in an overflow");
|
||||
fail("this operation should result in an overflow");
|
||||
} catch (IllegalArgumentException ie) {
|
||||
// do nothing
|
||||
}
|
||||
try {
|
||||
UnitsConversionUtil.convert("m", "p", Long.MAX_VALUE - 1);
|
||||
Assert.fail("this operation should result in an overflow");
|
||||
fail("this operation should result in an overflow");
|
||||
} catch (IllegalArgumentException ie) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompare() {
|
||||
void testCompare() {
|
||||
String unitA = "P";
|
||||
long valueA = 1;
|
||||
String unitB = "p";
|
||||
long valueB = 2;
|
||||
Assert.assertEquals(1,
|
||||
assertEquals(1,
|
||||
UnitsConversionUtil.compare(unitA, valueA, unitB, valueB));
|
||||
Assert.assertEquals(-1,
|
||||
assertEquals(-1,
|
||||
UnitsConversionUtil.compare(unitB, valueB, unitA, valueA));
|
||||
Assert.assertEquals(0,
|
||||
assertEquals(0,
|
||||
UnitsConversionUtil.compare(unitA, valueA, unitA, valueA));
|
||||
Assert.assertEquals(-1,
|
||||
assertEquals(-1,
|
||||
UnitsConversionUtil.compare(unitA, valueA, unitA, valueB));
|
||||
Assert.assertEquals(1,
|
||||
assertEquals(1,
|
||||
UnitsConversionUtil.compare(unitA, valueB, unitA, valueA));
|
||||
|
||||
unitB = "T";
|
||||
Assert.assertEquals(1,
|
||||
assertEquals(1,
|
||||
UnitsConversionUtil.compare(unitA, valueA, unitB, valueB));
|
||||
Assert.assertEquals(-1,
|
||||
assertEquals(-1,
|
||||
UnitsConversionUtil.compare(unitB, valueB, unitA, valueA));
|
||||
Assert.assertEquals(0,
|
||||
assertEquals(0,
|
||||
UnitsConversionUtil.compare(unitA, valueA, unitB, 1000L));
|
||||
|
||||
unitA = "p";
|
||||
unitB = "n";
|
||||
Assert.assertEquals(-1,
|
||||
assertEquals(-1,
|
||||
UnitsConversionUtil.compare(unitA, valueA, unitB, valueB));
|
||||
Assert.assertEquals(1,
|
||||
assertEquals(1,
|
||||
UnitsConversionUtil.compare(unitB, valueB, unitA, valueA));
|
||||
Assert.assertEquals(0,
|
||||
assertEquals(0,
|
||||
UnitsConversionUtil.compare(unitA, 1000L, unitB, valueA));
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue