public void should_generate_different_ids_in_a_short_period() { var snowflake = new Snowflake(); var lastId = snowflake.Next(); var nextId = snowflake.Next(); nextId.Should().NotBe(lastId); }
public void should_have_same_machine_id_in_all_generated_ids() { var snowflake = new Snowflake(); var lastId = snowflake.Next(); var nextId = snowflake.Next(); var lastMachineId = lastId & MachineIdMask; var nextMachineId = nextId & MachineIdMask; nextMachineId.Should().Be(lastMachineId); }
public void should_generate_equal_or_greater_timestamp_in_id_when_clock_shifted_back() { var now = DateTime.UtcNow; var clockMock = new Mock <IClock>(); clockMock.SetupSequence(c => c.UtcNow) .Returns(now) .Returns(now.AddSeconds(-1)); var snowflake = new Snowflake(clock: clockMock.Object, timestampOffset: now.AddMinutes(-1)); var firstId = snowflake.Next(); var lastId = snowflake.Next(); (lastId & TimestampMask).Should().BeGreaterOrEqualTo(firstId & TimestampMask); }
public async Task should_have_same_specified_machine_id_in_all_generated_ids(string machineIdScript) { var machineId = await CSharpScript.EvaluateAsync <int>(machineIdScript, ScriptOptions.Default.WithReferences(typeof(Snowflake).Assembly)); var snowflake = new Snowflake(machineId); var lastId = snowflake.Next(); var nextId = snowflake.Next(); var idWithOnlyMachineId = ((long)machineId) << (64 - MachineIdOffset - MachineIdLength); (lastId & MachineIdMask).Should().Be(idWithOnlyMachineId); (nextId & MachineIdMask).Should().Be(idWithOnlyMachineId); }
public EventMessage(string endpoint, EventFlowType flow, IEnumerable <EventParameter> parameters) { Id = Snowflake.Next(); Endpoint = endpoint; Flow = flow; Parameters = parameters; }
public void should_generate_positive_number() { var snowflake = new Snowflake(); var nextId = snowflake.Next(); nextId.Should().BeGreaterThan(0); }
public void should_generate_larger_next_id_when_the_last_id_was_generated_100_years_ago() { var now = DateTime.UtcNow; var lastTimestamp = now; var nextTimestamp = now.AddYears(100); var clockMock = new Mock <IClock>(); clockMock.SetupSequence(c => c.UtcNow) .Returns(lastTimestamp) .Returns(nextTimestamp); var snowflake = new Snowflake(clock: clockMock.Object, timestampOffset: now); var lastId = snowflake.Next(); var nextId = snowflake.Next(); nextId.Should().BeGreaterThan(lastId); }
public void should_generate_one_id_in_less_than_one_millisecond() { var snowflake = new Snowflake(); // ReSharper disable once UnusedVariable var warmupId = snowflake.Next(); var stopwatch = new Stopwatch(); stopwatch.Start(); // ReSharper disable once UnusedVariable var id = snowflake.Next(); var elapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds; #if DEBUG var limit = 3; #else var limit = 1; #endif elapsedMilliseconds.Should().BeLessThan(limit); }
public void should_generate_4096_unique_ids_in_less_than_100_millisecond() { var snowflake = new Snowflake(); // ReSharper disable once UnusedVariable var warmupId = snowflake.Next(); var stopwatch = new Stopwatch(); stopwatch.Start(); var ids = Enumerable.Range(1, 4096) .Select(i => snowflake.Next()) .AsParallel() .ToArray(); var elapsed = stopwatch.Elapsed; #if DEBUG var limit = 200; #else var limit = 100; #endif elapsed.TotalMilliseconds.Should().BeLessThan(limit); var distinctIds = ids.Distinct(); distinctIds.Count().Should().Be(4096); }
public EventHandler(string endpoint, Delegate @delegate) { Id = Snowflake.Next(); Endpoint = endpoint; Delegate = @delegate; }