|
22 | 22 | import java.util.List;
|
23 | 23 | import java.util.Arrays;
|
24 | 24 |
|
| 25 | +import org.apache.commons.lang3.tuple.Pair; |
25 | 26 | import org.apache.commons.math3.stat.inference.ChiSquareTest;
|
26 | 27 | import org.apache.hadoop.conf.Configuration;
|
27 | 28 | import org.junit.Assert;
|
@@ -234,4 +235,41 @@ private NodeElement getNewNode(String name, String rackLocation) {
|
234 | 235 | node.setNetworkLocation(rackLocation);
|
235 | 236 | return node;
|
236 | 237 | }
|
| 238 | + |
| 239 | + private NodeElement getNewNode(NetworkTopology cluster, |
| 240 | + String name, String rackLocation) { |
| 241 | + NodeElement node = getNewNode(name, rackLocation); |
| 242 | + cluster.add(node); |
| 243 | + return node; |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + @SuppressWarnings("unchecked") |
| 248 | + public void testWeights() { |
| 249 | + // create the topology |
| 250 | + NetworkTopology cluster = NetworkTopology.getInstance(new Configuration()); |
| 251 | + NodeElement node1 = getNewNode(cluster, "node1", "/r1"); |
| 252 | + NodeElement node2 = getNewNode(cluster, "node2", "/r1"); |
| 253 | + NodeElement node3 = getNewNode(cluster, "node3", "/r2"); |
| 254 | + for (Pair<Integer, NodeElement> test: new Pair[]{Pair.of(0, node1), |
| 255 | + Pair.of(2, node2), Pair.of(4, node3)}) { |
| 256 | + int expect = test.getLeft(); |
| 257 | + assertEquals(test.toString(), expect, cluster.getWeight(node1, test.getRight())); |
| 258 | + assertEquals(test.toString(), expect, |
| 259 | + cluster.getWeightUsingNetworkLocation(node1, test.getRight())); |
| 260 | + } |
| 261 | + // Reset so that we can have 2 levels |
| 262 | + cluster = NetworkTopology.getInstance(new Configuration()); |
| 263 | + NodeElement node5 = getNewNode(cluster, "node5", "/pod1/r1"); |
| 264 | + NodeElement node6 = getNewNode(cluster, "node6", "/pod1/r1"); |
| 265 | + NodeElement node7 = getNewNode(cluster, "node7", "/pod1/r2"); |
| 266 | + NodeElement node8 = getNewNode(cluster, "node8", "/pod2/r3"); |
| 267 | + for (Pair<Integer, NodeElement> test: new Pair[]{Pair.of(0, node5), |
| 268 | + Pair.of(2, node6), Pair.of(4, node7), Pair.of(6, node8)}) { |
| 269 | + int expect = test.getLeft(); |
| 270 | + assertEquals(test.toString(), expect, cluster.getWeight(node5, test.getRight())); |
| 271 | + assertEquals(test.toString(), expect, |
| 272 | + cluster.getWeightUsingNetworkLocation(node5, test.getRight())); |
| 273 | + } |
| 274 | + } |
237 | 275 | }
|
0 commit comments