watcher_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2013 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. "time"
  17. )
  18. func TestWatch(t *testing.T) {
  19. s := CreateStore(100)
  20. watchers := make([]*Watcher, 10)
  21. for i, _ := range watchers {
  22. // create a new watcher
  23. watchers[i] = NewWatcher()
  24. // add to the watchers list
  25. s.AddWatcher("foo", watchers[i], 0)
  26. }
  27. s.Set("/foo/foo", "bar", time.Unix(0, 0), 1)
  28. for _, watcher := range watchers {
  29. // wait for the notification for any changing
  30. res := <-watcher.C
  31. if res == nil {
  32. t.Fatal("watcher is cleared")
  33. }
  34. }
  35. for i, _ := range watchers {
  36. // create a new watcher
  37. watchers[i] = NewWatcher()
  38. // add to the watchers list
  39. s.AddWatcher("foo/foo/foo", watchers[i], 0)
  40. }
  41. s.watcher.stopWatchers()
  42. for _, watcher := range watchers {
  43. // wait for the notification for any changing
  44. res := <-watcher.C
  45. if res != nil {
  46. t.Fatal("watcher is cleared")
  47. }
  48. }
  49. }
  50. // BenchmarkWatch creates 10K watchers watch at /foo/[path] each time.
  51. // Path is randomly chosen with max depth 10.
  52. // It should take less than 15ms to wake up 10K watchers.
  53. func BenchmarkWatch(b *testing.B) {
  54. s := CreateStore(100)
  55. keys := GenKeys(10000, 10)
  56. b.ResetTimer()
  57. for i := 0; i < b.N; i++ {
  58. watchers := make([]*Watcher, 10000)
  59. for i := 0; i < 10000; i++ {
  60. // create a new watcher
  61. watchers[i] = NewWatcher()
  62. // add to the watchers list
  63. s.AddWatcher(keys[i], watchers[i], 0)
  64. }
  65. s.watcher.stopWatchers()
  66. for _, watcher := range watchers {
  67. // wait for the notification for any changing
  68. <-watcher.C
  69. }
  70. s.watcher = newWatcherHub()
  71. }
  72. }