watcher_test.go 1.7 KB

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