Merge pull request #5674 from chrootsu/redlock

redlock: added definitions and tests
This commit is contained in:
Horiuchi_H
2015-09-08 09:57:29 +09:00
2 changed files with 166 additions and 0 deletions

103
redlock/redlock-test.ts Normal file
View File

@@ -0,0 +1,103 @@
/// <reference path="./redlock.d.ts" />
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<Lock>;
let result: Promise<Lock>;
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<void>;
let result: Promise<void>;
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<Lock>;
let result: Promise<Lock>;
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<Lock>;
let result: Promise<Lock>;
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<void>;
let result: void;
result = lock.unlock();
result = lock.unlock(callback);
}
// extend
{
let lock: Lock;
let ttl: number;
let callback: NodeifyCallback<Lock>;
let result: Promise<Lock>;
result = lock.extend(ttl);
result = lock.extend(ttl, callback);
}
}

63
redlock/redlock.d.ts vendored Normal file
View File

@@ -0,0 +1,63 @@
// Type definitions for Redlock
// Project: https://github.com/mike-marcacci/node-redlock
// Definitions by: Ilya Mochalov <https://github.com/chrootsu>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../bluebird/bluebird.d.ts" />
declare module RedlockTypes {
interface LockError extends Error {}
interface NodeifyCallback<T> {
(err: any, value?: T): void;
}
interface Lock {
redlock: Redlock;
resource: string;
value: any;
expiration: number;
unlock(callback?: NodeifyCallback<void>): void;
extend(ttl: number, callback?: NodeifyCallback<Lock>): Promise<Lock>;
}
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<Lock>): Promise<Lock>;
lock(resource: string, ttl: number, callback?: NodeifyCallback<Lock>): Promise<Lock>;
disposer(resource: string, ttl: number): any; // return bluebird.Disposer
release(lock: Lock, callback?: NodeifyCallback<void>): Promise<void>;
unlock(lock: Lock, callback?: NodeifyCallback<void>): Promise<void>;
extend(lock: Lock, ttl: number, callback?: NodeifyCallback<Lock>): Promise<Lock>;
_lock(resource: string, value: string, ttl: number, callback?: NodeifyCallback<Lock>): Promise<Lock>;
_random(): string;
}
}
declare module "redlock" {
import Redlock = RedlockTypes.Redlock;
export = Redlock;
}