mirror of
https://github.com/sony/sonyflake.git
synced 2026-09-19 12:54:14 +00:00
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:
@@ -157,6 +157,10 @@ func New(st Settings) (*Sonyflake, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if sf.machine < 0 || sf.machine >= 1<<sf.bitsMachine {
|
||||||
|
return nil, ErrInvalidMachineID
|
||||||
|
}
|
||||||
|
|
||||||
if st.CheckMachineID != nil && !st.CheckMachineID(sf.machine) {
|
if st.CheckMachineID != nil && !st.CheckMachineID(sf.machine) {
|
||||||
return nil, ErrInvalidMachineID
|
return nil, ErrInvalidMachineID
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,6 +65,24 @@ func TestNew(t *testing.T) {
|
|||||||
},
|
},
|
||||||
err: errGetMachineID,
|
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",
|
name: "invalid machine id",
|
||||||
settings: Settings{
|
settings: Settings{
|
||||||
|
|||||||
Reference in New Issue
Block a user