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