Fix one possible data race in admin tests (#6537)

go test shows the following warning:
```
WARNING: DATA RACE
Write at 0x000002909e18 by goroutine 276:
  github.com/minio/minio/cmd.testAdminCmdRunnerSignalService()
      /home/travis/gopath/src/github.com/minio/minio/cmd/admin-rpc_test.go:44 +0x94

 Previous read at 0x000002909e18 by goroutine 194:
  github.com/minio/minio/cmd.testServiceSignalReceiver()
      /home/travis/gopath/src/github.com/minio/minio/cmd/admin-handlers_test.go:467 +0x70
```

The reason for this data race is that some admin tests are not waiting for go routines
that they created to be properly exited, which triggers the race detector.
master
Anis Elleuch 6 years ago committed by kannappanr
parent 6c7c6bec91
commit 5c765bc63e
  1. 23
      cmd/admin-handlers_test.go

@ -27,6 +27,7 @@ import (
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
@ -506,10 +507,16 @@ func testServicesCmdHandler(cmd cmdType, t *testing.T) {
globalMinioAddr = "127.0.0.1:9000" globalMinioAddr = "127.0.0.1:9000"
initGlobalAdminPeers(mustGetNewEndpointList("http://127.0.0.1:9000/d1")) initGlobalAdminPeers(mustGetNewEndpointList("http://127.0.0.1:9000/d1"))
var wg sync.WaitGroup
// Setting up a go routine to simulate ServerRouter's // Setting up a go routine to simulate ServerRouter's
// handleServiceSignals for stop and restart commands. // handleServiceSignals for stop and restart commands.
if cmd == restartCmd { if cmd == restartCmd {
go testServiceSignalReceiver(cmd, t) wg.Add(1)
go func() {
defer wg.Done()
testServiceSignalReceiver(cmd, t)
}()
} }
credentials := globalServerConfig.GetCredential() credentials := globalServerConfig.GetCredential()
@ -545,6 +552,9 @@ func testServicesCmdHandler(cmd cmdType, t *testing.T) {
t.Errorf("Expected to receive %d status code but received %d. Body (%s)", t.Errorf("Expected to receive %d status code but received %d. Body (%s)",
http.StatusOK, rec.Code, string(resp)) http.StatusOK, rec.Code, string(resp))
} }
// Wait until testServiceSignalReceiver() called in a goroutine quits.
wg.Wait()
} }
// Test for service status management REST API. // Test for service status management REST API.
@ -699,9 +709,15 @@ func TestSetConfigHandler(t *testing.T) {
globalMinioAddr = "127.0.0.1:9000" globalMinioAddr = "127.0.0.1:9000"
initGlobalAdminPeers(mustGetNewEndpointList("http://127.0.0.1:9000/d1")) initGlobalAdminPeers(mustGetNewEndpointList("http://127.0.0.1:9000/d1"))
var wg sync.WaitGroup
// SetConfigHandler restarts minio setup - need to start a // SetConfigHandler restarts minio setup - need to start a
// signal receiver to receive on globalServiceSignalCh. // signal receiver to receive on globalServiceSignalCh.
go testServiceSignalReceiver(restartCmd, t) wg.Add(1)
go func() {
defer wg.Done()
testServiceSignalReceiver(restartCmd, t)
}()
// Prepare query params for set-config mgmt REST API. // Prepare query params for set-config mgmt REST API.
queryVal := url.Values{} queryVal := url.Values{}
@ -762,6 +778,9 @@ func TestSetConfigHandler(t *testing.T) {
t.Errorf("Got unexpected response code or body %d - %s", rec.Code, respBody) t.Errorf("Got unexpected response code or body %d - %s", rec.Code, respBody)
} }
} }
// Wait until testServiceSignalReceiver finishes its execution
wg.Wait()
} }
func TestAdminServerInfo(t *testing.T) { func TestAdminServerInfo(t *testing.T) {

Loading…
Cancel
Save