Skip to content

Commit 8348010

Browse files
committed
[FAB-15626] avoid use of testing.T in async logs
- Replace use of NewTestLogger with a fabric logger that writes to a gbytes.Buffer. - Reset production logger at end of test Signed-off-by: Matthew Sykes <matthew.sykes@gmail.com> Change-Id: I4255a6318a529587b5d4fd3a6a34964a1868c27c
1 parent 2220849 commit 8348010

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

gossip/state/state_test.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"math/rand"
1414
"net"
15+
"strings"
1516
"sync"
1617
"sync/atomic"
1718
"testing"
@@ -21,7 +22,7 @@ import (
2122
"github.com/hyperledger/fabric/bccsp/factory"
2223
"github.com/hyperledger/fabric/common/configtx/test"
2324
errors2 "github.com/hyperledger/fabric/common/errors"
24-
"github.com/hyperledger/fabric/common/flogging/floggingtest"
25+
"github.com/hyperledger/fabric/common/flogging"
2526
"github.com/hyperledger/fabric/common/metrics/disabled"
2627
"github.com/hyperledger/fabric/common/util"
2728
corecomm "github.com/hyperledger/fabric/core/comm"
@@ -48,6 +49,7 @@ import (
4849
"github.com/hyperledger/fabric/protos/ledger/rwset"
4950
transientstore2 "github.com/hyperledger/fabric/protos/transientstore"
5051
"github.com/hyperledger/fabric/protoutil"
52+
"github.com/onsi/gomega/gbytes"
5153
"github.com/stretchr/testify/assert"
5254
"github.com/stretchr/testify/mock"
5355
)
@@ -751,7 +753,15 @@ func TestHaltChainProcessing(t *testing.T) {
751753
}
752754
}
753755

754-
l, recorder := floggingtest.NewTestLogger(t)
756+
buf := gbytes.NewBuffer()
757+
logging, err := flogging.New(flogging.Config{
758+
LogSpec: "debug",
759+
Writer: buf,
760+
})
761+
assert.NoError(t, err, "failed to create logging")
762+
763+
defer func(l gossiputil.Logger) { logger = l }(logger)
764+
l := logging.Logger("state_test")
755765
logger = l
756766

757767
mc := &mockCommitter{Mock: &mock.Mock{}}
@@ -771,9 +781,10 @@ func TestHaltChainProcessing(t *testing.T) {
771781
peerNode := newPeerNodeWithGossipWithValidator(0, mc, noopPeerIdentityAcceptor, g, v)
772782
defer peerNode.shutdown()
773783
gossipMsgs <- newBlockMsg(1)
774-
assertLogged(t, recorder, "Got error while committing")
775-
assertLogged(t, recorder, "Aborting chain processing")
776-
assertLogged(t, recorder, "foobar")
784+
785+
assertLogged(t, buf, "Got error while committing")
786+
assertLogged(t, buf, "Aborting chain processing")
787+
assertLogged(t, buf, "foobar")
777788
}
778789

779790
func TestFailures(t *testing.T) {
@@ -1680,7 +1691,7 @@ func waitUntilTrueOrTimeout(t *testing.T, predicate func() bool, timeout time.Du
16801691
t.Log("Stop waiting until timeout or true")
16811692
}
16821693

1683-
func assertLogged(t *testing.T, r *floggingtest.Recorder, msg string) {
1684-
observed := func() bool { return len(r.MessagesContaining(msg)) > 0 }
1694+
func assertLogged(t *testing.T, buf *gbytes.Buffer, msg string) {
1695+
observed := func() bool { return strings.Contains(string(buf.Contents()), msg) }
16851696
waitUntilTrueOrTimeout(t, observed, 30*time.Second)
16861697
}

0 commit comments

Comments
 (0)