watcher_test.go 2.8 KB

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