Skip to content

Commit

Permalink
Added after syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ncthbrt committed Dec 2, 2017
1 parent ac124f5 commit 2adc1e2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/actor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ class Actor {
this.immediate = undefined;
this.parent.childSpawned(this);
if (shutdown) {
this.shutdownPeriod = shutdown;
if (!shutdown.duration) {
throw new Error('Shutdown should be specified as a duration. It is recommended to use the after() function to do this');
}
this.shutdownPeriod = shutdown.duration;
this.timeout = setTimeout(() => this.stop(), this.shutdownPeriod);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const { spawn, spawnStateless } = require('./actor');
const { stop, state$, query, dispatch } = require('./functions');
const { spawnPersistent, configurePersistence } = require('./persistence');
const { after } = require('./utils');
module.exports = {
...require('./system'),
spawn,
after,
spawnStateless,
query,
dispatch,
Expand Down
43 changes: 43 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class After {
constructor (amount) {
this._amount = amount;
Object.freeze(this);
}

get hours () {
return { duration: (this._amount * 60 * 60 * 1000) | 0 };
}

get hour () {
return this.hours;
}

get minutes () {
return { duration: (this._amount * 60 * 1000) | 0 };
}

get minute () {
return this.minutes;
}

get seconds () {
return { duration: (this._amount * 1000) | 0 };
}

get second () {
return this.seconds;
}

get milliseconds () {
return { duration: this._amount | 0 };
}
get millisecond () {
return this.milliseconds;
}
}

const after = (amount) => new After(amount);

module.exports = {
after
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nact",
"version": "3.1.5",
"version": "3.2.0",
"description": "nact ⇒ node.js + actors = your services have never been so µ",
"main": "lib/index.js",
"scripts": {
Expand Down
22 changes: 18 additions & 4 deletions test/actor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
chai.should();
const { start, spawn, spawnStateless, dispatch, stop, query, state$ } = require('../lib');
const { start, spawn, after, spawnStateless, dispatch, stop, query, state$ } = require('../lib');
const { Promise } = require('bluebird');
const { LocalPath } = require('../lib/paths');
const delay = Promise.delay.bind(Promise);
Expand Down Expand Up @@ -195,7 +195,7 @@ describe('Actor', function () {
});
});

describe('#stop()', function () {
describe('timeout', function () {
let system;
beforeEach(() => { system = start(); });
afterEach(() => {
Expand All @@ -206,19 +206,33 @@ describe('Actor', function () {

it('should automatically stop after timeout if timeout is specified', async function () {
console.error = ignore;
let child = spawnStateless(system, (msg) => {}, 'test', { shutdown: 100 });
let child = spawnStateless(system, (msg) => {}, 'test', { shutdown: after(100).milliseconds });
await delay(110);
isStopped(child).should.be.true;
});

it('should automatically renew timeout after message', async function () {
let child = spawnStateless(system, ignore, 'test1', { shutdown: 60 });
let child = spawnStateless(system, ignore, 'test1', { shutdown: after(60).milliseconds });
await delay(30);
dispatch(child, {});
await delay(40);
isStopped(child).should.not.be.true;
});

it('should throw if timeout does not include a duration field', async function () {
(() => spawnStateless(system, ignore, 'test1', { shutdown: {} })).should.throw();
});
});

describe('#stop()', function () {
let system;
beforeEach(() => { system = start(); });
afterEach(() => {
stop(system);
// reset console
delete console.error;
});

it('should prevent children from being spawned after being called', function () {
let child = spawnStateless(system, ignore);
stop(child);
Expand Down
29 changes: 29 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-env mocha */
/* eslint-disable no-unused-expressions */
const chai = require('chai');
chai.should();
const { after } = require('../lib');

describe('#after', function () {
it('should correctly calculate milliseconds', function () {
after(100).milliseconds.duration.should.equal(100);
after(1).millisecond.duration.should.equal(1);
after(0).milliseconds.duration.should.equal(0);
});
it('should correctly calculate seconds', function () {
after(1).second.duration.should.equal(1000);
after(0).seconds.duration.should.equal(0);
after(10).seconds.duration.should.equal(10000);
after(1.5).seconds.duration.should.equal(1500);
});

it('should correctly calculate minutes', function () {
after(1).minute.duration.should.equal(60000);
after(0).minutes.duration.should.equal(0);
});

it('should correctly calculate hours', function () {
after(1).hour.duration.should.equal(3600000);
after(0).hours.duration.should.equal(0);
});
});

0 comments on commit 2adc1e2

Please sign in to comment.