本文整理汇总了Golang中github.com/youtube/vitess/go/vt/vtgate/proto.KeyspaceIdQuery.KeyspaceIds方法的典型用法代码示例。如果您正苦于以下问题:Golang KeyspaceIdQuery.KeyspaceIds方法的具体用法?Golang KeyspaceIdQuery.KeyspaceIds怎么用?Golang KeyspaceIdQuery.KeyspaceIds使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/youtube/vitess/go/vt/vtgate/proto.KeyspaceIdQuery
的用法示例。
在下文中一共展示了KeyspaceIdQuery.KeyspaceIds方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestVTGateExecuteKeyspaceIds
func TestVTGateExecuteKeyspaceIds(t *testing.T) {
s := createSandbox("TestVTGateExecuteKeyspaceIds")
sbc1 := &sandboxConn{}
sbc2 := &sandboxConn{}
s.MapTestConn("-20", sbc1)
s.MapTestConn("20-40", sbc2)
kid10, err := key.HexKeyspaceId("10").Unhex()
if err != nil {
t.Errorf("want nil, got %+v", err)
}
q := proto.KeyspaceIdQuery{
Sql: "query",
Keyspace: "TestVTGateExecuteKeyspaceIds",
KeyspaceIds: []key.KeyspaceId{kid10},
TabletType: topo.TYPE_MASTER,
}
// Test for successful execution
qr := new(proto.QueryResult)
err = RpcVTGate.ExecuteKeyspaceIds(nil, &q, qr)
if err != nil {
t.Errorf("want nil, got %v", err)
}
wantqr := new(proto.QueryResult)
proto.PopulateQueryResult(singleRowResult, wantqr)
if !reflect.DeepEqual(wantqr, qr) {
t.Errorf("want \n%+v, got \n%+v", singleRowResult, qr)
}
if qr.Session != nil {
t.Errorf("want nil, got %+v\n", qr.Session)
}
if sbc1.ExecCount != 1 {
t.Errorf("want 1, got %v\n", sbc1.ExecCount)
}
// Test for successful execution in transaction
q.Session = new(proto.Session)
RpcVTGate.Begin(nil, q.Session)
if !q.Session.InTransaction {
t.Errorf("want true, got false")
}
RpcVTGate.ExecuteKeyspaceIds(nil, &q, qr)
wantSession := &proto.Session{
InTransaction: true,
ShardSessions: []*proto.ShardSession{{
Keyspace: "TestVTGateExecuteKeyspaceIds",
Shard: "-20",
TransactionId: 1,
TabletType: topo.TYPE_MASTER,
}},
}
if !reflect.DeepEqual(wantSession, q.Session) {
t.Errorf("want \n%+v, got \n%+v", wantSession, q.Session)
}
RpcVTGate.Commit(nil, q.Session)
if sbc1.CommitCount.Get() != 1 {
t.Errorf("want 1, got %d", sbc1.CommitCount.Get())
}
// Test for multiple shards
kid30, err := key.HexKeyspaceId("30").Unhex()
if err != nil {
t.Errorf("want nil, got %+v", err)
}
q.KeyspaceIds = []key.KeyspaceId{kid10, kid30}
RpcVTGate.ExecuteKeyspaceIds(nil, &q, qr)
if qr.RowsAffected != 2 {
t.Errorf("want 2, got %v", qr.RowsAffected)
}
}
示例2: TestVTGateStreamExecuteKeyspaceIds
func TestVTGateStreamExecuteKeyspaceIds(t *testing.T) {
s := createSandbox("TestVTGateStreamExecuteKeyspaceIds")
sbc := &sandboxConn{}
s.MapTestConn("-20", sbc)
kid10, err := key.HexKeyspaceId("10").Unhex()
if err != nil {
t.Errorf("want nil, got %+v", err)
}
sq := proto.KeyspaceIdQuery{
Sql: "query",
Keyspace: "TestVTGateStreamExecuteKeyspaceIds",
KeyspaceIds: []key.KeyspaceId{kid10},
TabletType: topo.TYPE_MASTER,
}
// Test for successful execution
var qrs []*proto.QueryResult
err = RpcVTGate.StreamExecuteKeyspaceIds(nil, &sq, func(r *proto.QueryResult) error {
qrs = append(qrs, r)
return nil
})
if err != nil {
t.Errorf("want nil, got %v", err)
}
row := new(proto.QueryResult)
proto.PopulateQueryResult(singleRowResult, row)
want := []*proto.QueryResult{row}
if !reflect.DeepEqual(want, qrs) {
t.Errorf("want \n%+v, got \n%+v", want, qrs)
}
// Test for successful execution in transaction
sq.Session = new(proto.Session)
qrs = nil
RpcVTGate.Begin(nil, sq.Session)
err = RpcVTGate.StreamExecuteKeyspaceIds(nil, &sq, func(r *proto.QueryResult) error {
qrs = append(qrs, r)
return nil
})
want = []*proto.QueryResult{
row,
&proto.QueryResult{
Session: &proto.Session{
InTransaction: true,
ShardSessions: []*proto.ShardSession{{
Keyspace: "TestVTGateStreamExecuteKeyspaceIds",
Shard: "-20",
TransactionId: 1,
TabletType: topo.TYPE_MASTER,
}},
},
},
}
if !reflect.DeepEqual(want, qrs) {
t.Errorf("want \n%+v, got \n%+v", want, qrs)
}
RpcVTGate.Commit(nil, sq.Session)
if sbc.CommitCount.Get() != 1 {
t.Errorf("want 1, got %d", sbc.CommitCount.Get())
}
// Test for successful execution - multiple keyspaceids in single shard
sq.Session = nil
qrs = nil
kid15, err := key.HexKeyspaceId("15").Unhex()
if err != nil {
t.Errorf("want nil, got %+v", err)
}
sq.KeyspaceIds = []key.KeyspaceId{kid10, kid15}
err = RpcVTGate.StreamExecuteKeyspaceIds(nil, &sq, func(r *proto.QueryResult) error {
qrs = append(qrs, r)
return nil
})
if err != nil {
t.Errorf("want nil, got %v", err)
}
row = new(proto.QueryResult)
proto.PopulateQueryResult(singleRowResult, row)
want = []*proto.QueryResult{row}
if !reflect.DeepEqual(want, qrs) {
t.Errorf("want \n%+v, got \n%+v", want, qrs)
}
// Test for error condition - multiple shards
kid30, err := key.HexKeyspaceId("30").Unhex()
if err != nil {
t.Errorf("want nil, got %+v", err)
}
sq.KeyspaceIds = []key.KeyspaceId{kid10, kid30}
err = RpcVTGate.StreamExecuteKeyspaceIds(nil, &sq, func(r *proto.QueryResult) error {
qrs = append(qrs, r)
return nil
})
if err == nil {
t.Errorf("want not nil, got nil")
}
}