watchable_store_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. "os"
  18. "reflect"
  19. "testing"
  20. "github.com/coreos/etcd/storage/storagepb"
  21. )
  22. func TestWatch(t *testing.T) {
  23. s := newWatchableStore(tmpPath)
  24. defer func() {
  25. s.store.Close()
  26. os.Remove(tmpPath)
  27. }()
  28. testKey := []byte("foo")
  29. testValue := []byte("bar")
  30. s.Put(testKey, testValue)
  31. w := s.NewWatchStream()
  32. w.Watch(testKey, true, 0)
  33. if _, ok := s.synced[string(testKey)]; !ok {
  34. // the key must have had an entry in synced
  35. t.Errorf("existence = %v, want true", ok)
  36. }
  37. }
  38. func TestNewWatcherCancel(t *testing.T) {
  39. s := newWatchableStore(tmpPath)
  40. defer func() {
  41. s.store.Close()
  42. os.Remove(tmpPath)
  43. }()
  44. testKey := []byte("foo")
  45. testValue := []byte("bar")
  46. s.Put(testKey, testValue)
  47. w := s.NewWatchStream()
  48. _, cancel := w.Watch(testKey, true, 0)
  49. cancel()
  50. if _, ok := s.synced[string(testKey)]; ok {
  51. // the key shoud have been deleted
  52. t.Errorf("existence = %v, want false", ok)
  53. }
  54. }
  55. // TestCancelUnsynced tests if running CancelFunc removes watchers from unsynced.
  56. func TestCancelUnsynced(t *testing.T) {
  57. // manually create watchableStore instead of newWatchableStore
  58. // because newWatchableStore automatically calls syncWatchers
  59. // method to sync watchers in unsynced map. We want to keep watchers
  60. // in unsynced to test if syncWatchers works as expected.
  61. s := &watchableStore{
  62. store: newDefaultStore(tmpPath),
  63. unsynced: make(map[*watcher]struct{}),
  64. // to make the test not crash from assigning to nil map.
  65. // 'synced' doesn't get populated in this test.
  66. synced: make(map[string]map[*watcher]struct{}),
  67. }
  68. defer func() {
  69. s.store.Close()
  70. os.Remove(tmpPath)
  71. }()
  72. // Put a key so that we can spawn watchers on that key.
  73. // (testKey in this test). This increases the rev to 1,
  74. // and later we can we set the watcher's startRev to 1,
  75. // and force watchers to be in unsynced.
  76. testKey := []byte("foo")
  77. testValue := []byte("bar")
  78. s.Put(testKey, testValue)
  79. w := s.NewWatchStream()
  80. // arbitrary number for watchers
  81. watcherN := 100
  82. // create watcherN of CancelFunc of
  83. // synced and unsynced
  84. cancels := make([]CancelFunc, watcherN)
  85. for i := 0; i < watcherN; i++ {
  86. // use 1 to keep watchers in unsynced
  87. _, cancel := w.Watch(testKey, true, 1)
  88. cancels[i] = cancel
  89. }
  90. for idx := range cancels {
  91. cancels[idx]()
  92. }
  93. // After running CancelFunc
  94. //
  95. // unsynced should be empty
  96. // because cancel removes watcher from unsynced
  97. if len(s.unsynced) != 0 {
  98. t.Errorf("unsynced size = %d, want 0", len(s.unsynced))
  99. }
  100. }
  101. // TestSyncWatchers populates unsynced watcher map and tests syncWatchers
  102. // method to see if it correctly sends events to channel of unsynced watchers
  103. // and moves these watchers to synced.
  104. func TestSyncWatchers(t *testing.T) {
  105. s := &watchableStore{
  106. store: newDefaultStore(tmpPath),
  107. unsynced: make(map[*watcher]struct{}),
  108. synced: make(map[string]map[*watcher]struct{}),
  109. }
  110. defer func() {
  111. s.store.Close()
  112. os.Remove(tmpPath)
  113. }()
  114. testKey := []byte("foo")
  115. testValue := []byte("bar")
  116. s.Put(testKey, testValue)
  117. w := s.NewWatchStream()
  118. // arbitrary number for watchers
  119. watcherN := 100
  120. for i := 0; i < watcherN; i++ {
  121. // use 1 to keep watchers in unsynced
  122. w.Watch(testKey, true, 1)
  123. }
  124. // Before running s.syncWatchers()
  125. //
  126. // synced should be empty
  127. // because we manually populate unsynced only
  128. if len(s.synced[string(testKey)]) != 0 {
  129. t.Fatalf("synced[string(testKey)] size = %d, want 0", len(s.synced[string(testKey)]))
  130. }
  131. // unsynced should not be empty
  132. // because we manually populated unsynced only
  133. if len(s.unsynced) == 0 {
  134. t.Errorf("unsynced size = %d, want %d", len(s.unsynced), watcherN)
  135. }
  136. // this should move all unsynced watchers
  137. // to synced ones
  138. s.syncWatchers()
  139. // After running s.syncWatchers()
  140. //
  141. // synced should not be empty
  142. // because syncwatchers populates synced
  143. // in this test case
  144. if len(s.synced[string(testKey)]) == 0 {
  145. t.Errorf("synced[string(testKey)] size = 0, want %d", len(s.synced[string(testKey)]))
  146. }
  147. // unsynced should be empty
  148. // because syncwatchers is expected to move
  149. // all watchers from unsynced to synced
  150. // in this test case
  151. if len(s.unsynced) != 0 {
  152. t.Errorf("unsynced size = %d, want 0", len(s.unsynced))
  153. }
  154. // All of the watchers actually share one channel
  155. // so we only need to check one shared channel
  156. // (See watcher.go for more detail).
  157. if len(w.(*watchStream).ch) != watcherN {
  158. t.Errorf("watched event size = %d, want %d", len(w.(*watchStream).ch), watcherN)
  159. }
  160. wr := <-w.(*watchStream).ch
  161. evs := wr.Events
  162. if len(evs) != 1 {
  163. t.Errorf("len(evs) got = %d, want = 1", len(evs))
  164. }
  165. if evs[0].Type != storagepb.PUT {
  166. t.Errorf("got = %v, want = %v", evs[0].Type, storagepb.PUT)
  167. }
  168. if !bytes.Equal(evs[0].Kv.Key, testKey) {
  169. t.Errorf("got = %s, want = %s", evs[0].Kv.Key, testKey)
  170. }
  171. if !bytes.Equal(evs[0].Kv.Value, testValue) {
  172. t.Errorf("got = %s, want = %s", evs[0].Kv.Value, testValue)
  173. }
  174. }
  175. func TestUnsafeAddWatcher(t *testing.T) {
  176. s := newWatchableStore(tmpPath)
  177. defer func() {
  178. s.store.Close()
  179. os.Remove(tmpPath)
  180. }()
  181. testKey := []byte("foo")
  182. testValue := []byte("bar")
  183. s.Put(testKey, testValue)
  184. size := 10
  185. ws := make([]*watcher, size)
  186. for i := 0; i < size; i++ {
  187. ws[i] = &watcher{
  188. key: testKey,
  189. prefix: true,
  190. cur: 0,
  191. }
  192. }
  193. // to test if unsafeAddWatcher is correctly updating
  194. // synced map when adding new watcher.
  195. for i, wa := range ws {
  196. if err := unsafeAddWatcher(&s.synced, string(testKey), wa); err != nil {
  197. t.Errorf("#%d: error = %v, want nil", i, err)
  198. }
  199. if v, ok := s.synced[string(testKey)]; !ok {
  200. t.Errorf("#%d: ok = %v, want ok true", i, ok)
  201. } else {
  202. if len(v) != i+1 {
  203. t.Errorf("#%d: len(v) = %d, want %d", i, len(v), i+1)
  204. }
  205. if _, ok := v[wa]; !ok {
  206. t.Errorf("#%d: ok = %v, want ok true", i, ok)
  207. }
  208. }
  209. }
  210. }
  211. func TestNewMapwatcherToEventMap(t *testing.T) {
  212. k0, k1, k2 := []byte("foo0"), []byte("foo1"), []byte("foo2")
  213. v0, v1, v2 := []byte("bar0"), []byte("bar1"), []byte("bar2")
  214. ws := []*watcher{{key: k0}, {key: k1}, {key: k2}}
  215. evs := []storagepb.Event{
  216. {
  217. Type: storagepb.PUT,
  218. Kv: &storagepb.KeyValue{Key: k0, Value: v0},
  219. },
  220. {
  221. Type: storagepb.PUT,
  222. Kv: &storagepb.KeyValue{Key: k1, Value: v1},
  223. },
  224. {
  225. Type: storagepb.PUT,
  226. Kv: &storagepb.KeyValue{Key: k2, Value: v2},
  227. },
  228. }
  229. tests := []struct {
  230. sync map[string]map[*watcher]struct{}
  231. evs []storagepb.Event
  232. wwe map[*watcher][]storagepb.Event
  233. }{
  234. // no watcher in sync, some events should return empty wwe
  235. {
  236. map[string]map[*watcher]struct{}{},
  237. evs,
  238. map[*watcher][]storagepb.Event{},
  239. },
  240. // one watcher in sync, one event that does not match the key of that
  241. // watcher should return empty wwe
  242. {
  243. map[string]map[*watcher]struct{}{
  244. string(k2): {ws[2]: struct{}{}},
  245. },
  246. evs[:1],
  247. map[*watcher][]storagepb.Event{},
  248. },
  249. // one watcher in sync, one event that matches the key of that
  250. // watcher should return wwe with that matching watcher
  251. {
  252. map[string]map[*watcher]struct{}{
  253. string(k1): {ws[1]: struct{}{}},
  254. },
  255. evs[1:2],
  256. map[*watcher][]storagepb.Event{
  257. ws[1]: evs[1:2],
  258. },
  259. },
  260. // two watchers in sync that watches two different keys, one event
  261. // that matches the key of only one of the watcher should return wwe
  262. // with the matching watcher
  263. {
  264. map[string]map[*watcher]struct{}{
  265. string(k0): {ws[0]: struct{}{}},
  266. string(k2): {ws[2]: struct{}{}},
  267. },
  268. evs[2:],
  269. map[*watcher][]storagepb.Event{
  270. ws[2]: evs[2:],
  271. },
  272. },
  273. // two watchers in sync that watches the same key, two events that
  274. // match the keys should return wwe with those two watchers
  275. {
  276. map[string]map[*watcher]struct{}{
  277. string(k0): {ws[0]: struct{}{}},
  278. string(k1): {ws[1]: struct{}{}},
  279. },
  280. evs[:2],
  281. map[*watcher][]storagepb.Event{
  282. ws[0]: evs[:1],
  283. ws[1]: evs[1:2],
  284. },
  285. },
  286. }
  287. for i, tt := range tests {
  288. gwe := newWatcherToEventMap(tt.sync, tt.evs)
  289. if len(gwe) != len(tt.wwe) {
  290. t.Errorf("#%d: len(gwe) got = %d, want = %d", i, len(gwe), len(tt.wwe))
  291. }
  292. // compare gwe and tt.wwe
  293. for w, mevs := range gwe {
  294. if len(mevs) != len(tt.wwe[w]) {
  295. t.Errorf("#%d: len(mevs) got = %d, want = %d", i, len(mevs), len(tt.wwe[w]))
  296. }
  297. if !reflect.DeepEqual(mevs, tt.wwe[w]) {
  298. t.Errorf("#%d: reflect.DeepEqual events got = %v, want = true", i, false)
  299. }
  300. }
  301. }
  302. }