本文整理汇总了C#中IdGenerator.CreateId方法的典型用法代码示例。如果您正苦于以下问题:C# IdGenerator.CreateId方法的具体用法?C# IdGenerator.CreateId怎么用?C# IdGenerator.CreateId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IdGenerator
的用法示例。
在下文中一共展示了IdGenerator.CreateId方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Constructor_Throws_OnTimestampWraparound
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
}
示例2: Sequence_ShouldReset_EveryNewTick
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());
}
示例3: Sequence_ShouldIncrease_EveryInvocation
public void Sequence_ShouldIncrease_EveryInvocation()
{
// 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, MaskConfig.Default, ts);
Assert.AreEqual(0, g.CreateId());
Assert.AreEqual(1, g.CreateId());
Assert.AreEqual(2, g.CreateId());
}
示例4: GeneratorId_ShouldBePresent_InID2
public void GeneratorId_ShouldBePresent_InID2()
{
// We setup our generator so that the time (current - epoch) results in 0, generator id 4095 so that all 12 bits
// are set for the generator.
var ts = new MockTimeSource(TESTEPOCH);
var m = new MaskConfig(40, 12, 11); // We use a custom mask-config with 12 bits for the generator this time
var g = new IdGenerator(4095, TESTEPOCH, m, ts);
// Make sure all expected bits are set
Assert.AreEqual(-1 & ((1 << 12) - 1), g.Id);
Assert.AreEqual((1 << 12) - 1 << 11, g.CreateId());
}
示例5: GeneratorId_ShouldBePresent_InID1
public void GeneratorId_ShouldBePresent_InID1()
{
// We setup our generator so that the time (current - epoch) results in 0, generator id 1023 so that all 10 bits
// are set for the generator.
var ts = new MockTimeSource(TESTEPOCH);
var m = MaskConfig.Default; // We use a default mask-config with 11 bits for the generator this time
var g = new IdGenerator(1023, TESTEPOCH, m, ts);
// Make sure all expected bits are set
Assert.AreEqual((1 << m.GeneratorIdBits) - 1 << m.SequenceBits, g.CreateId());
}
示例6: CreateId_Throws_OnSequenceOverflow
public void CreateId_Throws_OnSequenceOverflow()
{
var ts = new MockTimeSource(TESTEPOCH);
var g = new IdGenerator(0, TESTEPOCH, new MaskConfig(41, 20, 2), ts);
// We have a 2-bit sequence; generating 4 id's shouldn't be a problem
for (int i = 0; i < 4; i++)
Assert.AreEqual(i, g.CreateId());
// However, if we invoke once more we should get an SequenceOverflowException
g.CreateId();
}
示例7: CreateId_Throws_OnClockBackwards
public void CreateId_Throws_OnClockBackwards()
{
var ts = new MockTimeSource(DateTime.UtcNow);
var m = MaskConfig.Default;
var g = new IdGenerator(0, TESTEPOCH, m, ts);
g.CreateId();
ts.PreviousTick(); //Set clock back 1 'tick' (ms)
g.CreateId();
}