watch_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.
  14. package integration
  15. import (
  16. "fmt"
  17. "reflect"
  18. "sort"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  22. "github.com/coreos/etcd/clientv3"
  23. "github.com/coreos/etcd/integration"
  24. "github.com/coreos/etcd/pkg/testutil"
  25. storagepb "github.com/coreos/etcd/storage/storagepb"
  26. )
  27. type watcherTest func(*testing.T, *watchctx)
  28. type watchctx struct {
  29. clus *integration.ClusterV3
  30. w clientv3.Watcher
  31. wclient *clientv3.Client
  32. kv clientv3.KV
  33. ch <-chan clientv3.WatchResponse
  34. }
  35. func runWatchTest(t *testing.T, f watcherTest) {
  36. defer testutil.AfterTest(t)
  37. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  38. defer clus.Terminate(t)
  39. wclient := clus.RandClient()
  40. w := clientv3.NewWatcher(wclient)
  41. defer w.Close()
  42. // select a different client from wclient so puts succeed if
  43. // a test knocks out the watcher client
  44. kvclient := clus.RandClient()
  45. for kvclient == wclient {
  46. kvclient = clus.RandClient()
  47. }
  48. kv := clientv3.NewKV(kvclient)
  49. wctx := &watchctx{clus, w, wclient, kv, nil}
  50. f(t, wctx)
  51. }
  52. // TestWatchMultiWatcher modifies multiple keys and observes the changes.
  53. func TestWatchMultiWatcher(t *testing.T) {
  54. runWatchTest(t, testWatchMultiWatcher)
  55. }
  56. func testWatchMultiWatcher(t *testing.T, wctx *watchctx) {
  57. numKeyUpdates := 4
  58. keys := []string{"foo", "bar", "baz"}
  59. donec := make(chan struct{})
  60. readyc := make(chan struct{})
  61. for _, k := range keys {
  62. // key watcher
  63. go func(key string) {
  64. ch := wctx.w.Watch(context.TODO(), key, 0)
  65. if ch == nil {
  66. t.Fatalf("expected watcher channel, got nil")
  67. }
  68. readyc <- struct{}{}
  69. for i := 0; i < numKeyUpdates; i++ {
  70. resp, ok := <-ch
  71. if !ok {
  72. t.Fatalf("watcher unexpectedly closed")
  73. }
  74. v := fmt.Sprintf("%s-%d", key, i)
  75. gotv := string(resp.Events[0].Kv.Value)
  76. if gotv != v {
  77. t.Errorf("#%d: got %s, wanted %s", i, gotv, v)
  78. }
  79. }
  80. donec <- struct{}{}
  81. }(k)
  82. }
  83. // prefix watcher on "b" (bar and baz)
  84. go func() {
  85. prefixc := wctx.w.WatchPrefix(context.TODO(), "b", 0)
  86. if prefixc == nil {
  87. t.Fatalf("expected watcher channel, got nil")
  88. }
  89. readyc <- struct{}{}
  90. evs := []*storagepb.Event{}
  91. for i := 0; i < numKeyUpdates*2; i++ {
  92. resp, ok := <-prefixc
  93. if !ok {
  94. t.Fatalf("watcher unexpectedly closed")
  95. }
  96. evs = append(evs, resp.Events...)
  97. }
  98. // check response
  99. expected := []string{}
  100. bkeys := []string{"bar", "baz"}
  101. for _, k := range bkeys {
  102. for i := 0; i < numKeyUpdates; i++ {
  103. expected = append(expected, fmt.Sprintf("%s-%d", k, i))
  104. }
  105. }
  106. got := []string{}
  107. for _, ev := range evs {
  108. got = append(got, string(ev.Kv.Value))
  109. }
  110. sort.Strings(got)
  111. if reflect.DeepEqual(expected, got) == false {
  112. t.Errorf("got %v, expected %v", got, expected)
  113. }
  114. // ensure no extra data
  115. select {
  116. case resp, ok := <-prefixc:
  117. if !ok {
  118. t.Fatalf("watcher unexpectedly closed")
  119. }
  120. t.Fatalf("unexpected event %+v", resp)
  121. case <-time.After(time.Second):
  122. }
  123. donec <- struct{}{}
  124. }()
  125. // wait for watcher bring up
  126. for i := 0; i < len(keys)+1; i++ {
  127. <-readyc
  128. }
  129. // generate events
  130. ctx := context.TODO()
  131. for i := 0; i < numKeyUpdates; i++ {
  132. for _, k := range keys {
  133. v := fmt.Sprintf("%s-%d", k, i)
  134. if _, err := wctx.kv.Put(ctx, k, v); err != nil {
  135. t.Fatal(err)
  136. }
  137. }
  138. }
  139. // wait for watcher shutdown
  140. for i := 0; i < len(keys)+1; i++ {
  141. <-donec
  142. }
  143. }
  144. // TestWatchReconnInit tests watcher resumes correctly if connection lost
  145. // before any data was sent.
  146. func TestWatchReconnInit(t *testing.T) {
  147. runWatchTest(t, testWatchReconnInit)
  148. }
  149. func testWatchReconnInit(t *testing.T, wctx *watchctx) {
  150. if wctx.ch = wctx.w.Watch(context.TODO(), "a", 0); wctx.ch == nil {
  151. t.Fatalf("expected non-nil channel")
  152. }
  153. // take down watcher connection
  154. wctx.wclient.ActiveConnection().Close()
  155. // watcher should recover
  156. putAndWatch(t, wctx, "a", "a")
  157. }
  158. // TestWatchReconnRunning tests watcher resumes correctly if connection lost
  159. // after data was sent.
  160. func TestWatchReconnRunning(t *testing.T) {
  161. runWatchTest(t, testWatchReconnRunning)
  162. }
  163. func testWatchReconnRunning(t *testing.T, wctx *watchctx) {
  164. if wctx.ch = wctx.w.Watch(context.TODO(), "a", 0); wctx.ch == nil {
  165. t.Fatalf("expected non-nil channel")
  166. }
  167. putAndWatch(t, wctx, "a", "a")
  168. // take down watcher connection
  169. wctx.wclient.ActiveConnection().Close()
  170. // watcher should recover
  171. putAndWatch(t, wctx, "a", "b")
  172. }
  173. // TestWatchCancelInit tests watcher closes correctly after no events.
  174. func TestWatchCancelInit(t *testing.T) {
  175. runWatchTest(t, testWatchCancelInit)
  176. }
  177. func testWatchCancelInit(t *testing.T, wctx *watchctx) {
  178. ctx, cancel := context.WithCancel(context.Background())
  179. if wctx.ch = wctx.w.Watch(ctx, "a", 0); wctx.ch == nil {
  180. t.Fatalf("expected non-nil watcher channel")
  181. }
  182. cancel()
  183. select {
  184. case <-time.After(time.Second):
  185. t.Fatalf("took too long to cancel")
  186. case _, ok := <-wctx.ch:
  187. if ok {
  188. t.Fatalf("expected watcher channel to close")
  189. }
  190. }
  191. }
  192. // TestWatchCancelRunning tests watcher closes correctly after events.
  193. func TestWatchCancelRunning(t *testing.T) {
  194. runWatchTest(t, testWatchCancelRunning)
  195. }
  196. func testWatchCancelRunning(t *testing.T, wctx *watchctx) {
  197. ctx, cancel := context.WithCancel(context.Background())
  198. if wctx.ch = wctx.w.Watch(ctx, "a", 0); wctx.ch == nil {
  199. t.Fatalf("expected non-nil watcher channel")
  200. }
  201. if _, err := wctx.kv.Put(ctx, "a", "a"); err != nil {
  202. t.Fatal(err)
  203. }
  204. cancel()
  205. select {
  206. case <-time.After(time.Second):
  207. t.Fatalf("took too long to cancel")
  208. case v, ok := <-wctx.ch:
  209. if !ok {
  210. // closed before getting put; OK
  211. break
  212. }
  213. // got the PUT; should close next
  214. select {
  215. case <-time.After(time.Second):
  216. t.Fatalf("took too long to close")
  217. case v, ok = <-wctx.ch:
  218. if ok {
  219. t.Fatalf("expected watcher channel to close, got %v", v)
  220. }
  221. }
  222. }
  223. }
  224. func putAndWatch(t *testing.T, wctx *watchctx, key, val string) {
  225. if _, err := wctx.kv.Put(context.TODO(), key, val); err != nil {
  226. t.Fatal(err)
  227. }
  228. select {
  229. case <-time.After(5 * time.Second):
  230. t.Fatalf("watch timed out")
  231. case v, ok := <-wctx.ch:
  232. if !ok {
  233. t.Fatalf("unexpected watch close")
  234. }
  235. if string(v.Events[0].Kv.Value) != val {
  236. t.Fatalf("bad value got %v, wanted %v", v.Events[0].Kv.Value, val)
  237. }
  238. }
  239. }