watcher_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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[WatchID]struct{})
  24. for i := 0; i < 10; i++ {
  25. id := 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. if err := w.Cancel(id); err != nil {
  36. t.Error(err)
  37. }
  38. }
  39. s.Put([]byte("foo2"), []byte("bar"), NoLease)
  40. // unsynced watchers
  41. for i := 10; i < 20; i++ {
  42. id := 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. resp := <-w.Chan()
  48. if resp.WatchID != id {
  49. t.Errorf("#%d: watch id in event = %d, want %d", i, resp.WatchID, id)
  50. }
  51. if err := w.Cancel(id); err != nil {
  52. t.Error(err)
  53. }
  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 WatchID
  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. }