forked from multiversx/mx-chain-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatusComponents_test.go
320 lines (283 loc) · 11.1 KB
/
statusComponents_test.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package factory_test
import (
"testing"
"github.com/ElrondNetwork/elrond-go-core/core/check"
logger "github.com/ElrondNetwork/elrond-go-logger"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/errors"
"github.com/ElrondNetwork/elrond-go/factory"
"github.com/ElrondNetwork/elrond-go/factory/mock"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/testscommon"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var log = logger.GetOrCreate("factory/factory_test")
func TestNewStatusComponentsFactory_NilCoreComponentsShouldErr(t *testing.T) {
t.Parallel()
shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
args, _ := getStatusComponentsFactoryArgsAndProcessComponents(shardCoordinator)
args.CoreComponents = nil
scf, err := factory.NewStatusComponentsFactory(args)
assert.True(t, check.IfNil(scf))
assert.Equal(t, errors.ErrNilCoreComponentsHolder, err)
}
func TestNewStatusComponentsFactory_NilNodesCoordinatorShouldErr(t *testing.T) {
t.Parallel()
shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
args, _ := getStatusComponentsFactoryArgsAndProcessComponents(shardCoordinator)
args.NodesCoordinator = nil
scf, err := factory.NewStatusComponentsFactory(args)
assert.True(t, check.IfNil(scf))
assert.Equal(t, errors.ErrNilNodesCoordinator, err)
}
func TestNewStatusComponentsFactory_NilEpochStartNotifierShouldErr(t *testing.T) {
t.Parallel()
shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
args, _ := getStatusComponentsFactoryArgsAndProcessComponents(shardCoordinator)
args.EpochStartNotifier = nil
scf, err := factory.NewStatusComponentsFactory(args)
assert.True(t, check.IfNil(scf))
assert.Equal(t, errors.ErrNilEpochStartNotifier, err)
}
func TestNewStatusComponentsFactory_NilNetworkComponentsShouldErr(t *testing.T) {
t.Parallel()
shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
args, _ := getStatusComponentsFactoryArgsAndProcessComponents(shardCoordinator)
args.NetworkComponents = nil
scf, err := factory.NewStatusComponentsFactory(args)
assert.True(t, check.IfNil(scf))
assert.Equal(t, errors.ErrNilNetworkComponentsHolder, err)
}
func TestNewStatusComponentsFactory_NilShardCoordinatorShouldErr(t *testing.T) {
t.Parallel()
shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
args, _ := getStatusComponentsFactoryArgsAndProcessComponents(shardCoordinator)
args.ShardCoordinator = nil
scf, err := factory.NewStatusComponentsFactory(args)
assert.True(t, check.IfNil(scf))
assert.Equal(t, errors.ErrNilShardCoordinator, err)
}
func TestNewStatusComponents_InvalidRoundDurationShouldErr(t *testing.T) {
t.Parallel()
shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
coreArgs := getCoreArgs()
coreArgs.NodesFilename = "mock/testdata/nodesSetupMockInvalidRound.json"
coreComponentsFactory, _ := factory.NewCoreComponentsFactory(coreArgs)
coreComponents, err := factory.NewManagedCoreComponents(coreComponentsFactory)
require.Nil(t, err)
require.NotNil(t, coreComponents)
err = coreComponents.Create()
require.Nil(t, err)
networkComponents := getNetworkComponents()
dataComponents := getDataComponents(coreComponents, shardCoordinator)
stateComponents := getStateComponents(coreComponents, shardCoordinator)
statusArgs := factory.StatusComponentsFactoryArgs{
Config: testscommon.GetGeneralConfig(),
ExternalConfig: config.ExternalConfig{},
ShardCoordinator: shardCoordinator,
NodesCoordinator: &mock.NodesCoordinatorMock{},
EpochStartNotifier: &mock.EpochStartNotifierStub{},
CoreComponents: coreComponents,
DataComponents: dataComponents,
NetworkComponents: networkComponents,
StateComponents: stateComponents,
IsInImportMode: false,
EconomicsConfig: config.EconomicsConfig{},
}
scf, err := factory.NewStatusComponentsFactory(statusArgs)
assert.Nil(t, err)
assert.NotNil(t, scf)
statusComponents, err := scf.Create()
assert.Nil(t, statusComponents)
assert.Equal(t, errors.ErrInvalidRoundDuration, err)
}
func TestNewStatusComponentsFactory_ShouldWork(t *testing.T) {
t.Parallel()
shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
args, _ := getStatusComponentsFactoryArgsAndProcessComponents(shardCoordinator)
scf, err := factory.NewStatusComponentsFactory(args)
require.NoError(t, err)
require.False(t, check.IfNil(scf))
}
func TestStatusComponentsFactory_Create(t *testing.T) {
t.Parallel()
shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
args, _ := getStatusComponentsFactoryArgsAndProcessComponents(shardCoordinator)
scf, err := factory.NewStatusComponentsFactory(args)
require.Nil(t, err)
res, err := scf.Create()
require.NoError(t, err)
require.NotNil(t, res)
}
// ------------ Test StatusComponents --------------------
func TestStatusComponents_Close_ShouldWork(t *testing.T) {
shardCoordinator := mock.NewMultiShardsCoordinatorMock(2)
statusArgs, _ := getStatusComponentsFactoryArgsAndProcessComponents(shardCoordinator)
scf, _ := factory.NewStatusComponentsFactory(statusArgs)
cc, err := scf.Create()
require.Nil(t, err)
err = cc.Close()
require.NoError(t, err)
}
func getStatusComponents(
coreComponents factory.CoreComponentsHolder,
networkComponents factory.NetworkComponentsHolder,
dataComponents factory.DataComponentsHolder,
stateComponents factory.StateComponentsHolder,
shardCoordinator sharding.Coordinator,
nodesCoordinator sharding.NodesCoordinator,
) factory.StatusComponentsHandler {
indexerURL := "url"
elasticUsername := "user"
elasticPassword := "pass"
statusArgs := factory.StatusComponentsFactoryArgs{
Config: testscommon.GetGeneralConfig(),
ExternalConfig: config.ExternalConfig{
ElasticSearchConnector: config.ElasticSearchConfig{
Enabled: false,
URL: indexerURL,
Username: elasticUsername,
Password: elasticPassword,
EnabledIndexes: []string{"transactions", "blocks"},
},
},
EconomicsConfig: config.EconomicsConfig{},
ShardCoordinator: shardCoordinator,
NodesCoordinator: nodesCoordinator,
EpochStartNotifier: coreComponents.EpochStartNotifierWithConfirm(),
CoreComponents: coreComponents,
DataComponents: dataComponents,
NetworkComponents: networkComponents,
StateComponents: stateComponents,
IsInImportMode: false,
}
statusComponentsFactory, _ := factory.NewStatusComponentsFactory(statusArgs)
managedStatusComponents, err := factory.NewManagedStatusComponents(statusComponentsFactory)
if err != nil {
log.Error("getStatusComponents NewManagedStatusComponents", "error", err.Error())
return nil
}
err = managedStatusComponents.Create()
if err != nil {
log.Error("getStatusComponents Create", "error", err.Error())
return nil
}
return managedStatusComponents
}
func getStatusComponentsFactoryArgsAndProcessComponents(shardCoordinator sharding.Coordinator) (factory.StatusComponentsFactoryArgs, factory.ProcessComponentsHolder) {
coreComponents := getCoreComponents()
networkComponents := getNetworkComponents()
dataComponents := getDataComponents(coreComponents, shardCoordinator)
cryptoComponents := getCryptoComponents(coreComponents)
stateComponents := getStateComponents(coreComponents, shardCoordinator)
processComponents := getProcessComponents(
shardCoordinator,
coreComponents,
networkComponents,
dataComponents,
cryptoComponents,
stateComponents,
)
indexerURL := "url"
elasticUsername := "user"
elasticPassword := "pass"
return factory.StatusComponentsFactoryArgs{
Config: testscommon.GetGeneralConfig(),
ExternalConfig: config.ExternalConfig{
ElasticSearchConnector: config.ElasticSearchConfig{
Enabled: false,
URL: indexerURL,
Username: elasticUsername,
Password: elasticPassword,
EnabledIndexes: []string{"transactions", "blocks"},
},
},
EconomicsConfig: config.EconomicsConfig{},
ShardCoordinator: mock.NewMultiShardsCoordinatorMock(2),
NodesCoordinator: &mock.NodesCoordinatorMock{},
EpochStartNotifier: &mock.EpochStartNotifierStub{},
CoreComponents: coreComponents,
DataComponents: dataComponents,
NetworkComponents: networkComponents,
StateComponents: stateComponents,
IsInImportMode: false,
}, processComponents
}
func getNetworkComponents() factory.NetworkComponentsHolder {
networkArgs := getNetworkArgs()
networkComponentsFactory, _ := factory.NewNetworkComponentsFactory(networkArgs)
networkComponents, _ := factory.NewManagedNetworkComponents(networkComponentsFactory)
_ = networkComponents.Create()
return networkComponents
}
func getDataComponents(coreComponents factory.CoreComponentsHolder, shardCoordinator sharding.Coordinator) factory.DataComponentsHolder {
dataArgs := getDataArgs(coreComponents, shardCoordinator)
dataComponentsFactory, _ := factory.NewDataComponentsFactory(dataArgs)
dataComponents, _ := factory.NewManagedDataComponents(dataComponentsFactory)
_ = dataComponents.Create()
return dataComponents
}
func getCryptoComponents(coreComponents factory.CoreComponentsHolder) factory.CryptoComponentsHolder {
cryptoArgs := getCryptoArgs(coreComponents)
cryptoComponentsFactory, _ := factory.NewCryptoComponentsFactory(cryptoArgs)
cryptoComponents, err := factory.NewManagedCryptoComponents(cryptoComponentsFactory)
if err != nil {
log.Error("getCryptoComponents NewManagedCryptoComponents", "error", err.Error())
return nil
}
err = cryptoComponents.Create()
if err != nil {
log.Error("getCryptoComponents Create", "error", err.Error())
return nil
}
return cryptoComponents
}
func getStateComponents(coreComponents factory.CoreComponentsHolder, shardCoordinator sharding.Coordinator) factory.StateComponentsHolder {
stateArgs := getStateArgs(coreComponents, shardCoordinator)
stateComponentsFactory, err := factory.NewStateComponentsFactory(stateArgs)
if err != nil {
log.Error("getStateComponents NewStateComponentsFactory", "error", err.Error())
return nil
}
stateComponents, err := factory.NewManagedStateComponents(stateComponentsFactory)
if err != nil {
log.Error("getStateComponents NewManagedStateComponents", "error", err.Error())
return nil
}
err = stateComponents.Create()
if err != nil {
log.Error("getStateComponents Create", "error", err.Error())
return nil
}
return stateComponents
}
func getProcessComponents(
shardCoordinator sharding.Coordinator,
coreComponents factory.CoreComponentsHolder,
networkComponents factory.NetworkComponentsHolder,
dataComponents factory.DataComponentsHolder,
cryptoComponents factory.CryptoComponentsHolder,
stateComponents factory.StateComponentsHolder,
) factory.ProcessComponentsHolder {
processArgs := getProcessArgs(
shardCoordinator,
coreComponents,
dataComponents,
cryptoComponents,
stateComponents,
networkComponents,
)
processComponentsFactory, _ := factory.NewProcessComponentsFactory(processArgs)
managedProcessComponents, err := factory.NewManagedProcessComponents(processComponentsFactory)
if err != nil {
log.Error("getProcessComponents NewManagedProcessComponents", "error", err.Error())
return nil
}
err = managedProcessComponents.Create()
if err != nil {
log.Error("getProcessComponents Create", "error", err.Error())
return nil
}
return managedProcessComponents
}