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

Code before:
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()
}

type action string

const (
	Add    action = "adding"
	Update action = "updating"
	Delete action = "deleting"
	Clear  action = "clearing"
)

type step struct {
	Action      action
	Pods        []*framework.QueuedPodInfo
	ExpectedMap map[string]*framework.QueuedPodInfo
}

func TestUnschedulablePods(t *testing.T) {
	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(),
	}

Review comment (suggestion):
You can move these definitions inside `TestUnschedulablePods` func

Code after:
	m.val.Add(1)
}

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(),