watcher_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 (
  16. "testing"
  17. "github.com/coreos/etcd/storage/backend"
  18. )
  19. // TestWatcherWatchID tests that each watcher provides unique watchID,
  20. // and the watched event attaches the correct watchID.
  21. func TestWatcherWatchID(t *testing.T) {
  22. b, tmpPath := backend.NewDefaultTmpBackend()
  23. s := WatchableKV(newWatchableStore(b))
  24. defer cleanup(s, b, tmpPath)
  25. w := s.NewWatchStream()
  26. defer w.Close()
  27. idm := make(map[WatchID]struct{})
  28. for i := 0; i < 10; i++ {
  29. id := w.Watch([]byte("foo"), false, 0)
  30. if _, ok := idm[id]; ok {
  31. t.Errorf("#%d: id %d exists", i, id)
  32. }
  33. idm[id] = struct{}{}
  34. s.Put([]byte("foo"), []byte("bar"), NoLease)
  35. resp := <-w.Chan()
  36. if resp.WatchID != id {
  37. t.Errorf("#%d: watch id in event = %d, want %d", i, resp.WatchID, id)
  38. }
  39. if err := w.Cancel(id); err != nil {
  40. t.Error(err)
  41. }
  42. }
  43. s.Put([]byte("foo2"), []byte("bar"), NoLease)
  44. // unsynced watchers
  45. for i := 10; i < 20; i++ {
  46. id := w.Watch([]byte("foo2"), false, 1)
  47. if _, ok := idm[id]; ok {
  48. t.Errorf("#%d: id %d exists", i, id)
  49. }
  50. idm[id] = struct{}{}
  51. resp := <-w.Chan()
  52. if resp.WatchID != id {
  53. t.Errorf("#%d: watch id in event = %d, want %d", i, resp.WatchID, id)
  54. }
  55. if err := w.Cancel(id); err != nil {
  56. t.Error(err)
  57. }
  58. }
  59. }
  60. // TestWatchStreamCancel ensures cancel calls the cancel func of the watcher
  61. // with given id inside watchStream.
  62. func TestWatchStreamCancelWatcherByID(t *testing.T) {
  63. b, tmpPath := backend.NewDefaultTmpBackend()
  64. s := WatchableKV(newWatchableStore(b))
  65. defer cleanup(s, b, tmpPath)
  66. w := s.NewWatchStream()
  67. defer w.Close()
  68. id := w.Watch([]byte("foo"), false, 0)
  69. tests := []struct {
  70. cancelID WatchID
  71. werr error
  72. }{
  73. // no error should be returned when cancel the created watcher.
  74. {id, nil},
  75. // not exist error should be returned when cancel again.
  76. {id, ErrWatcherNotExist},
  77. // not exist error should be returned when cancel a bad id.
  78. {id + 1, ErrWatcherNotExist},
  79. }
  80. for i, tt := range tests {
  81. gerr := w.Cancel(tt.cancelID)
  82. if gerr != tt.werr {
  83. t.Errorf("#%d: err = %v, want %v", i, gerr, tt.werr)
  84. }
  85. }
  86. if l := len(w.(*watchStream).cancels); l != 0 {
  87. t.Errorf("cancels = %d, want 0", l)
  88. }
  89. }