watch_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. for i := 0; i < numKeyUpdates; i++ {
  131. for _, k := range keys {
  132. v := fmt.Sprintf("%s-%d", k, i)
  133. if _, err := wctx.kv.Put(k, v, 0); err != nil {
  134. t.Fatal(err)
  135. }
  136. }
  137. }
  138. // wait for watcher shutdown
  139. for i := 0; i < len(keys)+1; i++ {
  140. <-donec
  141. }
  142. }
  143. // TestWatchReconnInit tests watcher resumes correctly if connection lost
  144. // before any data was sent.
  145. func TestWatchReconnInit(t *testing.T) {
  146. runWatchTest(t, testWatchReconnInit)
  147. }
  148. func testWatchReconnInit(t *testing.T, wctx *watchctx) {
  149. if wctx.ch = wctx.w.Watch(context.TODO(), "a", 0); wctx.ch == nil {
  150. t.Fatalf("expected non-nil channel")
  151. }
  152. // take down watcher connection
  153. wctx.wclient.ActiveConnection().Close()
  154. // watcher should recover
  155. putAndWatch(t, wctx, "a", "a")
  156. }
  157. // TestWatchReconnRunning tests watcher resumes correctly if connection lost
  158. // after data was sent.
  159. func TestWatchReconnRunning(t *testing.T) {
  160. runWatchTest(t, testWatchReconnRunning)
  161. }
  162. func testWatchReconnRunning(t *testing.T, wctx *watchctx) {
  163. if wctx.ch = wctx.w.Watch(context.TODO(), "a", 0); wctx.ch == nil {
  164. t.Fatalf("expected non-nil channel")
  165. }
  166. putAndWatch(t, wctx, "a", "a")
  167. // take down watcher connection
  168. wctx.wclient.ActiveConnection().Close()
  169. // watcher should recover
  170. putAndWatch(t, wctx, "a", "b")
  171. }
  172. // TestWatchCancelInit tests watcher closes correctly after no events.
  173. func TestWatchCancelInit(t *testing.T) {
  174. runWatchTest(t, testWatchCancelInit)
  175. }
  176. func testWatchCancelInit(t *testing.T, wctx *watchctx) {
  177. ctx, cancel := context.WithCancel(context.Background())
  178. if wctx.ch = wctx.w.Watch(ctx, "a", 0); wctx.ch == nil {
  179. t.Fatalf("expected non-nil watcher channel")
  180. }
  181. cancel()
  182. select {
  183. case <-time.After(time.Second):
  184. t.Fatalf("took too long to cancel")
  185. case _, ok := <-wctx.ch:
  186. if ok {
  187. t.Fatalf("expected watcher channel to close")
  188. }
  189. }
  190. }
  191. // TestWatchCancelRunning tests watcher closes correctly after events.
  192. func TestWatchCancelRunning(t *testing.T) {
  193. runWatchTest(t, testWatchCancelRunning)
  194. }
  195. func testWatchCancelRunning(t *testing.T, wctx *watchctx) {
  196. ctx, cancel := context.WithCancel(context.Background())
  197. if wctx.ch = wctx.w.Watch(ctx, "a", 0); wctx.ch == nil {
  198. t.Fatalf("expected non-nil watcher channel")
  199. }
  200. if _, err := wctx.kv.Put("a", "a", 0); err != nil {
  201. t.Fatal(err)
  202. }
  203. cancel()
  204. select {
  205. case <-time.After(time.Second):
  206. t.Fatalf("took too long to cancel")
  207. case v, ok := <-wctx.ch:
  208. if !ok {
  209. // closed before getting put; OK
  210. break
  211. }
  212. // got the PUT; should close next
  213. select {
  214. case <-time.After(time.Second):
  215. t.Fatalf("took too long to close")
  216. case v, ok = <-wctx.ch:
  217. if ok {
  218. t.Fatalf("expected watcher channel to close, got %v", v)
  219. }
  220. }
  221. }
  222. }
  223. func putAndWatch(t *testing.T, wctx *watchctx, key, val string) {
  224. if _, err := wctx.kv.Put(key, val, 0); err != nil {
  225. t.Fatal(err)
  226. }
  227. select {
  228. case <-time.After(5 * time.Second):
  229. t.Fatalf("watch timed out")
  230. case v, ok := <-wctx.ch:
  231. if !ok {
  232. t.Fatalf("unexpected watch close")
  233. }
  234. if string(v.Events[0].Kv.Value) != val {
  235. t.Fatalf("bad value got %v, wanted %v", v.Events[0].Kv.Value, val)
  236. }
  237. }
  238. }