Enhance Sonyflake error handling by adding tests for invalid machine IDs, including cases for too large and negative values. (#73)

This commit is contained in:
Yoshiyuki Mineo
2025-05-18 06:16:04 +00:00
committed by GitHub
parent 774342570a
commit 5347433c8c
2 changed files with 22 additions and 0 deletions

View File

@@ -157,6 +157,10 @@ func New(st Settings) (*Sonyflake, error) {
return nil, err
}
if sf.machine < 0 || sf.machine >= 1<<sf.bitsMachine {
return nil, ErrInvalidMachineID
}
if st.CheckMachineID != nil && !st.CheckMachineID(sf.machine) {
return nil, ErrInvalidMachineID
}

View File

@@ -65,6 +65,24 @@ func TestNew(t *testing.T) {
},
err: errGetMachineID,
},
{
name: "too large machine id",
settings: Settings{
MachineID: func() (int, error) {
return 1 << defaultBitsMachine, nil
},
},
err: ErrInvalidMachineID,
},
{
name: "negative machine id",
settings: Settings{
MachineID: func() (int, error) {
return -1, nil
},
},
err: ErrInvalidMachineID,
},
{
name: "invalid machine id",
settings: Settings{