Skip to content

Commit

Permalink
Bring back custom Promise class to not introduce breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jbienkowski311 committed Jul 18, 2019
1 parent 72967a0 commit 89ddc3c
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 7 deletions.
7 changes: 7 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const deprecationLog = (className, methodName) => {
console.warn(`ZOOKEEPER LOG: ${className}::${methodName} is being deprecated!`);
};

exports = module.exports = {
deprecationLog,
};
64 changes: 64 additions & 0 deletions lib/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-disable no-param-reassign */
/* eslint-disable no-return-assign */

const { deprecationLog } = require('./helper');

class ZkPromise extends Promise {
/**
* @deprecated
*/
get(propertyName) {
deprecationLog(ZkPromise.name, 'get');
return this.then(object => object[propertyName]);
}

/**
* @deprecated
*/
put(propertyName, value) {
deprecationLog(ZkPromise.name, 'put');
return this.then(object => object[propertyName] = value);
}

/**
* @deprecated
*/
call(functionName, ...args) {
deprecationLog(ZkPromise.name, 'call');
return this.then(object => object[functionName](...args));
}

/**
* @deprecated
*/
addCallback(callback) {
deprecationLog(ZkPromise.name, 'addCallback');
return this.then(callback);
}

/**
* @deprecated
*/
addErrback(callback) {
deprecationLog(ZkPromise.name, 'addErrback');
return this.catch(callback);
}

/**
* @deprecated
*/
addBoth(callback) {
deprecationLog(ZkPromise.name, 'addBoth');
return this.then(callback, callback);
}

/**
* @deprecated
*/
addCallbacks(callback, errback) {
deprecationLog(ZkPromise.name, 'addCallbacks');
return this.then(callback, errback);
}
}

exports = module.exports = ZkPromise;
5 changes: 3 additions & 2 deletions lib/zk_promise.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// needed to not break the interface
/* eslint-disable camelcase */
const ZkPromise = require('./promise');
const ZooKeeper = require('./zookeeper');

class ZooKeeperPromise extends ZooKeeper {
on_connected() {
return new Promise((resolve) => {
return new ZkPromise((resolve) => {
this.once('connect', () => resolve(this));
});
}
Expand Down Expand Up @@ -68,7 +69,7 @@ class ZooKeeperPromise extends ZooKeeper {
}

promisify(fn, args) {
return new Promise((resolve, reject) => {
return new ZkPromise((resolve, reject) => {
const callback = (rc, error, result) => {
if (rc) {
reject(rc);
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@
"shelljs": "0.8.x"
},
"devDependencies": {
"eslint": "5.x",
"eslint-config-airbnb-base": "13.x",
"eslint-plugin-import": "2.x",
"log4js": "4.x",
"proxyquire": "2.x",
"sinon": "7.x",
"tap-spec": "5.x",
"tape": "4.x",
"log4js": "4.x",
"webworker": "0.x",
"eslint": "5.x",
"eslint-config-airbnb-base": "13.x",
"eslint-plugin-import": "2.x"
"webworker": "0.x"
},
"main": "lib/index",
"scripts": {
Expand Down
32 changes: 32 additions & 0 deletions tests/util/helpertest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const sinon = require('sinon');
const test = require('tape');
const { deprecationLog } = require('../../lib/helper');

class Test {
static method() {
deprecationLog(Test.name, 'method');
}
}

test('deprecation log is called with proper arguments', (t) => {
const deprecationLogStub = sinon.stub();
t.plan(3);

deprecationLogStub(Test.name, 'method');

t.equal(deprecationLogStub.callCount, 1);
t.equal(deprecationLogStub.getCall(0).args[0], 'Test');
t.equal(deprecationLogStub.getCall(0).args[1], 'method');
});

test('deprecation log gives proper message', (t) => {
const consoleStub = sinon.stub(console, 'warn');
t.plan(2);

Test.method();

t.equal(consoleStub.callCount, 1);
t.equal(consoleStub.getCall(0).args[0], 'ZOOKEEPER LOG: Test::method is being deprecated!');

consoleStub.restore();
});
120 changes: 120 additions & 0 deletions tests/util/promisetest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const sinon = require('sinon');
const test = require('tape');

const ZkPromise = require('../../lib/promise');

test('ZkPromise get', (t) => {
t.plan(1);

const expected = 'value';
const promise = ZkPromise.resolve({
property: expected,
});

promise.get('property').then((actual) => {
t.equal(actual, expected);
});
});

test('ZkPromise put', (t) => {
t.plan(1);

const expected = 'value';
const promise = ZkPromise.resolve({});

promise.put('property', expected).then((actual) => {
t.equal(actual, expected);
});
});

test('ZkPromise call', (t) => {
t.plan(1);

const promise = ZkPromise.resolve({
multiply: (a, b) => a * b,
});

promise.call('multiply', 2, 3).then((actual) => {
t.equal(actual, 2 * 3);
});
});

test('ZkPromise addCallback', (t) => {
t.plan(3);

const callback = sinon.stub().callsFake(value => value * 2);
const promise = ZkPromise.resolve(10);

promise.addCallback(callback).then((actual) => {
t.equal(actual, 20);
t.equal(callback.callCount, 1);
t.equal(callback.getCall(0).args[0], 10);
});
});

test('ZkPromise addErrback', (t) => {
t.plan(3);

const callback = sinon.stub().callsFake(value => value * 2);
const promise = ZkPromise.reject(10);

promise.addErrback(callback).then((actual) => {
t.equal(actual, 20);
t.equal(callback.callCount, 1);
t.equal(callback.getCall(0).args[0], 10);
});
});

test('ZkPromise addBoth', (t) => {
t.plan(7);

const callback = sinon.stub().callsFake(value => value * 2);
const errback = sinon.stub().callsFake(() => {
throw new Error('errback!');
});
// eslint-disable-next-line max-len
const promise = succeeds => new ZkPromise((resolve, reject) => (succeeds ? resolve(10) : reject(10)));

promise(true).addBoth(callback)
.then((actual) => {
t.equal(actual, 20);
t.equal(callback.callCount, 1);
t.equal(callback.getCall(0).args[0], 10);
return promise(false).addBoth(errback);
})
.catch((actual) => {
t.true(actual instanceof Error);
t.equal(actual.message, 'errback!');
t.equal(errback.callCount, 1);
t.equal(errback.getCall(0).args[0], 10);
});
});

test('ZkPromise addCallbacks', (t) => {
t.plan(9);

const callback = sinon.stub().callsFake(value => value * 2);
const errback = sinon.stub().callsFake(() => {
throw new Error('errback!');
});
// eslint-disable-next-line max-len
const promise = succeeds => new ZkPromise((resolve, reject) => (succeeds ? resolve(10) : reject(10)));

promise(true).addCallbacks(callback, errback)
.then((actual) => {
t.equal(actual, 20);
t.equal(callback.callCount, 1);
t.equal(callback.getCall(0).args[0], 10);
t.equal(errback.callCount, 0);
callback.resetHistory();
errback.resetHistory();
return promise(false).addCallbacks(callback, errback);
})
.catch((actual) => {
t.true(actual instanceof Error);
t.equal(actual.message, 'errback!');
t.equal(callback.callCount, 0);
t.equal(errback.callCount, 1);
t.equal(errback.getCall(0).args[0], 10);
});
});

0 comments on commit 89ddc3c

Please sign in to comment.