func removeNeedlessRangeReqs(txn *pb.TxnRequest) { f := func(ops []*pb.RequestOp) []*pb.RequestOp { j := 0 for i := 0; i < len(ops); i++ { if _, ok := ops[i].Request.(*pb.RequestOp_RequestRange); ok { continue } ops[j] = ops[i] j++ } return ops[:j] } txn.Success = f(txn.Success) txn.Failure = f(txn.Failure) }
func successState(txn *pb.TxnRequest, r *bufio.Reader) stateFunc { fmt.Println("entry success request[method key value(end_range)] (end with empty line):") line, err := r.ReadString('\n') if err != nil { ExitWithError(ExitInvalidInput, err) } if len(line) == 1 { return failureState } // remove trialling \n line = line[:len(line)-1] ru, err := parseRequestUnion(line) if err != nil { ExitWithError(ExitInvalidInput, err) } txn.Success = append(txn.Success, ru) return successState }
// testV3WatchMultipleEventsTxn tests Watch APIs when it receives multiple events. func testV3WatchMultipleEventsTxn(t *testing.T, startRev int64) { clus := newClusterGRPC(t, &clusterConfig{size: 3}) wAPI := pb.NewWatchClient(clus.RandConn()) wStream, wErr := wAPI.Watch(context.TODO()) if wErr != nil { t.Fatalf("wAPI.Watch error: %v", wErr) } if err := wStream.Send(&pb.WatchRequest{CreateRequest: &pb.WatchCreateRequest{Prefix: []byte("foo"), StartRevision: startRev}}); err != nil { t.Fatalf("wStream.Send error: %v", err) } kvc := pb.NewKVClient(clus.RandConn()) txn := pb.TxnRequest{} for i := 0; i < 3; i++ { ru := &pb.RequestUnion{} ru.RequestPut = &pb.PutRequest{Key: []byte(fmt.Sprintf("foo%d", i)), Value: []byte("bar")} txn.Success = append(txn.Success, ru) } tresp, err := kvc.Txn(context.Background(), &txn) if err != nil { t.Fatalf("kvc.Txn error: %v", err) } if !tresp.Succeeded { t.Fatalf("kvc.Txn failed: %+v", tresp) } events := []*storagepb.Event{} for len(events) < 3 { resp, err := wStream.Recv() if err != nil { t.Errorf("wStream.Recv error: %v", err) } if resp.Created { continue } events = append(events, resp.Events...) } sort.Sort(eventsSortByKey(events)) wevents := []*storagepb.Event{ { Type: storagepb.PUT, Kv: &storagepb.KeyValue{Key: []byte("foo0"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1}, }, { Type: storagepb.PUT, Kv: &storagepb.KeyValue{Key: []byte("foo1"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1}, }, { Type: storagepb.PUT, Kv: &storagepb.KeyValue{Key: []byte("foo2"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1}, }, } if !reflect.DeepEqual(events, wevents) { t.Errorf("events got = %+v, want = %+v", events, wevents) } rok, nr := WaitResponse(wStream, 1*time.Second) if !rok { t.Errorf("unexpected pb.WatchResponse is received %+v", nr) } // can't defer because tcp ports will be in use clus.Terminate(t) }
// testV3WatchMultipleEventsTxn tests Watch APIs when it receives multiple events. func testV3WatchMultipleEventsTxn(t *testing.T, startRev int64) { clus := NewClusterV3(t, &ClusterConfig{Size: 3}) ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() wStream, wErr := toGRPC(clus.RandClient()).Watch.Watch(ctx) if wErr != nil { t.Fatalf("wAPI.Watch error: %v", wErr) } wreq := &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{ CreateRequest: &pb.WatchCreateRequest{ Key: []byte("foo"), RangeEnd: []byte("fop"), StartRevision: startRev}}} if err := wStream.Send(wreq); err != nil { t.Fatalf("wStream.Send error: %v", err) } kvc := toGRPC(clus.RandClient()).KV txn := pb.TxnRequest{} for i := 0; i < 3; i++ { ru := &pb.RequestUnion{} ru.Request = &pb.RequestUnion_RequestPut{ RequestPut: &pb.PutRequest{ Key: []byte(fmt.Sprintf("foo%d", i)), Value: []byte("bar")}} txn.Success = append(txn.Success, ru) } tresp, err := kvc.Txn(context.Background(), &txn) if err != nil { t.Fatalf("kvc.Txn error: %v", err) } if !tresp.Succeeded { t.Fatalf("kvc.Txn failed: %+v", tresp) } events := []*mvccpb.Event{} for len(events) < 3 { resp, err := wStream.Recv() if err != nil { t.Errorf("wStream.Recv error: %v", err) } if resp.Created { continue } events = append(events, resp.Events...) } sort.Sort(eventsSortByKey(events)) wevents := []*mvccpb.Event{ { Type: mvccpb.PUT, Kv: &mvccpb.KeyValue{Key: []byte("foo0"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1}, }, { Type: mvccpb.PUT, Kv: &mvccpb.KeyValue{Key: []byte("foo1"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1}, }, { Type: mvccpb.PUT, Kv: &mvccpb.KeyValue{Key: []byte("foo2"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1}, }, } if !reflect.DeepEqual(events, wevents) { t.Errorf("events got = %+v, want = %+v", events, wevents) } rok, nr := waitResponse(wStream, 1*time.Second) if !rok { t.Errorf("unexpected pb.WatchResponse is received %+v", nr) } // can't defer because tcp ports will be in use clus.Terminate(t) }