public void GeneratedUUID_ShouldHaveProperVersion(GuidGenerationMode mode) { var generator = new TimeBasedGenerator(); var expectedVersion = 0x10; var guid = generator.GenerateGuid(mode); var array = guid.ToByteArray(); Assert.Equal(expectedVersion, array[7] & 0xf0); }
/// <summary> /// Generates a RFC4122 time based UUID in the give <paramref name="mode"/>. /// </summary> /// <param name="mode">Use <c>UUIDGenerationMode.FasterGeneration</c> for faster UUID generation, without synchronizing the system clock. /// Use <c>UUIDGenerationMode.WithUniquenessGuarantee</c> to synchronize system clock. Later approach may be slower than the former one.</param> /// <returns></returns> public Guid GenerateGuid(GuidGenerationMode mode) { if (mode == GuidGenerationMode.FasterGeneration) { var clockSequenceData = ReadClockSequenceBytes(); return(GenerateGuid(DateTimeOffset.UtcNow, clockSequenceData, macAddressBytes)); } var now = DateTimeOffset.UtcNow; if (now <= lastClockSyncedAt) { lock (mutex) { if (now <= lastClockSyncedAt) { UpdateClockSequenceBytes(); lastClockSyncedAt = now; } } } return(GenerateGuid(now, ReadClockSequenceBytes(), macAddressBytes)); }