watcher_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 watchID,
  17. // and the watched event attaches the correct watchID.
  18. func TestWatcherWatchID(t *testing.T) {
  19. s := WatchableKV(newWatchableStore(tmpPath))
  20. defer cleanup(s, tmpPath)
  21. w := s.NewWatchStream()
  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 watchers
  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. }
  56. // TestWatchStreamCancel ensures cancel calls the cancel func of the watcher
  57. // with given id inside watchStream.
  58. func TestWatchStreamCancelWatcherByID(t *testing.T) {
  59. s := WatchableKV(newWatchableStore(tmpPath))
  60. defer cleanup(s, tmpPath)
  61. w := s.NewWatchStream()
  62. defer w.Close()
  63. id, _ := w.Watch([]byte("foo"), false, 0)
  64. tests := []struct {
  65. cancelID int64
  66. werr error
  67. }{
  68. // no error should be returned when cancel the created watcher.
  69. {id, nil},
  70. // not exist error should be returned when cancel again.
  71. {id, ErrWatcherNotExist},
  72. // not exist error should be returned when cancel a bad id.
  73. {id + 1, ErrWatcherNotExist},
  74. }
  75. for i, tt := range tests {
  76. gerr := w.Cancel(tt.cancelID)
  77. if gerr != tt.werr {
  78. t.Errorf("#%d: err = %v, want %v", i, gerr, tt.werr)
  79. }
  80. }
  81. if l := len(w.(*watchStream).cancels); l != 0 {
  82. t.Errorf("cancels = %d, want 0", l)
  83. }
  84. }