forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mockeventservice.go
88 lines (73 loc) · 2.44 KB
/
mockeventservice.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package mocks
import (
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/events/service/dispatcher"
pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
)
// MockEventService implements a mock event service
type MockEventService struct {
TxStatusRegCh chan *dispatcher.TxStatusReg
TxValidationCode pb.TxValidationCode
Timeout bool
}
// NewMockEventService returns a new mock event service
func NewMockEventService() *MockEventService {
return &MockEventService{
TxStatusRegCh: make(chan *dispatcher.TxStatusReg, 1),
}
}
// RegisterBlockEvent registers for block events.
func (m *MockEventService) RegisterBlockEvent(filter ...fab.BlockFilter) (fab.Registration, <-chan *fab.BlockEvent, error) {
eventCh := make(chan *fab.BlockEvent)
reg := &dispatcher.BlockReg{
Eventch: eventCh,
}
return reg, eventCh, nil
}
// RegisterFilteredBlockEvent registers for filtered block events.
func (m *MockEventService) RegisterFilteredBlockEvent() (fab.Registration, <-chan *fab.FilteredBlockEvent, error) {
eventCh := make(chan *fab.FilteredBlockEvent)
reg := &dispatcher.FilteredBlockReg{
Eventch: eventCh,
}
return reg, eventCh, nil
}
// RegisterChaincodeEvent registers for chaincode events.
func (m *MockEventService) RegisterChaincodeEvent(ccID, eventFilter string) (fab.Registration, <-chan *fab.CCEvent, error) {
eventCh := make(chan *fab.CCEvent)
reg := &dispatcher.ChaincodeReg{
Eventch: eventCh,
ChaincodeID: ccID,
EventFilter: eventFilter,
}
return reg, eventCh, nil
}
// RegisterTxStatusEvent registers for transaction status events.
func (m *MockEventService) RegisterTxStatusEvent(txID string) (fab.Registration, <-chan *fab.TxStatusEvent, error) {
eventCh := make(chan *fab.TxStatusEvent)
reg := &dispatcher.TxStatusReg{
Eventch: eventCh,
TxID: txID,
}
m.TxStatusRegCh <- reg
if !m.Timeout {
go func() {
select {
case txStatusReg := <-m.TxStatusRegCh:
txStatusReg.Eventch <- &fab.TxStatusEvent{TxID: txStatusReg.TxID, TxValidationCode: m.TxValidationCode}
case <-time.After(5 * time.Second):
panic("time out not expected")
}
}()
}
return reg, eventCh, nil
}
// Unregister removes the given registration.
func (m *MockEventService) Unregister(reg fab.Registration) {
// Nothing to do
}