Skip to content

Commit

Permalink
New features
Browse files Browse the repository at this point in the history
Unit test and code coverage update
New option added to generator
  • Loading branch information
T-PWK committed Nov 8, 2014
1 parent 4029ef5 commit 9273402
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 1.4.0 (Nov 7, 2014)

Features:

- add `seqMask` generator option to specify maximum number of ids in a sequence i.e. per millisecond
5 changes: 3 additions & 2 deletions flake-id-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
this.seq = 0;
this.lastTime = 0;
this.overflow = false;
this.seqMask = this.options.seqMask || 0xFFF;
};

FlakeId.POW10 = Math.pow(2, 10); // 2 ^ 10
Expand All @@ -44,7 +45,7 @@
}

// Increase sequence counter
this.seq = (this.seq + 1) & 0xFFF;
this.seq = (this.seq + 1) & this.seqMask;

// sequence counter exceeded its max value (4095)
// - set overflow flag and wait till next millisecond
Expand Down Expand Up @@ -78,4 +79,4 @@
}
};

}());
}());
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flake-idgen",
"version": "0.1.3",
"version": "0.1.4",
"description": "Flake ID generator yields k-ordered, conflict-free ids in a distributed environment",
"main": "flake-id-gen.js",
"scripts": {
Expand All @@ -21,7 +21,7 @@
],
"author": {
"name": "Tom Pawlak",
"url": "http://tompawlak.blogspot.com"
"url": "http://blog.tompawlak.org/"
},
"homepage": "https://github.com/T-PWK/flake-idgen",
"licenses": [
Expand Down
49 changes: 47 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,36 @@

var assert = require('assert'),
FlakeId = require('./flake-id-gen'),
idGen1 = new FlakeId();
idGen1 = new FlakeId(),
idGen2 = new FlakeId({id:0x100}),
idGen3 = new FlakeId({seqMask:0x0F});

test(idGen1);
test(idGen2);

testWithFn(idGen1);
testWithFn(idGen2);
testWithFn(idGen3);

testAsync(idGen1);
testAsync(idGen2);


function testAsync(generator) {
var ids = [];
generator.next(function (err, id) {
assert.ifError(err);
ids.push(id.toString('hex'));

generator.next(function (err, id) {
assert.ifError(err);
ids.push(id.toString('hex'));

assert.ok(ids[0] < ids[1]);
});

});
}

function test(generator) {
var ids = new Array(1000), i;
Expand All @@ -20,4 +47,22 @@
}
}

}());
function testWithFn(generator) {
var ids = new Array(1000), i, index = 0;

for(i = 0; i < ids.length; i++) {
generator.next(function (err, id) {
assert.ifError(err);
ids[index++] = id.toString('hex');

if(index == ids.length) {
for(i = 0; i < ids.length - 1; i++) {
assert.notEqual(ids[i], ids[i+1]); // Two sibling ids are not equal
assert.ok(ids[i] < ids[i+1]); // Each id is greater than an id generated before
}
}
});
}
}

}());

0 comments on commit 9273402

Please sign in to comment.