watcher_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. import (
  15. "testing"
  16. )
  17. func TestWatcher(t *testing.T) {
  18. s := newStore()
  19. wh := s.WatcherHub
  20. w, err := wh.watch("/foo", true, false, 1, 1)
  21. if err != nil {
  22. t.Fatalf("%v", err)
  23. }
  24. c := w.EventChan()
  25. select {
  26. case <-c:
  27. t.Fatal("should not receive from channel before send the event")
  28. default:
  29. // do nothing
  30. }
  31. e := newEvent(Create, "/foo/bar", 1, 1)
  32. wh.notify(e)
  33. re := <-c
  34. if e != re {
  35. t.Fatal("recv != send")
  36. }
  37. w, _ = wh.watch("/foo", false, false, 2, 1)
  38. c = w.EventChan()
  39. e = newEvent(Create, "/foo/bar", 2, 2)
  40. wh.notify(e)
  41. select {
  42. case re = <-c:
  43. t.Fatal("should not receive from channel if not recursive ", re)
  44. default:
  45. // do nothing
  46. }
  47. e = newEvent(Create, "/foo", 3, 3)
  48. wh.notify(e)
  49. re = <-c
  50. if e != re {
  51. t.Fatal("recv != send")
  52. }
  53. // ensure we are doing exact matching rather than prefix matching
  54. w, _ = wh.watch("/fo", true, false, 1, 1)
  55. c = w.EventChan()
  56. select {
  57. case re = <-c:
  58. t.Fatal("should not receive from channel:", re)
  59. default:
  60. // do nothing
  61. }
  62. e = newEvent(Create, "/fo/bar", 3, 3)
  63. wh.notify(e)
  64. re = <-c
  65. if e != re {
  66. t.Fatal("recv != send")
  67. }
  68. }