watcher_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 The etcd Authors
  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 v2store
  15. import "testing"
  16. func TestWatcher(t *testing.T) {
  17. s := newStore()
  18. wh := s.WatcherHub
  19. w, err := wh.watch("/foo", true, false, 1, 1)
  20. if err != nil {
  21. t.Fatalf("%v", err)
  22. }
  23. c := w.EventChan()
  24. select {
  25. case <-c:
  26. t.Fatal("should not receive from channel before send the event")
  27. default:
  28. // do nothing
  29. }
  30. e := newEvent(Create, "/foo/bar", 1, 1)
  31. wh.notify(e)
  32. re := <-c
  33. if e != re {
  34. t.Fatal("recv != send")
  35. }
  36. w, _ = wh.watch("/foo", false, false, 2, 1)
  37. c = w.EventChan()
  38. e = newEvent(Create, "/foo/bar", 2, 2)
  39. wh.notify(e)
  40. select {
  41. case re = <-c:
  42. t.Fatal("should not receive from channel if not recursive ", re)
  43. default:
  44. // do nothing
  45. }
  46. e = newEvent(Create, "/foo", 3, 3)
  47. wh.notify(e)
  48. re = <-c
  49. if e != re {
  50. t.Fatal("recv != send")
  51. }
  52. // ensure we are doing exact matching rather than prefix matching
  53. w, _ = wh.watch("/fo", true, false, 1, 1)
  54. c = w.EventChan()
  55. select {
  56. case re = <-c:
  57. t.Fatal("should not receive from channel:", re)
  58. default:
  59. // do nothing
  60. }
  61. e = newEvent(Create, "/fo/bar", 3, 3)
  62. wh.notify(e)
  63. re = <-c
  64. if e != re {
  65. t.Fatal("recv != send")
  66. }
  67. }