github.com/tsuna/gohbase@v0.0.0-20250731002811-4ffcadfba63e/hrpc/call_test.go (about) 1 // Copyright (C) 2017 The GoHBase Authors. All rights reserved. 2 // This file is part of GoHBase. 3 // Use of this source code is governed by the Apache License 2.0 4 // that can be found in the COPYING file. 5 6 package hrpc 7 8 import ( 9 "reflect" 10 "strconv" 11 "testing" 12 13 "github.com/tsuna/gohbase/pb" 14 "google.golang.org/protobuf/proto" 15 ) 16 17 func TestCellFromCellBlock(t *testing.T) { 18 cellblock := []byte{0, 0, 0, 48, 0, 0, 0, 19, 0, 0, 0, 21, 0, 4, 114, 111, 119, 55, 2, 99, 19 102, 97, 0, 0, 1, 92, 13, 97, 5, 32, 4, 72, 101, 108, 108, 111, 32, 109, 121, 32, 110, 20 97, 109, 101, 32, 105, 115, 32, 68, 111, 103, 46} 21 22 cell, n, err := cellFromCellBlock(cellblock) 23 if err != nil { 24 t.Error(err) 25 } 26 27 if int(n) != len(cellblock) { 28 t.Errorf("expected %d bytes read, got %d bytes read", len(cellblock), n) 29 } 30 31 expectedCell := &pb.Cell{ 32 Row: []byte("row7"), 33 Family: []byte("cf"), 34 Qualifier: []byte("a"), 35 Timestamp: proto.Uint64(1494873081120), 36 Value: []byte("Hello my name is Dog."), 37 CellType: pb.CellType_PUT.Enum(), 38 } 39 40 if !proto.Equal(expectedCell, cell) { 41 t.Errorf("expected cell %v, got cell %v", expectedCell, cell) 42 } 43 44 // test error cases 45 for i := range cellblock { 46 t.Run(strconv.Itoa(i), func(t *testing.T) { 47 cell, n, err := cellFromCellBlock(cellblock[:i]) 48 if err == nil { 49 t.Error("expected error, got none") 50 } 51 52 if int(n) != 0 { 53 t.Errorf("expected %d bytes read, got %d bytes read", 0, n) 54 } 55 56 if cell != nil { 57 t.Errorf("unexpected cell: %v", cell) 58 } 59 }) 60 } 61 62 cellblock[3] = 42 63 _, _, err = cellFromCellBlock(cellblock) 64 expectedErr := "HBase has lied about KeyValue length: expected 42, got 48" 65 if err == nil || err.Error() != expectedErr { 66 t.Errorf("expected error %q, got error %q", expectedErr, err) 67 } 68 } 69 70 func TestDeserializeCellblocks(t *testing.T) { 71 cellblocks := []byte{0, 0, 0, 50, 0, 0, 0, 41, 0, 0, 0, 1, 0, 26, 84, 101, 115, 116, 83, 99, 72 97, 110, 84, 105, 109, 101, 82, 97, 110, 103, 101, 86, 101, 114, 115, 105, 111, 110, 115, 73 49, 2, 99, 102, 97, 0, 0, 0, 0, 0, 0, 0, 51, 4, 49, 0, 0, 0, 50, 0, 0, 0, 41, 0, 0, 0, 1, 74 0, 26, 84, 101, 115, 116, 83, 99, 97, 110, 84, 105, 109, 101, 82, 97, 110, 103, 101, 86, 75 101, 114, 115, 105, 111, 110, 115, 50, 2, 99, 102, 97, 0, 0, 0, 0, 0, 0, 0, 52, 4, 49} 76 77 cells, read, err := deserializeCellBlocks(cellblocks, 2) 78 if err != nil { 79 t.Error(err) 80 } 81 if int(read) != len(cellblocks) { 82 t.Errorf("invalid number of bytes read: expected %d, got %d", len(cellblocks), int(read)) 83 } 84 85 expectedCells := []*pb.Cell{ 86 &pb.Cell{ 87 Row: []byte("TestScanTimeRangeVersions1"), 88 Family: []byte("cf"), 89 Qualifier: []byte("a"), 90 Timestamp: proto.Uint64(51), 91 Value: []byte("1"), 92 CellType: pb.CellType_PUT.Enum(), 93 }, 94 &pb.Cell{ 95 Row: []byte("TestScanTimeRangeVersions2"), 96 Family: []byte("cf"), 97 Qualifier: []byte("a"), 98 Timestamp: proto.Uint64(52), 99 Value: []byte("1"), 100 CellType: pb.CellType_PUT.Enum(), 101 }, 102 } 103 104 if !reflect.DeepEqual(expectedCells, cells) { 105 t.Errorf("expected %v, got %v", expectedCells, cells) 106 } 107 108 // test error cases 109 cells, _, err = deserializeCellBlocks(cellblocks[:100], 2) 110 expectedErr := "buffer is too small: expected 54, got 46" 111 if err == nil || err.Error() != expectedErr { 112 t.Errorf("expected error %q, got error %q", expectedErr, err) 113 } 114 if cells != nil { 115 t.Errorf("expected no cells, got %v", cells) 116 } 117 118 cells, read, err = deserializeCellBlocks(cellblocks, 1) 119 if err != nil { 120 t.Error(err) 121 } 122 if expected := expectedCells[:1]; !reflect.DeepEqual(expected, cells) { 123 t.Errorf("expected cells %v, got cells %v", expected, cells) 124 } 125 if int(read) != 54 { 126 t.Errorf("invalid number of bytes read: expected %d, got %d", 54, int(read)) 127 } 128 }