forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyspace.go
84 lines (76 loc) · 2.51 KB
/
keyspace.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
// package test contains utilities to test topo.Server
// implementations. If you are testing your implementation, you will
// want to call CheckAll in your test method. For an example, look at
// the tests in github.com/youtube/vitess/go/vt/zktopo.
package test
import (
"testing"
"github.com/youtube/vitess/go/vt/key"
"github.com/youtube/vitess/go/vt/topo"
)
func CheckKeyspace(t *testing.T, ts topo.Server) {
keyspaces, err := ts.GetKeyspaces()
if err != nil {
t.Errorf("GetKeyspaces(empty): %v", err)
}
if len(keyspaces) != 0 {
t.Errorf("len(GetKeyspaces()) != 0: %v", keyspaces)
}
if err := ts.CreateKeyspace("test_keyspace", &topo.Keyspace{}); err != nil {
t.Errorf("CreateKeyspace: %v", err)
}
if err := ts.CreateKeyspace("test_keyspace", &topo.Keyspace{}); err != topo.ErrNodeExists {
t.Errorf("CreateKeyspace(again) is not ErrNodeExists: %v", err)
}
keyspaces, err = ts.GetKeyspaces()
if err != nil {
t.Errorf("GetKeyspaces: %v", err)
}
if len(keyspaces) != 1 || keyspaces[0] != "test_keyspace" {
t.Errorf("GetKeyspaces: want %v, got %v", []string{"test_keyspace"}, keyspaces)
}
if err := ts.CreateKeyspace("test_keyspace2", &topo.Keyspace{
ShardingColumnName: "user_id",
ShardingColumnType: key.KIT_UINT64,
ServedFrom: map[topo.TabletType]string{
topo.TYPE_MASTER: "test_keyspace3",
},
}); err != nil {
t.Errorf("CreateKeyspace: %v", err)
}
keyspaces, err = ts.GetKeyspaces()
if err != nil {
t.Errorf("GetKeyspaces: %v", err)
}
if len(keyspaces) != 2 ||
keyspaces[0] != "test_keyspace" ||
keyspaces[1] != "test_keyspace2" {
t.Errorf("GetKeyspaces: want %v, got %v", []string{"test_keyspace", "test_keyspace2"}, keyspaces)
}
ki, err := ts.GetKeyspace("test_keyspace2")
if err != nil {
t.Fatalf("GetKeyspace: %v", err)
}
if ki.ShardingColumnName != "user_id" ||
ki.ShardingColumnType != key.KIT_UINT64 ||
ki.ServedFrom[topo.TYPE_MASTER] != "test_keyspace3" {
t.Errorf("GetKeyspace: unexpected keyspace, got %v", *ki)
}
ki.ShardingColumnName = "other_id"
ki.ShardingColumnType = key.KIT_BYTES
delete(ki.ServedFrom, topo.TYPE_MASTER)
ki.ServedFrom[topo.TYPE_REPLICA] = "test_keyspace4"
err = ts.UpdateKeyspace(ki)
if err != nil {
t.Fatalf("UpdateKeyspace: %v", err)
}
ki, err = ts.GetKeyspace("test_keyspace2")
if err != nil {
t.Fatalf("GetKeyspace: %v", err)
}
if ki.ShardingColumnName != "other_id" ||
ki.ShardingColumnType != key.KIT_BYTES ||
ki.ServedFrom[topo.TYPE_REPLICA] != "test_keyspace4" {
t.Errorf("GetKeyspace: unexpected keyspace, got %v", *ki)
}
}