v3_watch_test.go 20 KB

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