Permalink
Browse files

Merge remote branch 'ctavan/master' into explicit_time

  • Loading branch information...
2 parents 2f6608a + b8e34e7 commit c2649cf9d2071e8e5567ca3c9d0665becae7d6a0 @broofa committed Dec 30, 2011
Showing with 55 additions and 4 deletions.
  1. +47 −0 test/test.js
  2. +8 −4 uuid.js
View
47 test/test.js
@@ -68,6 +68,53 @@ assert(
'IDs created at same msec are different'
);
+// Verify that this node can never produce 2 UUIDs within the same 100ns interval
+var tn = 0;
+var thrown = false;
+try {
+ uuid.v1({msecs: t, nsecs: tn});
+ uuid.v1({msecs: t, nsecs: tn});
+} catch(e) {
+ thrown = true;
+}
+assert(
+ thrown === true,
+ 'IDs created at same 100 nsec throw an error (nsecs = 0)'
+);
+
+// Same as above but tn > 0
+tn = 10;
+thrown = false;
+try {
+ uuid.v1({msecs: t, nsecs: tn});
+ uuid.v1({msecs: t, nsecs: tn});
+} catch(e) {
+ thrown = true;
+}
+assert(
+ thrown === true,
+ 'IDs created at same 100 nsec throw an error (nsecs > 0)'
+);
+
+// Verify that also a regression by 100ns increments the clockseq
+tn = 0;
+var uidtn = uuid.v1({msecs: t, nsecs: tn});
+var uidtnb = uuid.v1({msecs: t, nsecs: tn-1});
+assert(
+ parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
+ 'IDs created at t and t - 100ns have different clockseq (nsec = 0)'
+);
+// Verify that also a regression by 100ns increments the clockseq, also passing
+// a ms-tick
+tn = 100;
+var uidtn = uuid.v1({msecs: t, nsecs: tn});
+var uidtnb = uuid.v1({msecs: t, nsecs: tn-1});
+assert(
+ parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
+ 'IDs created at t and t - 100ns have different clockseq (nsec > 0)'
+);
+
+// Set everything explicitly
var id = uuid.v1({
msecs: 1321651533573,
nsecs: 5432,
View
12 uuid.js
@@ -134,28 +134,32 @@
// additional 100-nanosecond unit offset (set below)
var msecs = (options.msecs || new Date().getTime()) + EPOCH_OFFSET;
- if (msecs < _last) {
+ // Additional 100-nanosecond units offset
+ var nsecs = options.nsecs || _count;
+
+ if (msecs + nsecs < _last) {
// Clock regression - Per 4.2.1.2, increment clock seq
_clockSeq = (_clockSeq + 1) & 0x3fff
_count = 0;
} else {
// Per 4.2.1.2, use a count of uuid's generated during the current
// clock cycle to simulate higher resolution clock
- _count = (msecs == _last) ? _count + 1 : 0;
+ _count = (msecs + nsecs == _last) ? _count + 1 : 0;
}
- _last = msecs;
// Per 4.2.1.2 If generator creates more than one id per uuid 100-ns
// interval, throw an error
// (Requires generating > 10M uuids/sec. While unlikely, it's not
// entirely inconceivable given the benchmark results we're getting)
- if (_count >= 10000) {
+ if (_count >= 10000 || (options.nsecs !== undefined && _count > 0)) {
throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
}
// Additional 100-nanosecond units offset
var nsecs = options.nsecs || _count;
+ _last = msecs + nsecs;
+
// Per 4.1.4, timestamp composition
// time_low

0 comments on commit c2649cf

Please sign in to comment.