Skip to content

Commit

Permalink
[FAB-10518] Ensure testing prints to stdout is flushed
Browse files Browse the repository at this point in the history
Change-Id: Iedf6d7106b0ac33f99f0d18864058fbe275703e5
Signed-off-by: Troy Ronda <troy@troyronda.com>
  • Loading branch information
troyronda committed Jun 5, 2018
1 parent 9ef508f commit cc5f23c
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 80 deletions.
4 changes: 2 additions & 2 deletions pkg/core/config/config_test.go
Expand Up @@ -8,13 +8,13 @@ package config

import (
"bytes"
"fmt"
"os"
"testing"

"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
"github.com/hyperledger/fabric-sdk-go/pkg/util/test"

"github.com/pkg/errors"
"github.com/spf13/viper"
Expand Down Expand Up @@ -244,7 +244,7 @@ func setUp(m *testing.M) {
var err error
cfgBackend, err := FromFile(configTestFilePath)()
if err != nil {
fmt.Println(err.Error())
test.Logf(err.Error())
}
if len(cfgBackend) != 1 {
panic("invalid backend found")
Expand Down
5 changes: 3 additions & 2 deletions pkg/fab/comm/comm_test.go
Expand Up @@ -15,6 +15,7 @@ import (

eventmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/util/test"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
"google.golang.org/grpc"
Expand Down Expand Up @@ -73,14 +74,14 @@ func startEndorsers(count int, address string) ([]*grpc.Server, []string, error)
func startEndorserServer(grpcServer *grpc.Server, address string) (*mocks.MockEndorserServer, string, bool) {
lis, err := net.Listen("tcp", address)
if err != nil {
fmt.Printf("Error starting test server %s\n", err)
test.Logf("Error starting test server [%s]", err)
return nil, "", false
}
addr := lis.Addr().String()

endorserServer := &mocks.MockEndorserServer{}
pb.RegisterEndorserServer(grpcServer, endorserServer)
fmt.Printf("Starting test server on %s\n", addr)
test.Logf("Starting test server [%s]", addr)
go grpcServer.Serve(lis)
return endorserServer, addr, true
}
29 changes: 15 additions & 14 deletions pkg/fab/events/client/client_test.go
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/util/test"

"github.com/hyperledger/fabric-sdk-go/pkg/fab/events/api"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/events/client/dispatcher"
Expand Down Expand Up @@ -824,7 +825,7 @@ func checkConcurrentEvents(blockTestErr chan error, t *testing.T, fblockTestErr

func checkIfAllEventsRecv(blockTestDone bool, fblockTestDone bool, ccTestDone bool, txStatusTestDone bool) bool {
if blockTestDone && fblockTestDone && ccTestDone && txStatusTestDone {
fmt.Println("All tests completed successfully")
test.Logf("All tests completed successfully")
return true
}
return false
Expand Down Expand Up @@ -880,15 +881,15 @@ func listenBlockEvents(channelID string, eventch <-chan *fab.BlockEvent, expecte
select {
case _, ok := <-eventch:
if !ok {
fmt.Println("Block events channel was closed")
test.Logf("Block events channel was closed")
return
}
numReceived++
case <-time.After(5 * time.Second):
if numReceived != expected {
errch <- errors.Errorf("Expected [%d] events but received [%d]", expected, numReceived)
} else {
fmt.Printf("Received %d block events\n", numReceived)
test.Logf("Received %d block events", numReceived)
errch <- nil
}
return
Expand All @@ -903,7 +904,7 @@ func listenFilteredBlockEvents(channelID string, eventch <-chan *fab.FilteredBlo
select {
case fbevent, ok := <-eventch:
if !ok {
fmt.Print("Filtered block events channel was closed \n")
test.Logf("Filtered block events channel was closed")
return
}
if fbevent.FilteredBlock == nil {
Expand All @@ -919,7 +920,7 @@ func listenFilteredBlockEvents(channelID string, eventch <-chan *fab.FilteredBlo
if numReceived != expected {
errch <- errors.Errorf("Expected [%d] events but received [%d]", expected, numReceived)
} else {
fmt.Printf("Received %d filtered block events\n", numReceived)
test.Logf("Received %d filtered block events", numReceived)
errch <- nil
}
return
Expand All @@ -935,7 +936,7 @@ func listenChaincodeEvents(channelID string, eventch <-chan *fab.CCEvent, expect
select {
case event, ok := <-eventch:
if !ok {
fmt.Println("CC events channel was closed")
test.Logf("CC events channel was closed")
return
}
if event.BlockNumber > 0 && event.BlockNumber <= lastBlockNum {
Expand All @@ -947,7 +948,7 @@ func listenChaincodeEvents(channelID string, eventch <-chan *fab.CCEvent, expect
if numReceived != expected {
errch <- errors.Errorf("Expected [%d] events but received [%d]", expected, numReceived)
} else {
fmt.Printf("Received %d CC events\n", numReceived)
test.Logf("Received %d CC events", numReceived)
errch <- nil
}
return
Expand Down Expand Up @@ -1216,15 +1217,15 @@ func listenConnection(eventch chan *dispatcher.ConnectionEvent, outcome chan moc

for {
e, ok := <-eventch
fmt.Printf("listenConnection - got event [%+v] - ok=[%t]\n", e, ok)
test.Logf("listenConnection - got event [%+v] - ok=[%t]", e, ok)
if !ok {
fmt.Println("listenConnection - Returning terminated outcome")
test.Logf("listenConnection - Returning terminated outcome")
outcome <- mockconn.ClosedOutcome
break
}
if e.Connected {
if state == Disconnected {
fmt.Println("listenConnection - Returning reconnected outcome")
test.Logf("listenConnection - Returning reconnected outcome")
outcome <- mockconn.ReconnectedOutcome
}
state = Connected
Expand Down Expand Up @@ -1273,11 +1274,11 @@ var clientProvider = func(context context.Client, chConfig fab.ChannelCfg, disco
opts = append(opts, WithBlockEvents())
return newClient(context, chConfig, discoveryService, connectionProvider, opts,
func() error {
fmt.Println("AfterConnect called")
test.Logf("AfterConnect called")
return nil
},
func() error {
fmt.Println("BeforeReconnect called")
test.Logf("BeforeReconnect called")
return nil
})
}
Expand All @@ -1294,11 +1295,11 @@ var failAfterConnectClientProvider = func(context context.Client, chConfig fab.C
var filteredClientProvider = func(context context.Client, chConfig fab.ChannelCfg, discoveryService fab.DiscoveryService, connectionProvider api.ConnectionProvider, opts []options.Opt) (*Client, error) {
return newClient(context, chConfig, discoveryService, connectionProvider, opts,
func() error {
fmt.Println("AfterConnect called")
test.Logf("AfterConnect called")
return nil
},
func() error {
fmt.Println("BeforeReconnect called")
test.Logf("BeforeReconnect called")
return nil
})
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/fab/events/endpoint/endpoint_test.go
Expand Up @@ -7,14 +7,14 @@ SPDX-License-Identifier: Apache-2.0
package endpoint

import (
"fmt"
"testing"
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
clientmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/client/mocks"
fabmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/mocks"
mspmocks "github.com/hyperledger/fabric-sdk-go/pkg/msp/test/mockmsp"
"github.com/hyperledger/fabric-sdk-go/pkg/util/test"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -135,7 +135,7 @@ func newMockConfig(channelPeers ...fab.ChannelPeer) *mockConfig {
}

func (c *mockConfig) ChannelPeers(name string) ([]fab.ChannelPeer, bool) {
fmt.Printf("mockConfig.ChannelPeers - returning %#v\n", c.channelPeers)
test.Logf("mockConfig.ChannelPeers [%#n]", c.channelPeers)
return c.channelPeers, true
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/fab/events/service/mocks/mockproducer.go
Expand Up @@ -7,11 +7,11 @@ SPDX-License-Identifier: Apache-2.0
package mocks

import (
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/util/test"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
)

Expand Down Expand Up @@ -133,13 +133,13 @@ func send(eventch chan<- interface{}, event interface{}) {
// During shutdown, events may still be produced and we may
// get a 'send on closed channel' panic. Just log and ignore the error.
if p := recover(); p != nil {
fmt.Printf("panic while submitting event %#v: %s\n", event, p)
test.Logf("panic while submitting event %#v: %s", event, p)
}
}()

select {
case eventch <- event:
case <-time.After(5 * time.Second):
fmt.Print("***** Timed out sending event.\n")
test.Logf("***** Timed out sending event.")
}
}
7 changes: 4 additions & 3 deletions pkg/fab/mocks/mockbroadcastserver.go
Expand Up @@ -7,12 +7,12 @@ SPDX-License-Identifier: Apache-2.0
package mocks

import (
"io"

"fmt"
"io"
"net"

po "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric-sdk-go/pkg/util/test"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -98,11 +98,12 @@ func StartMockBroadcastServer(broadcastTestURL string, grpcServer *grpc.Server)
}
addr := lis.Addr().String()

test.Logf("Starting MockEventServer [%s]", addr)
broadcastServer := new(MockBroadcastServer)
po.RegisterAtomicBroadcastServer(grpcServer, broadcastServer)
go func() {
if err := grpcServer.Serve(lis); err != nil {
fmt.Printf("StartMockBroadcastServer failed to start %s\n", err)
test.Logf("StartMockBroadcastServer failed [%s]", err)
}
}()

Expand Down
6 changes: 4 additions & 2 deletions pkg/fab/mocks/mockeventserver.go
Expand Up @@ -11,6 +11,7 @@ import (
"net"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-sdk-go/pkg/util/test"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"google.golang.org/grpc"
)
Expand All @@ -30,12 +31,13 @@ func StartMockEventServer(testAddress string) (*MockEventServer, error) {
if err != nil {
return nil, fmt.Errorf("starting test server failed %s", err)
}

test.Logf("Starting MockEventServer [%s]", lis.Addr())
eventServer := &MockEventServer{grpcServer: grpcServer}
pb.RegisterEventsServer(grpcServer, eventServer)
fmt.Printf("Starting mock event server\n")
go func() {
if err := grpcServer.Serve(lis); err != nil {
fmt.Printf("StartMockEventServer failed %s\n", err)
test.Logf("StartMockEventServer failed [%s]", err)
}
}()

Expand Down
3 changes: 2 additions & 1 deletion pkg/fab/orderer/orderer_test.go
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
"github.com/hyperledger/fabric-sdk-go/pkg/util/test"
"google.golang.org/grpc"
grpccodes "google.golang.org/grpc/codes"

Expand Down Expand Up @@ -116,7 +117,7 @@ func startMockServer(grpcServer *grpc.Server) (*mocks.MockBroadcastServer, strin
if err != nil {
panic(fmt.Sprintf("Error starting test server %s", err))
}
fmt.Printf("Starting test server on %s\n", addr)
test.Logf("Starting test server on %s", addr)
go grpcServer.Serve(lis)

return broadcastServer, addr
Expand Down
2 changes: 1 addition & 1 deletion pkg/fab/resource/resource_test.go
Expand Up @@ -261,7 +261,7 @@ func TestGenesisBlockWithRetry(t *testing.T) {
if err != nil {
t.Fatalf("GenesisBlock failed: %s", err)
}
fmt.Printf("Block: %#v\n", block)
t.Logf("Block [%#v]", block)
}

/*
Expand Down
19 changes: 7 additions & 12 deletions pkg/util/concurrent/futurevalue/futurevalue_test.go
Expand Up @@ -10,6 +10,8 @@ import (
"fmt"
"sync"
"testing"

"github.com/hyperledger/fabric-sdk-go/pkg/util/test"
)

func ExampleValue_Get() {
Expand Down Expand Up @@ -64,10 +66,10 @@ func TestFutureValueGet(t *testing.T) {
defer wg.Done()
value, err := fv.Get()
if err != nil {
fail(t, "received error: %s", err)
test.Failf(t, "received error: %s", err)
}
if value != expectedValue {
fail(t, "expecting value [%s] but received [%s]", expectedValue, value)
test.Failf(t, "expecting value [%s] but received [%s]", expectedValue, value)
}
}()
}
Expand Down Expand Up @@ -97,7 +99,7 @@ func TestFutureValueGetWithError(t *testing.T) {
go func() {
defer wg.Done()
if _, err := fv.Get(); err == nil {
fail(t, "expecting error but received none")
test.Failf(t, "expecting error but received none")
}
}()
}
Expand All @@ -119,23 +121,16 @@ func TestMustGetPanic(t *testing.T) {
go func() {
defer func() {
if r := recover(); r == nil {
fail(t, "Expecting panic but got none")
test.Failf(t, "Expecting panic but got none")
}
done <- true
}()
fv.MustGet()
fail(t, "Expecting panic but got none")
test.Failf(t, "Expecting panic but got none")
}()

if _, err := fv.Initialize(); err == nil {
t.Fatal("expecting error but received none")
}
<-done
}

// fail - as t.Fatalf() is not goroutine safe, this function behaves like t.Fatalf().
func fail(t *testing.T, template string, args ...interface{}) {
fmt.Printf(template, args...)
fmt.Println()
t.Fail()
}

0 comments on commit cc5f23c

Please sign in to comment.