Skip to content

Commit

Permalink
Test destruct method
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Jul 25, 2016
1 parent 726cd70 commit dd924e2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/Skrin.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ Skrin.prototype = {
debug('read %s', key);
var that = this;

if (that.wasDestructed) {
return Promise.reject(new Error('read operations not permitted on already destructed instances'));
}

if (!that.mutexByKey[key]) {
debug('read - locking %s', key);

Expand Down Expand Up @@ -300,10 +304,10 @@ Skrin.prototype = {
destruct: function () {
debug('destruct');

this.waitForLock = Promise.reject(new Error('destruct has been called on this instance'));
debug('destruct - closing file watchers');
this.watcher.close();

return Promise.resolve();
this.wasDestructed = true;
}
};

Expand Down
49 changes: 49 additions & 0 deletions test/skrin-destruct-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var Skrin = require('../lib/Skrin');
var pathModule = require('path');
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var sinon = require('sinon');
var expect = require('unexpected').clone().use(require('unexpected-sinon'));
var getTemporaryFilePath = require('gettemporaryfilepath');

function touchAsync(path) {
var now = new Date();
return fs.utimesAsync(path, now, now);
}

function getSkrinInstance() {
return new Skrin({
cacheDir: getTemporaryFilePath(),
populate: sinon.spy(function (key) {
var startCompileTime = Date.now();
return Promise.resolve().delay(10).then(function () {
return {
metadata: {
sourcePaths: [pathToFooTxt],
compileTime: Date.now() - startCompileTime
},
payloads: {
transpiledOutput: 'the transpiled output of ' + key,
sourceMap: 'the source map of ' + key
}
};
});
})
});
}

describe('Skrin.destruct', function () {
it('should not throw when called', function () {
var skrin = getSkrinInstance();

expect(skrin.destruct.bind(skrin), 'not to throw');
});

it('should reject read operations when destruct was called', function () {
var skrin = getSkrinInstance();

skrin.destruct();

expect(skrin.read(), 'to be rejected with', new Error('read operations not permitted on already destructed instances'));
});
});

0 comments on commit dd924e2

Please sign in to comment.