|
| 1 | +// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm) |
| 2 | +// go-aah/essentials source code and usage is governed by a MIT style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package ess |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/binary" |
| 9 | + "encoding/hex" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "aahframework.org/test.v0/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestGUIDNew(t *testing.T) { |
| 17 | + // Generate 10 ids |
| 18 | + ids := make([]string, 10) |
| 19 | + for i := 0; i < 10; i++ { |
| 20 | + ids[i] = NewGUID() |
| 21 | + } |
| 22 | + for i := 1; i < 10; i++ { |
| 23 | + prevID := ids[i-1] |
| 24 | + id := ids[i] |
| 25 | + // Test for uniqueness among all other 9 generated ids |
| 26 | + for j, tid := range ids { |
| 27 | + if j != i { |
| 28 | + assert.NotEqualf(t, id, tid, "Generated ID is not unique") |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Check that timestamp was incremented and is within 30 seconds of the previous one |
| 33 | + secs := getTime(id).Sub(getTime(prevID)).Seconds() |
| 34 | + assert.Equalf(t, (secs >= 0 && secs <= 30), true, "wrong timestamp in generated Id") |
| 35 | + |
| 36 | + // Check that machine ids are the same |
| 37 | + assert.Equal(t, getMachine(id), getMachine(prevID)) |
| 38 | + |
| 39 | + // Check that pids are the same |
| 40 | + assert.Equal(t, getPid(id), getPid(prevID)) |
| 41 | + |
| 42 | + // Test for proper increment |
| 43 | + delta := int(getCounter(id) - getCounter(prevID)) |
| 44 | + assert.Equalf(t, delta, 1, "wrong increment in generated Id") |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +type guidParts struct { |
| 49 | + id string |
| 50 | + timestamp int64 |
| 51 | + machine []byte |
| 52 | + pid uint16 |
| 53 | + counter int32 |
| 54 | +} |
| 55 | + |
| 56 | +var uniqueIds = []guidParts{ |
| 57 | + guidParts{ |
| 58 | + "4d88e15b60f486e428412dc9", |
| 59 | + 1300816219, |
| 60 | + []byte{0x60, 0xf4, 0x86}, |
| 61 | + 0xe428, |
| 62 | + 4271561, |
| 63 | + }, |
| 64 | + guidParts{ |
| 65 | + "000000000000000000000000", |
| 66 | + 0, |
| 67 | + []byte{0x00, 0x00, 0x00}, |
| 68 | + 0x0000, |
| 69 | + 0, |
| 70 | + }, |
| 71 | + guidParts{ |
| 72 | + "00000000aabbccddee000001", |
| 73 | + 0, |
| 74 | + []byte{0xaa, 0xbb, 0xcc}, |
| 75 | + 0xddee, |
| 76 | + 1, |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | +func TestGUIDPartsExtraction(t *testing.T) { |
| 81 | + for i, v := range uniqueIds { |
| 82 | + assert.Equalf(t, getTime(v.id), time.Unix(v.timestamp, 0), "#%d timestamp", i) |
| 83 | + assert.Equalf(t, getMachine(v.id), v.machine, "#%d machine", i) |
| 84 | + assert.Equalf(t, getPid(v.id), v.pid, "#%d pid", i) |
| 85 | + assert.Equalf(t, getCounter(v.id), v.counter, "#%d counter", i) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func BenchmarkNewGUID(b *testing.B) { |
| 90 | + b.RunParallel(func(pb *testing.PB) { |
| 91 | + for pb.Next() { |
| 92 | + _ = NewGUID() |
| 93 | + } |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +func getMachine(id string) []byte { |
| 98 | + return byteSlice(id, 4, 7) |
| 99 | +} |
| 100 | + |
| 101 | +func getTime(id string) time.Time { |
| 102 | + secs := int64(binary.BigEndian.Uint32(byteSlice(id, 0, 4))) |
| 103 | + return time.Unix(secs, 0) |
| 104 | +} |
| 105 | + |
| 106 | +func getPid(id string) uint16 { |
| 107 | + return binary.BigEndian.Uint16(byteSlice(id, 7, 9)) |
| 108 | +} |
| 109 | + |
| 110 | +func getCounter(id string) int32 { |
| 111 | + b := byteSlice(id, 9, 12) |
| 112 | + // Counter is stored as big-endian 3-byte value |
| 113 | + return int32(uint32(b[0])<<16 | uint32(b[1])<<8 | uint32(b[2])) |
| 114 | +} |
| 115 | + |
| 116 | +func byteSlice(id string, s, e int) []byte { |
| 117 | + if len(id) == 24 { |
| 118 | + b, _ := hex.DecodeString(id) |
| 119 | + return b[s:e] |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
0 commit comments