watcher_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "testing"
  16. // TestWatcherWatchID tests that each watcher provides unique watch ID,
  17. // and the watched event attaches the correct watch ID.
  18. func TestWatcherWatchID(t *testing.T) {
  19. s := WatchableKV(newWatchableStore(tmpPath))
  20. defer cleanup(s, tmpPath)
  21. w := s.NewWatcher()
  22. defer w.Close()
  23. idm := make(map[int64]struct{})
  24. for i := 0; i < 10; i++ {
  25. id, cancel := w.Watch([]byte("foo"), false, 0)
  26. if _, ok := idm[id]; ok {
  27. t.Errorf("#%d: id %d exists", i, id)
  28. }
  29. idm[id] = struct{}{}
  30. s.Put([]byte("foo"), []byte("bar"))
  31. evs := <-w.Chan()
  32. for j, ev := range evs {
  33. if ev.WatchID != id {
  34. t.Errorf("#%d.%d: watch id in event = %d, want %d", i, j, ev.WatchID, id)
  35. }
  36. }
  37. cancel()
  38. }
  39. s.Put([]byte("foo2"), []byte("bar"))
  40. // unsynced watchings
  41. for i := 10; i < 20; i++ {
  42. id, cancel := w.Watch([]byte("foo2"), false, 1)
  43. if _, ok := idm[id]; ok {
  44. t.Errorf("#%d: id %d exists", i, id)
  45. }
  46. idm[id] = struct{}{}
  47. evs := <-w.Chan()
  48. for j, ev := range evs {
  49. if ev.WatchID != id {
  50. t.Errorf("#%d.%d: watch id in event = %d, want %d", i, j, ev.WatchID, id)
  51. }
  52. }
  53. cancel()
  54. }
  55. }