v3_watch_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.package recipe
  14. package integration
  15. import (
  16. "bytes"
  17. "fmt"
  18. "reflect"
  19. "sort"
  20. "sync"
  21. "testing"
  22. "time"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  24. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  25. "github.com/coreos/etcd/pkg/testutil"
  26. "github.com/coreos/etcd/storage/storagepb"
  27. )
  28. // TestV3WatchFromCurrentRevision tests Watch APIs from current revision.
  29. func TestV3WatchFromCurrentRevision(t *testing.T) {
  30. defer testutil.AfterTest(t)
  31. tests := []struct {
  32. putKeys []string
  33. watchRequest *pb.WatchRequest
  34. wresps []*pb.WatchResponse
  35. }{
  36. // watch the key, matching
  37. {
  38. []string{"foo"},
  39. &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  40. CreateRequest: &pb.WatchCreateRequest{
  41. Key: []byte("foo")}}},
  42. []*pb.WatchResponse{
  43. {
  44. Header: &pb.ResponseHeader{Revision: 1},
  45. Created: true,
  46. },
  47. {
  48. Header: &pb.ResponseHeader{Revision: 2},
  49. Created: false,
  50. Events: []*storagepb.Event{
  51. {
  52. Type: storagepb.PUT,
  53. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  54. },
  55. },
  56. },
  57. },
  58. },
  59. // watch the key, non-matching
  60. {
  61. []string{"foo"},
  62. &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  63. CreateRequest: &pb.WatchCreateRequest{
  64. Key: []byte("helloworld")}}},
  65. []*pb.WatchResponse{
  66. {
  67. Header: &pb.ResponseHeader{Revision: 1},
  68. Created: true,
  69. },
  70. },
  71. },
  72. // watch the prefix, matching
  73. {
  74. []string{"fooLong"},
  75. &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  76. CreateRequest: &pb.WatchCreateRequest{
  77. Prefix: []byte("foo")}}},
  78. []*pb.WatchResponse{
  79. {
  80. Header: &pb.ResponseHeader{Revision: 1},
  81. Created: true,
  82. },
  83. {
  84. Header: &pb.ResponseHeader{Revision: 2},
  85. Created: false,
  86. Events: []*storagepb.Event{
  87. {
  88. Type: storagepb.PUT,
  89. Kv: &storagepb.KeyValue{Key: []byte("fooLong"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  90. },
  91. },
  92. },
  93. },
  94. },
  95. // watch the prefix, non-matching
  96. {
  97. []string{"foo"},
  98. &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  99. CreateRequest: &pb.WatchCreateRequest{
  100. Prefix: []byte("helloworld")}}},
  101. []*pb.WatchResponse{
  102. {
  103. Header: &pb.ResponseHeader{Revision: 1},
  104. Created: true,
  105. },
  106. },
  107. },
  108. // multiple puts, one watcher with matching key
  109. {
  110. []string{"foo", "foo", "foo"},
  111. &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  112. CreateRequest: &pb.WatchCreateRequest{
  113. Key: []byte("foo")}}},
  114. []*pb.WatchResponse{
  115. {
  116. Header: &pb.ResponseHeader{Revision: 1},
  117. Created: true,
  118. },
  119. {
  120. Header: &pb.ResponseHeader{Revision: 2},
  121. Created: false,
  122. Events: []*storagepb.Event{
  123. {
  124. Type: storagepb.PUT,
  125. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  126. },
  127. },
  128. },
  129. {
  130. Header: &pb.ResponseHeader{Revision: 3},
  131. Created: false,
  132. Events: []*storagepb.Event{
  133. {
  134. Type: storagepb.PUT,
  135. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 3, Version: 2},
  136. },
  137. },
  138. },
  139. {
  140. Header: &pb.ResponseHeader{Revision: 4},
  141. Created: false,
  142. Events: []*storagepb.Event{
  143. {
  144. Type: storagepb.PUT,
  145. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 4, Version: 3},
  146. },
  147. },
  148. },
  149. },
  150. },
  151. // multiple puts, one watcher with matching prefix
  152. {
  153. []string{"foo", "foo", "foo"},
  154. &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  155. CreateRequest: &pb.WatchCreateRequest{
  156. Prefix: []byte("foo")}}},
  157. []*pb.WatchResponse{
  158. {
  159. Header: &pb.ResponseHeader{Revision: 1},
  160. Created: true,
  161. },
  162. {
  163. Header: &pb.ResponseHeader{Revision: 2},
  164. Created: false,
  165. Events: []*storagepb.Event{
  166. {
  167. Type: storagepb.PUT,
  168. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  169. },
  170. },
  171. },
  172. {
  173. Header: &pb.ResponseHeader{Revision: 3},
  174. Created: false,
  175. Events: []*storagepb.Event{
  176. {
  177. Type: storagepb.PUT,
  178. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 3, Version: 2},
  179. },
  180. },
  181. },
  182. {
  183. Header: &pb.ResponseHeader{Revision: 4},
  184. Created: false,
  185. Events: []*storagepb.Event{
  186. {
  187. Type: storagepb.PUT,
  188. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 4, Version: 3},
  189. },
  190. },
  191. },
  192. },
  193. },
  194. }
  195. for i, tt := range tests {
  196. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  197. wAPI := clus.RandClient().Watch
  198. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  199. defer cancel()
  200. wStream, err := wAPI.Watch(ctx)
  201. if err != nil {
  202. t.Fatalf("#%d: wAPI.Watch error: %v", i, err)
  203. }
  204. if err := wStream.Send(tt.watchRequest); err != nil {
  205. t.Fatalf("#%d: wStream.Send error: %v", i, err)
  206. }
  207. go func() {
  208. for _, k := range tt.putKeys {
  209. kvc := clus.RandClient().KV
  210. req := &pb.PutRequest{Key: []byte(k), Value: []byte("bar")}
  211. if _, err := kvc.Put(context.TODO(), req); err != nil {
  212. t.Fatalf("#%d: couldn't put key (%v)", i, err)
  213. }
  214. }
  215. }()
  216. var createdWatchId int64
  217. for j, wresp := range tt.wresps {
  218. resp, err := wStream.Recv()
  219. if err != nil {
  220. t.Errorf("#%d.%d: wStream.Recv error: %v", i, j, err)
  221. }
  222. if resp.Header == nil {
  223. t.Fatalf("#%d.%d: unexpected nil resp.Header", i, j)
  224. }
  225. if resp.Header.Revision != wresp.Header.Revision {
  226. t.Errorf("#%d.%d: resp.Header.Revision got = %d, want = %d", i, j, resp.Header.Revision, wresp.Header.Revision)
  227. }
  228. if wresp.Created != resp.Created {
  229. t.Errorf("#%d.%d: resp.Created got = %v, want = %v", i, j, resp.Created, wresp.Created)
  230. }
  231. if resp.Created {
  232. createdWatchId = resp.WatchId
  233. }
  234. if resp.WatchId != createdWatchId {
  235. t.Errorf("#%d.%d: resp.WatchId got = %d, want = %d", i, j, resp.WatchId, createdWatchId)
  236. }
  237. if !reflect.DeepEqual(resp.Events, wresp.Events) {
  238. t.Errorf("#%d.%d: resp.Events got = %+v, want = %+v", i, j, resp.Events, wresp.Events)
  239. }
  240. }
  241. rok, nr := waitResponse(wStream, 1*time.Second)
  242. if !rok {
  243. t.Errorf("unexpected pb.WatchResponse is received %+v", nr)
  244. }
  245. // can't defer because tcp ports will be in use
  246. clus.Terminate(t)
  247. }
  248. }
  249. // TestV3WatchCancelSynced tests Watch APIs cancellation from synced map.
  250. func TestV3WatchCancelSynced(t *testing.T) {
  251. defer testutil.AfterTest(t)
  252. testV3WatchCancel(t, 0)
  253. }
  254. // TestV3WatchCancelUnsynced tests Watch APIs cancellation from unsynced map.
  255. func TestV3WatchCancelUnsynced(t *testing.T) {
  256. defer testutil.AfterTest(t)
  257. testV3WatchCancel(t, 1)
  258. }
  259. func testV3WatchCancel(t *testing.T, startRev int64) {
  260. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  261. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  262. defer cancel()
  263. wStream, errW := clus.RandClient().Watch.Watch(ctx)
  264. if errW != nil {
  265. t.Fatalf("wAPI.Watch error: %v", errW)
  266. }
  267. wreq := &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  268. CreateRequest: &pb.WatchCreateRequest{
  269. Key: []byte("foo"), StartRevision: startRev}}}
  270. if err := wStream.Send(wreq); err != nil {
  271. t.Fatalf("wStream.Send error: %v", err)
  272. }
  273. wresp, errR := wStream.Recv()
  274. if errR != nil {
  275. t.Errorf("wStream.Recv error: %v", errR)
  276. }
  277. if !wresp.Created {
  278. t.Errorf("wresp.Created got = %v, want = true", wresp.Created)
  279. }
  280. creq := &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CancelRequest{
  281. CancelRequest: &pb.WatchCancelRequest{
  282. WatchId: wresp.WatchId}}}
  283. if err := wStream.Send(creq); err != nil {
  284. t.Fatalf("wStream.Send error: %v", err)
  285. }
  286. cresp, err := wStream.Recv()
  287. if err != nil {
  288. t.Errorf("wStream.Recv error: %v", err)
  289. }
  290. if !cresp.Canceled {
  291. t.Errorf("cresp.Canceled got = %v, want = true", cresp.Canceled)
  292. }
  293. kvc := clus.RandClient().KV
  294. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}); err != nil {
  295. t.Errorf("couldn't put key (%v)", err)
  296. }
  297. // watch got canceled, so this should block
  298. rok, nr := waitResponse(wStream, 1*time.Second)
  299. if !rok {
  300. t.Errorf("unexpected pb.WatchResponse is received %+v", nr)
  301. }
  302. clus.Terminate(t)
  303. }
  304. func TestV3WatchMultipleWatchersSynced(t *testing.T) {
  305. defer testutil.AfterTest(t)
  306. testV3WatchMultipleWatchers(t, 0)
  307. }
  308. func TestV3WatchMultipleWatchersUnsynced(t *testing.T) {
  309. defer testutil.AfterTest(t)
  310. testV3WatchMultipleWatchers(t, 1)
  311. }
  312. // testV3WatchMultipleWatchers tests multiple watchers on the same key
  313. // and one watcher with matching prefix. It first puts the key
  314. // that matches all watchers, and another key that matches only
  315. // one watcher to test if it receives expected events.
  316. func testV3WatchMultipleWatchers(t *testing.T, startRev int64) {
  317. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  318. kvc := clus.RandClient().KV
  319. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  320. defer cancel()
  321. wStream, errW := clus.RandClient().Watch.Watch(ctx)
  322. if errW != nil {
  323. t.Fatalf("wAPI.Watch error: %v", errW)
  324. }
  325. watchKeyN := 4
  326. for i := 0; i < watchKeyN+1; i++ {
  327. var wreq *pb.WatchRequest
  328. if i < watchKeyN {
  329. wreq = &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  330. CreateRequest: &pb.WatchCreateRequest{
  331. Key: []byte("foo"), StartRevision: startRev}}}
  332. } else {
  333. wreq = &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  334. CreateRequest: &pb.WatchCreateRequest{
  335. Prefix: []byte("fo"), StartRevision: startRev}}}
  336. }
  337. if err := wStream.Send(wreq); err != nil {
  338. t.Fatalf("wStream.Send error: %v", err)
  339. }
  340. }
  341. ids := make(map[int64]struct{})
  342. for i := 0; i < watchKeyN+1; i++ {
  343. wresp, err := wStream.Recv()
  344. if err != nil {
  345. t.Fatalf("wStream.Recv error: %v", err)
  346. }
  347. if !wresp.Created {
  348. t.Fatalf("wresp.Created got = %v, want = true", wresp.Created)
  349. }
  350. ids[wresp.WatchId] = struct{}{}
  351. }
  352. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}); err != nil {
  353. t.Fatalf("couldn't put key (%v)", err)
  354. }
  355. for i := 0; i < watchKeyN+1; i++ {
  356. wresp, err := wStream.Recv()
  357. if err != nil {
  358. t.Fatalf("wStream.Recv error: %v", err)
  359. }
  360. if _, ok := ids[wresp.WatchId]; !ok {
  361. t.Errorf("watchId %d is not created!", wresp.WatchId)
  362. } else {
  363. delete(ids, wresp.WatchId)
  364. }
  365. if len(wresp.Events) == 0 {
  366. t.Errorf("#%d: no events received", i)
  367. }
  368. for _, ev := range wresp.Events {
  369. if string(ev.Kv.Key) != "foo" {
  370. t.Errorf("ev.Kv.Key got = %s, want = foo", ev.Kv.Key)
  371. }
  372. if string(ev.Kv.Value) != "bar" {
  373. t.Errorf("ev.Kv.Value got = %s, want = bar", ev.Kv.Value)
  374. }
  375. }
  376. }
  377. // now put one key that has only one matching watcher
  378. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("fo"), Value: []byte("bar")}); err != nil {
  379. t.Fatalf("couldn't put key (%v)", err)
  380. }
  381. wresp, err := wStream.Recv()
  382. if err != nil {
  383. t.Errorf("wStream.Recv error: %v", err)
  384. }
  385. if len(wresp.Events) != 1 {
  386. t.Fatalf("len(wresp.Events) got = %d, want = 1", len(wresp.Events))
  387. }
  388. if string(wresp.Events[0].Kv.Key) != "fo" {
  389. t.Errorf("wresp.Events[0].Kv.Key got = %s, want = fo", wresp.Events[0].Kv.Key)
  390. }
  391. // now Recv should block because there is no more events coming
  392. rok, nr := waitResponse(wStream, 1*time.Second)
  393. if !rok {
  394. t.Errorf("unexpected pb.WatchResponse is received %+v", nr)
  395. }
  396. clus.Terminate(t)
  397. }
  398. func TestV3WatchMultipleEventsTxnSynced(t *testing.T) {
  399. defer testutil.AfterTest(t)
  400. testV3WatchMultipleEventsTxn(t, 0)
  401. }
  402. func TestV3WatchMultipleEventsTxnUnsynced(t *testing.T) {
  403. defer testutil.AfterTest(t)
  404. testV3WatchMultipleEventsTxn(t, 1)
  405. }
  406. // testV3WatchMultipleEventsTxn tests Watch APIs when it receives multiple events.
  407. func testV3WatchMultipleEventsTxn(t *testing.T, startRev int64) {
  408. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  409. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  410. defer cancel()
  411. wStream, wErr := clus.RandClient().Watch.Watch(ctx)
  412. if wErr != nil {
  413. t.Fatalf("wAPI.Watch error: %v", wErr)
  414. }
  415. wreq := &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  416. CreateRequest: &pb.WatchCreateRequest{
  417. Prefix: []byte("foo"), StartRevision: startRev}}}
  418. if err := wStream.Send(wreq); err != nil {
  419. t.Fatalf("wStream.Send error: %v", err)
  420. }
  421. kvc := clus.RandClient().KV
  422. txn := pb.TxnRequest{}
  423. for i := 0; i < 3; i++ {
  424. ru := &pb.RequestUnion{}
  425. ru.Request = &pb.RequestUnion_RequestPut{
  426. RequestPut: &pb.PutRequest{
  427. Key: []byte(fmt.Sprintf("foo%d", i)), Value: []byte("bar")}}
  428. txn.Success = append(txn.Success, ru)
  429. }
  430. tresp, err := kvc.Txn(context.Background(), &txn)
  431. if err != nil {
  432. t.Fatalf("kvc.Txn error: %v", err)
  433. }
  434. if !tresp.Succeeded {
  435. t.Fatalf("kvc.Txn failed: %+v", tresp)
  436. }
  437. events := []*storagepb.Event{}
  438. for len(events) < 3 {
  439. resp, err := wStream.Recv()
  440. if err != nil {
  441. t.Errorf("wStream.Recv error: %v", err)
  442. }
  443. if resp.Created {
  444. continue
  445. }
  446. events = append(events, resp.Events...)
  447. }
  448. sort.Sort(eventsSortByKey(events))
  449. wevents := []*storagepb.Event{
  450. {
  451. Type: storagepb.PUT,
  452. Kv: &storagepb.KeyValue{Key: []byte("foo0"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  453. },
  454. {
  455. Type: storagepb.PUT,
  456. Kv: &storagepb.KeyValue{Key: []byte("foo1"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  457. },
  458. {
  459. Type: storagepb.PUT,
  460. Kv: &storagepb.KeyValue{Key: []byte("foo2"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  461. },
  462. }
  463. if !reflect.DeepEqual(events, wevents) {
  464. t.Errorf("events got = %+v, want = %+v", events, wevents)
  465. }
  466. rok, nr := waitResponse(wStream, 1*time.Second)
  467. if !rok {
  468. t.Errorf("unexpected pb.WatchResponse is received %+v", nr)
  469. }
  470. // can't defer because tcp ports will be in use
  471. clus.Terminate(t)
  472. }
  473. type eventsSortByKey []*storagepb.Event
  474. func (evs eventsSortByKey) Len() int { return len(evs) }
  475. func (evs eventsSortByKey) Swap(i, j int) { evs[i], evs[j] = evs[j], evs[i] }
  476. func (evs eventsSortByKey) Less(i, j int) bool { return bytes.Compare(evs[i].Kv.Key, evs[j].Kv.Key) < 0 }
  477. func TestV3WatchMultipleEventsPutUnsynced(t *testing.T) {
  478. defer testutil.AfterTest(t)
  479. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  480. defer clus.Terminate(t)
  481. kvc := clus.RandClient().KV
  482. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo0"), Value: []byte("bar")}); err != nil {
  483. t.Fatalf("couldn't put key (%v)", err)
  484. }
  485. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo1"), Value: []byte("bar")}); err != nil {
  486. t.Fatalf("couldn't put key (%v)", err)
  487. }
  488. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  489. defer cancel()
  490. wStream, wErr := clus.RandClient().Watch.Watch(ctx)
  491. if wErr != nil {
  492. t.Fatalf("wAPI.Watch error: %v", wErr)
  493. }
  494. wreq := &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  495. CreateRequest: &pb.WatchCreateRequest{
  496. Prefix: []byte("foo"), StartRevision: 1}}}
  497. if err := wStream.Send(wreq); err != nil {
  498. t.Fatalf("wStream.Send error: %v", err)
  499. }
  500. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo0"), Value: []byte("bar")}); err != nil {
  501. t.Fatalf("couldn't put key (%v)", err)
  502. }
  503. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo1"), Value: []byte("bar")}); err != nil {
  504. t.Fatalf("couldn't put key (%v)", err)
  505. }
  506. allWevents := []*storagepb.Event{
  507. {
  508. Type: storagepb.PUT,
  509. Kv: &storagepb.KeyValue{Key: []byte("foo0"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  510. },
  511. {
  512. Type: storagepb.PUT,
  513. Kv: &storagepb.KeyValue{Key: []byte("foo1"), Value: []byte("bar"), CreateRevision: 3, ModRevision: 3, Version: 1},
  514. },
  515. {
  516. Type: storagepb.PUT,
  517. Kv: &storagepb.KeyValue{Key: []byte("foo0"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 4, Version: 2},
  518. },
  519. {
  520. Type: storagepb.PUT,
  521. Kv: &storagepb.KeyValue{Key: []byte("foo1"), Value: []byte("bar"), CreateRevision: 3, ModRevision: 5, Version: 2},
  522. },
  523. }
  524. events := []*storagepb.Event{}
  525. for len(events) < 4 {
  526. resp, err := wStream.Recv()
  527. if err != nil {
  528. t.Errorf("wStream.Recv error: %v", err)
  529. }
  530. if resp.Created {
  531. continue
  532. }
  533. events = append(events, resp.Events...)
  534. // if PUT requests are committed by now, first receive would return
  535. // multiple events, but if not, it returns a single event. In SSD,
  536. // it should return 4 events at once.
  537. }
  538. if !reflect.DeepEqual(events, allWevents) {
  539. t.Errorf("events got = %+v, want = %+v", events, allWevents)
  540. }
  541. rok, nr := waitResponse(wStream, 1*time.Second)
  542. if !rok {
  543. t.Errorf("unexpected pb.WatchResponse is received %+v", nr)
  544. }
  545. }
  546. func TestV3WatchMultipleStreamsSynced(t *testing.T) {
  547. defer testutil.AfterTest(t)
  548. testV3WatchMultipleStreams(t, 0)
  549. }
  550. func TestV3WatchMultipleStreamsUnsynced(t *testing.T) {
  551. defer testutil.AfterTest(t)
  552. testV3WatchMultipleStreams(t, 1)
  553. }
  554. // testV3WatchMultipleStreams tests multiple watchers on the same key on multiple streams.
  555. func testV3WatchMultipleStreams(t *testing.T, startRev int64) {
  556. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  557. wAPI := clus.RandClient().Watch
  558. kvc := clus.RandClient().KV
  559. streams := make([]pb.Watch_WatchClient, 5)
  560. for i := range streams {
  561. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  562. defer cancel()
  563. wStream, errW := wAPI.Watch(ctx)
  564. if errW != nil {
  565. t.Fatalf("wAPI.Watch error: %v", errW)
  566. }
  567. wreq := &pb.WatchRequest{RequestUnion: &pb.WatchRequest_CreateRequest{
  568. CreateRequest: &pb.WatchCreateRequest{
  569. Key: []byte("foo"), StartRevision: startRev}}}
  570. if err := wStream.Send(wreq); err != nil {
  571. t.Fatalf("wStream.Send error: %v", err)
  572. }
  573. streams[i] = wStream
  574. }
  575. for _, wStream := range streams {
  576. wresp, err := wStream.Recv()
  577. if err != nil {
  578. t.Fatalf("wStream.Recv error: %v", err)
  579. }
  580. if !wresp.Created {
  581. t.Fatalf("wresp.Created got = %v, want = true", wresp.Created)
  582. }
  583. }
  584. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}); err != nil {
  585. t.Fatalf("couldn't put key (%v)", err)
  586. }
  587. var wg sync.WaitGroup
  588. wg.Add(len(streams))
  589. wevents := []*storagepb.Event{
  590. {
  591. Type: storagepb.PUT,
  592. Kv: &storagepb.KeyValue{Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1},
  593. },
  594. }
  595. for i := range streams {
  596. go func(i int) {
  597. defer wg.Done()
  598. wStream := streams[i]
  599. wresp, err := wStream.Recv()
  600. if err != nil {
  601. t.Fatalf("wStream.Recv error: %v", err)
  602. }
  603. if wresp.WatchId != 0 {
  604. t.Errorf("watchId got = %d, want = 0", wresp.WatchId)
  605. }
  606. if !reflect.DeepEqual(wresp.Events, wevents) {
  607. t.Errorf("wresp.Events got = %+v, want = %+v", wresp.Events, wevents)
  608. }
  609. // now Recv should block because there is no more events coming
  610. rok, nr := waitResponse(wStream, 1*time.Second)
  611. if !rok {
  612. t.Errorf("unexpected pb.WatchResponse is received %+v", nr)
  613. }
  614. }(i)
  615. }
  616. wg.Wait()
  617. clus.Terminate(t)
  618. }
  619. // waitResponse waits on the given stream for given duration.
  620. // If there is no more events, true and a nil response will be
  621. // returned closing the WatchClient stream. Or the response will
  622. // be returned.
  623. func waitResponse(wc pb.Watch_WatchClient, timeout time.Duration) (bool, *pb.WatchResponse) {
  624. rCh := make(chan *pb.WatchResponse)
  625. go func() {
  626. resp, _ := wc.Recv()
  627. rCh <- resp
  628. }()
  629. select {
  630. case nr := <-rCh:
  631. return false, nr
  632. case <-time.After(timeout):
  633. }
  634. wc.CloseSend()
  635. rv, ok := <-rCh
  636. if rv != nil || !ok {
  637. return false, rv
  638. }
  639. return true, nil
  640. }