Skip to content

Commit 0806ef2

Browse files
committed
scheduler: refactor testing structures for better reusability
1 parent 7afbd68 commit 0806ef2

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

pkg/scheduler/internal/queue/scheduling_queue_test.go

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,48 @@ func TestHighProirotyFlushUnschedulableQLeftover(t *testing.T) {
987987
}
988988
}
989989

990+
type operation func(queue *PriorityQueue, pInfo *podInfo)
991+
992+
var (
993+
addPodActiveQ = func(queue *PriorityQueue, pInfo *podInfo) {
994+
queue.lock.Lock()
995+
queue.activeQ.Add(pInfo)
996+
queue.lock.Unlock()
997+
}
998+
updatePodActiveQ = func(queue *PriorityQueue, pInfo *podInfo) {
999+
queue.lock.Lock()
1000+
queue.activeQ.Update(pInfo)
1001+
queue.lock.Unlock()
1002+
}
1003+
addPodUnschedulableQ = func(queue *PriorityQueue, pInfo *podInfo) {
1004+
queue.lock.Lock()
1005+
// Update pod condition to unschedulable.
1006+
podutil.UpdatePodCondition(&pInfo.pod.Status, &v1.PodCondition{
1007+
Type: v1.PodScheduled,
1008+
Status: v1.ConditionFalse,
1009+
Reason: v1.PodReasonUnschedulable,
1010+
Message: "fake scheduling failure",
1011+
})
1012+
queue.unschedulableQ.addOrUpdate(pInfo)
1013+
queue.lock.Unlock()
1014+
}
1015+
addPodBackoffQ = func(queue *PriorityQueue, pInfo *podInfo) {
1016+
queue.lock.Lock()
1017+
queue.podBackoffQ.Add(pInfo)
1018+
queue.lock.Unlock()
1019+
}
1020+
moveAllToActiveQ = func(queue *PriorityQueue, _ *podInfo) {
1021+
queue.MoveAllToActiveQueue()
1022+
}
1023+
backoffPod = func(queue *PriorityQueue, pInfo *podInfo) {
1024+
queue.backoffPod(pInfo.pod)
1025+
}
1026+
flushBackoffQ = func(queue *PriorityQueue, _ *podInfo) {
1027+
queue.clock.(*clock.FakeClock).Step(2 * time.Second)
1028+
queue.flushBackoffQCompleted()
1029+
}
1030+
)
1031+
9901032
// TestPodTimestamp tests the operations related to podInfo.
9911033
func TestPodTimestamp(t *testing.T) {
9921034
pod1 := &v1.Pod{
@@ -1021,101 +1063,61 @@ func TestPodTimestamp(t *testing.T) {
10211063
timestamp: timestamp.Add(time.Second),
10221064
}
10231065

1024-
var queue *PriorityQueue
1025-
type operation = func()
1026-
addPodActiveQ := func(pInfo *podInfo) operation {
1027-
return func() {
1028-
queue.lock.Lock()
1029-
defer queue.lock.Unlock()
1030-
queue.activeQ.Add(pInfo)
1031-
}
1032-
}
1033-
updatePodActiveQ := func(pInfo *podInfo) operation {
1034-
return func() {
1035-
queue.lock.Lock()
1036-
defer queue.lock.Unlock()
1037-
queue.activeQ.Update(pInfo)
1038-
}
1039-
}
1040-
addPodUnschedulableQ := func(pInfo *podInfo) operation {
1041-
return func() {
1042-
queue.lock.Lock()
1043-
defer queue.lock.Unlock()
1044-
// Update pod condition to unschedulable.
1045-
podutil.UpdatePodCondition(&pInfo.pod.Status, &v1.PodCondition{
1046-
Type: v1.PodScheduled,
1047-
Status: v1.ConditionFalse,
1048-
Reason: v1.PodReasonUnschedulable,
1049-
Message: "fake scheduling failure",
1050-
})
1051-
queue.unschedulableQ.addOrUpdate(pInfo)
1052-
}
1053-
}
1054-
addPodBackoffQ := func(pInfo *podInfo) operation {
1055-
return func() {
1056-
queue.lock.Lock()
1057-
defer queue.lock.Unlock()
1058-
queue.podBackoffQ.Add(pInfo)
1059-
}
1060-
}
1061-
moveAllToActiveQ := func() operation {
1062-
return func() {
1063-
queue.MoveAllToActiveQueue()
1064-
}
1065-
}
1066-
backoffPod := func(pInfo *podInfo) operation {
1067-
return func() {
1068-
queue.backoffPod(pInfo.pod)
1069-
}
1070-
}
1071-
flushBackoffQ := func() operation {
1072-
return func() {
1073-
queue.clock.(*clock.FakeClock).Step(2 * time.Second)
1074-
queue.flushBackoffQCompleted()
1075-
}
1076-
}
10771066
tests := []struct {
10781067
name string
10791068
operations []operation
1069+
operants []*podInfo
10801070
expected []*podInfo
10811071
}{
10821072
{
10831073
name: "add two pod to activeQ and sort them by the timestamp",
10841074
operations: []operation{
1085-
addPodActiveQ(pInfo2), addPodActiveQ(pInfo1),
1075+
addPodActiveQ,
1076+
addPodActiveQ,
10861077
},
1078+
operants: []*podInfo{pInfo2, pInfo1},
10871079
expected: []*podInfo{pInfo1, pInfo2},
10881080
},
10891081
{
10901082
name: "update two pod to activeQ and sort them by the timestamp",
10911083
operations: []operation{
1092-
updatePodActiveQ(pInfo2), updatePodActiveQ(pInfo1),
1084+
updatePodActiveQ,
1085+
updatePodActiveQ,
10931086
},
1087+
operants: []*podInfo{pInfo2, pInfo1},
10941088
expected: []*podInfo{pInfo1, pInfo2},
10951089
},
10961090
{
10971091
name: "add two pod to unschedulableQ then move them to activeQ and sort them by the timestamp",
10981092
operations: []operation{
1099-
addPodUnschedulableQ(pInfo2), addPodUnschedulableQ(pInfo1), moveAllToActiveQ(),
1093+
addPodUnschedulableQ,
1094+
addPodUnschedulableQ,
1095+
moveAllToActiveQ,
11001096
},
1097+
operants: []*podInfo{pInfo2, pInfo1, nil},
11011098
expected: []*podInfo{pInfo1, pInfo2},
11021099
},
11031100
{
11041101
name: "add one pod to BackoffQ and move it to activeQ",
11051102
operations: []operation{
1106-
addPodActiveQ(pInfo2), addPodBackoffQ(pInfo1), backoffPod(pInfo1), flushBackoffQ(), moveAllToActiveQ(),
1103+
addPodActiveQ,
1104+
addPodBackoffQ,
1105+
backoffPod,
1106+
flushBackoffQ,
1107+
moveAllToActiveQ,
11071108
},
1109+
operants: []*podInfo{pInfo2, pInfo1, pInfo1, nil, nil},
11081110
expected: []*podInfo{pInfo1, pInfo2},
11091111
},
11101112
}
11111113

11121114
for _, test := range tests {
11131115
t.Run(test.name, func(t *testing.T) {
1114-
queue = NewPriorityQueueWithClock(nil, clock.NewFakeClock(timestamp))
1116+
queue := NewPriorityQueueWithClock(nil, clock.NewFakeClock(timestamp))
11151117
var podInfoList []*podInfo
11161118

1117-
for _, op := range test.operations {
1118-
op()
1119+
for i, op := range test.operations {
1120+
op(queue, test.operants[i])
11191121
}
11201122

11211123
for i := 0; i < len(test.expected); i++ {

0 commit comments

Comments
 (0)