Commit 18bbc7e9 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Make tests pass with NYC

parent 8c0ec798
......@@ -9,7 +9,7 @@ const winston = require('winston');
const util = require('util');
const CallbackLogger = winston.transports.CallbackLogger = function (options:any) {
const CallbackLogger:any = winston.transports.CallbackLogger = function (options:any) {
this.name = 'customLogger';
this.level = options.level || 'info';
......@@ -119,6 +119,21 @@ logger.mute = () => {
}
};
logger.unmute = () => {
if (muted) {
muted = false
logger.add(winston.transports.Console, {
level: 'trace',
levels: customLevels.levels,
handleExceptions: false,
colorize: true,
timestamp: function() {
return moment().format();
}
})
}
}
/**
* Convenience function to get logger directly
*/
......
......@@ -229,7 +229,7 @@ export class BmaApi {
resolve(httpServer);
});
});
this.logger && this.logger.info(name + ' listening on http://' + (netInterface.match(/:/) ? '[' + netInterface + ']' : netInterface) + ':' + port);
this.logger && this.logger.info(this.name + ' listening on http://' + (netInterface.match(/:/) ? '[' + netInterface + ']' : netInterface) + ':' + port);
} catch (e) {
this.logger && this.logger.warn('Could NOT listen to http://' + netInterface + ':' + port);
this.logger && this.logger.warn(e);
......
......@@ -67,6 +67,10 @@ export class WorkerFarm {
return this.stopPromise;
}
shutDownEngine() {
this.theEngine.shutDown()
}
/**
* Starts a new computation of PoW
* @param stuff The necessary data for computing the PoW
......@@ -106,7 +110,7 @@ export class BlockProver {
}
}
getWorker() {
getWorker(): Promise<WorkerFarm> {
if (!this.workerFarmPromise) {
this.workerFarmPromise = (async () => {
return new WorkerFarm(this.server, this.logger)
......@@ -121,6 +125,9 @@ export class BlockProver {
let farm = await this.getWorker();
if (farm.isComputing() && !farm.isStopping()) {
await farm.stopPoW()
} else {
// We force the stop anyway, just to be sure
await farm.stopPoW()
}
if (this.waitResolve) {
this.waitResolve();
......
......@@ -52,4 +52,8 @@ export class PowEngine {
setOnInfoMessage(callback:any) {
return this.cluster.onInfoMessage = callback
}
async shutDown() {
return this.cluster.shutDownWorkers()
}
}
......@@ -8,6 +8,12 @@ import {parsers} from "../../../lib/common-libs/parsers/index"
const querablep = require('querablep');
interface Querable<T> extends Promise<T> {
isFulfilled(): boolean
isResolved(): boolean
isRejected(): boolean
}
export class PermanentProver {
logger:any
......@@ -16,7 +22,7 @@ export class PermanentProver {
generator:BlockGeneratorWhichProves
loops:number
private permanenceStarted = false
private permanencePromise:Querable<any>|null = null
private blockchainChangedResolver:any = null
private promiseOfWaitingBetween2BlocksOfOurs:any = null
......@@ -25,7 +31,7 @@ export class PermanentProver {
private continuePromise:any = null
private pullingResolveCallback:any = null
private timeoutPullingCallback:any = null
private pullingFinishedPromise:any = null
private pullingFinishedPromise:Querable<any>|null = null
private timeoutPulling:any = null
constructor(private server:any) {
......@@ -47,8 +53,7 @@ export class PermanentProver {
}
allowedToStart() {
if (!this.permanenceStarted) {
this.permanenceStarted = true
if (!this.permanencePromise || !this.permanencePromise.isFulfilled()) {
this.startPermanence()
}
this.resolveContinuePromise(true);
......@@ -56,7 +61,7 @@ export class PermanentProver {
// When we detected a pulling, we stop the PoW loop
pullingDetected() {
if (this.pullingFinishedPromise.isResolved()) {
if (this.pullingFinishedPromise && this.pullingFinishedPromise.isResolved()) {
this.pullingFinishedPromise = querablep(Promise.race([
// We wait for end of pulling signal
new Promise((res) => this.pullingResolveCallback = res),
......@@ -79,6 +84,12 @@ export class PermanentProver {
}
async startPermanence() {
let permanenceResolve = () => {}
this.permanencePromise = querablep(new Promise(res => {
permanenceResolve = res
}))
/******************
* Main proof loop
*****************/
......@@ -199,6 +210,8 @@ export class PermanentProver {
// Informative variable
this.logger.trace('PoW loops = %s', this.loops);
}
permanenceResolve()
}
async blockchainChanged(gottenBlock:any) {
......
......@@ -54,7 +54,7 @@ export class Master {
// Stop the slaves' current work
this.cancelWork()
}
// this.logger.debug(`ENGINE c#${this.clusterId}#${this.slavesMap[worker.id].index}:`, message)
this.logger.warn(`ENGINE c#${this.clusterId}#${this.slavesMap[worker.id].index}:`, message)
}
initCluster() {
......@@ -154,6 +154,15 @@ export class Master {
return p
}
async shutDownWorkers() {
if (this.workersOnline) {
await Promise.all(this.workersOnline)
await Promise.all(this.slaves.map(async (s:any) => {
s.worker.kill()
}))
}
}
proveByWorkers(stuff:any) {
// Eventually spawn the workers
......@@ -166,7 +175,9 @@ export class Master {
this.currentPromise = this.newPromise(uuid)
return (async () => {
this.logger.info(`Waiting workers to be all online...`)
await Promise.all(this.workersOnline)
this.logger.info(`All online!`)
if (!this.currentPromise) {
this.logger.info(`Proof canceled during workers' initialization`)
......@@ -174,7 +185,9 @@ export class Master {
}
// Start the salves' job
this.slaves.forEach((s:any) => {
this.logger.info(`Sending newPow signal for each of %s workers...`, this.slaves.length)
this.slaves.forEach((s:any, index) => {
this.logger.info(`Sending signal for worker #%s`, index)
s.worker.send({
uuid,
command: 'newPoW',
......
......@@ -34,6 +34,7 @@ process.on('uncaughtException', (err:any) => {
process.on('message', async (message) => {
console.log('proof => command:', message.command)
switch (message.command) {
case 'newPoW':
......@@ -41,11 +42,15 @@ process.on('message', async (message) => {
askedStop = true
// Very important: do not await if the computation is already done, to keep the lock on JS engine
console.log('computing.isFulfilled ?', computing.isFulfilled())
if (!computing.isFulfilled()) {
await computing;
}
console.log('beginNewProofOfWork()...')
const res = await beginNewProofOfWork(message.value);
console.log('proof.res!')
answer(message, res);
})()
break;
......
......@@ -27,7 +27,7 @@
"test": "nyc --reporter html mocha",
"start": "node bin/duniter start",
"build": "tsc && cd \"node_modules/duniter-ui\" && npm install && npm run build",
"test-travis": "nyc --reporter lcovonly mocha"
"test-travis": "nyc mocha test/"
},
"nyc": {
"require": [
......
......@@ -4,7 +4,6 @@ const should = require('should');
const FileDAL = require('../../app/lib/dal/fileDAL').FileDAL
const dir = require('../../app/lib/system/directory');
const indexer = require('../../app/lib/indexer').Indexer
const toolbox = require('../integration/tools/toolbox');
let dal;
......
......@@ -140,5 +140,7 @@ describe("Triming", function(){
(yield server.dal.bindexDAL.head(13)).should.have.property('number').equal(0);
yield server.commit();
should.not.exists(yield server.dal.bindexDAL.head(14)); // Trimed
yield server.closeCluster()
}));
});
......@@ -7,6 +7,7 @@ const duniter = require('../../index');
const bma = require('../../app/modules/bma').BmaDependency.duniter.methods.bma;
const rp = require('request-promise');
const httpTest = require('./tools/http');
const shutDownEngine = require('./tools/shutDownEngine');
const expectAnswer = httpTest.expectAnswer;
......@@ -38,6 +39,10 @@ describe("Branches", () => co(function*() {
yield bmapi.openConnections();
}));
after(() => {
return shutDownEngine(s1)
})
describe("Server 1 /blockchain", function() {
it('should have a 3 blocks fork window size', function() {
......
......@@ -10,6 +10,7 @@ const rp = require('request-promise');
const httpTest = require('./tools/http');
const commit = require('./tools/commit');
const sync = require('./tools/sync');
const shutDownEngine = require('./tools/shutDownEngine');
const expectJSON = httpTest.expectJSON;
const expectHttpCode = httpTest.expectHttpCode;
......@@ -109,6 +110,13 @@ describe("SelfFork", function() {
return require('../../app/modules/crawler').CrawlerDependency.duniter.methods.pullBlocks(s1, s2p.pubkey);
}));
after(() => {
return Promise.all([
shutDownEngine(s1),
shutDownEngine(s2)
])
})
describe("Server 1 /blockchain", function() {
it('/block/0 should exist', function() {
......
......@@ -8,6 +8,7 @@ const user = require('./tools/user');
const rp = require('request-promise');
const httpTest = require('./tools/http');
const commit = require('./tools/commit');
const shutDownEngine = require('./tools/shutDownEngine');
const expectJSON = httpTest.expectJSON;
const expectAnswer = httpTest.expectAnswer;
......@@ -65,6 +66,12 @@ describe("Pending data", function() {
});
});
after(() => {
return Promise.all([
shutDownEngine(s1)
])
})
describe("Server 1 /blockchain", function() {
it('/current should exist', function() {
......
......@@ -60,4 +60,8 @@ describe("Revert root", function() {
yield s1.expectError('/blockchain/block/0', 404, 'Block not found');
yield s1.expectError('/wot/lookup/cat', 404, 'No matching identity'); // Revert completely removes the identity
}));
after(() => {
return s1.closeCluster()
})
});
......@@ -8,6 +8,7 @@ const user = require('./tools/user');
const rp = require('request-promise');
const httpTest = require('./tools/http');
const commit = require('./tools/commit');
const shutDownEngine = require('./tools/shutDownEngine');
require('../../app/modules/prover/lib/constants').Constants.CORES_MAXIMUM_USE_IN_PARALLEL = 1
require('../../app/modules/bma').BmaDependency.duniter.methods.noLimit(); // Disables the HTTP limiter
......@@ -65,6 +66,12 @@ describe("Revert two blocks", function() {
});
});
after(() => {
return Promise.all([
shutDownEngine(s1)
])
})
describe("before revert", () => {
it('/block/0 should exist', function() {
......
......@@ -78,4 +78,8 @@ describe("Revert balance", () => {
block.documentType = 'block'
// yield s1.singleWritePromise(block)
}))
after(() => {
return s1.closeCluster()
})
})
......@@ -169,6 +169,10 @@ describe("Revert memberships", function() {
yield shouldHavePendingMS(0); // Undone memberships are lost
}));
after(() => {
return s1.closeCluster()
})
/*********
*
* Identity state testing functions
......
......@@ -9,6 +9,7 @@ const rp = require('request-promise');
const httpTest = require('./tools/http');
const commit = require('./tools/commit');
const sync = require('./tools/sync');
const shutDownEngine = require('./tools/shutDownEngine');
const constants = require('../../app/lib/constants');
const expectJSON = httpTest.expectJSON;
......@@ -91,6 +92,13 @@ describe("Switch", function() {
// S1 should have switched to the other branch
}));
after(() => {
return Promise.all([
shutDownEngine(s1),
shutDownEngine(s2)
])
})
describe("Server 1 /blockchain", function() {
it('/block/8 should exist on S1', function() {
......
......@@ -10,6 +10,7 @@ const constants = require('../../app/lib/constants');
const rp = require('request-promise');
const httpTest = require('./tools/http');
const commit = require('./tools/commit');
const shutDownEngine = require('./tools/shutDownEngine');
const expectAnswer = httpTest.expectAnswer;
......@@ -82,6 +83,12 @@ describe("Certification chainability", function() {
});
});
after(() => {
return Promise.all([
shutDownEngine(s1)
])
})
it('block 0 should have 2 certs', function() {
return expectAnswer(rp('http://127.0.0.1:9225/blockchain/block/0', { json: true }), function(res) {
res.should.have.property('number').equal(0);
......
......@@ -84,4 +84,8 @@ describe("Certifier must be a member", function() {
yield s1.commit({ time: now + 23 });
yield tic.cert(tac);
}));
after(() => {
return s1.closeCluster()
})
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment