watcher_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // synced watchings
  25. for i := 0; i < 10; i++ {
  26. id, cancel := w.Watch([]byte("foo"), false, 0)
  27. if _, ok := idm[id]; ok {
  28. t.Errorf("#%d: id %d exists", i, id)
  29. }
  30. idm[id] = struct{}{}
  31. s.Put([]byte("foo"), []byte("bar"))
  32. ev := <-w.Chan()
  33. if ev.WatchID != id {
  34. t.Errorf("#%d: watch id in event = %d, want %d", i, ev.WatchID, id)
  35. }
  36. cancel()
  37. }
  38. s.Put([]byte("foo2"), []byte("bar"))
  39. // unsynced watchings
  40. for i := 10; i < 20; i++ {
  41. id, cancel := w.Watch([]byte("foo2"), false, 1)
  42. if _, ok := idm[id]; ok {
  43. t.Errorf("#%d: id %d exists", i, id)
  44. }
  45. idm[id] = struct{}{}
  46. ev := <-w.Chan()
  47. if ev.WatchID != id {
  48. t.Errorf("#%d: watch id in event = %d, want %d", i, ev.WatchID, id)
  49. }
  50. cancel()
  51. }
  52. }