Fix Data Race in nodeshutdown restart

This commit is contained in:
Shiming Zhang 2021-06-29 16:23:37 +08:00
parent 1151dc1ee5
commit a42c066af7

View File

@ -21,6 +21,7 @@ package nodeshutdown
import (
"fmt"
"strings"
"sync"
"testing"
"time"
@ -329,15 +330,18 @@ func TestRestart(t *testing.T) {
syncNodeStatus := func() {}
var shutdownChan chan bool
var shutdownChanMut sync.Mutex
var connChan = make(chan struct{}, 1)
systemDbus = func() (dbusInhibiter, error) {
defer func() {
connChan <- struct{}{}
}()
shutdownChan = make(chan bool)
dbus := &fakeDbus{currentInhibitDelay: systemInhibitDelay, shutdownChan: shutdownChan, overrideSystemInhibitDelay: overrideSystemInhibitDelay}
ch := make(chan bool)
shutdownChanMut.Lock()
shutdownChan = ch
shutdownChanMut.Unlock()
dbus := &fakeDbus{currentInhibitDelay: systemInhibitDelay, shutdownChan: ch, overrideSystemInhibitDelay: overrideSystemInhibitDelay}
return dbus, nil
}
@ -354,7 +358,9 @@ func TestRestart(t *testing.T) {
case <-connChan:
}
time.Sleep(time.Second)
time.Sleep(dbusReconnectPeriod)
shutdownChanMut.Lock()
close(shutdownChan)
shutdownChanMut.Unlock()
}
}