@@ -22,6 +22,7 @@ import (
22
22
"context"
23
23
"fmt"
24
24
"strings"
25
+ "sync"
25
26
"testing"
26
27
"time"
27
28
@@ -813,9 +814,12 @@ func TestAsyncPreemption(t *testing.T) {
813
814
t .Run (test .name , func (t * testing.T ) {
814
815
// We need to use a custom preemption plugin to test async preemption behavior
815
816
delayedPreemptionPluginName := "delay-preemption"
817
+ var lock sync.Mutex
816
818
// keyed by the pod name
817
819
preemptionDoneChannels := make (map [string ]chan struct {})
818
820
defer func () {
821
+ lock .Lock ()
822
+ defer lock .Unlock ()
819
823
for _ , ch := range preemptionDoneChannels {
820
824
close (ch )
821
825
}
@@ -841,7 +845,10 @@ func TestAsyncPreemption(t *testing.T) {
841
845
preemptPodFn := preemptionPlugin .Evaluator .PreemptPod
842
846
preemptionPlugin .Evaluator .PreemptPod = func (ctx context.Context , c preemption.Candidate , preemptor , victim * v1.Pod , pluginName string ) error {
843
847
// block the preemption goroutine to complete until the test case allows it to proceed.
844
- if ch , ok := preemptionDoneChannels [preemptor .Name ]; ok {
848
+ lock .Lock ()
849
+ ch , ok := preemptionDoneChannels [preemptor .Name ]
850
+ lock .Unlock ()
851
+ if ok {
845
852
<- ch
846
853
}
847
854
return preemptPodFn (ctx , c , preemptor , victim , pluginName )
@@ -941,7 +948,9 @@ func TestAsyncPreemption(t *testing.T) {
941
948
t .Fatal (lastFailure )
942
949
}
943
950
951
+ lock .Lock ()
944
952
preemptionDoneChannels [scenario .schedulePod .podName ] = make (chan struct {})
953
+ lock .Unlock ()
945
954
testCtx .Scheduler .ScheduleOne (testCtx .Ctx )
946
955
if scenario .schedulePod .expectSuccess {
947
956
if err := wait .PollUntilContextTimeout (testCtx .Ctx , 200 * time .Millisecond , wait .ForeverTestTimeout , false , testutils .PodScheduled (cs , testCtx .NS .Name , scenario .schedulePod .podName )); err != nil {
@@ -953,12 +962,14 @@ func TestAsyncPreemption(t *testing.T) {
953
962
}
954
963
}
955
964
case scenario .completePreemption != "" :
965
+ lock .Lock ()
956
966
if _ , ok := preemptionDoneChannels [scenario .completePreemption ]; ! ok {
957
967
t .Fatalf ("The preemptor Pod %q is not running preemption" , scenario .completePreemption )
958
968
}
959
969
960
970
close (preemptionDoneChannels [scenario .completePreemption ])
961
971
delete (preemptionDoneChannels , scenario .completePreemption )
972
+ lock .Unlock ()
962
973
case scenario .podGatedInQueue != "" :
963
974
// make sure the Pod is in the queue in the first place.
964
975
if ! podInUnschedulablePodPool (t , testCtx .Scheduler .SchedulingQueue , scenario .podGatedInQueue ) {
0 commit comments