-
Notifications
You must be signed in to change notification settings - Fork 0
/
rid_test.go
605 lines (564 loc) · 16 KB
/
rid_test.go
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
// Acknowledgement: This source file is based on work in package github.com/rs/xid,
// a zero-configuration globally-unique ID generator. See LICENSE.rs-xid.
package rid
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"testing"
"time"
)
type idParts struct {
id ID
encoded string
ts int64
random uint64
}
var IDs = []idParts{
// sorted (ascending) should be IDs 2, 3, 0, 5, 4, 1
{
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
ID{0x63, 0xac, 0x76, 0xd3, 0xff, 0xff, 0xfc, 0x30, 0x37, 0xc2},
"dfp7emzzzzy30ey2",
1672246995,
281474912761794,
},
{
// zzzzzzzzzzzzzzzz ts:4294967295 rnd:281474976710655 2106-02-06 22:28:15 -0800 PST ID{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}
ID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
"zzzzzzzzzzzzzzzz",
4294967295,
281474976710655,
},
{
// 0000000000000000 ts:0 rnd: 0 1969-12-31 16:00:00 -0800 PST ID{0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}
ID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
"0000000000000000",
0,
0,
},
{
// dfp7em00001p0t5j ts:1672246992 rnd: 56649906 2022-12-28 09:03:12 -0800 PST ID{0x63,0xac,0x76,0xd0,0x0,0x0,0x3,0x60,0x68,0xb2}
ID{0x63, 0xac, 0x76, 0xd0, 0x0, 0x0, 0x3, 0x60, 0x68, 0xb2},
"dfp7em00001p0t5j",
1672246992,
56649906,
},
{
// dgb58zr000000000 ts:1674859647 rnd: 0 2023-01-27 14:47:27 -0800 PST ID{0x63,0xd4,0x54,0x7f,0x0,0x0,0x0,0x0,0x0,0x0}
ID{0x63, 0xd4, 0x54, 0x7f, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
"dgb58zr000000000",
1674859647,
0,
},
{
// dgb53lewel4ndk94 ts:1674858957 rnd:207175420364068 2023-01-27 14:35:57 -0800 PST ID{0x63,0xd4,0x51,0xcd,0xbc,0x6c,0xc9,0x56,0x45,0x24}
ID{0x63, 0xd4, 0x51, 0xcd, 0xbc, 0x6c, 0xc9, 0x56, 0x45, 0x24},
"dgb53lewel4ndk94",
1674858957,
207175420364068,
},
}
func TestIDPartsExtraction(t *testing.T) {
for i, v := range IDs {
t.Run(fmt.Sprintf("Test%d", i), func(t *testing.T) {
if got, want := v.id.Time(), time.Unix(v.ts, 0); got != want {
t.Errorf("Time() = %v, want %v", got, want)
}
if got, want := v.id.Timestamp(), v.ts; got != want {
t.Errorf("Time() = %v, want %v", got, want)
}
if got, want := v.id.Random(), v.random; got != want {
t.Errorf("Random() = %v, want %v", got, want)
}
})
}
}
func TestNew(t *testing.T) {
// Generate N ids, see if all unique
// Parallel generation test is in ./cmd/eval/uniqcheck/main.go
numIDS := 1000
ids := make([]ID, numIDS)
for i := 0; i < numIDS; i++ {
ids[i] = New()
}
for i := 1; i < numIDS; i++ {
prevID := ids[i-1]
id := ids[i]
// Test for uniqueness among all other generated ids
for j, tid := range ids {
if j != i {
// can't use ID.Compare for this test as it compares only the time
// component of IDs
if bytes.Equal(id[:], tid[:]) {
t.Errorf("generated ID is not unique (%d/%d)\n%v", i, j, ids)
}
}
}
// Check that timestamp was incremented and is within 30 seconds (30000 ms) of the previous one
secs := id.Time().Sub(prevID.Time()).Seconds()
if secs < 0 || secs > 30 {
t.Error("wrong timestamp in generated ID")
}
}
}
func TestIDString(t *testing.T) {
for _, v := range IDs {
if got, want := v.encoded, v.id.String(); got != want {
t.Errorf("String() = %v, want %v", got, want)
}
}
}
func TestIDEncode(t *testing.T) {
id := ID{0x63, 0xac, 0x76, 0xd3, 0xff, 0xff, 0xfc, 0x30, 0x37, 0xc2}
text := make([]byte, encodedLen)
if got, want := string(id.Encode(text)), "dfp7emzzzzy30ey2"; got != want {
t.Errorf("Encode() = %v, want %v", got, want)
}
}
func TestFromString(t *testing.T) {
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
got, err := FromString("dfp7emzzzzy30ey2")
if err != nil {
t.Fatal(err)
}
want := ID{0x63, 0xac, 0x76, 0xd3, 0xff, 0xff, 0xfc, 0x30, 0x37, 0xc2}
if got != want {
t.Errorf("FromString() = %v, want %v", got, want)
}
// nil ID
got, err = FromString("0000000000000000")
if err != nil {
t.Fatal(err)
}
want = ID{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
if got != want {
t.Errorf("FromString() = %v, want %v", got, want)
}
// max ID
got, err = FromString("zzzzzzzzzzzzzzzz")
if err != nil {
t.Fatal(err)
}
want = ID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
if got != want {
t.Errorf("FromString() = %v, want %v", got, want)
}
}
func TestFromStringInvalid(t *testing.T) {
_, err := FromString("012345")
if err != ErrInvalidID {
t.Errorf("FromString(invalid length) err=%v, want %v", err, ErrInvalidID)
}
id, err := FromString("062ez870acdtzd2y3qajilou") // i, l, o, u never in our IDs
if err != ErrInvalidID {
t.Errorf("FromString(062ez870acdtzd2y3qajilou - invalid chars) err=%v, want %v", err, ErrInvalidID)
}
if id != nilID {
t.Errorf("FromString() =%v, there want %v", id, nilID)
}
}
func TestID_UnmarshalText(t *testing.T) {
tests := []struct {
name string
text string
id *ID
wantErr bool
}{
{
// 0000000000000000 ts:0 rnd: 0 1969-12-31 16:00:00 -0800 PST ID{0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0}
"valid",
"0000000000000000",
&nilID,
false,
},
{
"invalid chars",
"000000000000000u",
&nilID,
true,
},
{
"invalid length too long",
"12345678901",
&nilID,
true,
},
{
"invalid length too short",
"dfb7emm",
&nilID,
true,
},
{
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
"valid id",
"dfp7emzzzzy30ey2",
&ID{0x63, 0xac, 0x76, 0xd3, 0xff, 0xff, 0xfc, 0x30, 0x37, 0xc2},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.id.UnmarshalText([]byte(tt.text)); (err != nil) != tt.wantErr {
t.Errorf("ID.UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestID_UnmarshalTextError(t *testing.T) {
id := nilID
if err := id.UnmarshalText([]byte("invalid")); err != ErrInvalidID {
t.Errorf("ID.UnmarshalText() error = %v, wantErr %v", err, ErrInvalidID)
}
id = New() // make a non nil ID
if err := id.UnmarshalText([]byte("foo")); id != nilID {
t.Errorf("ID.UnmarshalText() want nil ID, ErrInvalidID, got %v, %v", id, err)
}
}
func TestID_IsNil(t *testing.T) {
tests := []struct {
name string
id ID
want bool
}{
{
name: "ID not nil",
id: New(),
want: false,
},
{
name: "Nil ID",
id: ID{},
want: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
if got, want := tt.id.IsNil(), tt.want; got != want {
t.Errorf("IsNil() = %v, want %v", got, want)
}
})
}
}
// is an alias function, no need to repeat above, just for coverage report
func TestID_IsZero(t *testing.T) {
id := ID{}
if !id.IsZero() {
t.Errorf("ID.IsZero() = %v, want %v", id.IsZero(), true)
}
}
func TestNilID(t *testing.T) {
got := ID{}
if want := NilID(); !reflect.DeepEqual(got, want) {
t.Error("NilID() not equal ID{}")
}
}
func TestNilID_IsNil(t *testing.T) {
if !NilID().IsNil() {
t.Error("NilID().IsNil() is not true")
}
}
func TestFromBytes_Invariant(t *testing.T) {
want := New()
got, err := FromBytes(want.Bytes())
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got[:], want[:]) {
t.Error("FromBytes(id.Bytes()) != id")
}
// invalid
got, err = FromBytes([]byte{0x1, 0x2})
if !bytes.Equal(got[:], nilID[:]) {
t.Error("FromBytes([]byte{0x1, 0x2}) - invalid - != nilID")
}
if err == nil {
t.Fatal(err)
}
}
type jsonType struct {
ID *ID
Str string
}
func TestIDJSONMarshaling(t *testing.T) {
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
id := ID{0x63, 0xac, 0x76, 0xd3, 0xff, 0xff, 0xfc, 0x30, 0x37, 0xc2}
v := jsonType{ID: &id, Str: "test"}
data, err := json.Marshal(&v)
if err != nil {
t.Fatal(err)
}
if got, want := string(data), `{"ID":"dfp7emzzzzy30ey2","Str":"test"}`; got != want {
t.Errorf("json.Marshal() = %v, want %v", got, want)
}
}
func TestIDJSONUnmarshaling(t *testing.T) {
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
data := []byte(`{"ID":"dfp7emzzzzy30ey2","Str":"test"}`)
v := jsonType{}
err := json.Unmarshal(data, &v)
if err != nil {
t.Fatal(err)
}
want := ID{0x63, 0xac, 0x76, 0xd3, 0xff, 0xff, 0xfc, 0x30, 0x37, 0xc2}
if got := *v.ID; !bytes.Equal(got[:], want[:]) {
t.Errorf("json.Unmarshal() = %v, want %v", got, want)
}
// should not fail
err = json.Unmarshal([]byte(`null`), &v)
if err != nil {
t.Errorf("json.Unmarshal() err=%v, want %v", err, nil)
}
}
func TestIDJSONUnmarshalingError(t *testing.T) {
v := jsonType{}
// callers are responsible for forcing lower case input for Base32
// otherwise valid id:
err := json.Unmarshal([]byte(`{"ID":"DFP8T54NN0JZ37HW"}`), &v)
if err != ErrInvalidID {
t.Errorf("json.Unmarshal() err=%v, want %v", err, ErrInvalidID)
}
// too short
err = json.Unmarshal([]byte(`{"ID":"dfp8t54nn0jz37h"}`), &v)
if err != ErrInvalidID {
t.Errorf("json.Unmarshal() err=%v, want %v", err, ErrInvalidID)
}
// no 'a' in character set
err = json.Unmarshal([]byte(`{"ID":"0000000000000a"}`), &v)
if err != ErrInvalidID {
t.Errorf("json.Unmarshal() err=%v, want %v", err, ErrInvalidID)
}
// invalid on multiple levels
err = json.Unmarshal([]byte(`{"ID":1}`), &v)
if err != ErrInvalidID {
t.Errorf("json.Unmarshal() err=%v, want %v", err, ErrInvalidID)
}
}
func TestIDDriverValue(t *testing.T) {
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
id := ID{0x63, 0xac, 0x76, 0xd3, 0xff, 0xff, 0xfc, 0x30, 0x37, 0xc2}
got, err := id.Value()
if err != nil {
t.Fatal(err)
}
if want := "dfp7emzzzzy30ey2"; got != want {
t.Errorf("Value() = %v, want %v", got, want)
}
}
func TestIDDriverScan(t *testing.T) {
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
got := ID{}
err := got.Scan("dfp7emzzzzy30ey2")
if err != nil {
t.Fatal(err)
}
want := ID{0x63, 0xac, 0x76, 0xd3, 0xff, 0xff, 0xfc, 0x30, 0x37, 0xc2}
if !bytes.Equal(got[:], want[:]) {
t.Errorf("Scan() = %v, want %v", got, want)
}
}
func TestIDDriverScanError(t *testing.T) {
id := ID{}
if got, want := id.Scan(0), errors.New("rid: scanning unsupported type: int"); got.Error() != want.Error() {
t.Errorf("Scan() err=%v, want %v", got, want)
}
if got, want := id.Scan("0"), ErrInvalidID; got != want {
t.Errorf("Scan() err=%v, want %v", got, want)
if id != nilID {
t.Errorf("Scan() id=%v, want %v", got, nilID)
}
}
}
func TestIDDriverScanByteFromDatabase(t *testing.T) {
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
got := ID{}
bs := []byte("dfp7emzzzzy30ey2")
err := got.Scan(bs)
if err != nil {
t.Fatal(err)
}
want := ID{0x63, 0xac, 0x76, 0xd3, 0xff, 0xff, 0xfc, 0x30, 0x37, 0xc2}
if !bytes.Equal(got[:], want[:]) {
t.Errorf("Scan() = %v, want %v", got, want)
}
}
func TestFromBytes_InvalidBytes(t *testing.T) {
cases := []struct {
length int
shouldFail bool
}{
{rawLen - 1, true},
{rawLen, false},
{rawLen + 1, true},
}
for _, c := range cases {
b := make([]byte, c.length)
_, err := FromBytes(b)
if got, want := err != nil, c.shouldFail; got != want {
t.Errorf("FromBytes() error got %v, want %v", got, want)
}
}
}
func TestCompare(t *testing.T) {
pairs := []struct {
left ID
right ID
expected int
}{
{IDs[1].id, IDs[0].id, 1},
{ID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, IDs[2].id, 0},
{IDs[0].id, IDs[0].id, 0},
{IDs[2].id, IDs[1].id, -1},
{IDs[5].id, IDs[4].id, -1},
}
for _, p := range pairs {
if p.expected != p.left.Compare(p.right) {
t.Errorf("%s Compare to %s should return %d", p.left, p.right, p.expected)
}
if -1*p.expected != p.right.Compare(p.left) {
t.Errorf("%s Compare to %s should return %d", p.right, p.left, -1*p.expected)
}
}
}
var IDList = []ID{IDs[0].id, IDs[1].id, IDs[2].id, IDs[3].id, IDs[4].id, IDs[5].id}
func TestSorter_Len(t *testing.T) {
if got, want := sorter([]ID{}).Len(), 0; got != want {
t.Errorf("Len() %v, want %v", got, want)
}
if got, want := sorter(IDList).Len(), 6; got != want {
t.Errorf("Len() %v, want %v", got, want)
}
}
func TestSorter_Less(t *testing.T) {
// sorted (ascending) should be IDs 2, 3, 0, 1
sorter := sorter(IDList)
if !sorter.Less(0, 1) {
t.Errorf("Less(0, 1) not true")
}
if sorter.Less(3, 2) {
t.Errorf("Less(2, 1) true")
}
if sorter.Less(0, 0) {
t.Errorf("Less(0, 0) true")
}
}
func TestSorter_Swap(t *testing.T) {
ids := make([]ID, 0)
ids = append(ids, IDList...)
sorter := sorter(ids)
sorter.Swap(0, 1)
if got, want := ids[0], IDList[1]; !reflect.DeepEqual(got, want) {
t.Error("ids[0] != IDList[1]")
}
if got, want := ids[1], IDList[0]; !reflect.DeepEqual(got, want) {
t.Error("ids[1] != IDList[0]")
}
sorter.Swap(2, 2)
if got, want := ids[2], IDList[2]; !reflect.DeepEqual(got, want) {
t.Error("ids[2], IDList[2]")
}
}
func TestSort(t *testing.T) {
ids := make([]ID, 0)
ids = append(ids, IDList...)
Sort(ids)
// sorted (ascending) should be IDs 2, 3, 0, 5, 4, 1
if got, want := ids, []ID{IDList[2], IDList[3], IDList[0], IDList[5], IDList[4], IDList[1]}; !reflect.DeepEqual(got, want) {
t.Errorf("\ngot %v\nwant %v\n", got, want)
}
}
func TestFastrand48New(t *testing.T) {
t.Run("check-dupes", func(t *testing.T) {
// see eval/uniqcheck/main.go for proof of utility testing in concurrent environments
var id ID
// since the underlying structure of ID is an array, not a slice, ID can be a key
keys := make(map[ID]bool)
count := 100000
for i := 0; i < count; i++ {
id = New()
if keys[id] {
// It's not actually an error but something to consider; no application using
// this package ought to be generating 100,000 IDs a second let alone millions.
t.Errorf("Duplicate random number %d generated within %d iterations", id, count)
}
keys[id] = true
}
})
}
// Benchmarks
var (
// added to avoid compiler over-optimization and silly results
benchResultID ID
benchResultString string
)
// Create new ID
func BenchmarkNew(b *testing.B) {
var r ID
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
r = New()
}
benchResultID = r
})
}
// common use case, generate an ID, encode as a string:
func BenchmarkNewString(b *testing.B) {
var r string
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
r = New().String()
}
benchResultString = r
})
}
// encoding performance only
func BenchmarkString(b *testing.B) {
id := New()
var r string
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
r = id.String()
}
benchResultString = r
})
}
// decoding performance only
func BenchmarkFromString(b *testing.B) {
var r ID
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
str := "dfp7emzzzzy30ey2"
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
r, _ = FromString(str)
}
benchResultID = r
})
}
// examples
func ExampleNew() {
id := New()
fmt.Printf(`ID:
String() %s
Timestamp() %d
Random() %d
Time() %v
Bytes() %3v\n`, id.String(), id.Timestamp(), id.Random(), id.Time().UTC(), id.Bytes())
}
func ExampleNewWithTime() {
id := NewWithTime(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC))
fmt.Printf(`ID: Timestamp() %d Time() %v`, id.Timestamp(), id.Time().UTC())
// Output: ID: Timestamp() 1577836800 Time() 2020-01-01 00:00:00 +0000 UTC
}
func ExampleFromString() {
// dfp7emzzzzy30ey2 ts:1672246995 rnd:281474912761794 2022-12-28 09:03:15 -0800 PST ID{0x63,0xac,0x76,0xd3,0xff,0xff,0xfc,0x30,0x37,0xc2}
id, err := FromString("dfp7emzzzzy30ey2")
if err != nil {
panic(err)
}
fmt.Println(id.Timestamp(), id.Random())
// Output: 1672246995 281474912761794
}