Software /
code /
prosody
Comparison
spec/util_uuid_spec.lua @ 13317:e6a5f196fc1f
util.uuid: Add UUIDv7
Allows sorting by id as a substitute for sorting by timestamp since it
has the timestamp in the encoded in the first part, and only things that
happen extremely close together may get out of order by such a sort,
which might not matter.
From draft-ietf-uuidrev-rfc4122bis formerly draft-peabody-dispatch-new-uuid-format
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 15 Aug 2021 14:44:21 +0200 |
parent | 12604:bd9e006a7a74 |
comparison
equal
deleted
inserted
replaced
13316:a27a329e93ca | 13317:e6a5f196fc1f |
---|---|
18 }, "%-") .. "$"; | 18 }, "%-") .. "$"; |
19 | 19 |
20 for _ = 1, 100 do | 20 for _ = 1, 100 do |
21 assert.is_string(uuid.generate():match(pattern)); | 21 assert.is_string(uuid.generate():match(pattern)); |
22 end | 22 end |
23 | |
24 assert.truthy(uuid.generate() ~= uuid.generate(), "does not generate the same UUIDv4 twice") | |
25 end); | |
26 end); | |
27 describe("#v7", function() | |
28 it("should also follow the UUID pattern", function() | |
29 local pattern = "^" .. table.concat({ | |
30 string.rep("%x", 8), | |
31 string.rep("%x", 4), | |
32 "7" .. -- version | |
33 string.rep("%x", 3), | |
34 "[89ab]" .. -- reserved bits of 1 and 0 | |
35 string.rep("%x", 3), | |
36 string.rep("%x", 12), | |
37 }, "%-") .. "$"; | |
38 | |
39 local one = uuid.v7(); -- one before the loop to ensure some time passes | |
40 for _ = 1, 100 do | |
41 assert.is_string(uuid.v7():match(pattern)); | |
42 end | |
43 -- one after the loop when some time should have passed | |
44 assert.truthy(one < uuid.v7(), "should be ordererd") | |
23 end); | 45 end); |
24 end); | 46 end); |
25 end); | 47 end); |