public void CreateId_Throws_OnTimestampWraparound() { var ts = new MockTimeSource(long.MaxValue); // Set clock to 1 'tick' before wraparound var g = new IdGenerator(0, new IdGeneratorOptions(timeSource: ts)); Assert.IsTrue(g.CreateId() > 0); // Should succeed; ts.NextTick(); g.CreateId(); // Should fail }
public void TryCreateId_Returns_False_OnTimestampWraparound() { var ts = new MockTimeSource(long.MaxValue); // Set clock to 1 'tick' before wraparound var g = new IdGenerator(0, new IdGeneratorOptions(timeSource: ts)); Assert.IsTrue(g.TryCreateId(out var _)); // Should succeed; ts.NextTick(); Assert.IsFalse(g.TryCreateId(out var _)); // Should fail }
public void Constructor_Throws_OnTimestampWraparound() { var m = MaskConfig.Default; var ts = new MockTimeSource(long.MaxValue); // Set clock to 1 'tick' before wraparound var g = new IdGenerator(0, TESTEPOCH, m, ts); Assert.IsTrue(g.CreateId() > 0); // Should succeed; ts.NextTick(); g.CreateId(); // Should fail }
public void Constructor_Throws_OnTimestampWraparound() { var mc = MaskConfig.Default; var ts = new MockTimeSource(mc.WraparoundDate(TESTEPOCH).AddMilliseconds(-1)); //Set clock to 1 ms before epoch var g = new IdGenerator(0, TESTEPOCH, MaskConfig.Default, ts); Assert.IsTrue(g.CreateId() > 0); //Should succeed; ts.NextTick(); g.CreateId(); //Should fail }
public void Constructor_Throws_OnTimestampWraparound() { var mc = MaskConfig.Default; var ts = new MockTimeSource(mc.WraparoundDate(TESTEPOCH).AddMilliseconds(-1)); //Set clock to 1 ms before epoch var g = new IdGenerator(0, TESTEPOCH, MaskConfig.Default, ts); Assert.IsTrue(g.CreateId() > 0); //Should succeed; ts.NextTick(); g.CreateId(); //Should fail }
public void Sequence_ShouldReset_EveryNewTick() { // We setup our generator so that the time is 0, generator id 0 and we're only left with the sequence // increasing each invocation of CreateId(); var ts = new MockTimeSource(0); var g = new IdGenerator(0, new IdGeneratorOptions(timeSource: ts)); Assert.AreEqual(0, g.CreateId()); Assert.AreEqual(1, g.CreateId()); ts.NextTick(); // Since the timestamp has increased, we should now have a much higher value (since the timestamp is // shifted left a number of bits (specifically GeneratorIdBits + SequenceBits) Assert.AreEqual((1 << (g.Options.IdStructure.GeneratorIdBits + g.Options.IdStructure.SequenceBits)) + 0, g.CreateId()); Assert.AreEqual((1 << (g.Options.IdStructure.GeneratorIdBits + g.Options.IdStructure.SequenceBits)) + 1, g.CreateId()); }
public void Sequence_ShouldReset_EveryNewTick() { // We setup our generator so that the time (current - epoch) results in 0, generator id 0 and we're only // left with the sequence increasing each invocation of CreateId(); var ts = new MockTimeSource(TESTEPOCH); var m = MaskConfig.Default; var g = new IdGenerator(0, TESTEPOCH, m, ts); Assert.AreEqual(0, g.CreateId()); Assert.AreEqual(1, g.CreateId()); ts.NextTick(); // Since the timestamp has increased, we should now have a much higher value (since the timestamp is shifted // left a number of bits (specifically GeneratorIdBits + SequenceBits) Assert.AreEqual((1 << (m.GeneratorIdBits + m.SequenceBits)) + 0, g.CreateId()); Assert.AreEqual((1 << (m.GeneratorIdBits + m.SequenceBits)) + 1, g.CreateId()); }
public void Sequence_ShouldReset_EveryNewTick() { // We setup our generator so that the time (current - epoch) results in 0, generator id 0 and we're only // left with the sequence increasing each invocation of CreateId(); var ts = new MockTimeSource(TESTEPOCH); var m = MaskConfig.Default; var g = new IdGenerator(0, TESTEPOCH, m, ts); Assert.AreEqual(0, g.CreateId()); Assert.AreEqual(1, g.CreateId()); ts.NextTick(); // Since the timestamp has increased, we should now have a much higher value (since the timestamp is shifted // left a number of bits (specifically GeneratorIdBits + SequenceBits) Assert.AreEqual((1 << (m.GeneratorIdBits + m.SequenceBits)) + 0, g.CreateId()); Assert.AreEqual((1 << (m.GeneratorIdBits + m.SequenceBits)) + 1, g.CreateId()); }