Commit 9b00b7ef authored by Dr. Stefan Schimanski's avatar Dr. Stefan Schimanski

Avoid MockPodsListWatch deadlock of blocked channel while lock is hold

parent 1831a057
...@@ -184,7 +184,8 @@ func (lw *MockPodsListWatch) Pods() api.PodList { ...@@ -184,7 +184,8 @@ func (lw *MockPodsListWatch) Pods() api.PodList {
lw.lock.Lock() lw.lock.Lock()
defer lw.lock.Unlock() defer lw.lock.Unlock()
return lw.list obj, _ := api.Scheme.DeepCopy(&lw.list)
return *(obj.(*api.PodList))
} }
func (lw *MockPodsListWatch) Pod(name string) *api.Pod { func (lw *MockPodsListWatch) Pod(name string) *api.Pod {
...@@ -200,44 +201,56 @@ func (lw *MockPodsListWatch) Pod(name string) *api.Pod { ...@@ -200,44 +201,56 @@ func (lw *MockPodsListWatch) Pod(name string) *api.Pod {
return nil return nil
} }
func (lw *MockPodsListWatch) Add(pod *api.Pod, notify bool) { func (lw *MockPodsListWatch) Add(pod *api.Pod, notify bool) {
lw.lock.Lock() func() {
defer lw.lock.Unlock() lw.lock.Lock()
defer lw.lock.Unlock()
lw.list.Items = append(lw.list.Items, *pod)
}()
lw.list.Items = append(lw.list.Items, *pod)
if notify { if notify {
lw.fakeWatcher.Add(pod) lw.fakeWatcher.Add(pod)
} }
} }
func (lw *MockPodsListWatch) Modify(pod *api.Pod, notify bool) { func (lw *MockPodsListWatch) Modify(pod *api.Pod, notify bool) {
lw.lock.Lock() found := false
defer lw.lock.Unlock() func() {
lw.lock.Lock()
for i, otherPod := range lw.list.Items { defer lw.lock.Unlock()
if otherPod.Name == pod.Name {
lw.list.Items[i] = *pod for i, otherPod := range lw.list.Items {
if notify { if otherPod.Name == pod.Name {
lw.fakeWatcher.Modify(pod) lw.list.Items[i] = *pod
found = true
return
} }
return
} }
log.Fatalf("Cannot find pod %v to modify in MockPodsListWatch", pod.Name)
}()
if notify && found {
lw.fakeWatcher.Modify(pod)
} }
log.Fatalf("Cannot find pod %v to modify in MockPodsListWatch", pod.Name)
} }
func (lw *MockPodsListWatch) Delete(pod *api.Pod, notify bool) { func (lw *MockPodsListWatch) Delete(pod *api.Pod, notify bool) {
lw.lock.Lock() var notifyPod *api.Pod
defer lw.lock.Unlock() func() {
lw.lock.Lock()
for i, otherPod := range lw.list.Items { defer lw.lock.Unlock()
if otherPod.Name == pod.Name {
lw.list.Items = append(lw.list.Items[:i], lw.list.Items[i+1:]...) for i, otherPod := range lw.list.Items {
if notify { if otherPod.Name == pod.Name {
lw.fakeWatcher.Delete(&otherPod) lw.list.Items = append(lw.list.Items[:i], lw.list.Items[i+1:]...)
notifyPod = &otherPod
return
} }
return
} }
log.Fatalf("Cannot find pod %v to delete in MockPodsListWatch", pod.Name)
}()
if notifyPod != nil && notify {
lw.fakeWatcher.Delete(notifyPod)
} }
log.Fatalf("Cannot find pod %v to delete in MockPodsListWatch", pod.Name)
} }
// Create a pod with a given index, requiring one port // Create a pod with a given index, requiring one port
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment