github.com/tsuna/gohbase@v0.0.0-20250731002811-4ffcadfba63e/discovery_test.go (about)

     1  // Copyright (C) 2015  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 gohbase
     7  
     8  import (
     9  	"bytes"
    10  	"testing"
    11  
    12  	"github.com/tsuna/gohbase/hrpc"
    13  	"github.com/tsuna/gohbase/region"
    14  )
    15  
    16  func TestRegionDiscovery(t *testing.T) {
    17  	client := newClient("~invalid.quorum~") // We shouldn't connect to ZK.
    18  
    19  	reg := client.getRegionFromCache([]byte("test"), []byte("theKey"))
    20  	if reg != nil {
    21  		t.Errorf("Found region %#v even though the cache was empty?!", reg)
    22  	}
    23  
    24  	// Inject a "test" table with a single region that covers the entire key
    25  	// space (both the start and stop keys are empty).
    26  	family := []byte("info")
    27  	metaRow := &hrpc.Result{Cells: []*hrpc.Cell{
    28  		&hrpc.Cell{
    29  			Row:       []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
    30  			Family:    family,
    31  			Qualifier: []byte("regioninfo"),
    32  			Value: []byte("PBUF\b\xc4\xcd\xe9\x99\xe0)\x12\x0f\n\adefault\x12\x04test" +
    33  				"\x1a\x00\"\x00(\x000\x008\x00"),
    34  		},
    35  		&hrpc.Cell{
    36  			Row:       []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
    37  			Family:    family,
    38  			Qualifier: []byte("seqnumDuringOpen"),
    39  			Value:     []byte("\x00\x00\x00\x00\x00\x00\x00\x02"),
    40  		},
    41  		&hrpc.Cell{
    42  			Row:       []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
    43  			Family:    family,
    44  			Qualifier: []byte("server"),
    45  			Value:     []byte("localhost:50966"),
    46  		},
    47  		&hrpc.Cell{
    48  			Row:       []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
    49  			Family:    family,
    50  			Qualifier: []byte("serverstartcode"),
    51  			Value:     []byte("\x00\x00\x01N\x02\x92R\xb1"),
    52  		},
    53  	}}
    54  
    55  	reg, _, err := region.ParseRegionInfo(metaRow)
    56  	if err != nil {
    57  		t.Fatalf("Failed to discover region: %s", err)
    58  	}
    59  	client.regions.put(reg)
    60  
    61  	reg = client.getRegionFromCache([]byte("test"), []byte("theKey"))
    62  	if reg == nil {
    63  		t.Fatal("Region not found even though we injected it in the cache.")
    64  	}
    65  	expected := region.NewInfo(
    66  		0,
    67  		nil,
    68  		[]byte("test"),
    69  		[]byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
    70  		nil,
    71  		nil,
    72  	)
    73  	if !bytes.Equal(reg.Table(), expected.Table()) ||
    74  		!bytes.Equal(reg.Name(), expected.Name()) ||
    75  		!bytes.Equal(reg.StartKey(), expected.StartKey()) ||
    76  		!bytes.Equal(reg.StopKey(), expected.StopKey()) {
    77  		t.Errorf("Found region %#v \nbut expected %#v", reg, expected)
    78  	}
    79  
    80  	reg = client.getRegionFromCache([]byte("notfound"), []byte("theKey"))
    81  	if reg != nil {
    82  		t.Errorf("Found region %#v even though this table doesn't exist", reg)
    83  	}
    84  }