watch_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 clientv3.WatchChan
  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. // TestWatchReconnRequest tests the send failure path when requesting a watcher.
  145. func TestWatchReconnRequest(t *testing.T) {
  146. runWatchTest(t, testWatchReconnRequest)
  147. }
  148. func testWatchReconnRequest(t *testing.T, wctx *watchctx) {
  149. // take down watcher connection
  150. donec := make(chan struct{})
  151. go func() {
  152. for {
  153. wctx.wclient.ActiveConnection().Close()
  154. select {
  155. case <-donec:
  156. return
  157. default:
  158. }
  159. }
  160. }()
  161. // should reconnect when requesting watch
  162. if wctx.ch = wctx.w.Watch(context.TODO(), "a", 0); wctx.ch == nil {
  163. t.Fatalf("expected non-nil channel")
  164. }
  165. close(donec)
  166. // ensure watcher works
  167. putAndWatch(t, wctx, "a", "a")
  168. }
  169. // TestWatchReconnInit tests watcher resumes correctly if connection lost
  170. // before any data was sent.
  171. func TestWatchReconnInit(t *testing.T) {
  172. runWatchTest(t, testWatchReconnInit)
  173. }
  174. func testWatchReconnInit(t *testing.T, wctx *watchctx) {
  175. if wctx.ch = wctx.w.Watch(context.TODO(), "a", 0); wctx.ch == nil {
  176. t.Fatalf("expected non-nil channel")
  177. }
  178. // take down watcher connection
  179. wctx.wclient.ActiveConnection().Close()
  180. // watcher should recover
  181. putAndWatch(t, wctx, "a", "a")
  182. }
  183. // TestWatchReconnRunning tests watcher resumes correctly if connection lost
  184. // after data was sent.
  185. func TestWatchReconnRunning(t *testing.T) {
  186. runWatchTest(t, testWatchReconnRunning)
  187. }
  188. func testWatchReconnRunning(t *testing.T, wctx *watchctx) {
  189. if wctx.ch = wctx.w.Watch(context.TODO(), "a", 0); wctx.ch == nil {
  190. t.Fatalf("expected non-nil channel")
  191. }
  192. putAndWatch(t, wctx, "a", "a")
  193. // take down watcher connection
  194. wctx.wclient.ActiveConnection().Close()
  195. // watcher should recover
  196. putAndWatch(t, wctx, "a", "b")
  197. }
  198. // TestWatchCancelInit tests watcher closes correctly after no events.
  199. func TestWatchCancelInit(t *testing.T) {
  200. runWatchTest(t, testWatchCancelInit)
  201. }
  202. func testWatchCancelInit(t *testing.T, wctx *watchctx) {
  203. ctx, cancel := context.WithCancel(context.Background())
  204. if wctx.ch = wctx.w.Watch(ctx, "a", 0); wctx.ch == nil {
  205. t.Fatalf("expected non-nil watcher channel")
  206. }
  207. cancel()
  208. select {
  209. case <-time.After(time.Second):
  210. t.Fatalf("took too long to cancel")
  211. case _, ok := <-wctx.ch:
  212. if ok {
  213. t.Fatalf("expected watcher channel to close")
  214. }
  215. }
  216. }
  217. // TestWatchCancelRunning tests watcher closes correctly after events.
  218. func TestWatchCancelRunning(t *testing.T) {
  219. runWatchTest(t, testWatchCancelRunning)
  220. }
  221. func testWatchCancelRunning(t *testing.T, wctx *watchctx) {
  222. ctx, cancel := context.WithCancel(context.Background())
  223. if wctx.ch = wctx.w.Watch(ctx, "a", 0); wctx.ch == nil {
  224. t.Fatalf("expected non-nil watcher channel")
  225. }
  226. if _, err := wctx.kv.Put(ctx, "a", "a"); err != nil {
  227. t.Fatal(err)
  228. }
  229. cancel()
  230. select {
  231. case <-time.After(time.Second):
  232. t.Fatalf("took too long to cancel")
  233. case v, ok := <-wctx.ch:
  234. if !ok {
  235. // closed before getting put; OK
  236. break
  237. }
  238. // got the PUT; should close next
  239. select {
  240. case <-time.After(time.Second):
  241. t.Fatalf("took too long to close")
  242. case v, ok = <-wctx.ch:
  243. if ok {
  244. t.Fatalf("expected watcher channel to close, got %v", v)
  245. }
  246. }
  247. }
  248. }
  249. func putAndWatch(t *testing.T, wctx *watchctx, key, val string) {
  250. if _, err := wctx.kv.Put(context.TODO(), key, val); err != nil {
  251. t.Fatal(err)
  252. }
  253. select {
  254. case <-time.After(5 * time.Second):
  255. t.Fatalf("watch timed out")
  256. case v, ok := <-wctx.ch:
  257. if !ok {
  258. t.Fatalf("unexpected watch close")
  259. }
  260. if string(v.Events[0].Kv.Value) != val {
  261. t.Fatalf("bad value got %v, wanted %v", v.Events[0].Kv.Value, val)
  262. }
  263. }
  264. }