1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import junit.framework.TestCase;
27
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.apache.hadoop.hbase.util.Writables;
30
31 public class TestHMsg extends TestCase {
32 public void testList() {
33 List<HMsg> msgs = new ArrayList<HMsg>();
34 HMsg hmsg = null;
35 final int size = 10;
36 for (int i = 0; i < size; i++) {
37 byte [] b = Bytes.toBytes(i);
38 hmsg = new HMsg(HMsg.Type.STOP_REGIONSERVER,
39 new HRegionInfo(new HTableDescriptor(Bytes.toBytes("test")), b, b));
40 msgs.add(hmsg);
41 }
42 assertEquals(size, msgs.size());
43 int index = msgs.indexOf(hmsg);
44 assertNotSame(-1, index);
45 msgs.remove(index);
46 assertEquals(size - 1, msgs.size());
47 byte [] other = Bytes.toBytes("other");
48 hmsg = new HMsg(HMsg.Type.STOP_REGIONSERVER,
49 new HRegionInfo(new HTableDescriptor(Bytes.toBytes("test")), other, other));
50 assertEquals(-1, msgs.indexOf(hmsg));
51
52 byte [] b = Bytes.toBytes(1);
53 hmsg = new HMsg(HMsg.Type.STOP_REGIONSERVER,
54 new HRegionInfo(new HTableDescriptor(Bytes.toBytes("test")), b, b));
55 assertNotSame(-1, msgs.indexOf(hmsg));
56 }
57
58 public void testSerialization() throws IOException {
59
60 byte [] abytes = Bytes.toBytes("a");
61 byte [] bbytes = Bytes.toBytes("b");
62 byte [] parentbytes = Bytes.toBytes("parent");
63 HRegionInfo parent =
64 new HRegionInfo(new HTableDescriptor(Bytes.toBytes("parent")),
65 parentbytes, parentbytes);
66
67 HMsg hmsg = new HMsg(HMsg.Type.STOP_REGIONSERVER, parent);
68 byte [] bytes = Writables.getBytes(hmsg);
69 HMsg close = (HMsg)Writables.getWritable(bytes, new HMsg());
70 assertTrue(close.equals(hmsg));
71
72 HRegionInfo daughtera =
73 new HRegionInfo(new HTableDescriptor(Bytes.toBytes("a")), abytes, abytes);
74 HRegionInfo daughterb =
75 new HRegionInfo(new HTableDescriptor(Bytes.toBytes("b")), bbytes, bbytes);
76 HMsg splithmsg = new HMsg(HMsg.Type.REGION_SPLIT,
77 parent, daughtera, daughterb, Bytes.toBytes("REGION_SPLIT"));
78 bytes = Writables.getBytes(splithmsg);
79 hmsg = (HMsg)Writables.getWritable(bytes, new HMsg());
80 assertTrue(splithmsg.equals(hmsg));
81 }
82 }