From 2e5388b9ddf2d03f1004d843cba32c582068c2e6 Mon Sep 17 00:00:00 2001 From: Yoshiyuki Mineo Date: Mon, 5 May 2025 01:08:06 +0900 Subject: [PATCH] test(v2): refactor and modernize tests, improve error assertions, and update mocks for v2 --- README.md | 4 +- v2/mock/sonyflake_mock.go | 6 +- v2/sonyflake_test.go | 240 ++++++++++++++++++-------------------- 3 files changed, 121 insertions(+), 129 deletions(-) diff --git a/README.md b/README.md index fc859b8..aca67cb 100644 --- a/README.md +++ b/README.md @@ -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) - 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, -you can easily run multiple Sonyflake ID generators concurrently using goroutines. +you can easily run multiple Sonyflake instances parallelly using goroutines. Installation ------------ diff --git a/v2/mock/sonyflake_mock.go b/v2/mock/sonyflake_mock.go index 8b53be0..1bff6ab 100644 --- a/v2/mock/sonyflake_mock.go +++ b/v2/mock/sonyflake_mock.go @@ -3,7 +3,7 @@ package mock import ( - "fmt" + "errors" "net" "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. func NewFailingInterfaceAddrs() types.InterfaceAddrs { return func() ([]net.Addr, error) { - return nil, fmt.Errorf("test error") + return nil, ErrFailedToGetAddresses } } diff --git a/v2/sonyflake_test.go b/v2/sonyflake_test.go index 9f6454c..29b4138 100644 --- a/v2/sonyflake_test.go +++ b/v2/sonyflake_test.go @@ -12,61 +12,32 @@ import ( "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) { - genError := fmt.Errorf("an error occurred while generating ID") + errGetMachineID := fmt.Errorf("failed to get machine id") - tests := []struct { + testCases := []struct { name string settings Settings err error }{ { - name: "failure: time ahead", + name: "start time ahead", settings: Settings{ StartTime: time.Now().Add(time.Minute), }, err: ErrStartTimeAhead, }, { - name: "failure: machine ID", + name: "cannot get machine id", settings: Settings{ 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{ CheckMachineID: func(int) bool { return false @@ -80,26 +51,52 @@ func TestNew(t *testing.T) { }, } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - sonyflake, err := New(test.settings) + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + sf, err := New(tc.settings) - if !errors.Is(err, test.err) { - t.Fatalf("unexpected value, want %#v, got %#v", test.err, err) + if !errors.Is(err, tc.err) { + t.Fatalf("unexpected error: %v", err) } - if sonyflake == nil && err == nil { - t.Fatal("unexpected value, sonyflake should not be nil") + if err == nil && sf == 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) time.Sleep(sleepTime) - id := nextID(t) + id := nextID(t, sf) actualTime := ElapsedTime(id) if actualTime < sleepTime || actualTime > sleepTime+sonyflakeTimeUnit { @@ -111,29 +108,29 @@ func TestSonyflakeOnce(t *testing.T) { t.Errorf("unexpected sequence: %d", actualSequence) } - actualMachineID := MachineID(id) - if int(actualMachineID) != machineID { - t.Errorf("unexpected machine id: %d", actualMachineID) + actualMachine := MachineID(id) + if actualMachine != defaultMachineID(t) { + t.Errorf("unexpected machine: %d", actualMachine) } fmt.Println("sonyflake id:", id) fmt.Println("decompose:", Decompose(id)) } -func currentTime() int64 { - return toSonyflakeTime(time.Now()) -} +func TestNextID_InSequence(t *testing.T) { + now := time.Now() + sf := newSonyflake(t, Settings{StartTime: now}) + startTime := toSonyflakeTime(now) + machineID := int64(defaultMachineID(t)) -func TestSonyflakeFor10Sec(t *testing.T) { - var numID uint32 + var numID int var lastID int64 - var maxSequence int64 + var maxSeq int64 - initial := currentTime() - current := initial - for current-initial < 1000 { - id := nextID(t) - parts := Decompose(id) + currentTime := startTime + for currentTime-startTime < 100 { + id := nextID(t, sf) + currentTime = toSonyflakeTime(time.Now()) numID++ if id == lastID { @@ -144,54 +141,55 @@ func TestSonyflakeFor10Sec(t *testing.T) { } lastID = id - current = currentTime() + parts := Decompose(id) - actualMSB := parts["msb"] - if actualMSB != 0 { - t.Errorf("unexpected msb: %d", actualMSB) - } - - actualTime := int64(parts["time"]) - overtime := startTime + actualTime - current + actualTime := parts["time"] + overtime := startTime + actualTime - currentTime if overtime > 0 { t.Errorf("unexpected overtime: %d", overtime) } actualSequence := parts["sequence"] - if maxSequence < actualSequence { - maxSequence = actualSequence + if actualSequence > maxSeq { + maxSeq = actualSequence } - actualMachineID := parts["machine"] - if int(actualMachineID) != machineID { - t.Errorf("unexpected machine id: %d", actualMachineID) + actualMachine := parts["machine"] + if actualMachine != machineID { + t.Errorf("unexpected machine: %d", actualMachine) } } - if maxSequence != 1<