From 9c8b008a707b7a22b3fde2fb8295f110c278f036 Mon Sep 17 00:00:00 2001 From: Max Metral Date: Sat, 17 Jun 2017 09:48:12 -0400 Subject: [PATCH 1/2] Only react to a matching error from etcd, passing through other errors; fix renew() --- src/microlock.js | 26 ++++++++++++++++++++++---- test/integration/microlock_spec.js | 2 +- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/microlock.js b/src/microlock.js index 77ba7dc..5d51e51 100644 --- a/src/microlock.js +++ b/src/microlock.js @@ -133,7 +133,13 @@ export default class Microlock extends EventEmitter { return resolve(val) }); }) - .catch(() => this.__throwAlreadyLocked()); + .catch((e) => { + // Key already exists + if (e.errorCode === 105) { + this.__throwAlreadyLocked(); + } + throw e; + }); } unlock () { @@ -143,12 +149,18 @@ export default class Microlock extends EventEmitter { return resolve(res); }); }) - .catch(() => this.__throwLockNotOwned()); + .catch((e) => { + // KeyNotFound or CompareFailed + if (e.errorCode === 100 || e.errorCode === 101) { + this.__throwLockNotOwned(); + } + throw e; + }); } renew () { return new Promise((resolve, reject) => { - return this.__etcd.set(this.__key, this.__node_id, { + return this.__etcd.set(this.__key, null, { prevValue: this.__node_id, refresh: true, ttl: this.__ttl @@ -157,6 +169,12 @@ export default class Microlock extends EventEmitter { return resolve(val); }) }) - .catch(() => this.__throwLockNotOwned()); + .catch((e) => { + // KeyNotFound or CompareFailed + if (e.errorCode === 100 || e.errorCode === 101) { + this.__throwLockNotOwned(); + } + throw e; + }); } } diff --git a/test/integration/microlock_spec.js b/test/integration/microlock_spec.js index 09fa82b..f205963 100644 --- a/test/integration/microlock_spec.js +++ b/test/integration/microlock_spec.js @@ -12,7 +12,7 @@ import Microlock, { InvalidTtlError, AlreadyLockedError, LockNotOwnedError -} from '../../lib/microlock'; +} from '../../src/microlock'; describe('microlock', () => { let etcd = null From db547e49e47708bce275e548eb3ef0bcea9a82c2 Mon Sep 17 00:00:00 2001 From: Max Metral Date: Mon, 19 Jun 2017 12:12:11 -0400 Subject: [PATCH 2/2] Revert to using built code for integration tests --- test/integration/microlock_spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/microlock_spec.js b/test/integration/microlock_spec.js index f205963..09fa82b 100644 --- a/test/integration/microlock_spec.js +++ b/test/integration/microlock_spec.js @@ -12,7 +12,7 @@ import Microlock, { InvalidTtlError, AlreadyLockedError, LockNotOwnedError -} from '../../src/microlock'; +} from '../../lib/microlock'; describe('microlock', () => { let etcd = null