forked from hyperledger/fabric-sdk-go
/
mockconnection.go
executable file
·90 lines (76 loc) · 2.62 KB
/
mockconnection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
ab "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/protos/orderer"
clientmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/client/mocks"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/events/deliverclient/connection"
cb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"github.com/pkg/errors"
)
const (
// Connect is the Connect operation (used in the OperationMap)
Connect clientmocks.Operation = "connect"
// BadRequestResult indicates that the operation should use invalid seek info
BadRequestResult clientmocks.Result = "bad-request"
// ForbiddenResult indicates that the user does not have permission to perform the operation
ForbiddenResult clientmocks.Result = "forbidden"
)
// ConnFactory is a connection factory that creates mock Deliver connections
var ConnFactory = func(opts ...clientmocks.Opt) clientmocks.Connection {
return NewConnection(opts...)
}
// MockConnection is a fake connection used for unit testing
type MockConnection struct {
clientmocks.MockConnection
}
// NewConnection returns a new MockConnection using the given options
func NewConnection(opts ...clientmocks.Opt) *MockConnection {
return &MockConnection{
MockConnection: *clientmocks.NewMockConnection(opts...),
}
}
// Receive implements the MockConnection interface
func (c *MockConnection) Receive(eventch chan<- interface{}) {
result, ok := c.Result(Connect)
if ok {
switch result.Result {
case BadRequestResult:
eventch <- c.newDeliverStatusResponse(cb.Status_BAD_REQUEST)
return
case ForbiddenResult:
eventch <- c.newDeliverStatusResponse(cb.Status_FORBIDDEN)
return
}
}
c.MockConnection.Receive(eventch)
}
// Send mockcore sending seek info to the deliver server
func (c *MockConnection) Send(sinfo *ab.SeekInfo) error {
if c.Closed() {
return errors.New("mock connection is closed")
}
switch seek := sinfo.Start.Type.(type) {
case *ab.SeekPosition_Specified:
// Deliver all blocks from the given block number
fromBlock := seek.Specified.Number
c.Ledger().SendFrom(fromBlock)
case *ab.SeekPosition_Oldest:
// Deliver all blocks from the beginning
c.Ledger().SendFrom(0)
}
return nil
}
func (c *MockConnection) newDeliverStatusResponse(status cb.Status) *connection.Event {
return connection.NewEvent(
&pb.DeliverResponse{
Type: &pb.DeliverResponse_Status{
Status: status,
},
},
c.SourceURL(),
)
}