mirror of
https://github.com/sony/sonyflake.git
synced 2026-12-02 01:59:29 +00:00
test(v2): refactor and modernize tests, improve error assertions, and update mocks for v2
This commit is contained in:
1 parent
901da0b022
commit
2e5388b9dd
3 files changed
+121
-129
No files matched your search
@@ -18,10 +18,10 @@ As a result, Sonyflake has the following advantages and disadvantages:
|
|||||||
|
|
||||||
- The lifetime (174 years) is longer than that of Snowflake (69 years)
|
- The lifetime (174 years) is longer than that of Snowflake (69 years)
|
||||||
- It can work in more distributed machines (2^16) than Snowflake (2^10)
|
- It can work in more distributed machines (2^16) than Snowflake (2^10)
|
||||||
- It can generate 2^8 IDs per 10 msec at most in a single machine/thread (fewer than Snowflake)
|
- It can generate 2^8 IDs per 10 msec at most in a single instance (fewer than Snowflake)
|
||||||
|
|
||||||
However, if you want more generation rate in a single host,
|
However, if you want more generation rate in a single host,
|
||||||
you can easily run multiple Sonyflake ID generators concurrently using goroutines.
|
you can easily run multiple Sonyflake instances parallelly using goroutines.
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/sony/sonyflake/v2/types"
|
"github.com/sony/sonyflake/v2/types"
|
||||||
@@ -19,10 +19,12 @@ func NewSuccessfulInterfaceAddrs() types.InterfaceAddrs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrFailedToGetAddresses = errors.New("failed to get addresses")
|
||||||
|
|
||||||
// NewFailingInterfaceAddrs returns an error.
|
// NewFailingInterfaceAddrs returns an error.
|
||||||
func NewFailingInterfaceAddrs() types.InterfaceAddrs {
|
func NewFailingInterfaceAddrs() types.InterfaceAddrs {
|
||||||
return func() ([]net.Addr, error) {
|
return func() ([]net.Addr, error) {
|
||||||
return nil, fmt.Errorf("test error")
|
return nil, ErrFailedToGetAddresses
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+115
-125
@@ -12,61 +12,32 @@ import (
|
|||||||
"github.com/sony/sonyflake/v2/types"
|
"github.com/sony/sonyflake/v2/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
var sf *Sonyflake
|
|
||||||
|
|
||||||
var startTime int64
|
|
||||||
var machineID int
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
var st Settings
|
|
||||||
st.StartTime = time.Now()
|
|
||||||
|
|
||||||
var err error
|
|
||||||
sf, err = New(st)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
startTime = toSonyflakeTime(st.StartTime)
|
|
||||||
|
|
||||||
ip, _ := lower16BitPrivateIP(defaultInterfaceAddrs)
|
|
||||||
machineID = int(ip)
|
|
||||||
}
|
|
||||||
|
|
||||||
func nextID(t *testing.T) int64 {
|
|
||||||
id, err := sf.NextID()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("id not generated")
|
|
||||||
}
|
|
||||||
return id
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
genError := fmt.Errorf("an error occurred while generating ID")
|
errGetMachineID := fmt.Errorf("failed to get machine id")
|
||||||
|
|
||||||
tests := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
settings Settings
|
settings Settings
|
||||||
err error
|
err error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "failure: time ahead",
|
name: "start time ahead",
|
||||||
settings: Settings{
|
settings: Settings{
|
||||||
StartTime: time.Now().Add(time.Minute),
|
StartTime: time.Now().Add(time.Minute),
|
||||||
},
|
},
|
||||||
err: ErrStartTimeAhead,
|
err: ErrStartTimeAhead,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "failure: machine ID",
|
name: "cannot get machine id",
|
||||||
settings: Settings{
|
settings: Settings{
|
||||||
MachineID: func() (int, error) {
|
MachineID: func() (int, error) {
|
||||||
return 0, genError
|
return 0, errGetMachineID
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
err: genError,
|
err: errGetMachineID,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "failure: invalid machine ID",
|
name: "invalid machine id",
|
||||||
settings: Settings{
|
settings: Settings{
|
||||||
CheckMachineID: func(int) bool {
|
CheckMachineID: func(int) bool {
|
||||||
return false
|
return false
|
||||||
@@ -80,26 +51,52 @@ func TestNew(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, tc := range testCases {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
sonyflake, err := New(test.settings)
|
sf, err := New(tc.settings)
|
||||||
|
|
||||||
if !errors.Is(err, test.err) {
|
if !errors.Is(err, tc.err) {
|
||||||
t.Fatalf("unexpected value, want %#v, got %#v", test.err, err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sonyflake == nil && err == nil {
|
if err == nil && sf == nil {
|
||||||
t.Fatal("unexpected value, sonyflake should not be nil")
|
t.Fatal("sonyflake instance must be created")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSonyflakeOnce(t *testing.T) {
|
func newSonyflake(t *testing.T, st Settings) *Sonyflake {
|
||||||
|
sf, err := New(st)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create sonyflake: %v", err)
|
||||||
|
}
|
||||||
|
return sf
|
||||||
|
}
|
||||||
|
|
||||||
|
func nextID(t *testing.T, sf *Sonyflake) int64 {
|
||||||
|
id, err := sf.NextID()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to generate id: %v", err)
|
||||||
|
}
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultMachineID(t *testing.T) int {
|
||||||
|
ip, err := lower16BitPrivateIP(defaultInterfaceAddrs)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get private ip address: %v", err)
|
||||||
|
}
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNextID(t *testing.T) {
|
||||||
|
sf := newSonyflake(t, Settings{StartTime: time.Now()})
|
||||||
|
|
||||||
sleepTime := time.Duration(50 * sonyflakeTimeUnit)
|
sleepTime := time.Duration(50 * sonyflakeTimeUnit)
|
||||||
time.Sleep(sleepTime)
|
time.Sleep(sleepTime)
|
||||||
|
|
||||||
id := nextID(t)
|
id := nextID(t, sf)
|
||||||
|
|
||||||
actualTime := ElapsedTime(id)
|
actualTime := ElapsedTime(id)
|
||||||
if actualTime < sleepTime || actualTime > sleepTime+sonyflakeTimeUnit {
|
if actualTime < sleepTime || actualTime > sleepTime+sonyflakeTimeUnit {
|
||||||
@@ -111,29 +108,29 @@ func TestSonyflakeOnce(t *testing.T) {
|
|||||||
t.Errorf("unexpected sequence: %d", actualSequence)
|
t.Errorf("unexpected sequence: %d", actualSequence)
|
||||||
}
|
}
|
||||||
|
|
||||||
actualMachineID := MachineID(id)
|
actualMachine := MachineID(id)
|
||||||
if int(actualMachineID) != machineID {
|
if actualMachine != defaultMachineID(t) {
|
||||||
t.Errorf("unexpected machine id: %d", actualMachineID)
|
t.Errorf("unexpected machine: %d", actualMachine)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("sonyflake id:", id)
|
fmt.Println("sonyflake id:", id)
|
||||||
fmt.Println("decompose:", Decompose(id))
|
fmt.Println("decompose:", Decompose(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentTime() int64 {
|
func TestNextID_InSequence(t *testing.T) {
|
||||||
return toSonyflakeTime(time.Now())
|
now := time.Now()
|
||||||
}
|
sf := newSonyflake(t, Settings{StartTime: now})
|
||||||
|
startTime := toSonyflakeTime(now)
|
||||||
|
machineID := int64(defaultMachineID(t))
|
||||||
|
|
||||||
func TestSonyflakeFor10Sec(t *testing.T) {
|
var numID int
|
||||||
var numID uint32
|
|
||||||
var lastID int64
|
var lastID int64
|
||||||
var maxSequence int64
|
var maxSeq int64
|
||||||
|
|
||||||
initial := currentTime()
|
currentTime := startTime
|
||||||
current := initial
|
for currentTime-startTime < 100 {
|
||||||
for current-initial < 1000 {
|
id := nextID(t, sf)
|
||||||
id := nextID(t)
|
currentTime = toSonyflakeTime(time.Now())
|
||||||
parts := Decompose(id)
|
|
||||||
numID++
|
numID++
|
||||||
|
|
||||||
if id == lastID {
|
if id == lastID {
|
||||||
@@ -144,54 +141,55 @@ func TestSonyflakeFor10Sec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
lastID = id
|
lastID = id
|
||||||
|
|
||||||
current = currentTime()
|
parts := Decompose(id)
|
||||||
|
|
||||||
actualMSB := parts["msb"]
|
actualTime := parts["time"]
|
||||||
if actualMSB != 0 {
|
overtime := startTime + actualTime - currentTime
|
||||||
t.Errorf("unexpected msb: %d", actualMSB)
|
|
||||||
}
|
|
||||||
|
|
||||||
actualTime := int64(parts["time"])
|
|
||||||
overtime := startTime + actualTime - current
|
|
||||||
if overtime > 0 {
|
if overtime > 0 {
|
||||||
t.Errorf("unexpected overtime: %d", overtime)
|
t.Errorf("unexpected overtime: %d", overtime)
|
||||||
}
|
}
|
||||||
|
|
||||||
actualSequence := parts["sequence"]
|
actualSequence := parts["sequence"]
|
||||||
if maxSequence < actualSequence {
|
if actualSequence > maxSeq {
|
||||||
maxSequence = actualSequence
|
maxSeq = actualSequence
|
||||||
}
|
}
|
||||||
|
|
||||||
actualMachineID := parts["machine"]
|
actualMachine := parts["machine"]
|
||||||
if int(actualMachineID) != machineID {
|
if actualMachine != machineID {
|
||||||
t.Errorf("unexpected machine id: %d", actualMachineID)
|
t.Errorf("unexpected machine: %d", actualMachine)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if maxSequence != 1<<BitLenSequence-1 {
|
if maxSeq != 1<<BitLenSequence-1 {
|
||||||
t.Errorf("unexpected max sequence: %d", maxSequence)
|
t.Errorf("unexpected max sequence: %d", maxSeq)
|
||||||
}
|
}
|
||||||
fmt.Println("max sequence:", maxSequence)
|
fmt.Println("max sequence:", maxSeq)
|
||||||
fmt.Println("number of id:", numID)
|
fmt.Println("number of id:", numID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSonyflakeInParallel(t *testing.T) {
|
func TestNextID_InParallel(t *testing.T) {
|
||||||
|
sf1 := newSonyflake(t, Settings{MachineID: func() (int, error) { return 1, nil }})
|
||||||
|
sf2 := newSonyflake(t, Settings{MachineID: func() (int, error) { return 2, nil }})
|
||||||
|
|
||||||
numCPU := runtime.NumCPU()
|
numCPU := runtime.NumCPU()
|
||||||
runtime.GOMAXPROCS(numCPU)
|
runtime.GOMAXPROCS(numCPU)
|
||||||
fmt.Println("number of cpu:", numCPU)
|
fmt.Println("number of cpu:", numCPU)
|
||||||
|
|
||||||
consumer := make(chan int64)
|
consumer := make(chan int64)
|
||||||
|
|
||||||
const numID = 10000
|
const numID = 1000
|
||||||
generate := func() {
|
generate := func(sf *Sonyflake) {
|
||||||
for i := 0; i < numID; i++ {
|
for i := 0; i < numID; i++ {
|
||||||
consumer <- nextID(t)
|
id := nextID(t, sf)
|
||||||
|
consumer <- id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const numGenerator = 10
|
var numGenerator int
|
||||||
for i := 0; i < numGenerator; i++ {
|
for i := 0; i < numCPU/2; i++ {
|
||||||
go generate()
|
go generate(sf1)
|
||||||
|
go generate(sf2)
|
||||||
|
numGenerator += 2
|
||||||
}
|
}
|
||||||
|
|
||||||
set := make(map[int64]struct{})
|
set := make(map[int64]struct{})
|
||||||
@@ -205,16 +203,18 @@ func TestSonyflakeInParallel(t *testing.T) {
|
|||||||
fmt.Println("number of id:", len(set))
|
fmt.Println("number of id:", len(set))
|
||||||
}
|
}
|
||||||
|
|
||||||
func pseudoSleep(period time.Duration) {
|
func pseudoSleep(sf *Sonyflake, period time.Duration) {
|
||||||
sf.startTime -= int64(period) / sonyflakeTimeUnit
|
sf.startTime -= int64(period) / sonyflakeTimeUnit
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNextIDError(t *testing.T) {
|
func TestNextID_ReturnsError(t *testing.T) {
|
||||||
year := time.Duration(365*24) * time.Hour
|
sf := newSonyflake(t, Settings{StartTime: time.Now()})
|
||||||
pseudoSleep(time.Duration(174) * year)
|
|
||||||
nextID(t)
|
|
||||||
|
|
||||||
pseudoSleep(time.Duration(1) * year)
|
year := time.Duration(365*24) * time.Hour
|
||||||
|
pseudoSleep(sf, time.Duration(174) * year)
|
||||||
|
nextID(t, sf)
|
||||||
|
|
||||||
|
pseudoSleep(sf, time.Duration(1) * year)
|
||||||
_, err := sf.NextID()
|
_, err := sf.NextID()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("time is not over")
|
t.Errorf("time is not over")
|
||||||
@@ -226,25 +226,25 @@ func TestPrivateIPv4(t *testing.T) {
|
|||||||
description string
|
description string
|
||||||
expected net.IP
|
expected net.IP
|
||||||
interfaceAddrs types.InterfaceAddrs
|
interfaceAddrs types.InterfaceAddrs
|
||||||
error string
|
error error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
description: "InterfaceAddrs returns an error",
|
description: "returns an error",
|
||||||
expected: nil,
|
expected: nil,
|
||||||
interfaceAddrs: mock.NewFailingInterfaceAddrs(),
|
interfaceAddrs: mock.NewFailingInterfaceAddrs(),
|
||||||
error: "test error",
|
error: mock.ErrFailedToGetAddresses,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "InterfaceAddrs returns an empty or nil list",
|
description: "empty address list",
|
||||||
expected: nil,
|
expected: nil,
|
||||||
interfaceAddrs: mock.NewNilInterfaceAddrs(),
|
interfaceAddrs: mock.NewNilInterfaceAddrs(),
|
||||||
error: "no private ip address",
|
error: ErrNoPrivateAddress,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "InterfaceAddrs returns one or more IPs",
|
description: "success",
|
||||||
expected: net.IP{192, 168, 0, 1},
|
expected: net.IP{192, 168, 0, 1},
|
||||||
interfaceAddrs: mock.NewSuccessfulInterfaceAddrs(),
|
interfaceAddrs: mock.NewSuccessfulInterfaceAddrs(),
|
||||||
error: "",
|
error: nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,17 +252,12 @@ func TestPrivateIPv4(t *testing.T) {
|
|||||||
t.Run(tc.description, func(t *testing.T) {
|
t.Run(tc.description, func(t *testing.T) {
|
||||||
actual, err := privateIPv4(tc.interfaceAddrs)
|
actual, err := privateIPv4(tc.interfaceAddrs)
|
||||||
|
|
||||||
if (err != nil) && (tc.error == "") {
|
if !errors.Is(err, tc.error) {
|
||||||
t.Errorf("expected no error, but got: %s", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
return
|
|
||||||
} else if (err != nil) && (tc.error != "") {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if net.IP.Equal(actual, tc.expected) {
|
if !net.IP.Equal(actual, tc.expected) {
|
||||||
return
|
t.Errorf("unexpected ip: %s", actual)
|
||||||
} else {
|
|
||||||
t.Errorf("error: expected: %s, but got: %s", tc.expected, actual)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -273,43 +268,38 @@ func TestLower16BitPrivateIP(t *testing.T) {
|
|||||||
description string
|
description string
|
||||||
expected int
|
expected int
|
||||||
interfaceAddrs types.InterfaceAddrs
|
interfaceAddrs types.InterfaceAddrs
|
||||||
error string
|
error error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
description: "InterfaceAddrs returns an empty or nil list",
|
description: "returns an error",
|
||||||
expected: 0,
|
expected: 0,
|
||||||
interfaceAddrs: mock.NewNilInterfaceAddrs(),
|
interfaceAddrs: mock.NewFailingInterfaceAddrs(),
|
||||||
error: "no private ip address",
|
error: mock.ErrFailedToGetAddresses,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "InterfaceAddrs returns one or more IPs",
|
description: "empty address list",
|
||||||
|
expected: 0,
|
||||||
|
interfaceAddrs: mock.NewNilInterfaceAddrs(),
|
||||||
|
error: ErrNoPrivateAddress,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "success",
|
||||||
expected: 1,
|
expected: 1,
|
||||||
interfaceAddrs: mock.NewSuccessfulInterfaceAddrs(),
|
interfaceAddrs: mock.NewSuccessfulInterfaceAddrs(),
|
||||||
error: "",
|
error: nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.description, func(t *testing.T) {
|
t.Run(tc.description, func(t *testing.T) {
|
||||||
actual, err := lower16BitPrivateIP(tc.interfaceAddrs)
|
actual, err := lower16BitPrivateIP(tc.interfaceAddrs)
|
||||||
|
|
||||||
if (err != nil) && (tc.error == "") {
|
if !errors.Is(err, tc.error) {
|
||||||
t.Errorf("expected no error, but got: %s", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
return
|
|
||||||
} else if (err != nil) && (tc.error != "") {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if actual == tc.expected {
|
if actual != tc.expected {
|
||||||
return
|
t.Errorf("unexpected ip: %d", actual)
|
||||||
} else {
|
|
||||||
t.Errorf("error: expected: %v, but got: %v", tc.expected, actual)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSonyflakeTimeUnit(t *testing.T) {
|
|
||||||
if time.Duration(sonyflakeTimeUnit) != 10*time.Millisecond {
|
|
||||||
t.Errorf("unexpected time unit")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user