From 9bc1dd0a571a790bacbab13aee3e960a29d91f3a Mon Sep 17 00:00:00 2001 From: Victor Nosov Date: Mon, 13 Jan 2020 09:38:10 +0300 Subject: [PATCH] Mock service refactoring (#37) * mock service refactoring * allow to pass mocked peer to cc service mock --- .../cpaper_asservice/bin/api/mock/main.go | 4 +- gateway/service/mock/chaincode.go | 60 +++++-------------- gateway/service/service_test.go | 3 +- 3 files changed, 19 insertions(+), 48 deletions(-) diff --git a/examples/cpaper_asservice/bin/api/mock/main.go b/examples/cpaper_asservice/bin/api/mock/main.go index d5f11b2f..fed0a225 100644 --- a/examples/cpaper_asservice/bin/api/mock/main.go +++ b/examples/cpaper_asservice/bin/api/mock/main.go @@ -12,7 +12,7 @@ import ( "github.com/s7techlab/cckit/examples/cpaper_asservice" cpaperservice "github.com/s7techlab/cckit/examples/cpaper_asservice/service" "github.com/s7techlab/cckit/gateway" - "github.com/s7techlab/cckit/gateway/service/mock" + servicemock "github.com/s7techlab/cckit/gateway/service/mock" "github.com/s7techlab/cckit/testing" "google.golang.org/grpc" ) @@ -42,7 +42,7 @@ func main() { cpaperMock := testing.NewMockStub(chaincodeName, cc) // Chaincode invocation service mock. For real network you can use example with hlf-sdk-go - cpaperMockService := mock.New().WithChannel(channelName, cpaperMock) + cpaperMockService := servicemock.New(testing.NewPeer().WithChannel(channelName, cpaperMock)) // default identity for signing requests to peeer (mocked) apiIdentity, err := testing.IdentityFromFile(`MSP`, `../../../testdata/admin.pem`, ioutil.ReadFile) diff --git a/gateway/service/mock/chaincode.go b/gateway/service/mock/chaincode.go index 1c6a6448..d5874728 100644 --- a/gateway/service/mock/chaincode.go +++ b/gateway/service/mock/chaincode.go @@ -3,7 +3,6 @@ package mock import ( "context" "errors" - "fmt" "sync" "github.com/hyperledger/fabric/core/chaincode/shim" @@ -18,25 +17,28 @@ const ( ) type ( - Channel map[string]*testing.MockStub - - Channels map[string]Channel - ChaincodeService struct { // channel name -> chaincode name - ChannelCC Channels - m sync.Mutex - Invoker ChaincodeInvoker + Peer *testing.MockedPeer + m sync.Mutex + Invoker ChaincodeInvoker } // ChaincodeInvoker allows to imitate peer errors or unavailability ChaincodeInvoker func(ctx context.Context, mockStub *testing.MockStub, in *service.ChaincodeExec) *peer.Response ) -func New() *ChaincodeService { +func New(peers ...*testing.MockedPeer) *ChaincodeService { + var p *testing.MockedPeer + if len(peers) > 0 { + p = peers[0] + } else { + p = testing.NewPeer() + } + return &ChaincodeService{ - ChannelCC: make(Channels), - Invoker: DefaultInvoker, + Peer: p, + Invoker: DefaultInvoker, } } @@ -80,7 +82,7 @@ func (cs *ChaincodeService) Exec(ctx context.Context, in *service.ChaincodeExec) cs.m.Lock() defer cs.m.Unlock() - if mockStub, err = cs.Chaincode(in.Input.Channel, in.Input.Chaincode); err != nil { + if mockStub, err = cs.Peer.Chaincode(in.Input.Channel, in.Input.Chaincode); err != nil { return nil, err } @@ -115,7 +117,7 @@ func (cs *ChaincodeService) Events(in *service.ChaincodeLocator, stream service. var ( mockStub *testing.MockStub ) - if mockStub, err = cs.Chaincode(in.Channel, in.Chaincode); err != nil { + if mockStub, err = cs.Peer.Chaincode(in.Channel, in.Chaincode); err != nil { return } events := mockStub.EventSubscription() @@ -134,35 +136,3 @@ func (cs *ChaincodeService) Events(in *service.ChaincodeLocator, stream service. } } } - -func (cs *ChaincodeService) WithChannel(channel string, mockStubs ...*testing.MockStub) *ChaincodeService { - if _, ok := cs.ChannelCC[channel]; !ok { - cs.ChannelCC[channel] = make(Channel) - } - - for _, ms := range mockStubs { - cs.ChannelCC[channel][ms.Name] = ms - for chName, chnl := range cs.ChannelCC { - for ccName, cc := range chnl { - - // add visibility of added cc to all other cc - cc.MockPeerChaincode(ms.Name+`/`+channel, ms) - - // add visibility of other cc to added cc - ms.MockPeerChaincode(ccName+`/`+chName, cc) - } - } - } - - return cs -} - -func (cs *ChaincodeService) Chaincode(channel string, chaincode string) (*testing.MockStub, error) { - ms, exists := cs.ChannelCC[channel][chaincode] - if !exists { - return nil, fmt.Errorf(`%s: channell=%s, chaincode=%s`, - service.ErrChaincodeNotExists, channel, chaincode) - } - - return ms, nil -} diff --git a/gateway/service/service_test.go b/gateway/service/service_test.go index ee3d6a40..74cc21c1 100644 --- a/gateway/service/service_test.go +++ b/gateway/service/service_test.go @@ -43,7 +43,8 @@ var _ = Describe(`Service`, func() { Expect(err).NotTo(HaveOccurred()) // peer imitation - cPaperService = mock.New().WithChannel(Channel, testcc.NewMockStub(ChaincodeName, ccImpl)) + cPaperService = mock.New() + cPaperService.Peer.WithChannel(Channel, testcc.NewMockStub(ChaincodeName, ccImpl)) // "sdk" for deal with cpaper chaincode cPaperGateway = cpservice.NewCPaperGateway(cPaperService, Channel, ChaincodeName)