watcher_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package store
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestWatch(t *testing.T) {
  7. s := CreateStore(100)
  8. watchers := make([]*Watcher, 10)
  9. for i, _ := range watchers {
  10. // create a new watcher
  11. watchers[i] = NewWatcher()
  12. // add to the watchers list
  13. s.AddWatcher("foo", watchers[i], 0)
  14. }
  15. s.Set("/foo/foo", "bar", time.Unix(0, 0), 1)
  16. for _, watcher := range watchers {
  17. // wait for the notification for any changing
  18. res := <-watcher.C
  19. if res == nil {
  20. t.Fatal("watcher is cleared")
  21. }
  22. }
  23. for i, _ := range watchers {
  24. // create a new watcher
  25. watchers[i] = NewWatcher()
  26. // add to the watchers list
  27. s.AddWatcher("foo/foo/foo", watchers[i], 0)
  28. }
  29. s.watcher.stopWatchers()
  30. for _, watcher := range watchers {
  31. // wait for the notification for any changing
  32. res := <-watcher.C
  33. if res != nil {
  34. t.Fatal("watcher is cleared")
  35. }
  36. }
  37. }
  38. // BenchmarkWatch creates 10K watchers watch at /foo/[path] each time.
  39. // Path is randomly chosen with max depth 10.
  40. // It should take less than 15ms to wake up 10K watchers.
  41. func BenchmarkWatch(b *testing.B) {
  42. s := CreateStore(100)
  43. keys := GenKeys(10000, 10)
  44. b.ResetTimer()
  45. for i := 0; i < b.N; i++ {
  46. watchers := make([]*Watcher, 10000)
  47. for i := 0; i < 10000; i++ {
  48. // create a new watcher
  49. watchers[i] = NewWatcher()
  50. // add to the watchers list
  51. s.AddWatcher(keys[i], watchers[i], 0)
  52. }
  53. s.watcher.stopWatchers()
  54. for _, watcher := range watchers {
  55. // wait for the notification for any changing
  56. <-watcher.C
  57. }
  58. s.watcher = newWatcherHub()
  59. }
  60. }