watcher_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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"), NoLease)
  31. resp := <-w.Chan()
  32. if resp.WatchID != id {
  33. t.Errorf("#%d: watch id in event = %d, want %d", i, resp.WatchID, id)
  34. }
  35. cancel()
  36. }
  37. s.Put([]byte("foo2"), []byte("bar"), NoLease)
  38. // unsynced watchers
  39. for i := 10; i < 20; i++ {
  40. id, cancel := w.Watch([]byte("foo2"), false, 1)
  41. if _, ok := idm[id]; ok {
  42. t.Errorf("#%d: id %d exists", i, id)
  43. }
  44. idm[id] = struct{}{}
  45. resp := <-w.Chan()
  46. if resp.WatchID != id {
  47. t.Errorf("#%d: watch id in event = %d, want %d", i, resp.WatchID, id)
  48. }
  49. cancel()
  50. }
  51. }
  52. // TestWatchStreamCancel ensures cancel calls the cancel func of the watcher
  53. // with given id inside watchStream.
  54. func TestWatchStreamCancelWatcherByID(t *testing.T) {
  55. s := WatchableKV(newWatchableStore(tmpPath))
  56. defer cleanup(s, tmpPath)
  57. w := s.NewWatchStream()
  58. defer w.Close()
  59. id, _ := w.Watch([]byte("foo"), false, 0)
  60. tests := []struct {
  61. cancelID int64
  62. werr error
  63. }{
  64. // no error should be returned when cancel the created watcher.
  65. {id, nil},
  66. // not exist error should be returned when cancel again.
  67. {id, ErrWatcherNotExist},
  68. // not exist error should be returned when cancel a bad id.
  69. {id + 1, ErrWatcherNotExist},
  70. }
  71. for i, tt := range tests {
  72. gerr := w.Cancel(tt.cancelID)
  73. if gerr != tt.werr {
  74. t.Errorf("#%d: err = %v, want %v", i, gerr, tt.werr)
  75. }
  76. }
  77. if l := len(w.(*watchStream).cancels); l != 0 {
  78. t.Errorf("cancels = %d, want 0", l)
  79. }
  80. }