Skip to content

Commit

Permalink
Implemented the lock tests for the 'ResourceTester'
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 16, 2017
1 parent 6097857 commit cb90c9f
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 81 deletions.
6 changes: 1 addition & 5 deletions lib/resource/std/resourceTester/Locks.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
import { ResourceTesterTestCallback } from './Types';
export declare function getLocks(callback: ResourceTesterTestCallback): void;
export declare function setLock(callback: ResourceTesterTestCallback): void;
export declare function removeLock(callback: ResourceTesterTestCallback): void;
export declare function getAvailableLocks(callback: ResourceTesterTestCallback): void;
export declare function getLock(callback: ResourceTesterTestCallback): void;
export declare function lock(callback: ResourceTesterTestCallback): void;
74 changes: 48 additions & 26 deletions lib/resource/std/resourceTester/Locks.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function getLocks(callback) {
var LockScope_1 = require("../../lock/LockScope");
var LockKind_1 = require("../../lock/LockKind");
var LockType_1 = require("../../lock/LockType");
var Lock_1 = require("../../lock/Lock");
function lock(callback) {
var _this = this;
callback = this.multiple(callback, 1);
if (!this.options.canLock) {
callback = this.multiple(callback, 5);
var lock_1 = new Lock_1.Lock(new LockKind_1.LockKind(LockScope_1.LockScope.Exclusive, LockType_1.LockType.Write), '123', null);
this.producer(false, function (r1) { return r1.getLock('123', function (e) { return callback(e, !e, 'getLock must return an error', false); }); });
this.producer(false, function (r1) { return r1.setLock(lock_1, function (e) { return callback(e, !e, 'setLock must return an error', false); }); });
this.producer(false, function (r1) { return r1.getAvailableLocks(function (e, kinds) { return callback(e, !e, 'getAvailableLocks must return an error', false); }); });
this.producer(false, function (r1) { return r1.getLocks(function (e, locks) { return callback(e, !e, 'getLocks must return an error', false); }); });
this.producer(false, function (r1) { return r1.removeLock('123', function (e, removed) { return callback(e, !e, 'removeLock must return an error', false); }); });
return;
}
callback = this.multiple(callback, 2);
var lock1 = new Lock_1.Lock(new LockKind_1.LockKind(LockScope_1.LockScope.Exclusive, LockType_1.LockType.Write), '123', null);
this.producer(false, function (r1) {
r1.getLocks(function (e, locks) {
callback(e, !e, 'getLocks error', _this.options.canGetLocks, function () {
callback(null, !!locks && !!locks.prototype[Symbol.iterator], 'getLocks returns an invalid value : must be an iterable', _this.options.canWrite);
r1.setLock(lock1, function (e) {
callback(e, !e, 'setLock error', undefined, function () {
r1.getLock(lock1.uuid, function (e, lock) {
callback(e, !e, 'getLock error - cannot find the lock', undefined, function () {
callback(null, lock && lock.isSame && lock.isSame(lock1), 'The lock returned by getLock is not the one stored previously by setLock', undefined, function () {
r1.getLocks(function (e, locks) {
callback(e, !e, 'getLocks error', undefined, function () {
callback(null, locks && locks.length === 1 && locks[0].isSame && locks[0].isSame(lock1), 'The lock added is not listed in the result of getLocks', undefined, function () {
r1.removeLock(lock1.uuid, function (e, removed) {
callback(e, !e && removed, 'removeLock error', undefined, function () {
r1.getLocks(function (e, locks) {
callback(e, !e, 'getLocks error', undefined, function () {
callback(null, locks && locks.length === 0, 'The lock has not really been removed');
});
});
});
});
});
});
});
});
});
});
});
});
});
this.producer(false, function (r1) {
r1.getAvailableLocks(function (e, locks) {
callback(e, !e, 'getAvailableLocks error', _this.options.canLock, function () {
callback(null, !!locks && !!locks.constructor.prototype[Symbol.iterator], 'getAvailableLocks returns an invalid value : must be an iterable', _this.options.canLock);
});
});
});
}
exports.getLocks = getLocks;
function setLock(callback) {
callback = this.multiple(callback, 1);
callback(null, true, '');
}
exports.setLock = setLock;
function removeLock(callback) {
callback = this.multiple(callback, 1);
callback(null, true, '');
}
exports.removeLock = removeLock;
function getAvailableLocks(callback) {
callback = this.multiple(callback, 1);
callback(null, true, '');
}
exports.getAvailableLocks = getAvailableLocks;
function getLock(callback) {
callback = this.multiple(callback, 1);
callback(null, true, '');
}
exports.getLock = getLock;
exports.lock = lock;
6 changes: 1 addition & 5 deletions lib/resource/std/resourceTester/ResourceTester.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ export declare class ResourceTester<T extends IResource> {
protected writeRead: typeof content.writeRead;
protected mimeType: typeof content.mimeType;
protected size: typeof content.size;
protected getLocks: typeof locks.getLocks;
protected setLock: typeof locks.setLock;
protected removeLock: typeof locks.removeLock;
protected getAvailableLocks: typeof locks.getAvailableLocks;
protected getLock: typeof locks.getLock;
protected lock: typeof locks.lock;
protected addChild: typeof children.addChild;
protected removeChild: typeof children.removeChild;
protected getChildren: typeof children.getChildren;
Expand Down
9 changes: 3 additions & 6 deletions lib/resource/std/resourceTester/ResourceTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ var DefaultResourceTesterOptions = (function () {
this.canBeCreated = true;
this.canBeDeleted = true;
this.canBeRenamed = true;
this.canGetLocks = true;
this.canGetSize = true;
this.canBeMoved = true;
this.canWrite = true;
this.canRead = true;
this.canLock = true;
}
return DefaultResourceTesterOptions;
}());
Expand All @@ -38,11 +38,7 @@ var ResourceTester = (function () {
this.writeRead = content.writeRead;
this.mimeType = content.mimeType;
this.size = content.size;
this.getLocks = locks.getLocks;
this.setLock = locks.setLock;
this.removeLock = locks.removeLock;
this.getAvailableLocks = locks.getAvailableLocks;
this.getLock = locks.getLock;
this.lock = locks.lock;
this.addChild = children.addChild;
this.removeChild = children.removeChild;
this.getChildren = children.getChildren;
Expand Down Expand Up @@ -138,6 +134,7 @@ var ResourceTester = (function () {
test('writeRead');
test('mimeType');
test('size');
test('lock');
test('addChild');
test('removeChild');
test('getChildren');
Expand Down
2 changes: 1 addition & 1 deletion lib/resource/std/resourceTester/Types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export interface ResourceTesterOptions {
canBeCreated: boolean;
canBeDeleted: boolean;
canBeRenamed: boolean;
canGetLocks: boolean;
canGetSize: boolean;
canBeMoved: boolean;
canWrite: boolean;
canRead: boolean;
canLock: boolean;
}
export declare type ResourceTesterTestCallback = (error: Error, isValid: boolean, text: string, optionReverse?: boolean, cbNext?: () => void) => void;
export declare type ResourceTesterProducerCallback<T> = (resource: T) => void;
Expand Down
79 changes: 54 additions & 25 deletions src/resource/std/resourceTester/Locks.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,64 @@
import { ResourceTesterTestCallback } from './Types'
import { IResource } from '../../IResource'
import { LockScope } from '../../lock/LockScope'
import { LockKind } from '../../lock/LockKind'
import { LockType } from '../../lock/LockType'
import { Lock } from '../../lock/Lock'

// ****************************** Locks ****************************** //
export function getLocks(callback : ResourceTesterTestCallback)
export function lock(callback : ResourceTesterTestCallback)
{
callback = this.multiple(callback, 1);
if(!this.options.canLock)
{
callback = this.multiple(callback, 5);

const lock = new Lock(new LockKind(LockScope.Exclusive, LockType.Write), '123', null);

this.producer(false, (r1 : IResource) => r1.getLock('123', (e) => callback(e, !e, 'getLock must return an error', false)));
this.producer(false, (r1 : IResource) => r1.setLock(lock, (e) => callback(e, !e, 'setLock must return an error', false)));
this.producer(false, (r1 : IResource) => r1.getAvailableLocks((e, kinds) => callback(e, !e, 'getAvailableLocks must return an error', false)));
this.producer(false, (r1 : IResource) => r1.getLocks((e, locks) => callback(e, !e, 'getLocks must return an error', false)));
this.producer(false, (r1 : IResource) => r1.removeLock('123', (e, removed) => callback(e, !e, 'removeLock must return an error', false)));
return;
}

callback = this.multiple(callback, 2);

const lock1 = new Lock(new LockKind(LockScope.Exclusive, LockType.Write), '123', null);

this.producer(false, (r1 : IResource) => {
r1.setLock(lock1, (e) => {
callback(e, !e, 'setLock error', undefined, () => {
r1.getLock(lock1.uuid, (e, lock) => {
callback(e, !e, 'getLock error - cannot find the lock', undefined, () => {
callback(null, lock && lock.isSame && lock.isSame(lock1), 'The lock returned by getLock is not the one stored previously by setLock', undefined, () => {
r1.getLocks((e, locks) => {
callback(e, !e, 'getLocks error', undefined, () => {
callback(null, locks && locks.length === 1 && locks[0].isSame && locks[0].isSame(lock1), 'The lock added is not listed in the result of getLocks', undefined, () => {
r1.removeLock(lock1.uuid, (e, removed) => {
callback(e, !e && removed, 'removeLock error', undefined, () => {
r1.getLocks((e, locks) => {
callback(e, !e, 'getLocks error', undefined, () => {
callback(null, locks && locks.length === 0, 'The lock has not really been removed');
});
});
});
});
});
});
});
});
});
})
});
})
})

this.producer(false, (r1) => {
r1.getLocks((e, locks) => {
callback(e, !e, 'getLocks error', this.options.canGetLocks, () => {
callback(null, !!locks && !!(locks as any).prototype[Symbol.iterator], 'getLocks returns an invalid value : must be an iterable', this.options.canWrite)
r1.getAvailableLocks((e, locks) => {
callback(e, !e, 'getAvailableLocks error', this.options.canLock, () => {
callback(null, !!locks && !!(locks as any).constructor.prototype[Symbol.iterator], 'getAvailableLocks returns an invalid value : must be an iterable', this.options.canLock)
});
})
})
}
export function setLock(callback : ResourceTesterTestCallback)
{
callback = this.multiple(callback, 1);
callback(null, true, '');
}
export function removeLock(callback : ResourceTesterTestCallback)
{
callback = this.multiple(callback, 1);
callback(null, true, '');
}
export function getAvailableLocks(callback : ResourceTesterTestCallback)
{
callback = this.multiple(callback, 1);
callback(null, true, '');
}
export function getLock(callback : ResourceTesterTestCallback)
{
callback = this.multiple(callback, 1);
callback(null, true, '');
}
16 changes: 4 additions & 12 deletions src/resource/std/resourceTester/ResourceTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class DefaultResourceTesterOptions implements ResourceTesterOptions
canBeCreated : boolean = true
canBeDeleted : boolean = true
canBeRenamed : boolean = true
canGetLocks : boolean = true
canGetSize : boolean = true
canBeMoved : boolean = true
canWrite : boolean = true
canRead : boolean = true
canLock : boolean = true
}

export class ResourceTester<T extends IResource>
Expand Down Expand Up @@ -135,12 +135,8 @@ export class ResourceTester<T extends IResource>
test('rename');
test('writeRead');
test('mimeType');
test('size'); /*
test('getLocks');
test('setLock');
test('removeLock');
test('getAvailableLocks');
test('getLock');*/
test('size');
test('lock');
test('addChild');
test('removeChild');
test('getChildren');
Expand All @@ -165,11 +161,7 @@ export class ResourceTester<T extends IResource>
protected size = content.size

// ****************************** Locks ****************************** //
protected getLocks = locks.getLocks
protected setLock = locks.setLock
protected removeLock = locks.removeLock
protected getAvailableLocks = locks.getAvailableLocks
protected getLock = locks.getLock
protected lock = locks.lock

// ****************************** Children ****************************** //
protected addChild = children.addChild
Expand Down
2 changes: 1 addition & 1 deletion src/resource/std/resourceTester/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export interface ResourceTesterOptions
canBeCreated : boolean
canBeDeleted : boolean
canBeRenamed : boolean
canGetLocks : boolean
canGetSize : boolean
canBeMoved : boolean
canWrite : boolean
canRead : boolean
canLock : boolean
}

export type ResourceTesterTestCallback = (error : Error, isValid : boolean, text : string, optionReverse ?: boolean, cbNext ?: () => void) => void;
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit cb90c9f

Please sign in to comment.