From 297baac3d335a618460d082be1e166e04edccd7a Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Tue, 7 Aug 2018 17:21:46 +0300 Subject: [PATCH] fix(db): under mac and linux using alternative pool mechanism this must fix test brokeup and closing stub --- src/background/mysql.js | 72 ++++++++++++++++++++++++++++++++++----- src/background/spider.js | 2 +- tests/bigtableapi.test.js | 8 +++-- tests/init.js | 1 + tests/sphinx.test.js | 34 ++++++++++++------ 5 files changed, 96 insertions(+), 21 deletions(-) diff --git a/src/background/mysql.js b/src/background/mysql.js index bf01791..40c3053 100644 --- a/src/background/mysql.js +++ b/src/background/mysql.js @@ -113,14 +113,70 @@ const expand = (sphinx) => { return sphinx } -const pool = () => { - let sphinx = mysql.createPool({ - // bug under mac with some problems on big connection size, limit this to very low value on mac os x - connectionLimit: process.platform === 'darwin' ? 3 : config.sphinx.connectionLimit, - host : config.sphinx.host, - port : config.sphinx.port - }); - return expand(sphinx) +const pool = async () => { + if(/^win/.test(process.platform)) + { + logT('sql', 'using main pool mechanism') + let sphinx = mysql.createPool({ + // bug under mac with some problems on big connection size, limit this to very low value on mac os x + connectionLimit: process.platform === 'darwin' ? 3 : config.sphinx.connectionLimit, + host : config.sphinx.host, + port : config.sphinx.port + }); + sphinx = expand(sphinx) + const end = sphinx.end.bind(sphinx) + sphinx.end = async (cb) => new Promise(resolve => end(() => { + resolve() + if(cb) cb() + })) + return sphinx + } + else + { + logT('sql', 'using alternative pool mechanism') + let connectionPool = [] + let connectionsLimit = config.sphinx.connectionLimit + let currentConnection = 0 + for(let i = 0; i < connectionsLimit; i++) + { + connectionPool[i] = await single().waitConnection() + } + const buildPoolMethod = (name, ...args) => { + if(!connectionPool) + return + + const data = connectionPool[currentConnection][name](...args) + currentConnection = (currentConnection + 1) % connectionsLimit + return data + } + return new Proxy({ + query(...args) { + return buildPoolMethod('query', ...args) + }, + insertValues(...args) { + return buildPoolMethod('insertValues', ...args) + }, + updateValues(...args) { + return buildPoolMethod('updateValues', ...args) + }, + async end(cb) + { + await Promise.all(connectionPool.map(conn => new Promise(resolve => conn.end(resolve)))) + if(cb) + cb() + connectionPool = null + } + }, { + get(target, prop) + { + if(!target[prop]) + { + return connectionPool[0][prop] + } + return target[prop] + } + }) + } } const single = (callback) => { diff --git a/src/background/spider.js b/src/background/spider.js index 314119f..65b170b 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -45,7 +45,7 @@ module.exports = function (send, recive, dataDirectory, version, env) let filesId = 1; const events = new EventEmitter - let sphinx = pool(); + let sphinx = await pool(); // initialize p2p const p2p = new P2PServer(send) diff --git a/tests/bigtableapi.test.js b/tests/bigtableapi.test.js index ef17a7c..46456d2 100644 --- a/tests/bigtableapi.test.js +++ b/tests/bigtableapi.test.js @@ -8,8 +8,8 @@ const forBigTable = require('../src/background/forBigTable') describe("big table for check", () => { let sphinx; - it("init", function() { - sphinx = pool() + it("init", async function() { + sphinx = await pool() expect(sphinx) }) @@ -36,4 +36,8 @@ describe("big table for check", () => { await forBigTable(sphinx, 'feed', record => records.push(record), null, 15) expect(records.length === 13) }) + + it("close", async function() { + await sphinx.end() + }) }); diff --git a/tests/init.js b/tests/init.js index fefa84c..c623163 100644 --- a/tests/init.js +++ b/tests/init.js @@ -1,4 +1,5 @@ import {startApplication, stopApplication} from "../tests/application"; +global.logT = (...args) => {console.log(...args)} describe("application", () => { before(startApplication); diff --git a/tests/sphinx.test.js b/tests/sphinx.test.js index cd90c33..2f28bd0 100644 --- a/tests/sphinx.test.js +++ b/tests/sphinx.test.js @@ -1,4 +1,4 @@ -import { expect } from "chai"; +import { expect, assert } from "chai"; const mysql = require('mysql') const config = require('../src/background/config') @@ -60,15 +60,29 @@ describe("sphinx", () => { }) }) - it("query limit", function(done) { - const sphinx = pool() - let promises = [] - sphinx.query(`delete from feed where id >= 0`, () => { - for(let i = 0; i < 500; i++) - promises.push(sphinx.query(`insert into feed(id, data) values(${i}, 'a')`)) - Promise.all(promises).then(() => { - sphinx.query(`delete from feed where id >= 0`, () => done()) + it("query limit", function(done) { + const test = async () => { + const sphinx = await pool() + let promises = [] + sphinx.query(`delete from feed where id >= 0`, () => { + for(let i = 0; i < 500; i++) + promises.push(sphinx.query(`insert into feed(id, data) values(${i}, 'a')`)) + Promise.all(promises).then(() => { + sphinx.query(`delete from feed where id >= 0`, async () => { + await sphinx.end() + done() + }) + }) }) - }) + } + test() + }) + + it("escape", function () { + assert.equal(sphinx.escape(`naru'to`), `'naru\\'to'`) + }) + + it("close pool", function(done) { + sphinx.end(done) }) });