-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgenerator_test.go
More file actions
923 lines (752 loc) · 22.7 KB
/
generator_test.go
File metadata and controls
923 lines (752 loc) · 22.7 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
// +build test
package sno
import (
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
_ "unsafe"
"github.com/muyo/sno/internal"
)
// snotime is the actual time source used by Generators during tests.
//
// We split on build tags ("test") to swap out the snotime() implementations provided by platform specific
// code so that tests can use mocked time sources without in any way impacting a Generator's runtime performance
// in production builds.
//
// Note: Attempting to run the test suite without the "test" build tag will fail, resulting in several
// compilation errors.
var snotime = internal.Snotime
// monotime provides real monotonic clock readings to several tests.
//go:linkname monotime runtime.nanotime
func monotime() int64
// staticTime provides tests with a fake time source which returns a fixed time on each call.
// The time returned can be changed by directly (atomically) mutating the underlying variable.
func staticTime() uint64 {
return atomic.LoadUint64(staticWallNow)
}
// staticIncTime provides tests with a fake time source which returns a time based on a fixed time
// monotonically increasing by 1 TimeUnit on each call.
func staticIncTime() uint64 {
wall := atomic.LoadUint64(staticWallNow) + atomic.LoadUint64(staticInc)*TimeUnit
atomic.AddUint64(staticInc, 1)
return wall
}
var (
staticInc = new(uint64)
staticWallNow = func() *uint64 {
wall := snotime()
return &wall
}()
)
func TestGenerator_NewNoOverflow(t *testing.T) {
var (
part = Partition{255, 255}
seqPool = uint16(MaxSequence / 2)
seqMin = seqPool
seqMax = 2*seqPool - 1
// Scaled to not exceed bounds, otherwise we run into the seqOverflow race and order - which we
// test for in here - becomes non-deterministic.
sampleSize = int(seqPool)
g, err = NewGenerator(&GeneratorSnapshot{
Partition: part,
SequenceMin: seqMin,
SequenceMax: seqMax,
}, nil)
)
if err != nil {
t.Fatal(err)
}
ids := make([]ID, sampleSize)
for i := 0; i < sampleSize; i++ {
ids[i] = g.New(byte(i))
}
for i := 1; i < sampleSize; i++ {
curID, prevID := ids[i], ids[i-1]
seq := ids[i].Sequence()
if seq > seqMax {
t.Errorf("%d: sequence overflowing max boundary; max [%d], got [%d]", i, seqMin, seq)
}
if seq < seqMin {
t.Errorf("%d: sequence underflowing min boundary; min [%d], got [%d]", i, seqMin, seq)
}
// We're expecting the time to increment and never more than by one time unit, since
// we generated them in sequence.
timeDiff := curID.Timestamp() - prevID.Timestamp()
// Check if drift got applied in this edge case.
if timeDiff < 0 && curID[4]&1 == 0 {
t.Error("timestamp of next ID lower than previous and no tick-tock applied")
}
if timeDiff > TimeUnit {
t.Error("timestamp diff between IDs is higher than by one time unit")
}
if prevID.Partition() != part {
t.Errorf("%d: partition differs from generator's partition; expected [%d], got [%d]", i, part, prevID.Partition())
}
}
}
func TestGenerator_NewOverflows(t *testing.T) {
var (
part = Partition{255, 255}
seqPool = 512
seqOverflows = 16
seqMin = uint16(seqPool)
seqMax = uint16(2*seqPool - 1)
sampleSize = seqPool * seqOverflows
c = make(chan *SequenceOverflowNotification)
cc = make(chan struct{})
notesHi = new(int64)
g, err = NewGenerator(&GeneratorSnapshot{
Partition: part,
SequenceMin: seqMin,
SequenceMax: seqMax,
}, c)
)
if err != nil {
t.Fatal(err)
}
go func() {
for {
select {
case note := <-c:
if note.Count > 0 {
atomic.AddInt64(notesHi, 1)
}
case <-cc:
return
}
}
}()
ids := make([]ID, sampleSize)
for i := 0; i < sampleSize; i++ {
ids[i] = g.New(byte(i))
}
close(cc)
// TODO(alcore) The non-blocking writes are far from reliable. The notifications need a rework with
// deep profiling.
if atomic.LoadInt64(notesHi) < int64(seqOverflows)/4 {
t.Errorf("expected at least [%d] overflow notification, got [%d]", seqOverflows/4, atomic.LoadInt64(notesHi))
}
timeDist := make(map[int64]int)
for i := 0; i < sampleSize; i++ {
id := ids[i]
timeDist[id.Timestamp()]++
seq := id.Sequence()
if seq > seqMax {
t.Errorf("%d: sequence overflowing max boundary; max [%d], got [%d]", i, seqMin, seq)
}
if seq < seqMin {
t.Errorf("%d: sequence underflowing min boundary; min [%d], got [%d]", i, seqMin, seq)
}
if id.Partition() != part {
t.Errorf("%d: partition differs from generator's partition; expected [%d], got [%d]", i, part, id.Partition())
}
}
for tf, c := range timeDist {
if c > seqPool {
t.Errorf("count of IDs in the given timeframe exceeds pool; timestamp [%d], pool [%d], count [%d]", tf, seqPool, c)
}
}
}
func TestGenerator_NewTickTocks(t *testing.T) {
g, ids := testGeneratorNewTickTocksSetup(t)
t.Run("Tick", testGeneratorNewTickTocksTick(g, ids))
t.Run("SafetySlumber", testGeneratorNewTickTocksSafetySlumber(g, ids))
t.Run("Tock", testGeneratorNewTickTocksTock(g, ids))
t.Run("Race", testGeneratorNewTickTocksRace(g, ids))
}
func testGeneratorNewTickTocksSetup(t *testing.T) (*Generator, []ID) {
var (
seqPool = 8096
g, err = NewGenerator(&GeneratorSnapshot{
Partition: Partition{255, 255},
SequenceMin: uint16(seqPool),
SequenceMax: uint16(2*seqPool - 1),
}, nil)
)
if err != nil {
t.Fatal(err)
}
return g, make([]ID, g.Cap())
}
func testGeneratorNewTickTocksTick(g *Generator, ids []ID) func(*testing.T) {
return func(t *testing.T) {
// First batch follows normal time progression.
for i := 0; i < 512; i++ {
ids[i] = g.New(255)
}
wall := snotime()
atomic.StoreUint64(staticWallNow, wall-TimeUnit)
// Swap out the time source. Next batch is supposed to set a drift, have their tick-tock bit
// set to 1, and wallSafe on the generator must be set accordingly.
snotime = staticTime
if atomic.LoadUint32(&g.drifts) != 0 {
t.Errorf("expected [0] drifts recorded, got [%d]", atomic.LoadUint32(&g.drifts))
}
if atomic.LoadUint64(&g.wallSafe) != 0 {
t.Errorf("expected wallSafe to be [0], is [%d]", atomic.LoadUint64(&g.wallSafe))
}
for j := 512; j < 1024; j++ {
ids[j] = g.New(255)
}
if atomic.LoadUint32(&g.drifts) != 1 {
t.Errorf("expected [1] drift recorded, got [%d]", atomic.LoadUint32(&g.drifts))
}
if atomic.LoadUint64(&g.wallSafe) == atomic.LoadUint64(staticWallNow) {
t.Errorf("expected wallSafe to be [%d], was [%d]", atomic.LoadUint64(staticWallNow), atomic.LoadUint64(&g.wallSafe))
}
for i := 0; i < 512; i++ {
if ids[i][4]&1 != 0 {
t.Errorf("%d: expected tick-tock bit to not be set, was set", i)
}
}
for j := 512; j < 1024; j++ {
if ids[j][4]&1 != 1 {
t.Errorf("%d: expected tick-tock bit to be set, was not", j)
}
}
snotime = internal.Snotime
}
}
func testGeneratorNewTickTocksSafetySlumber(g *Generator, ids []ID) func(*testing.T) {
return func(t *testing.T) {
// Multi-regression, checking on a single goroutine.
atomic.AddUint64(staticWallNow, ^uint64(TimeUnit-1))
// Use a clock where the first call will return the static clock times
// but subsequent calls will return higher times. Since we didn't adjust the mono clock
// at all insofar, it's currently 1 TimeUnit (first drift) behind wallSafe, which got set
// during the initial drift. This is the time the next generation call(s) are supposed
// to sleep, as we are simulating a multi-regression (into an unsafe past where can't
// tick-tock again until reaching wallSafe).
snotime = staticIncTime
mono1 := monotime()
id := g.New(255)
if id[4]&1 != 1 {
t.Errorf("expected tick-tock bit to be set, was not")
}
mono2 := monotime()
monoDiff := mono2 - mono1
// We had 2 regressions by 1 TimeUnit each, so sleep duration should've been roughly
// the same since time was static (got incremented only after the sleep).
if monoDiff < 2*TimeUnit {
t.Errorf("expected to sleep for at least [%f]ns, took [%d] instead", 2*TimeUnit, monoDiff)
} else if monoDiff > 5*TimeUnit {
t.Errorf("expected to sleep for no more than [%f]ns, took [%d] instead", 5*TimeUnit, monoDiff)
}
if atomic.LoadUint32(&g.drifts) != 1 {
t.Errorf("expected [1] drift recorded, got [%d]", atomic.LoadUint32(&g.drifts))
}
snotime = internal.Snotime
}
}
func testGeneratorNewTickTocksTock(g *Generator, ids []ID) func(*testing.T) {
return func(t *testing.T) {
// At this point we are going to simulate another drift, somewhere in the 'far' future,
// with parallel load.
snotime = staticTime
atomic.AddUint64(staticWallNow, 100*TimeUnit)
g.New(255) // Updates wallHi
// Regress again. Not adjusting mono clock - calls below are supposed to simply drift - drift
// count is supposed to end at 2 (since we're still using the same generator) and tick-tock
// bit is supposed to be unset.
atomic.AddUint64(staticWallNow, ^uint64(2*TimeUnit-1))
var (
batchCount = 4
batchSize = g.Cap() / batchCount
wg sync.WaitGroup
)
wg.Add(batchCount)
for i := 0; i < batchCount; i++ {
go func(mul int) {
for i := mul * batchSize; i < mul*batchSize+batchSize; i++ {
ids[i] = g.New(255)
}
wg.Done()
}(i)
}
wg.Wait()
if atomic.LoadUint32(&g.drifts) != 2 {
t.Errorf("expected [2] drifts recorded, got [%d]", atomic.LoadUint32(&g.drifts))
}
for i := 0; i < g.Cap(); i++ {
if ids[i][4]&1 != 0 {
t.Errorf("%d: expected tick-tock bit to not be set, was set", i)
}
}
snotime = internal.Snotime
}
}
func testGeneratorNewTickTocksRace(g *Generator, ids []ID) func(*testing.T) {
return func(*testing.T) {
snotime = staticTime
atomic.AddUint64(staticWallNow, 100*TimeUnit)
g.New(255)
atomic.AddUint64(staticWallNow, ^uint64(TimeUnit-1))
var (
wgOuter sync.WaitGroup
wgInner sync.WaitGroup
)
wgOuter.Add(1000)
wgInner.Add(1000)
for i := 0; i < 1000; i++ {
go func() {
wgInner.Done()
wgInner.Wait()
for i := 0; i < 2; i++ {
_ = g.New(byte(i))
}
wgOuter.Done()
}()
}
wgOuter.Wait()
snotime = internal.Snotime
}
}
func TestGenerator_NewGeneratorRestoreRegressions(t *testing.T) {
// First one we simply check that the times get applied at all. We get rid of the time
// added while simulating the last drift.
g, err := NewGenerator(nil, nil)
if err != nil {
t.Fatal(err)
}
// Reset the static clock.
wall := snotime()
snotime = staticTime
atomic.StoreUint64(staticWallNow, wall)
// Simulate a regression.
g.New(255)
atomic.AddUint64(staticWallNow, ^uint64(TimeUnit-1))
g.New(255)
snapshot := g.Snapshot()
g, err = NewGenerator(&snapshot, nil)
if err != nil {
t.Fatal(err)
}
if uint64(snapshot.WallSafe) != atomic.LoadUint64(&g.wallSafe) {
t.Errorf("expected [%d], got [%d]", snapshot.WallSafe, atomic.LoadUint64(&g.wallSafe))
}
if uint64(snapshot.WallHi) != atomic.LoadUint64(&g.wallHi) {
t.Errorf("expected [%d], got [%d]", snapshot.WallHi, atomic.LoadUint64(&g.wallHi))
}
// Second test, with a snapshot taken "in the future" (relative to current wall clock time).
wall = internal.Snotime()
atomic.StoreUint64(staticWallNow, wall+100*TimeUnit)
// Simulate another regression. Takes place in the future - we are going to take a snapshot
// and create a generator using that snapshot, where the generator will use snotime (current time)
// as comparison and is supposed to handle this as if it is in the past relative to the snapshot.
g.New(255)
atomic.AddUint64(staticWallNow, ^uint64(TimeUnit-1))
g.New(255)
snotime = internal.Snotime
snapshot = g.Snapshot()
g, err = NewGenerator(&snapshot, nil)
if err != nil {
t.Fatal(err)
}
if uint64(snapshot.WallSafe) != atomic.LoadUint64(&g.wallSafe) {
t.Errorf("expected [%d], got [%d]", snapshot.WallSafe, atomic.LoadUint64(&g.wallSafe))
}
if wall > atomic.LoadUint64(&g.wallHi) {
t.Errorf("expected smaller than [%d], got [%d]", wall, atomic.LoadUint64(&g.wallHi))
}
}
func TestGenerator_NewWithTimeOverflows(t *testing.T) {
var (
part = Partition{255, 255}
seqPool = 12
seqOverflows = 4
seqMin = uint16(seqPool)
seqMax = uint16(2*seqPool - 1)
sampleSize = seqPool * seqOverflows
g, err = NewGenerator(&GeneratorSnapshot{
Partition: part,
SequenceMin: seqMin,
SequenceMax: seqMax,
}, nil)
)
if err != nil {
t.Fatal(err)
}
tn := time.Now()
pool := g.Cap()
ids := make([]ID, sampleSize)
for i := 0; i < sampleSize; i++ {
ids[i] = g.NewWithTime(byte(i), tn)
}
timeDist := make(map[int64]int)
for i, s := 0, 0; i < sampleSize; i, s = i+1, s+1 {
id := ids[i]
timeDist[id.Timestamp()]++
seq := id.Sequence()
if seq > seqMax {
t.Errorf("%d: sequence overflowing max boundary; max [%d], got [%d]", i, seqMin, seq)
}
if seq < seqMin {
t.Errorf("%d: sequence underflowing min boundary; min [%d], got [%d]", i, seqMin, seq)
}
// When we overflow with NewWithTime, the static sequence is supposed to roll over silently.
if s == pool {
s = 0
} else if i > 0 && seq-ids[i-1].Sequence() != 1 {
t.Errorf("%d: expected sequence to increment by 1, got [%d]", i, seq-ids[i-1].Sequence())
}
expectedSeq := uint16(s) + seqMin
if seq != expectedSeq {
t.Errorf("%d: expected sequence [%d], got [%d]", i, expectedSeq, seq)
}
if id.Partition() != part {
t.Errorf("%d: partition differs from generator's partition; expected [%d], got [%d]", i, part, id.Partition())
}
}
if len(timeDist) > 1 {
t.Error("IDs generated with the same time ended up with different timestamps")
}
// Race test.
var wg sync.WaitGroup
wg.Add(1000)
for i := 0; i < 1000; i++ {
go func() {
for i := 0; i < sampleSize; i++ {
_ = g.NewWithTime(byte(i), tn)
}
wg.Done()
}()
}
wg.Wait()
}
func TestGenerator_Uniqueness(t *testing.T) {
var (
collisions int
setSize = 4 * MaxSequence
)
ids := make(map[ID]struct{}, setSize)
for i := 1; i < setSize; i++ {
id := generator.New(255)
if _, found := ids[id]; found {
collisions++
} else {
ids[id] = struct{}{}
}
}
if collisions > 0 {
t.Errorf("generated %d colliding IDs in a set of %d", collisions, setSize)
}
}
func TestGenerator_Partition(t *testing.T) {
expected := Partition{'A', 255}
g, err := NewGenerator(&GeneratorSnapshot{
Partition: expected,
}, nil)
if err != nil {
t.Fatal(err)
}
actual := g.Partition()
if actual != expected {
t.Errorf("expected [%s], got [%s]", expected, actual)
}
}
func TestGenerator_SequenceBounds(t *testing.T) {
min := uint16(1024)
max := uint16(2047)
g, err := NewGenerator(&GeneratorSnapshot{
SequenceMin: min,
SequenceMax: max,
}, nil)
if err != nil {
t.Fatal(err)
}
if actual, expected := g.SequenceMin(), min; actual != expected {
t.Errorf("expected [%d], got [%d]", expected, actual)
}
if actual, expected := g.SequenceMax(), max; actual != expected {
t.Errorf("expected [%d], got [%d]", expected, actual)
}
if actual, expected := g.Cap(), int(max-min)+1; actual != expected {
t.Errorf("expected [%d], got [%d]", expected, actual)
}
if actual, expected := g.Len(), 0; actual != expected {
t.Errorf("expected [%d], got [%d]", expected, actual)
}
for i := 0; i < 5; i++ {
g.New(255)
}
if actual, expected := g.Len(), 5; actual != expected {
t.Errorf("expected [%d], got [%d]", expected, actual)
}
g, err = NewGenerator(&GeneratorSnapshot{
SequenceMin: 8,
SequenceMax: 16,
}, nil)
if err != nil {
t.Fatal(err)
}
// Simulate an overflow. All IDs over Cap() must be generated in a subsequent timeframe
// meaning Len will reflect the count in the last frame.
// TODO(alcore) This *can* occasionally fail as we are not using a deterministic time source,
// meaning first batch can get split up if time changes during the test and then end up
// spilling into the Len() we test for.
for i := 0; i < g.Cap()+7; i++ {
g.New(255)
}
if actual, expected := g.Len(), 7; actual != expected {
t.Errorf("expected [%d], got [%d]", expected, actual)
}
g, err = NewGenerator(&GeneratorSnapshot{
SequenceMin: 8,
SequenceMax: 16,
}, nil)
if err != nil {
t.Fatal(err)
}
for i := 0; i < g.Cap(); i++ {
g.New(255)
}
if actual, expected := g.Len(), g.Cap(); actual != expected {
t.Errorf("expected [%d], got [%d]", expected, actual)
}
}
func TestGenerator_Sequence_Single(t *testing.T) {
g, err := NewGenerator(nil, nil)
if err != nil {
t.Fatal(err)
}
expected0 := uint32(0)
expected1 := expected0
expected2 := expected1 + 1
actual0 := g.Sequence()
_ = g.New(255)
actual1 := g.Sequence()
_ = g.New(255)
actual2 := g.Sequence()
if actual0 != expected0 {
t.Errorf("expected [%d], got [%d]", expected0, actual0)
}
if actual1 != expected1 {
t.Errorf("expected [%d], got [%d]", expected1, actual1)
}
if actual2 != expected2 {
t.Errorf("expected [%d], got [%d]", expected2, actual2)
}
}
func TestGenerator_Sequence_Batch(t *testing.T) {
g, err := NewGenerator(nil, nil)
if err != nil {
t.Fatal(err)
}
expected := uint32(9)
for i := 0; i <= int(expected); i++ {
_ = g.New(255)
}
actual := g.Sequence()
if actual != expected {
t.Errorf("expected [%d], got [%d]", expected, actual)
}
}
func TestGenerator_FromSnapshot_Sequence(t *testing.T) {
seq := uint32(1024)
g, err := NewGenerator(&GeneratorSnapshot{
SequenceMin: uint16(seq),
Sequence: seq,
}, nil)
if err != nil {
t.Fatal(err)
}
expected1 := seq
expected2 := seq + 1
_ = g.New(255)
actual1 := g.Sequence()
_ = g.New(255)
actual2 := g.Sequence()
if actual1 != expected1 {
t.Errorf("expected [%d], got [%d]", expected1, actual1)
}
if actual2 != expected2 {
t.Errorf("expected [%d], got [%d]", expected2, actual2)
}
}
func TestGenerator_FromSnapshot_Pool_Defaults(t *testing.T) {
t.Parallel()
g, err := NewGenerator(&GeneratorSnapshot{
SequenceMin: 0,
SequenceMax: 0,
}, nil)
if err != nil {
t.Fatal(err)
}
if g.SequenceMin() != 0 {
t.Errorf("expected [%d], got [%d]", 0, g.SequenceMin())
}
if g.SequenceMax() != MaxSequence {
t.Errorf("expected [%d], got [%d]", MaxSequence, g.SequenceMax())
}
// Max as default when min is given.
g, err = NewGenerator(&GeneratorSnapshot{
SequenceMin: 2048,
}, nil)
if err != nil {
t.Fatal(err)
}
if g.SequenceMin() != 2048 {
t.Errorf("expected [%d], got [%d]", 2048, g.SequenceMin())
}
if g.SequenceMax() != MaxSequence {
t.Errorf("expected [%d], got [%d]", MaxSequence, g.SequenceMax())
}
}
func TestGenerator_FromSnapshot_Pool_BoundsOrder(t *testing.T) {
t.Parallel()
g, err := NewGenerator(&GeneratorSnapshot{
SequenceMin: 2048,
SequenceMax: 1024,
}, nil)
if err != nil {
t.Fatal(err)
}
if g.SequenceMin() != 1024 {
t.Errorf("expected [%d], got [%d]", 1024, g.SequenceMin())
}
if g.SequenceMax() != 2048 {
t.Errorf("expected [%d], got [%d]", 2048, g.SequenceMax())
}
}
func TestGenerator_FromSnapshot_Pool_None(t *testing.T) {
t.Parallel()
bound := uint16(2048)
_, err := NewGenerator(&GeneratorSnapshot{
SequenceMin: bound,
SequenceMax: bound,
}, nil)
if err == nil {
t.Errorf("expected error, got none")
return
}
verr, ok := err.(*InvalidSequenceBoundsError)
if !ok {
t.Errorf("expected error type [%T], got [%T]", &InvalidSequenceBoundsError{}, err)
return
}
if verr.Msg != errSequenceBoundsIdenticalMsg {
t.Errorf("expected error msg [%s], got [%s]", errSequenceBoundsIdenticalMsg, verr.Msg)
}
if verr.Min != bound {
t.Errorf("expected [%d], got [%d]", bound, verr.Min)
}
if verr.Max != bound {
t.Errorf("expected [%d], got [%d]", bound, verr.Max)
}
expectedMsg := fmt.Sprintf(errInvalidSequenceBoundsFmt, errSequenceBoundsIdenticalMsg, bound, 0, bound, 1)
if verr.Error() != expectedMsg {
t.Errorf("expected error msg [%s], got [%s]", expectedMsg, verr.Error())
}
}
func TestGenerator_FromSnapshot_Pool_Size(t *testing.T) {
t.Parallel()
seqMin := uint16(0)
seqMax := seqMin + minSequencePoolSize - 1
_, err := NewGenerator(&GeneratorSnapshot{
SequenceMin: seqMin,
SequenceMax: seqMax,
}, nil)
if err == nil {
t.Errorf("expected error, got none")
return
}
verr, ok := err.(*InvalidSequenceBoundsError)
if !ok {
t.Errorf("expected error type [%T], got [%T]", &InvalidSequenceBoundsError{}, err)
return
}
if verr.Msg != errSequencePoolTooSmallMsg {
t.Errorf("expected error msg [%s], got [%s]", errSequencePoolTooSmallMsg, verr.Msg)
}
if verr.Min != seqMin {
t.Errorf("expected [%d], got [%d]", seqMin, verr.Min)
}
if verr.Max != seqMax {
t.Errorf("expected [%d], got [%d]", seqMax, verr.Max)
}
expectedMsg := fmt.Sprintf(errInvalidSequenceBoundsFmt, errSequencePoolTooSmallMsg, seqMin, 0, seqMax, seqMax-seqMin+1)
if verr.Error() != expectedMsg {
t.Errorf("expected error msg [%s], got [%s]", expectedMsg, verr.Error())
}
}
func TestGenerator_FromSnapshot_Underflow(t *testing.T) {
t.Parallel()
seqMin := uint16(2048)
seq := uint32(seqMin - 1)
_, err := NewGenerator(&GeneratorSnapshot{
SequenceMin: seqMin,
Sequence: seq,
}, nil)
if err == nil {
t.Errorf("expected error, got none")
return
}
verr, ok := err.(*InvalidSequenceBoundsError)
if !ok {
t.Errorf("expected error type [%T], got [%T]", &InvalidSequenceBoundsError{}, err)
return
}
if verr.Msg != errSequenceUnderflowsBound {
t.Errorf("expected error msg [%s], got [%s]", errSequenceUnderflowsBound, verr.Msg)
}
if verr.Min != seqMin {
t.Errorf("expected [%d], got [%d]", seqMin, verr.Min)
}
if verr.Cur != seq {
t.Errorf("expected [%d], got [%d]", seq, verr.Cur)
}
expectedMsg := fmt.Sprintf(errInvalidSequenceBoundsFmt, errSequenceUnderflowsBound, seqMin, seq, MaxSequence, MaxSequence-seqMin+1)
if verr.Error() != expectedMsg {
t.Errorf("expected error msg [%s], got [%s]", expectedMsg, verr.Error())
}
}
func TestGenerator_Snapshot(t *testing.T) {
var (
part = Partition{128, 255}
seqMin = uint16(1024)
seqMax = uint16(2047)
seq = uint32(1024)
)
snap := &GeneratorSnapshot{
Partition: part,
SequenceMin: seqMin,
SequenceMax: seqMax,
Sequence: seq,
}
g, err := NewGenerator(snap, nil)
if err != nil {
t.Fatal(err)
}
actual := g.Snapshot()
if actual.Sequence != seq {
t.Errorf("expected [%d], got [%d]", seq, actual.Sequence)
}
atomic.AddUint32(&g.drifts, 1)
wallNow := snotime()
g.New(255) // First call will catch a zero wallHi and reset the sequence, while we want to measure an incr.
g.New(255)
actual = g.Snapshot()
if uint64(actual.Now) != wallNow {
t.Errorf("expected [%d], got [%d]", wallNow, actual.Now)
}
if uint64(actual.WallHi) != wallNow {
t.Errorf("expected [%d], got [%d]", wallNow, actual.WallHi)
}
if actual.Drifts != 1 {
t.Errorf("expected [%d], got [%d]", 1, actual.Drifts)
}
if actual.Sequence != seq+1 {
t.Errorf("expected [%d], got [%d]", seq+1, actual.Sequence)
}
if actual.Partition != part {
t.Errorf("expected [%s], got [%s]", part, actual.Partition)
}
if actual.SequenceMin != seqMin {
t.Errorf("expected [%d], got [%d]", seqMin, actual.SequenceMin)
}
if actual.SequenceMax != seqMax {
t.Errorf("expected [%d], got [%d]", seqMax, actual.SequenceMax)
}
}