package sonyflake import ( "errors" "net" "runtime" "testing" "time" "github.com/sony/sonyflake/v2/mock" "github.com/sony/sonyflake/v2/types" ) func TestNew(t *testing.T) { errGetMachineID := errors.New("failed to get machine id") testCases := []struct { name string settings Settings err error }{ { name: "invalid bit length for time", settings: Settings{ BitsSequence: 16, BitsMachineID: 16, }, err: ErrInvalidBitsTime, }, { name: "invalid bit length for sequence number", settings: Settings{ BitsSequence: -1, }, err: ErrInvalidBitsSequence, }, { name: "invalid bit length for machine id", settings: Settings{ BitsMachineID: 31, }, err: ErrInvalidBitsMachineID, }, { name: "invalid time unit", settings: Settings{ TimeUnit: time.Microsecond, }, err: ErrInvalidTimeUnit, }, { name: "start time ahead", settings: Settings{ StartTime: time.Now().Add(time.Minute), }, err: ErrStartTimeAhead, }, { name: "cannot get machine id", settings: Settings{ MachineID: func() (int, error) { return 0, 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", settings: Settings{ CheckMachineID: func(int) bool { return false }, }, err: ErrInvalidMachineID, }, { name: "success", settings: Settings{}, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { sf, err := New(tc.settings) if !errors.Is(err, tc.err) { t.Fatalf("unexpected error: %v", err) } if err == nil && sf == nil { t.Fatal("sonyflake instance must be created") } }) } } 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) { start := time.Now() sf := newSonyflake(t, Settings{StartTime: start}) sleepTime := int64(50) sf.now = func() time.Time { return start.Add(time.Duration(sleepTime * sf.timeUnit)) } id := nextID(t, sf) actualTime := sf.timePart(id) if actualTime != sleepTime { t.Errorf("unexpected time: %d", actualTime) } actualSequence := sf.sequencePart(id) if actualSequence != 0 { t.Errorf("unexpected sequence: %d", actualSequence) } actualMachine := sf.machinePart(id) if actualMachine != int64(defaultMachineID(t)) { t.Errorf("unexpected machine: %d", actualMachine) } t.Log("sonyflake id:", id) t.Log("decompose:", sf.Decompose(id)) } func TestNextID_InSequence(t *testing.T) { start := time.Now() sf := newSonyflake(t, Settings{ TimeUnit: time.Millisecond, StartTime: start, }) startTime := sf.toInternalTime(start) machineID := int64(defaultMachineID(t)) var numID int var lastID int64 var maxSeq int64 currentTime := startTime for currentTime-startTime < 100 { id := nextID(t, sf) currentTime = sf.toInternalTime(time.Now()) numID++ if id == lastID { t.Fatal("duplicated id") } if id < lastID { t.Fatal("must increase with time") } lastID = id parts := sf.Decompose(id) actualTime := parts["time"] overtime := startTime + actualTime - currentTime if overtime > 0 { t.Errorf("unexpected overtime: %d", overtime) } actualSequence := parts["sequence"] if actualSequence > maxSeq { maxSeq = actualSequence } actualMachine := parts["machine"] if actualMachine != machineID { t.Errorf("unexpected machine: %d", actualMachine) } } if maxSeq > 1<= time.Duration(sf.timeUnit) { t.Errorf("unexpected time: %v", diff) } } func TestComposeAndDecompose(t *testing.T) { now := time.Now() sf := newSonyflake(t, Settings{ TimeUnit: time.Millisecond, StartTime: now, }) testCases := []struct { name string time time.Time sequence int machineID int }{ { name: "zero values", time: now, sequence: 0, machineID: 0, }, { name: "max sequence", time: now, sequence: 1<