forked from tsuna/gohbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetacache_test.go
107 lines (93 loc) · 3.32 KB
/
metacache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (C) 2015 The GoHBase Authors. All rights reserved.
// This file is part of GoHBase.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package gohbase
import (
"reflect"
"testing"
"github.com/tsuna/gohbase/region"
"github.com/tsuna/gohbase/regioninfo"
)
func TestMetaCache(t *testing.T) {
client := newClient("~invalid.quorum~") // We shouldn't connect to ZK.
reg := client.getRegionFromCache([]byte("test"), []byte("theKey"))
if reg != nil {
t.Errorf("Found region %#v even though the cache was empty?!", reg)
}
// Inject an entry in the cache. This entry covers the entire key range.
wholeTable := ®ioninfo.Info{
Table: []byte("test"),
RegionName: []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
StopKey: []byte(""),
}
regClient := ®ion.Client{}
client.regions.put(wholeTable.RegionName, wholeTable)
client.clients.put(wholeTable, regClient)
reg = client.getRegionFromCache([]byte("test"), []byte("theKey"))
if !reflect.DeepEqual(reg, wholeTable) {
t.Errorf("Found region %#v but expected %#v", reg, wholeTable)
}
reg = client.getRegionFromCache([]byte("test"), []byte("")) // edge case.
if !reflect.DeepEqual(reg, wholeTable) {
t.Errorf("Found region %#v but expected %#v", reg, wholeTable)
}
// Clear our client.
client = newClient("~invalid.quorum~")
// Inject 3 entries in the cache.
region1 := ®ioninfo.Info{
Table: []byte("test"),
RegionName: []byte("test,,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
StopKey: []byte("foo"),
}
client.regions.put(region1.RegionName, region1)
client.clients.put(region1, regClient)
region2 := ®ioninfo.Info{
Table: []byte("test"),
RegionName: []byte("test,foo,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
StopKey: []byte("gohbase"),
}
client.regions.put(region2.RegionName, region2)
client.clients.put(region2, regClient)
region3 := ®ioninfo.Info{
Table: []byte("test"),
RegionName: []byte("test,gohbase,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
StopKey: []byte(""),
}
client.regions.put(region3.RegionName, region3)
client.clients.put(region3, regClient)
testcases := []struct {
key string
reg *regioninfo.Info
}{
{key: "theKey", reg: region3},
{key: "", reg: region1},
{key: "bar", reg: region1},
{key: "fon\xFF", reg: region1},
{key: "foo", reg: region2},
{key: "foo\x00", reg: region2},
{key: "gohbase", reg: region3},
}
for i, testcase := range testcases {
reg = client.getRegionFromCache([]byte("test"), []byte(testcase.key))
if !reflect.DeepEqual(reg, testcase.reg) {
t.Errorf("[#%d] Found region %#v but expected %#v", i, reg, testcase.reg)
}
}
// Change the last region (maybe it got split).
region3 = ®ioninfo.Info{
Table: []byte("test"),
RegionName: []byte("test,gohbase,1234567890042.56f833d5569a27c7a43fbf547b4924a4."),
StopKey: []byte("zab"),
}
client.regions.put(region3.RegionName, region3)
client.clients.put(region3, regClient)
reg = client.getRegionFromCache([]byte("test"), []byte("theKey"))
if !reflect.DeepEqual(reg, region3) {
t.Errorf("Found region %#v but expected %#v", reg, region3)
}
reg = client.getRegionFromCache([]byte("test"), []byte("zoo"))
if reg != nil {
t.Errorf("Shouldn't have found any region yet found %#v", reg)
}
}