Skip to content

Commit 5cc0dea

Browse files
committed
[FAB-11685] avoid data races in shim tests
The shim tests use many go routines and cause the go-logging infrastructure to be initialized many, many times. This introduces races. Avoid the races by making initialization run once and moving the logging specific test to drive the underlying, unexported method. Change-Id: I175d103b2965b0813baf588def35e3245b77b004 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 8db4c58 commit 5cc0dea

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

core/chaincode/shim/chaincode.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"io/ioutil"
1616
"os"
1717
"strings"
18+
"sync"
1819
"time"
1920
"unicode/utf8"
2021

@@ -163,10 +164,16 @@ func IsEnabledForLogLevel(logLevel string) bool {
163164
return chaincodeLogger.IsEnabledFor(lvl)
164165
}
165166

167+
var loggingSetup sync.Once
168+
166169
// SetupChaincodeLogging sets the chaincode logging format and the level
167170
// to the values of CORE_CHAINCODE_LOGGING_FORMAT, CORE_CHAINCODE_LOGGING_LEVEL
168171
// and CORE_CHAINCODE_LOGGING_SHIM set from core.yaml by chaincode_support.go
169172
func SetupChaincodeLogging() {
173+
loggingSetup.Do(setupChaincodeLogging)
174+
}
175+
176+
func setupChaincodeLogging() {
170177
// This is the default log config from 1.2
171178
const defaultLogFormat = "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
172179
const defaultLevel = logging.INFO

core/chaincode/shim/shim_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ func TestSetupChaincodeLogging_shim(t *testing.T) {
493493
viper.Set("chaincode.logging.level", tc.ccLogLevel)
494494
viper.Set("chaincode.logging.shim", tc.shimLogLevel)
495495

496-
SetupChaincodeLogging()
496+
setupChaincodeLogging()
497497

498498
_, ccErr := logging.LogLevel(tc.ccLogLevel)
499499
_, shimErr := logging.LogLevel(tc.shimLogLevel)

0 commit comments

Comments
 (0)