1 | var assert = require('assert');
|
2 |
|
3 | var uuid = require('../');
|
4 |
|
5 |
|
6 | var TIME = 1321644961388;
|
7 |
|
8 | function compare(name, ids) {
|
9 | test(name, function() {
|
10 |
|
11 | for (var i=0 ; i<ids.length ; ++i) {
|
12 | ids[i] = ids[i].split('-').reverse().join('-');
|
13 | }
|
14 | ids = ids.sort();
|
15 | var sorted = ([].concat(ids)).sort();
|
16 |
|
17 | assert(sorted.toString() == ids.toString(), name + ' have expected order');
|
18 | });
|
19 | }
|
20 |
|
21 |
|
22 | compare('uuids with current time', [
|
23 | uuid.v1(),
|
24 | uuid.v1(),
|
25 | uuid.v1(),
|
26 | uuid.v1(),
|
27 | uuid.v1()
|
28 | ]);
|
29 |
|
30 |
|
31 | compare('uuids with time option', [
|
32 | uuid.v1({msecs: TIME - 10*3600*1000}),
|
33 | uuid.v1({msecs: TIME - 1}),
|
34 | uuid.v1({msecs: TIME}),
|
35 | uuid.v1({msecs: TIME + 1}),
|
36 | uuid.v1({msecs: TIME + 28*24*3600*1000})
|
37 | ]);
|
38 |
|
39 | test('msec', function() {
|
40 | assert(
|
41 | uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),
|
42 | 'IDs created at same msec are different'
|
43 | );
|
44 | });
|
45 |
|
46 | test('exception thrown when > 10k ids created in 1ms', function() {
|
47 |
|
48 | var thrown = false;
|
49 | try {
|
50 | uuid.v1({msecs: TIME, nsecs: 10000});
|
51 | } catch (e) {
|
52 | thrown = true;
|
53 | }
|
54 | assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
|
55 | });
|
56 |
|
57 | test('clock regression by msec', function() {
|
58 |
|
59 | var uidt = uuid.v1({msecs: TIME});
|
60 | var uidtb = uuid.v1({msecs: TIME - 1});
|
61 | assert(
|
62 | parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
|
63 | 'Clock regression by msec increments the clockseq'
|
64 | );
|
65 | });
|
66 |
|
67 | test('clock regression by nsec', function() {
|
68 |
|
69 | var uidtn = uuid.v1({msecs: TIME, nsecs: 10});
|
70 | var uidtnb = uuid.v1({msecs: TIME, nsecs: 9});
|
71 | assert(
|
72 | parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
|
73 | 'Clock regression by nsec increments the clockseq'
|
74 | );
|
75 | });
|
76 |
|
77 | test('explicit options product expected id', function() {
|
78 |
|
79 | var id = uuid.v1({
|
80 | msecs: 1321651533573,
|
81 | nsecs: 5432,
|
82 | clockseq: 0x385c,
|
83 | node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
|
84 | });
|
85 | assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
|
86 | });
|
87 |
|
88 | test('ids spanning 1ms boundary are 100ns apart', function() {
|
89 |
|
90 | var u0 = uuid.v1({msecs: TIME, nsecs: 9999});
|
91 | var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0});
|
92 |
|
93 | var before = u0.split('-')[0], after = u1.split('-')[0];
|
94 | var dt = parseInt(after, 16) - parseInt(before, 16);
|
95 | assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
|
96 | });
|