Repository: kubernetes/kubernetes
File: pkg/scheduler/backend/queue/unschedulable_pods_test.go (Go)

Code before:

func (m *mockMetricRecorder) Clear() {
	m.val.Store(0)
}

func (m *mockMetricRecorder) Value() int64 {
	return m.val.Load()
}

func TestUnschedulablePods(t *testing.T) {
	type action string

	const (
		add    action = "adding"
		update action = "updating"
		delete action = "deleting"
		clear  action = "clearing"
	)

	type step struct {
		action       action
		pods         []*framework.QueuedPodInfo
		expectedPods []*framework.QueuedPodInfo
	}

	var actionToOperation = map[action]func(pInfo *framework.QueuedPodInfo, upm *unschedulablePods, gatedBefore bool){
		add: func(pInfo *framework.QueuedPodInfo, upm *unschedulablePods, _ bool) {
			upm.addOrUpdate(pInfo, false, framework.EventUnscheduledPodAdd.Label())
		},
		update: func(pInfo *framework.QueuedPodInfo, upm *unschedulablePods, gatedBefore bool) {
			upm.addOrUpdate(pInfo, gatedBefore, framework.EventUnscheduledPodUpdate.Label())
		},
		delete: func(pInfo *framework.QueuedPodInfo, upm *unschedulablePods, gatedBefore bool) {
			upm.delete(pInfo.Pod, gatedBefore)
		},
		clear: func(_ *framework.QueuedPodInfo, upm *unschedulablePods, _ bool) {
			upm.clear()
		},
	}

	var pods = []*v1.Pod{
		st.MakePod().Name("p0").Namespace("ns1").Annotation("annot1", "val1").NominatedNodeName("node1").Obj(),
		st.MakePod().Name("p1").Namespace("ns1").Annotation("annot", "val").Obj(),
		st.MakePod().Name("p2").Namespace("ns2").Annotation("annot2", "val2").Annotation("annot3", "val3").NominatedNodeName("node3").Obj(),
		st.MakePod().Name("p3").Namespace("ns1").Annotation("annot4", "val4").Obj(),
		st.MakePod().Name("p4").Namespace("ns2").Annotation("annot5", "val5").NominatedNodeName("node4").Obj(),
		st.MakePod().Name("p5").Namespace("ns1").Annotation("annot6", "val6").Obj(),
	}

	gated := func(p *v1.Pod, upm *unschedulablePods) bool {
		pInfo := upm.get(p)

Review comment (suggestion):
nit, would be better if data structure comes first:
```suggestion
	var actionToOperation = map[action]func(upm *unschedulablePods, pInfo *framework.QueuedPodInfo, gatedBefore bool){
```

Code after:
func (m *mockMetricRecorder) Dec() {
	m.val.Add(-1)
}

func (m *mockMetricRecorder) Clear() {
	m.val.Store(0)
}

func (m *mockMetricRecorder) value() int64 {
	return m.val.Load()
}

func TestUnschedulablePods(t *testing.T) {
	type action string

	const (
		add    action = "adding"
		update action = "updating"
		delete action = "deleting"
		clear  action = "clearing"
	)

	type step struct {
		action       action
		pods         []*framework.QueuedPodInfo
		expectedPods []*framework.QueuedPodInfo
	}

	var actionToOperation = map[action]func(upm *unschedulablePods, pInfo *framework.QueuedPodInfo, gatedBefore bool){
		add: func(upm *unschedulablePods, pInfo *framework.QueuedPodInfo, _ bool) {
			upm.addOrUpdate(pInfo, false, framework.EventUnscheduledPodAdd.Label())
		},
		update: func(upm *unschedulablePods, pInfo *framework.QueuedPodInfo, gatedBefore bool) {
			upm.addOrUpdate(pInfo, gatedBefore, framework.EventUnscheduledPodUpdate.Label())
		},
		delete: func(upm *unschedulablePods, pInfo *framework.QueuedPodInfo, gatedBefore bool) {
			upm.delete(pInfo.Pod, gatedBefore)
		},
		clear: func(upm *unschedulablePods, _ *framework.QueuedPodInfo, _ bool) {
			upm.clear()
		},
	}

	var pods = []*v1.Pod{
		st.MakePod().Name("p0").Namespace("ns1").Annotation("annot1", "val1").NominatedNodeName("node1").Obj(),
		st.MakePod().Name("p1").Namespace("ns1").Annotation("annot", "val").Obj(),
		st.MakePod().Name("p2").Namespace("ns2").Annotation("annot2", "val2").Annotation("annot3", "val3").NominatedNodeName("node3").Obj(),
		st.MakePod().Name("p3").Namespace("ns1").Annotation("annot4", "val4").Obj(),
		st.MakePod().Name("p4").Namespace("ns2").Annotation("annot5", "val5").NominatedNodeName("node4").Obj(),
		st.MakePod().Name("p5").Namespace("ns1").Annotation("annot6", "val6").Obj(),
	}