@@ -22,6 +22,7 @@ const stubDebug = sinon.stub();
22
22
23
23
const VALID_GENESIS_HASH = '8A+HyzS4sqZynD06BfNW7T1Vtv2SOXAOUJQK4itulus=' ;
24
24
const VALID_NETWORK_ID = 'test-network-id' ;
25
+ const VALID_CHANNEL_NAME = 'testchannel' ;
25
26
26
27
const stubPlatform = {
27
28
send : sinon . spy ( )
@@ -58,10 +59,18 @@ function getSyncServicesInstance() {
58
59
const stubGetCrudService = sinon . stub ( ) ;
59
60
stubGetCrudService . returns ( {
60
61
saveBlock : sinon . stub ( ) . resolves ( true ) ,
61
- saveTransaction : spySaveTransaction
62
+ saveTransaction : spySaveTransaction ,
63
+ getChannel : sinon . stub ( )
64
+ } ) ;
65
+ const stubGetMetricService = sinon . stub ( ) ;
66
+ stubGetMetricService . returns ( {
67
+ findMissingBlockNumber : sinon
68
+ . stub ( )
69
+ . returns ( [ { missing_id : 1 } , { missing_id : 2 } ] )
62
70
} ) ;
63
71
const stubPersistence = {
64
- getCrudService : stubGetCrudService
72
+ getCrudService : stubGetCrudService ,
73
+ getMetricService : stubGetMetricService
65
74
} ;
66
75
const sync = new SyncServices ( stubPlatform , stubPersistence ) ;
67
76
@@ -87,10 +96,19 @@ function setupClient() {
87
96
getNetworkId : stubGetNetworkID ,
88
97
getChannelGenHash : stubGetChannelGenHash ,
89
98
initializeNewChannel : sinon . stub ( ) . resolves ( true ) ,
99
+ initializeChannelFromDiscover : sinon . stub ( ) ,
90
100
fabricGateway : {
91
101
fabricConfig : {
92
102
getRWSetEncoding : sinon . stub ( )
93
- }
103
+ } ,
104
+ queryChainInfo : sinon . stub ( ) . returns ( { height : { low : 10 } } ) ,
105
+ queryBlock : sinon . fake ( ( channel_name , missing_id ) => {
106
+ if ( channel_name === VALID_CHANNEL_NAME ) {
107
+ return stubBlock ;
108
+ } else {
109
+ return null ;
110
+ }
111
+ } )
94
112
}
95
113
} ;
96
114
return stubClient ;
@@ -185,3 +203,37 @@ describe('processBlockEvent', () => {
185
203
. eventually . to . be . true ;
186
204
} ) ;
187
205
} ) ;
206
+
207
+ describe ( 'synchBlocks' , ( ) => {
208
+ let sync : SyncServices ;
209
+
210
+ before ( ( ) => {
211
+ sync = getSyncServicesInstance ( ) ;
212
+ } ) ;
213
+
214
+ beforeEach ( ( ) => {
215
+ resetAllStubs ( sync ) ;
216
+ } ) ;
217
+
218
+ it ( 'should return without error' , async ( ) => {
219
+ const stubClient = setupClient ( ) ;
220
+ const stubProcessBlockEvent = sinon . stub ( sync , 'processBlockEvent' ) ;
221
+
222
+ await sync . synchBlocks ( stubClient , VALID_CHANNEL_NAME ) ;
223
+ expect ( stubProcessBlockEvent . calledTwice ) . to . be . true ;
224
+ stubProcessBlockEvent . restore ( ) ;
225
+ } ) ;
226
+
227
+ it ( 'should return without error when processBlockEvent throws exception' , async ( ) => {
228
+ const stubClient = setupClient ( ) ;
229
+ const stubProcessBlockEvent = sinon . stub ( sync , 'processBlockEvent' ) ;
230
+ stubProcessBlockEvent . onFirstCall ( ) . throws ( 'Block already in processing' ) ;
231
+ stubError . reset ( ) ;
232
+
233
+ await sync . synchBlocks ( stubClient , VALID_CHANNEL_NAME ) ;
234
+ expect ( stubProcessBlockEvent . calledTwice ) . to . be . true ;
235
+ expect ( stubError . calledWith ( 'Failed to process Block # 1' ) ) . to . be . true ;
236
+ expect ( stubError . calledWith ( 'Failed to process Block # 2' ) ) . to . be . false ;
237
+ stubProcessBlockEvent . restore ( ) ;
238
+ } ) ;
239
+ } ) ;
0 commit comments