-
Notifications
You must be signed in to change notification settings - Fork 0
/
mocks.go
173 lines (145 loc) · 5.43 KB
/
mocks.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package testutil
import (
"errors"
"github.com/dymensionxyz/dymint/mocks"
"github.com/dymensionxyz/dymint/types"
"github.com/stretchr/testify/mock"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/proxy"
"github.com/dymensionxyz/dymint/da"
mockda "github.com/dymensionxyz/dymint/da/mock"
"github.com/dymensionxyz/dymint/store"
)
// ABCIMethod is a string representing an ABCI method
type ABCIMethod string
const (
// InitChain is the string representation of the InitChain ABCI method
InitChain ABCIMethod = "InitChain"
// CheckTx is the string representation of the CheckTx ABCI method
CheckTx ABCIMethod = "CheckTx"
// BeginBlock is the string representation of the BeginBlockMethod ABCI method
BeginBlock ABCIMethod = "BeginBlock"
// DeliverTx is the string representation of the DeliverTx ABCI method
DeliverTx ABCIMethod = "DeliverTx"
// EndBlock is the string representation of the EndBlock ABCI method
EndBlock ABCIMethod = "EndBlock"
// Commit is the string representation of the Commit ABCI method
Commit ABCIMethod = "Commit"
// Info is the string representation of the Info ABCI method
Info ABCIMethod = "Info"
)
// GetABCIProxyAppMock returns a dummy abci proxy app mock for testing
func GetABCIProxyAppMock(logger log.Logger) proxy.AppConns {
app := GetAppMock()
clientCreator := proxy.NewLocalClientCreator(app)
proxyApp := proxy.NewAppConns(clientCreator)
proxyApp.SetLogger(logger)
return proxyApp
}
// GetAppMock returns a dummy abci app mock for testing
func GetAppMock(excludeMethods ...ABCIMethod) *mocks.Application {
app := &mocks.Application{}
app.On("InitChain", mock.Anything).Return(abci.ResponseInitChain{})
app.On("CheckTx", mock.Anything).Return(abci.ResponseCheckTx{})
app.On("BeginBlock", mock.Anything).Return(abci.ResponseBeginBlock{})
app.On("DeliverTx", mock.Anything).Return(abci.ResponseDeliverTx{})
app.On("EndBlock", mock.Anything).Return(abci.ResponseEndBlock{})
app.On("Commit", mock.Anything).Return(abci.ResponseCommit{})
app.On("Info", mock.Anything).Return(abci.ResponseInfo{LastBlockHeight: 0, LastBlockAppHash: []byte{0}})
// iterate exclude methods and unset the mock
for _, method := range excludeMethods {
UnsetMockFn(app.On(string(method)))
}
return app
}
var UnsetMockFn = func(call *mock.Call) {
if call != nil {
var newList []*mock.Call
for _, c := range call.Parent.ExpectedCalls {
if c.Method != call.Method {
newList = append(newList, c)
}
}
call.Parent.ExpectedCalls = newList
}
}
// CountMockCalls returns the number of times a mock specific function was called
func CountMockCalls(totalCalls []mock.Call, methodName string) int {
var count int
for _, call := range totalCalls {
if call.Method == methodName {
count++
}
}
return count
}
// MockStore is a mock store for testing
type MockStore struct {
ShouldFailSetHeight bool
ShoudFailUpdateState bool
ShouldFailUpdateStateWithBatch bool
*store.DefaultStore
height uint64
}
// SetHeight sets the height of the mock store
// Don't set the height to mock failure in setting the height
func (m *MockStore) SetHeight(height uint64) {
// Fail the first time
if m.ShouldFailSetHeight {
return
}
m.height = height
}
// Height returns the height of the mock store
func (m *MockStore) Height() uint64 {
return m.height
}
// UpdateState updates the state of the mock store
func (m *MockStore) UpdateState(state types.State, batch store.Batch) (store.Batch, error) {
if batch != nil && m.ShouldFailUpdateStateWithBatch || m.ShoudFailUpdateState && batch == nil {
return nil, errors.New("failed to update state")
}
return m.DefaultStore.UpdateState(state, batch)
}
// NewMockStore returns a new mock store
func NewMockStore() *MockStore {
defaultStore := store.New(store.NewDefaultInMemoryKVStore())
return &MockStore{
DefaultStore: defaultStore.(*store.DefaultStore),
height: 0,
ShouldFailSetHeight: false,
ShoudFailUpdateState: false,
}
}
const batchNotFoundErrorMessage = "batch not found"
const connectionRefusedErrorMessage = "connection refused"
// DALayerClientSubmitBatchError is a mock data availability layer client that can be used to test error handling
type DALayerClientSubmitBatchError struct {
mockda.DataAvailabilityLayerClient
}
// SubmitBatch submits a batch to the data availability layer
func (s *DALayerClientSubmitBatchError) SubmitBatch(_ *types.Batch) da.ResultSubmitBatch {
return da.ResultSubmitBatch{BaseResult: da.BaseResult{Code: da.StatusError, Message: connectionRefusedErrorMessage}}
}
// DALayerClientRetrieveBatchesError is a mock data availability layer client that can be used to test error handling
type DALayerClientRetrieveBatchesError struct {
mockda.DataAvailabilityLayerClient
}
// RetrieveBatches retrieves batches from the data availability layer
func (m *DALayerClientRetrieveBatchesError) RetrieveBatches(_ uint64) da.ResultRetrieveBatch {
return da.ResultRetrieveBatch{BaseResult: da.BaseResult{Code: da.StatusError, Message: batchNotFoundErrorMessage}}
}
// SubscribeMock is a mock to provide a subscription like behavior for testing
type SubscribeMock struct {
messageCh chan interface{}
}
func NewSubscribeMock(messageCh chan interface{}) *SubscribeMock {
return &SubscribeMock{messageCh: make(chan interface{})}
}
func (s *SubscribeMock) Chan() <-chan interface{} {
return s.messageCh
}
func (s *SubscribeMock) Unsubscribe() {
close(s.messageCh)
}