github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/go-hbase/action_test.go (about) 1 package hbase 2 3 import ( 4 pb "github.com/insionng/yougam/libraries/golang/protobuf/proto" 5 . "github.com/insionng/yougam/libraries/pingcap/check" 6 "github.com/insionng/yougam/libraries/pingcap/go-hbase/proto" 7 ) 8 9 type mockAction struct { 10 } 11 12 func (m *mockAction) ToProto() pb.Message { 13 return &mockMessage{} 14 } 15 16 type mockMessage struct { 17 } 18 19 func (m *mockMessage) Reset() { 20 } 21 22 func (m *mockMessage) String() string { 23 return "mock message" 24 } 25 26 func (m *mockMessage) ProtoMessage() { 27 } 28 29 type Message interface { 30 Reset() 31 String() string 32 ProtoMessage() 33 } 34 35 type ActionTestSuit struct { 36 cli HBaseClient 37 tableName string 38 } 39 40 var _ = Suite(&ActionTestSuit{}) 41 42 func (s *ActionTestSuit) SetUpTest(c *C) { 43 var err error 44 s.cli, err = NewClient(getTestZkHosts(), "/hbase") 45 c.Assert(err, IsNil) 46 47 s.tableName = "test_action" 48 tblDesc := NewTableDesciptor(s.tableName) 49 cf := NewColumnFamilyDescriptor("cf") 50 tblDesc.AddColumnDesc(cf) 51 s.cli.CreateTable(tblDesc, nil) 52 } 53 54 func (s *ActionTestSuit) TearDownTest(c *C) { 55 err := s.cli.DisableTable(s.tableName) 56 c.Assert(err, IsNil) 57 58 err = s.cli.DropTable(s.tableName) 59 c.Assert(err, IsNil) 60 } 61 62 func (s *ActionTestSuit) TestAction(c *C) { 63 client, ok := s.cli.(*client) 64 c.Assert(ok, IsTrue) 65 66 row := []byte("row") 67 unknownRow := []byte("unknownRow") 68 value := []byte("value") 69 p := NewPut(row) 70 p.AddValue([]byte("cf"), []byte("q"), value) 71 72 // Test put action. 73 msg, err := client.do([]byte(s.tableName), row, p, true) 74 c.Assert(err, IsNil) 75 76 res, ok := msg.(*proto.MutateResponse) 77 c.Assert(ok, IsTrue) 78 c.Assert(res.GetProcessed(), IsTrue) 79 // cachedConns includes master conn and region conn. 80 c.Assert(client.cachedConns, HasLen, 2) 81 // cachedRegionInfo includes table to regions mapping. 82 c.Assert(client.cachedRegionInfo, HasLen, 1) 83 84 // Test not use cache conn and check cachedConns. 85 msg, err = client.do([]byte(s.tableName), row, p, false) 86 c.Assert(err, IsNil) 87 88 res, ok = msg.(*proto.MutateResponse) 89 c.Assert(ok, IsTrue) 90 c.Assert(res.GetProcessed(), IsTrue) 91 c.Assert(client.cachedConns, HasLen, 2) 92 c.Assert(client.cachedRegionInfo, HasLen, 1) 93 94 // Test use cache conn and check cachedConns. 95 msg, err = client.do([]byte(s.tableName), row, p, true) 96 c.Assert(err, IsNil) 97 98 res, ok = msg.(*proto.MutateResponse) 99 c.Assert(ok, IsTrue) 100 c.Assert(res.GetProcessed(), IsTrue) 101 c.Assert(client.cachedConns, HasLen, 2) 102 c.Assert(client.cachedRegionInfo, HasLen, 1) 103 104 // Test put value to a none-exist table. 105 _, err = client.do([]byte("unknown-table"), row, p, true) 106 c.Assert(err, NotNil) 107 c.Assert(client.cachedConns, HasLen, 2) 108 c.Assert(client.cachedRegionInfo, HasLen, 1) 109 110 // Test get action. 111 g := NewGet(row) 112 g.AddColumn([]byte("cf"), []byte("q")) 113 114 msg, err = client.do([]byte(s.tableName), row, g, true) 115 c.Assert(err, IsNil) 116 117 gres, ok := msg.(*proto.GetResponse) 118 c.Assert(ok, IsTrue) 119 rr := NewResultRow(gres.GetResult()) 120 c.Assert(rr, NotNil) 121 c.Assert(rr.SortedColumns[0].Value, DeepEquals, value) 122 c.Assert(client.cachedConns, HasLen, 2) 123 c.Assert(client.cachedRegionInfo, HasLen, 1) 124 125 // Test get action for an unknown row. 126 g = NewGet(unknownRow) 127 g.AddColumn([]byte("cf"), []byte("q")) 128 129 msg, err = client.do([]byte(s.tableName), row, g, true) 130 c.Assert(err, IsNil) 131 132 gres, ok = msg.(*proto.GetResponse) 133 c.Assert(ok, IsTrue) 134 rr = NewResultRow(gres.GetResult()) 135 c.Assert(rr, IsNil) 136 c.Assert(client.cachedConns, HasLen, 2) 137 c.Assert(client.cachedRegionInfo, HasLen, 1) 138 139 // Test delete action. 140 d := NewDelete(row) 141 d.AddFamily([]byte("cf")) 142 143 msg, err = client.do([]byte(s.tableName), row, d, true) 144 c.Assert(err, IsNil) 145 146 res, ok = msg.(*proto.MutateResponse) 147 c.Assert(ok, IsTrue) 148 c.Assert(res.GetProcessed(), IsTrue) 149 c.Assert(client.cachedConns, HasLen, 2) 150 c.Assert(client.cachedRegionInfo, HasLen, 1) 151 152 // Test CoprocessorServiceCall. 153 cs := &CoprocessorServiceCall{ 154 Row: row, 155 ServiceName: "ThemisService", 156 MethodName: "themisGet", 157 RequestParam: nil, 158 } 159 msg, err = client.do([]byte(s.tableName), row, cs, true) 160 c.Assert(err, NotNil) 161 c.Assert(client.cachedConns, HasLen, 1) 162 c.Assert(client.cachedRegionInfo, HasLen, 1) 163 164 mm := &mockMessage{} 165 param, _ := pb.Marshal(mm) 166 cs = &CoprocessorServiceCall{ 167 Row: row, 168 ServiceName: "ThemisService", 169 MethodName: "themisGet", 170 RequestParam: param, 171 } 172 msg, err = client.do([]byte(s.tableName), row, cs, true) 173 c.Assert(err, NotNil) 174 175 _, ok = msg.(*exception) 176 c.Assert(ok, IsTrue) 177 c.Assert(client.cachedConns, HasLen, 2) 178 c.Assert(client.cachedRegionInfo, HasLen, 0) 179 180 cs = &CoprocessorServiceCall{ 181 Row: row, 182 ServiceName: "UnknownService", 183 MethodName: "UnknownMethod", 184 RequestParam: param, 185 } 186 msg, err = client.do([]byte(s.tableName), row, cs, true) 187 c.Assert(err, NotNil) 188 189 _, ok = msg.(*exception) 190 c.Assert(ok, IsTrue) 191 c.Assert(client.cachedConns, HasLen, 2) 192 c.Assert(client.cachedRegionInfo, HasLen, 0) 193 194 // Test error. 195 m := &mockAction{} 196 msg, err = client.do([]byte(s.tableName), row, m, true) 197 c.Assert(err, NotNil) 198 c.Assert(client.cachedConns, HasLen, 2) 199 c.Assert(client.cachedRegionInfo, HasLen, 1) 200 }