watcher_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2015 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 storage
  15. import (
  16. "bytes"
  17. "testing"
  18. "github.com/coreos/etcd/lease"
  19. "github.com/coreos/etcd/storage/backend"
  20. )
  21. // TestWatcherWatchID tests that each watcher provides unique watchID,
  22. // and the watched event attaches the correct watchID.
  23. func TestWatcherWatchID(t *testing.T) {
  24. b, tmpPath := backend.NewDefaultTmpBackend()
  25. s := WatchableKV(newWatchableStore(b, &lease.FakeLessor{}))
  26. defer cleanup(s, b, tmpPath)
  27. w := s.NewWatchStream()
  28. defer w.Close()
  29. idm := make(map[WatchID]struct{})
  30. for i := 0; i < 10; i++ {
  31. id := w.Watch([]byte("foo"), nil, 0)
  32. if _, ok := idm[id]; ok {
  33. t.Errorf("#%d: id %d exists", i, id)
  34. }
  35. idm[id] = struct{}{}
  36. s.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  37. resp := <-w.Chan()
  38. if resp.WatchID != id {
  39. t.Errorf("#%d: watch id in event = %d, want %d", i, resp.WatchID, id)
  40. }
  41. if err := w.Cancel(id); err != nil {
  42. t.Error(err)
  43. }
  44. }
  45. s.Put([]byte("foo2"), []byte("bar"), lease.NoLease)
  46. // unsynced watchers
  47. for i := 10; i < 20; i++ {
  48. id := w.Watch([]byte("foo2"), nil, 1)
  49. if _, ok := idm[id]; ok {
  50. t.Errorf("#%d: id %d exists", i, id)
  51. }
  52. idm[id] = struct{}{}
  53. resp := <-w.Chan()
  54. if resp.WatchID != id {
  55. t.Errorf("#%d: watch id in event = %d, want %d", i, resp.WatchID, id)
  56. }
  57. if err := w.Cancel(id); err != nil {
  58. t.Error(err)
  59. }
  60. }
  61. }
  62. // TestWatcherWatchPrefix tests if Watch operation correctly watches
  63. // and returns events with matching prefixes.
  64. func TestWatcherWatchPrefix(t *testing.T) {
  65. b, tmpPath := backend.NewDefaultTmpBackend()
  66. s := WatchableKV(newWatchableStore(b, &lease.FakeLessor{}))
  67. defer cleanup(s, b, tmpPath)
  68. w := s.NewWatchStream()
  69. defer w.Close()
  70. idm := make(map[WatchID]struct{})
  71. val := []byte("bar")
  72. keyWatch, keyEnd, keyPut := []byte("foo"), []byte("fop"), []byte("foobar")
  73. for i := 0; i < 10; i++ {
  74. id := w.Watch(keyWatch, keyEnd, 0)
  75. if _, ok := idm[id]; ok {
  76. t.Errorf("#%d: unexpected duplicated id %x", i, id)
  77. }
  78. idm[id] = struct{}{}
  79. s.Put(keyPut, val, lease.NoLease)
  80. resp := <-w.Chan()
  81. if resp.WatchID != id {
  82. t.Errorf("#%d: watch id in event = %d, want %d", i, resp.WatchID, id)
  83. }
  84. if err := w.Cancel(id); err != nil {
  85. t.Errorf("#%d: unexpected cancel error %v", i, err)
  86. }
  87. if len(resp.Events) != 1 {
  88. t.Errorf("#%d: len(resp.Events) got = %d, want = 1", i, len(resp.Events))
  89. }
  90. if len(resp.Events) == 1 {
  91. if !bytes.Equal(resp.Events[0].Kv.Key, keyPut) {
  92. t.Errorf("#%d: resp.Events got = %s, want = %s", i, resp.Events[0].Kv.Key, keyPut)
  93. }
  94. }
  95. }
  96. keyWatch1, keyEnd1, keyPut1 := []byte("foo1"), []byte("foo2"), []byte("foo1bar")
  97. s.Put(keyPut1, val, lease.NoLease)
  98. // unsynced watchers
  99. for i := 10; i < 15; i++ {
  100. id := w.Watch(keyWatch1, keyEnd1, 1)
  101. if _, ok := idm[id]; ok {
  102. t.Errorf("#%d: id %d exists", i, id)
  103. }
  104. idm[id] = struct{}{}
  105. resp := <-w.Chan()
  106. if resp.WatchID != id {
  107. t.Errorf("#%d: watch id in event = %d, want %d", i, resp.WatchID, id)
  108. }
  109. if err := w.Cancel(id); err != nil {
  110. t.Error(err)
  111. }
  112. if len(resp.Events) != 1 {
  113. t.Errorf("#%d: len(resp.Events) got = %d, want = 1", i, len(resp.Events))
  114. }
  115. if len(resp.Events) == 1 {
  116. if !bytes.Equal(resp.Events[0].Kv.Key, keyPut1) {
  117. t.Errorf("#%d: resp.Events got = %s, want = %s", i, resp.Events[0].Kv.Key, keyPut1)
  118. }
  119. }
  120. }
  121. }
  122. // TestWatchStreamCancelWatcherByID ensures cancel calls the cancel func of the watcher
  123. // with given id inside watchStream.
  124. func TestWatchStreamCancelWatcherByID(t *testing.T) {
  125. b, tmpPath := backend.NewDefaultTmpBackend()
  126. s := WatchableKV(newWatchableStore(b, &lease.FakeLessor{}))
  127. defer cleanup(s, b, tmpPath)
  128. w := s.NewWatchStream()
  129. defer w.Close()
  130. id := w.Watch([]byte("foo"), nil, 0)
  131. tests := []struct {
  132. cancelID WatchID
  133. werr error
  134. }{
  135. // no error should be returned when cancel the created watcher.
  136. {id, nil},
  137. // not exist error should be returned when cancel again.
  138. {id, ErrWatcherNotExist},
  139. // not exist error should be returned when cancel a bad id.
  140. {id + 1, ErrWatcherNotExist},
  141. }
  142. for i, tt := range tests {
  143. gerr := w.Cancel(tt.cancelID)
  144. if gerr != tt.werr {
  145. t.Errorf("#%d: err = %v, want %v", i, gerr, tt.werr)
  146. }
  147. }
  148. if l := len(w.(*watchStream).cancels); l != 0 {
  149. t.Errorf("cancels = %d, want 0", l)
  150. }
  151. }