func TestResolverExecuteEntityIds(t *testing.T) { testResolverGeneric(t, "TestResolverExecuteEntityIds", func() (*mproto.QueryResult, error) { kid10, err := key.HexKeyspaceId("10").Unhex() if err != nil { return nil, err } kid25, err := key.HexKeyspaceId("25").Unhex() if err != nil { return nil, err } query := &proto.EntityIdsQuery{ Sql: "query", Keyspace: "TestResolverExecuteEntityIds", EntityColumnName: "col", EntityKeyspaceIDs: []proto.EntityId{ proto.EntityId{ ExternalID: 0, KeyspaceID: kid10, }, proto.EntityId{ ExternalID: "1", KeyspaceID: kid25, }, }, TabletType: topo.TYPE_MASTER, } res := NewResolver(new(sandboxTopo), "", "aa", 1*time.Millisecond, 0, 1*time.Millisecond) return res.ExecuteEntityIds(&context.DummyContext{}, query) }) }
func TestResolverDmlOnMultipleKeyspaceIds(t *testing.T) { kid10, err := key.HexKeyspaceId("10").Unhex() if err != nil { t.Errorf("Error encoding keyspace id") } kid25, err := key.HexKeyspaceId("25").Unhex() if err != nil { t.Errorf("Error encoding keyspace id") } query := &proto.KeyspaceIdQuery{ Sql: "update table set a = b", Keyspace: "TestResolverExecuteKeyspaceIds", KeyspaceIds: []key.KeyspaceId{kid10, kid25}, TabletType: topo.TYPE_MASTER, } res := NewResolver(new(sandboxTopo), "", "aa", 1*time.Millisecond, 0, 1*time.Millisecond) s := createSandbox("TestResolverDmlOnMultipleKeyspaceIds") sbc0 := &sandboxConn{} s.MapTestConn("-20", sbc0) sbc1 := &sandboxConn{} s.MapTestConn("20-40", sbc1) errStr := "DML should not span multiple keyspace_ids" _, err = res.ExecuteKeyspaceIds(&context.DummyContext{}, query) if err == nil { t.Errorf("want %v, got nil", errStr) } }
func hki(hexValue string) key.KeyspaceId { k, err := key.HexKeyspaceId(hexValue).Unhex() if err != nil { panic(err) } return k }
func TestVTGateExecuteBatchKeyspaceIds(t *testing.T) { s := createSandbox("TestVTGateExecuteBatchKeyspaceIds") s.MapTestConn("-20", &sandboxConn{}) s.MapTestConn("20-40", &sandboxConn{}) kid10, err := key.HexKeyspaceId("10").Unhex() if err != nil { t.Errorf("want nil, got %v", err) } kid30, err := key.HexKeyspaceId("30").Unhex() if err != nil { t.Errorf("want nil, got %v", err) } q := proto.KeyspaceIdBatchQuery{ Queries: []tproto.BoundQuery{{ "query", nil, }, { "query", nil, }}, Keyspace: "TestVTGateExecuteBatchKeyspaceIds", KeyspaceIds: []key.KeyspaceId{kid10, kid30}, TabletType: topo.TYPE_MASTER, } qrl := new(proto.QueryResultList) err = RpcVTGate.ExecuteBatchKeyspaceIds(&context.DummyContext{}, &q, qrl) if err != nil { t.Errorf("want nil, got %v", err) } if len(qrl.List) != 2 { t.Errorf("want 2, got %v", len(qrl.List)) } if qrl.List[0].RowsAffected != 2 { t.Errorf("want 2, got %v", qrl.List[0].RowsAffected) } if qrl.Session != nil { t.Errorf("want nil, got %+v\n", qrl.Session) } q.Session = new(proto.Session) RpcVTGate.Begin(&context.DummyContext{}, q.Session) RpcVTGate.ExecuteBatchKeyspaceIds(&context.DummyContext{}, &q, qrl) if len(q.Session.ShardSessions) != 2 { t.Errorf("want 2, got %d", len(q.Session.ShardSessions)) } }
func TestResolverStreamExecuteKeyRanges(t *testing.T) { kid10, err := key.HexKeyspaceId("10").Unhex() if err != nil { t.Errorf(err.Error()) } kid15, err := key.HexKeyspaceId("15").Unhex() if err != nil { t.Errorf(err.Error()) } kid25, err := key.HexKeyspaceId("25").Unhex() if err != nil { t.Errorf(err.Error()) } query := &proto.KeyRangeQuery{ Sql: "query", Keyspace: "TestResolverStreamExecuteKeyRanges", KeyRanges: []key.KeyRange{key.KeyRange{Start: kid10, End: kid15}}, TabletType: topo.TYPE_MASTER, } createSandbox("TestResolverStreamExecuteKeyRanges") // streaming a single shard testResolverStreamGeneric(t, "TestResolverStreamExecuteKeyRanges", func() (*mproto.QueryResult, error) { res := NewResolver(new(sandboxTopo), "", "aa", 1*time.Millisecond, 0, 1*time.Millisecond) qr := new(mproto.QueryResult) err = res.StreamExecuteKeyRanges(&context.DummyContext{}, query, func(r *mproto.QueryResult) error { appendResult(qr, r) return nil }) return qr, err }) // streaming multiple shards testResolverStreamGeneric(t, "TestResolverStreamExecuteKeyRanges", func() (*mproto.QueryResult, error) { query.KeyRanges = []key.KeyRange{key.KeyRange{Start: kid10, End: kid25}} res := NewResolver(new(sandboxTopo), "", "aa", 1*time.Millisecond, 0, 1*time.Millisecond) qr := new(mproto.QueryResult) err = res.StreamExecuteKeyRanges(&context.DummyContext{}, query, func(r *mproto.QueryResult) error { appendResult(qr, r) return nil }) return qr, err }) }
func TestResolverExecuteKeyRanges(t *testing.T) { testResolverGeneric(t, "TestResolverExecuteKeyRanges", func() (*mproto.QueryResult, error) { kid10, err := key.HexKeyspaceId("10").Unhex() if err != nil { return nil, err } kid25, err := key.HexKeyspaceId("25").Unhex() if err != nil { return nil, err } query := &proto.KeyRangeQuery{ Sql: "query", Keyspace: "TestResolverExecuteKeyRanges", KeyRanges: []key.KeyRange{key.KeyRange{Start: kid10, End: kid25}}, TabletType: topo.TYPE_MASTER, } res := NewResolver(new(sandboxTopo), "", "aa", 1*time.Millisecond, 0, 1*time.Millisecond) return res.ExecuteKeyRanges(&context.DummyContext{}, query) }) }
func TestResolverExecuteBatchKeyspaceIds(t *testing.T) { testResolverGeneric(t, "TestResolverExecuteBatchKeyspaceIds", func() (*mproto.QueryResult, error) { kid10, err := key.HexKeyspaceId("10").Unhex() if err != nil { return nil, err } kid25, err := key.HexKeyspaceId("25").Unhex() if err != nil { return nil, err } query := &proto.KeyspaceIdBatchQuery{ Queries: []tproto.BoundQuery{{"query", nil}}, Keyspace: "TestResolverExecuteBatchKeyspaceIds", KeyspaceIds: []key.KeyspaceId{kid10, kid25}, TabletType: topo.TYPE_MASTER, } res := NewResolver(new(sandboxTopo), "", "aa", 1*time.Millisecond, 0, 1*time.Millisecond) qrs, err := res.ExecuteBatchKeyspaceIds(&context.DummyContext{}, query) if err != nil { return nil, err } return &qrs.List[0], err }) }
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(&context.DummyContext{}, &q, qr) if err != nil { t.Errorf("want nil, got %v", err) } wantqr := new(proto.QueryResult) wantqr.Result = singleRowResult 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(&context.DummyContext{}, q.Session) if !q.Session.InTransaction { t.Errorf("want true, got false") } RpcVTGate.ExecuteKeyspaceIds(&context.DummyContext{}, &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(&context.DummyContext{}, 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(&context.DummyContext{}, &q, qr) if qr.Result.RowsAffected != 2 { t.Errorf("want 2, got %v", qr.Result.RowsAffected) } }
func TestVTGateStreamExecuteKeyspaceIds(t *testing.T) { s := createSandbox("TestVTGateStreamExecuteKeyspaceIds") sbc := &sandboxConn{} s.MapTestConn("-20", sbc) sbc1 := &sandboxConn{} s.MapTestConn("20-40", sbc1) 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(&context.DummyContext{}, &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) row.Result = singleRowResult 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(&context.DummyContext{}, sq.Session) err = RpcVTGate.StreamExecuteKeyspaceIds(&context.DummyContext{}, &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\ngot\n%#v", want, qrs) } RpcVTGate.Commit(&context.DummyContext{}, 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(&context.DummyContext{}, &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) row.Result = singleRowResult want = []*proto.QueryResult{row} if !reflect.DeepEqual(want, qrs) { t.Errorf("want \n%+v, got \n%+v", want, qrs) } // Test for successful execution - multiple keyspaceids in 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(&context.DummyContext{}, &sq, func(r *proto.QueryResult) error { qrs = append(qrs, r) return nil }) if err != nil { t.Errorf("want nil, got %v", err) } }