fix machine id to 48 bits, add test (#877)

another bone head fail here. hopefully can leave this alone meow...

newID does not get inlined, but doesn't allocate to trigger any stack
expansion either. net perf hit on my laptop is 5ns, and we get a test out of
it. it will push a new stack, so it's not negligible overhead and we could
avoid it. we could keep the logic in both places just to have a test for it
separate instead of re-using the function in the hot path. up to us.
This commit is contained in:
Reed Allman
2018-03-21 16:01:18 -07:00
committed by Tolga Ceylan
parent 86fda80240
commit b9ff601098
2 changed files with 46 additions and 10 deletions

View File

@@ -1,7 +1,12 @@
package id
import (
"encoding/binary"
"fmt"
"math"
"net"
"testing"
"time"
)
func BenchmarkGen(b *testing.B) {
@@ -28,3 +33,31 @@ func BenchmarkUnmarshalText(b *testing.B) {
_ = id
}
}
func TestIdRaw(t *testing.T) {
SetMachineIdHost(net.IP{127, 0, 0, 1}, 8080)
ts := time.Now()
ms := uint64(ts.Unix())*1000 + uint64(ts.Nanosecond()/int(time.Millisecond))
count := uint32(math.MaxUint32)
id := newID(ms, machineID, count)
fmt.Println(len(id), id)
var buf [8]byte
copy(buf[2:], id[:6])
idTime := binary.BigEndian.Uint64(buf[:])
if ms != idTime {
t.Fatal("id time doesn't not match time given", ms, idTime)
}
copy(buf[2:], id[6:12])
idMid := binary.BigEndian.Uint64(buf[:])
if idMid != machineID {
t.Fatal("machine id mismatch", idMid, machineID)
}
idCount := binary.BigEndian.Uint32(id[12:16])
if idCount != count {
t.Fatal("count mismatch", idCount, count)
}
}