-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathIdGeneratorTests.cs
264 lines (222 loc) · 10.3 KB
/
IdGeneratorTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using IdGen;
using IdGenTests.Mocks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Linq;
namespace IdGenTests;
[TestClass]
public class IdGeneratorTests
{
[TestMethod]
public void Sequence_ShouldIncrease_EveryInvocation()
{
// 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());
Assert.AreEqual(2, g.CreateId());
}
[TestMethod]
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());
}
[TestMethod]
public void GeneratorId_ShouldBePresent_InID1()
{
// We setup our generator so that the time is 0 and generator id equals 1023 so that all 10 bits are set
// for the generator.
var ts = new MockTimeSource(0);
var g = new IdGenerator(1023, new IdGeneratorOptions(timeSource: ts));
// Make sure all expected bits are set
Assert.AreEqual(((1 << g.Options.IdStructure.GeneratorIdBits) - 1) << g.Options.IdStructure.SequenceBits, g.CreateId());
}
[TestMethod]
public void GeneratorId_ShouldBePresent_InID2()
{
// We setup our generator so that the time is 0 and generator id equals 4095 so that all 12 bits are set
// for the generator.
var ts = new MockTimeSource();
var s = new IdStructure(40, 12, 11); // We use a custom IdStructure with 12 bits for the generator this time
var g = new IdGenerator(4095, new IdGeneratorOptions(s, ts));
// Make sure all expected bits are set
Assert.AreEqual(-1 & ((1 << 12) - 1), g.Id);
Assert.AreEqual(((1 << 12) - 1) << 11, g.CreateId());
}
[TestMethod]
public void GeneratorId_ShouldBeMasked_WhenReadFromProperty()
{
// We setup our generator so that the time is 0 and generator id equals 1023 so that all 10 bits are set
// for the generator.
var ts = new MockTimeSource();
var g = new IdGenerator(1023, new IdGeneratorOptions(timeSource: ts));
// Make sure all expected bits are set
Assert.AreEqual((1 << g.Options.IdStructure.GeneratorIdBits) - 1, g.Id);
}
[TestMethod]
public void Constructor_DoesNotThrow_OnMaxGeneratorId()
{
var structure = new IdStructure(41, 10, 12);
// 1023 is the max generator id for 10 bits.
var maxgeneratorid = 1023;
_ = new IdGenerator(maxgeneratorid, new IdGeneratorOptions(structure));
}
[TestMethod]
public void Constructor_DoesNotThrow_OnGeneratorId_0()
=> _ = new IdGenerator(0, new IdGeneratorOptions(new IdStructure(41, 10, 12)));
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void Constructor_Throws_OnNull_Options()
=> new IdGenerator(1024, null!);
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Constructor_Throws_OnInvalidGeneratorId_Positive_MaxPlusOne()
{
var structure = new IdStructure(41, 10, 12);
// 1023 is the max generator id for 10 bits.
var maxgeneratorid = 1023;
var maxPlusOne = maxgeneratorid + 1;
_ = new IdGenerator(maxPlusOne, new IdGeneratorOptions(structure));
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Constructor_Throws_OnInvalidGeneratorId_Negative()
=> new IdGenerator(-1);
[TestMethod]
public void Constructor_DoesNotThrow_OnMaxValidatorId()
=> _ = new IdGenerator(int.MaxValue, new IdGeneratorOptions { IdStructure = new IdStructure(16, 31, 16) });
[TestMethod]
public void Constructor_UsesCorrectId()
=> Assert.AreEqual(42, new IdGenerator(42).Id);
[TestMethod]
[ExpectedException(typeof(SequenceOverflowException))]
public void CreateId_Throws_OnSequenceOverflow()
{
var ts = new MockTimeSource();
var s = new IdStructure(41, 20, 2);
var g = new IdGenerator(0, new IdGeneratorOptions(idStructure: s, timeSource: ts));
// We have a 2-bit sequence; generating 4 id's shouldn't be a problem
for (var i = 0; i < 4; i++)
{
Assert.AreEqual(i, g.CreateId());
}
// However, if we invoke once more we should get an SequenceOverflowException
g.CreateId();
}
[TestMethod]
public void TryCreateId_Returns_False_OnSequenceOverflow()
{
var ts = new MockTimeSource();
var s = new IdStructure(41, 20, 2);
var g = new IdGenerator(0, new IdGeneratorOptions(idStructure: s, timeSource: ts));
// We have a 2-bit sequence; generating 4 id's shouldn't be a problem
for (var i = 0; i < 4; i++)
{
Assert.IsTrue(g.TryCreateId(out var _));
}
// However, if we invoke once more we should get an SequenceOverflowException
// which should be indicated by the false return value
Assert.IsFalse(g.TryCreateId(out var _));
}
[TestMethod]
public void Enumerable_ShoudReturn_Ids()
{
var g = new IdGenerator(0, IdGeneratorOptions.Default);
var ids = g.Take(1000).ToArray();
Assert.AreEqual(1000, ids.Distinct().Count());
}
[TestMethod]
public void Enumerable_ShoudReturn_Ids_InterfaceExplicit()
{
var g = (IEnumerable)new IdGenerator(0, IdGeneratorOptions.Default);
var ids = g.OfType<long>().Take(1000).ToArray();
Assert.AreEqual(1000, ids.Distinct().Count());
}
[TestMethod]
[ExpectedException(typeof(InvalidSystemClockException))]
public void CreateId_Throws_OnClockBackwards()
{
var ts = new MockTimeSource(100);
var g = new IdGenerator(0, new IdGeneratorOptions(timeSource: ts));
g.CreateId();
ts.PreviousTick(); // Set clock back 1 'tick', this results in the time going from "100" to "99"
g.CreateId();
}
[TestMethod]
public void TryCreateId_Returns_False_OnClockBackwards()
{
var ts = new MockTimeSource(100);
var g = new IdGenerator(0, new IdGeneratorOptions(timeSource: ts));
Assert.IsTrue(g.TryCreateId(out var _));
ts.PreviousTick(); // Set clock back 1 'tick', this results in the time going from "100" to "99"
Assert.IsFalse(g.TryCreateId(out var _));
}
[TestMethod]
[ExpectedException(typeof(InvalidSystemClockException))]
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
}
[TestMethod]
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
}
[TestMethod]
public void FromId_Returns_CorrectValue()
{
var s = new IdStructure(42, 8, 13);
var epoch = new DateTimeOffset(2018, 7, 31, 14, 48, 2, TimeSpan.FromHours(2)); // Just some "random" epoch...
var ts = new MockTimeSource(5, TimeSpan.FromSeconds(7), epoch); // Set clock at 5 ticks; each tick being 7 seconds...
// Set generator ID to 234
var g = new IdGenerator(234, new IdGeneratorOptions(s, ts));
// Generate a bunch of id's
long id = 0;
for (var i = 0; i < 35; i++)
{
id = g.CreateId();
}
var target = g.FromId(id);
Assert.AreEqual(34, target.SequenceNumber); // We generated 35 id's in the same tick, so sequence should be at 34.
Assert.AreEqual(234, target.GeneratorId); // With generator id 234
Assert.AreEqual(epoch.Add(TimeSpan.FromSeconds(5 * 7)), target.DateTimeOffset); // And the clock was at 5 ticks, with each tick being
// 7 seconds (so 35 seconds from epoch)
// And epoch was 2018-7-31 14:48:02 +02:00...
}
[TestMethod]
public void CreateId_Waits_OnSequenceOverflow()
{
// Use timesource that generates a new tick every 10 calls to GetTicks()
var ts = new MockAutoIncrementingIntervalTimeSource(10);
var s = new IdStructure(61, 0, 2);
var g = new IdGenerator(0, new IdGeneratorOptions(idStructure: s, timeSource: ts, sequenceOverflowStrategy: SequenceOverflowStrategy.SpinWait));
// We have a 2-bit sequence; generating 4 id's in a single time slot - wait for other then
Assert.AreEqual(0, g.CreateId());
Assert.AreEqual(1, g.CreateId());
Assert.AreEqual(2, g.CreateId());
Assert.AreEqual(3, g.CreateId());
Assert.AreEqual(4, g.CreateId()); // This should trigger a spinwait and return the next ID
Assert.AreEqual(5, g.CreateId());
}
}