Skip to content
/ etcd Public
forked from etcd-io/etcd

Commit

Permalink
integration: test with multi events in unsynced
Browse files Browse the repository at this point in the history
Related etcd-io#4216.
  • Loading branch information
gyuho committed Jan 19, 2016
1 parent 72195af commit bed42f5
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions integration/v3_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,6 @@ func TestV3WatchFromCurrentRevision(t *testing.T) {
},
},
},

// TODO: watch and receive multiple-events from synced (need Txn)
}

for i, tt := range tests {
Expand Down Expand Up @@ -636,6 +634,68 @@ func TestV3WatchMultipleEventsFromCurrentRevision(t *testing.T) {
clus.Terminate(t)
}

// TestV3WatchMultipleEventsFromUnsynced tests Watch APIs from unsynced watchers.
func TestV3WatchMultipleEventsFromUnsynced(t *testing.T) {
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: 1}}); err != nil {
t.Fatalf("wStream.Send error: %v", err)
}

kvc := pb.NewKVClient(clus.RandConn())
for i := 0; i < 3; i++ {
if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte(fmt.Sprintf("foo%d", i)), Value: []byte("bar")}); err != nil {
t.Fatalf("couldn't put key (%v)", err)
}
}

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: 3, ModRevision: 3, Version: 1},
},
{
Type: storagepb.PUT,
Kv: &storagepb.KeyValue{Key: []byte("foo2"), Value: []byte("bar"), CreateRevision: 4, ModRevision: 4, 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)
}

type eventsSortByKey []*storagepb.Event

func (evs eventsSortByKey) Len() int { return len(evs) }
Expand Down

0 comments on commit bed42f5

Please sign in to comment.