From e6aa707f0fcbf9b661b8922385ce6651a537915c Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Tue, 8 Sep 2015 03:58:21 +0500 Subject: [PATCH 1/2] redlock: added definitions --- redlock/redlock.d.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 redlock/redlock.d.ts diff --git a/redlock/redlock.d.ts b/redlock/redlock.d.ts new file mode 100644 index 0000000000..a5dd01be36 --- /dev/null +++ b/redlock/redlock.d.ts @@ -0,0 +1,63 @@ +// Type definitions for Redlock +// Project: https://github.com/mike-marcacci/node-redlock +// Definitions by: Ilya Mochalov +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module RedlockTypes { + interface LockError extends Error {} + + interface NodeifyCallback { + (err: any, value?: T): void; + } + + interface Lock { + redlock: Redlock; + resource: string; + value: any; + expiration: number; + + unlock(callback?: NodeifyCallback): void; + + extend(ttl: number, callback?: NodeifyCallback): Promise; + } + + interface RedlockOptions { + driftFactor?: number; + retryCount?: number; + retryDelay?: number; + } + + class Redlock { + LockError: LockError; + + driftFactor: number; + retryCount: number; + retryDelay: number; + + servers: any[]; // array of redis.RedisClient + + constructor(clients: any[], options?: RedlockOptions); + //new (clients: any[], options?: IRedlockOptions); + + acquire(resource: string, ttl: number, callback?: NodeifyCallback): Promise; + lock(resource: string, ttl: number, callback?: NodeifyCallback): Promise; + + disposer(resource: string, ttl: number): any; // return bluebird.Disposer + + release(lock: Lock, callback?: NodeifyCallback): Promise; + unlock(lock: Lock, callback?: NodeifyCallback): Promise; + + extend(lock: Lock, ttl: number, callback?: NodeifyCallback): Promise; + + _lock(resource: string, value: string, ttl: number, callback?: NodeifyCallback): Promise; + + _random(): string; + } +} + +declare module "redlock" { + import Redlock = RedlockTypes.Redlock; + export = Redlock; +} From fde71587c09c7d769c5b9c18c77accf3d31dc0c2 Mon Sep 17 00:00:00 2001 From: Ilya Mochalov Date: Tue, 8 Sep 2015 03:58:40 +0500 Subject: [PATCH 2/2] redlock: added tests --- redlock/redlock-test.ts | 103 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 redlock/redlock-test.ts diff --git a/redlock/redlock-test.ts b/redlock/redlock-test.ts new file mode 100644 index 0000000000..e7d61096cd --- /dev/null +++ b/redlock/redlock-test.ts @@ -0,0 +1,103 @@ +/// + +import Redlock = require('redlock'); + +import NodeifyCallback = RedlockTypes.NodeifyCallback; +import Lock = RedlockTypes.Lock; +import RedlockOptions = RedlockTypes.RedlockOptions; + +module RedlockTest { + // constructor + { + let redlock: Redlock; + let client: any; + let options: RedlockOptions; + redlock = new Redlock([client]); + redlock = new Redlock([client], options); + } + + // acquire, lock + { + let redlock: Redlock; + let resource: string; + let ttl: number; + let callback: NodeifyCallback; + let result: Promise; + result = redlock.acquire(resource, ttl); + result = redlock.acquire(resource, ttl, callback); + result = redlock.lock(resource, ttl); + result = redlock.lock(resource, ttl, callback); + } + + // disposer + { + let redlock: Redlock; + let resource: string; + let ttl: number; + let result: any; + result = redlock.acquire(resource, ttl); + } + + // release, unlock + { + let redlock: Redlock; + let lock: Lock; + let callback: NodeifyCallback; + let result: Promise; + result = redlock.release(lock); + result = redlock.release(lock, callback); + result = redlock.unlock(lock); + result = redlock.unlock(lock, callback); + } + + // extend + { + let redlock: Redlock; + let lock: Lock; + let ttl: number; + let callback: NodeifyCallback; + let result: Promise; + result = redlock.extend(lock, ttl); + result = redlock.extend(lock, ttl, callback); + } + + // _lock + { + let redlock: Redlock; + let resource: string; + let value: string; + let ttl: number; + let callback: NodeifyCallback; + let result: Promise; + result = redlock._lock(resource, value, ttl); + result = redlock._lock(resource, value, ttl, callback); + } + + // _random + { + let redlock: Redlock; + let result: string; + result = redlock._random(); + } +} + +module LockTest { + // unlock + { + let lock: Lock; + let callback: NodeifyCallback; + let result: void; + result = lock.unlock(); + result = lock.unlock(callback); + } + + // extend + { + let lock: Lock; + let ttl: number; + let callback: NodeifyCallback; + let result: Promise; + result = lock.extend(ttl); + result = lock.extend(ttl, callback); + } +}