1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | package org.apache.hadoop.hdfs.server.namenode; |
---|
20 | |
---|
21 | import org.apache.hadoop.hdfs.protocol.DatanodeID; |
---|
22 | |
---|
23 | import junit.framework.TestCase; |
---|
24 | |
---|
25 | public class TestHost2NodesMap extends TestCase { |
---|
26 | static private Host2NodesMap map = new Host2NodesMap(); |
---|
27 | private final static DatanodeDescriptor dataNodes[] = new DatanodeDescriptor[] { |
---|
28 | new DatanodeDescriptor(new DatanodeID("h1:5020"), "/d1/r1"), |
---|
29 | new DatanodeDescriptor(new DatanodeID("h2:5020"), "/d1/r1"), |
---|
30 | new DatanodeDescriptor(new DatanodeID("h3:5020"), "/d1/r2"), |
---|
31 | new DatanodeDescriptor(new DatanodeID("h3:5030"), "/d1/r2"), |
---|
32 | }; |
---|
33 | private final static DatanodeDescriptor NULL_NODE = null; |
---|
34 | private final static DatanodeDescriptor NODE = |
---|
35 | new DatanodeDescriptor(new DatanodeID("h3:5040"), "/d1/r4"); |
---|
36 | |
---|
37 | static { |
---|
38 | for(DatanodeDescriptor node:dataNodes) { |
---|
39 | map.add(node); |
---|
40 | } |
---|
41 | map.add(NULL_NODE); |
---|
42 | } |
---|
43 | |
---|
44 | public void testContains() throws Exception { |
---|
45 | for(int i=0; i<dataNodes.length; i++) { |
---|
46 | assertTrue(map.contains(dataNodes[i])); |
---|
47 | } |
---|
48 | assertFalse(map.contains(NULL_NODE)); |
---|
49 | assertFalse(map.contains(NODE)); |
---|
50 | } |
---|
51 | |
---|
52 | public void testGetDatanodeByHost() throws Exception { |
---|
53 | assertTrue(map.getDatanodeByHost("h1")==dataNodes[0]); |
---|
54 | assertTrue(map.getDatanodeByHost("h2")==dataNodes[1]); |
---|
55 | DatanodeDescriptor node = map.getDatanodeByHost("h3"); |
---|
56 | assertTrue(node==dataNodes[2] || node==dataNodes[3]); |
---|
57 | assertTrue(null==map.getDatanodeByHost("h4")); |
---|
58 | } |
---|
59 | |
---|
60 | public void testGetDatanodeByName() throws Exception { |
---|
61 | assertTrue(map.getDatanodeByName("h1:5020")==dataNodes[0]); |
---|
62 | assertTrue(map.getDatanodeByName("h1:5030")==null); |
---|
63 | assertTrue(map.getDatanodeByName("h2:5020")==dataNodes[1]); |
---|
64 | assertTrue(map.getDatanodeByName("h2:5030")==null); |
---|
65 | assertTrue(map.getDatanodeByName("h3:5020")==dataNodes[2]); |
---|
66 | assertTrue(map.getDatanodeByName("h3:5030")==dataNodes[3]); |
---|
67 | assertTrue(map.getDatanodeByName("h3:5040")==null); |
---|
68 | assertTrue(map.getDatanodeByName("h4")==null); |
---|
69 | assertTrue(map.getDatanodeByName(null)==null); |
---|
70 | } |
---|
71 | |
---|
72 | public void testRemove() throws Exception { |
---|
73 | assertFalse(map.remove(NODE)); |
---|
74 | |
---|
75 | assertTrue(map.remove(dataNodes[0])); |
---|
76 | assertTrue(map.getDatanodeByHost("h1")==null); |
---|
77 | assertTrue(map.getDatanodeByHost("h2")==dataNodes[1]); |
---|
78 | DatanodeDescriptor node = map.getDatanodeByHost("h3"); |
---|
79 | assertTrue(node==dataNodes[2] || node==dataNodes[3]); |
---|
80 | assertTrue(null==map.getDatanodeByHost("h4")); |
---|
81 | |
---|
82 | assertTrue(map.remove(dataNodes[2])); |
---|
83 | assertTrue(map.getDatanodeByHost("h1")==null); |
---|
84 | assertTrue(map.getDatanodeByHost("h2")==dataNodes[1]); |
---|
85 | assertTrue(map.getDatanodeByHost("h3")==dataNodes[3]); |
---|
86 | |
---|
87 | assertTrue(map.remove(dataNodes[3])); |
---|
88 | assertTrue(map.getDatanodeByHost("h1")==null); |
---|
89 | assertTrue(map.getDatanodeByHost("h2")==dataNodes[1]); |
---|
90 | assertTrue(map.getDatanodeByHost("h3")==null); |
---|
91 | |
---|
92 | assertFalse(map.remove(NULL_NODE)); |
---|
93 | assertTrue(map.remove(dataNodes[1])); |
---|
94 | assertFalse(map.remove(dataNodes[1])); |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|