Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better actionID generation

  • Loading branch information...
commit 586414f3636bc79880bcba6f7f71ea8b1d153d71 1 parent 0f45e11
@brianc authored
Showing with 34 additions and 6 deletions.
  1. +7 −2 lib/action.js
  2. +7 −2 test/action-tests.js
  3. +20 −2 test/initial-tests.js
View
9 lib/action.js
@@ -4,11 +4,16 @@ var noop = function() { };
var Action = function(data) {
this.data = data || {};
- this.actionID = this.data.actionID = Action.lastActionID++;
+ this.actionID = this.data.actionID = Action.getNextActionID();
this.callback = noop;
}
-Action.lastActionID = 0;
+Action.nextActionIndex = 0;
+Action.getNextActionID = function() {
+ Action.lastActionID = new Date().getTime() + '-' + ~~(Math.random() * 100000) + '-' + Action.nextActionIndex++;
+ return Action.lastActionID;
+}
+
//write's the action's data to a string fit to be sent to AMI
Action.prototype.serialize = function() {
View
9 test/action-tests.js
@@ -7,8 +7,13 @@ describe('Action', function() {
var action1 = new Action();
var action2 = new Action();
var action3 = new Action();
- action1.actionID.should.be.lessThan(action2.actionID);
- action2.actionID.should.be.lessThan(action3.actionID);
+ action1.actionID.should.not.equal(action2.actionID)
+ action2.actionID.should.not.equal(action3.actionID);
+ })
+
+ it('is saved as lastActionID', function() {
+ var anAction = new Action();
+ anAction.actionID.should.equal(Action.lastActionID);
})
})
View
22 test/initial-tests.js
@@ -59,19 +59,33 @@ describe('Client', function() {
it('sends correct login message', function() {
+ var socket = new MemorySocket();
+ var client = new ami.Client(socket);
client.send(loginAction);
- var expectedData = 'Action: login\r\nUsername: user\r\nSecret: pass\r\nActionID: ' + (Action.lastActionID-1) + '\r\n\r\n';
+ //enqueue a successful login message
+ var packet = 'Response: Success\r\nActionID: ' + Action.lastActionID + '\r\nMessage: Authentication accepted\r\n\r\n'
+ socket.emitSoon('data', Buffer(packet,'utf8'))
+ var expectedData = 'Action: login\r\nUsername: user\r\nSecret: pass\r\nActionID: ' + (Action.lastActionID) + '\r\n\r\n';
socket.data[0].should.equal(expectedData);
})
it('calls callback with no error', function(done) {
+ var socket = new MemorySocket();
+ var client = new ami.Client(socket);
client.send(loginAction, done);
+ //enqueue a successful login message
+ var packet = 'Response: Success\r\nActionID: ' + Action.lastActionID + '\r\nMessage: Authentication accepted\r\n\r\n'
+ socket.emitSoon('data', Buffer(packet,'utf8'))
})
it('emits a login success message', function(done) {
+ var socket = new MemorySocket();
+ var client = new ami.Client(socket);
client.send(loginAction);
+ var packet = 'Response: Success\r\nActionID: ' + Action.lastActionID + '\r\nMessage: Authentication accepted\r\n\r\n'
+ socket.emitSoon('data', Buffer(packet,'utf8'))
client.on('message', function(msg) {
- msg.actionID.should.equal((Action.lastActionID-1).toString());
+ msg.actionID.should.equal((Action.lastActionID).toString());
msg.response.should.equal('Success');
msg.message.should.equal('Authentication accepted');
done();
@@ -79,7 +93,11 @@ describe('Client', function() {
})
it('removes completed action from action queue', function() {
+ var socket = new MemorySocket();
+ var client = new ami.Client(socket);
client._pendingActions.length.should.equal(0);
+ var packet = 'Response: Success\r\nActionID: ' + Action.lastActionID + '\r\nMessage: Authentication accepted\r\n\r\n'
+ socket.emitSoon('data', Buffer(packet,'utf8'))
client.send(loginAction, function(err) {
process.nextTick(function() {
client._pendingActions.length.should.equal(0);
Please sign in to comment.
Something went wrong with that request. Please try again.