add watchHistory clone

This commit is contained in:
Xiang Li 2013-09-28 17:41:02 -07:00
parent 2c9d57a9fe
commit 33e010ebd8
3 changed files with 30 additions and 4 deletions

View File

@ -151,3 +151,25 @@ func (eh *EventHistory) scan(prefix string, index uint64) (*Event, error) {
}
}
// clone will be protected by a stop-world lock
// do not need to obtain internal lock
func (eh *EventHistory) clone() *EventHistory {
clonedQueue := eventQueue{
Capacity: eh.Queue.Capacity,
Events: make([]*Event, eh.Queue.Capacity),
Size: eh.Queue.Size,
Front: eh.Queue.Front,
}
for i, e := range eh.Queue.Events {
clonedQueue.Events[i] = e
}
return &EventHistory{
StartIndex: eh.StartIndex,
Queue: clonedQueue,
}
}

View File

@ -247,7 +247,7 @@ func TestExpire(t *testing.T) {
s.Create("/foo", "bar", expire, 1, 1)
_, err := s.InternalGet("/foo", 1, 1)
_, err := s.internalGet("/foo", 1, 1)
if err != nil {
t.Fatalf("can not get the node")
@ -255,7 +255,7 @@ func TestExpire(t *testing.T) {
time.Sleep(time.Second * 2)
_, err = s.InternalGet("/foo", 1, 1)
_, err = s.internalGet("/foo", 1, 1)
if err == nil {
t.Fatalf("can get the node after expiration time")
@ -266,7 +266,7 @@ func TestExpire(t *testing.T) {
s.Create("/foo", "bar", expire, 1, 1)
time.Sleep(time.Millisecond * 50)
_, err = s.InternalGet("/foo", 1, 1)
_, err = s.internalGet("/foo", 1, 1)
if err != nil {
t.Fatalf("cannot get the node before expiration", err.Error())

View File

@ -118,5 +118,9 @@ func (wh *watcherHub) notify(e *Event) {
}
func (wh *watcherHub) clone() *watcherHub {
return &watcherHub{}
clonedHistory := wh.EventHistory.clone()
return &watcherHub{
EventHistory: clonedHistory,
}
}