Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Mock service refactoring (#37)
Browse files Browse the repository at this point in the history
* mock service refactoring

* allow to pass mocked peer to cc service mock
  • Loading branch information
vitiko authored Jan 13, 2020
1 parent 5f4539f commit 9bc1dd0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 48 deletions.
4 changes: 2 additions & 2 deletions examples/cpaper_asservice/bin/api/mock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
60 changes: 15 additions & 45 deletions gateway/service/mock/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mock
import (
"context"
"errors"
"fmt"
"sync"

"github.com/hyperledger/fabric/core/chaincode/shim"
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion gateway/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9bc1dd0

Please sign in to comment.