forked from LiskArchive/lisk-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynchronous_tasks.js
120 lines (107 loc) · 3.49 KB
/
synchronous_tasks.js
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
'use strict';
const expect = require('chai').expect;
const Rx = require('rx');
const localCommon = require('../common.js');
const jobsQueue = require('../../../../helpers/jobs_queue.js');
describe('system test (delegates) - synchronous tasks', () => {
let library;
localCommon.beforeBlock('system_delegates_synchronous_tasks', lib => {
library = lib;
});
describe('when events are emitted after any of synchronous task starts', () => {
var intervalMs;
var durationMs;
var attemptToForgeRunningSubject;
var synchronizeBlockchainRunningSubject;
var synchronousTaskMock = function(isTaskRunningSubject, nextCb) {
isTaskRunningSubject.onNext(true);
setTimeout(() => {
isTaskRunningSubject.onNext(false);
nextCb();
}, durationMs);
};
before(done => {
attemptToForgeRunningSubject = new Rx.BehaviorSubject();
synchronizeBlockchainRunningSubject = new Rx.BehaviorSubject();
done();
});
after(done => {
attemptToForgeRunningSubject.dispose();
synchronizeBlockchainRunningSubject.dispose();
done();
});
describe('when "attempt to forge" synchronous task runs every 100 ms and takes 101 ms', () => {
intervalMs = 100;
durationMs = intervalMs + 1;
before(done => {
library.modules.delegates.onBlockchainReady =
library.rewiredModules.delegates.prototype.onBlockchainReady;
library.rewiredModules.delegates.__set__(
'__private.forgeInterval',
intervalMs
);
library.rewiredModules.delegates.__set__(
'__private.nextForge',
synchronousTaskMock.bind(null, attemptToForgeRunningSubject)
);
library.modules.delegates.onBlockchainReady();
done();
});
describe('when "blockchain synchronization" synchronous task runs every 100 ms and takes 101 ms', () => {
before(done => {
library.rewiredModules.loader.__set__(
'__private.syncInterval',
intervalMs
);
library.rewiredModules.loader.__set__(
'__private.sync',
synchronousTaskMock.bind(null, synchronizeBlockchainRunningSubject)
);
var originalLoaderSyncTimerJob = jobsQueue.jobs.loaderSyncTimer;
clearTimeout(originalLoaderSyncTimerJob); // Terminate original job
delete jobsQueue.jobs.loaderSyncTimer; // Remove original job
library.modules.loader.onPeersReady(); // Execute the mocked blockchain synchronization process
done();
});
describe('within 5000 ms', () => {
beforeEach(done => {
setTimeout(() => {
attemptToForgeRunningSubject.onCompleted();
synchronizeBlockchainRunningSubject.onCompleted();
attemptToForgeRunningSubject = new Rx.BehaviorSubject();
synchronizeBlockchainRunningSubject = new Rx.BehaviorSubject();
}, 5000);
done();
});
it('"attempt to forge" task should never start when "blockchain synchronization" task is running', done => {
attemptToForgeRunningSubject
.filter(isStarting => {
return isStarting;
})
.subscribe(
() => {
expect(synchronizeBlockchainRunningSubject.getValue()).to.be
.false;
},
done,
done
);
});
it('"blockchain synchronization" task should never start when "attempt to forge" task is running', done => {
synchronizeBlockchainRunningSubject
.filter(isStarting => {
return isStarting;
})
.subscribe(
() => {
expect(attemptToForgeRunningSubject.getValue()).to.be.false;
},
done,
done
);
});
});
});
});
});
});