From 0187fe6584a579c0f15144c6ff13a59cdaac54db Mon Sep 17 00:00:00 2001 From: Quetzy Garcia Date: Mon, 13 Mar 2023 12:46:52 +0000 Subject: [PATCH 1/6] feat(Sonyflake): define error variables --- sonyflake.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/sonyflake.go b/sonyflake.go index ebafa46..1baa3c1 100644 --- a/sonyflake.go +++ b/sonyflake.go @@ -50,6 +50,13 @@ type Sonyflake struct { machineID uint16 } +var ( + ErrStartTimeAhead = errors.New("the start time cannot be in the future") + ErrNoPrivateAddress = errors.New("no private IP address") + ErrOverTimeLimit = errors.New("over the time limit") + ErrInvalidMachineID = errors.New("invalid machine ID") +) + // NewSonyflake returns a new Sonyflake configured with the given Settings. // NewSonyflake returns nil in the following cases: // - Settings.StartTime is ahead of the current time. @@ -123,7 +130,7 @@ func sleepTime(overtime int64) time.Duration { func (sf *Sonyflake) toID() (uint64, error) { if sf.elapsedTime >= 1< Date: Mon, 13 Mar 2023 12:50:08 +0000 Subject: [PATCH 2/6] feat(Sonyflake): add New() function - minor logic improvements - return errors --- sonyflake.go | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/sonyflake.go b/sonyflake.go index 1baa3c1..bb1b362 100644 --- a/sonyflake.go +++ b/sonyflake.go @@ -57,35 +57,48 @@ var ( ErrInvalidMachineID = errors.New("invalid machine ID") ) -// NewSonyflake returns a new Sonyflake configured with the given Settings. -// NewSonyflake returns nil in the following cases: -// - Settings.StartTime is ahead of the current time. -// - Settings.MachineID returns an error. -// - Settings.CheckMachineID returns false. -func NewSonyflake(st Settings) *Sonyflake { +// New returns a new Sonyflake configured with the given Settings. If an error occurs, +// the *sonyflake.Sonyflake pointer will be nil and an error instance will be returned. +func New(st Settings) (*Sonyflake, error) { + if st.StartTime.After(time.Now()) { + return nil, ErrStartTimeAhead + } + sf := new(Sonyflake) sf.mutex = new(sync.Mutex) sf.sequence = uint16(1< Date: Mon, 13 Mar 2023 12:52:37 +0000 Subject: [PATCH 3/6] tests(Sonyflake): remove old TestNilSonyflake test function in favour of the New() function coverage --- sonyflake_test.go | 79 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/sonyflake_test.go b/sonyflake_test.go index 8c4999f..139902d 100644 --- a/sonyflake_test.go +++ b/sonyflake_test.go @@ -1,6 +1,7 @@ package sonyflake import ( + "errors" "fmt" "runtime" "testing" @@ -35,6 +36,60 @@ func nextID(t *testing.T) uint64 { return id } +func TestNew(t *testing.T) { + genError := fmt.Errorf("an error occurred while generating ID") + + tests := []struct { + name string + settings Settings + err error + }{ + { + name: "failure: time ahead", + settings: Settings{ + StartTime: time.Now().Add(time.Minute), + }, + err: ErrStartTimeAhead, + }, + { + name: "failure: machine ID", + settings: Settings{ + MachineID: func() (uint16, error) { + return 0, genError + }, + }, + err: genError, + }, + { + name: "failure: invalid machine ID", + settings: Settings{ + CheckMachineID: func(uint16) bool { + return false + }, + }, + err: ErrInvalidMachineID, + }, + { + name: "success", + settings: Settings{}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + sonyflake, err := New(test.settings) + + if !errors.Is(err, test.err) { + t.Fatalf("unexpected value, want %#v, got %#v", test.err, err) + } + + if sonyflake == nil && err == nil { + t.Fatal("unexpected value, sonyflake should not be nil") + } + }) + } +} + func TestSonyflakeOnce(t *testing.T) { sleepTime := time.Duration(50 * sonyflakeTimeUnit) time.Sleep(sleepTime) @@ -145,30 +200,6 @@ func TestSonyflakeInParallel(t *testing.T) { fmt.Println("number of id:", len(set)) } -func TestNilSonyflake(t *testing.T) { - var startInFuture Settings - startInFuture.StartTime = time.Now().Add(time.Duration(1) * time.Minute) - if NewSonyflake(startInFuture) != nil { - t.Errorf("sonyflake starting in the future") - } - - var noMachineID Settings - noMachineID.MachineID = func() (uint16, error) { - return 0, fmt.Errorf("no machine id") - } - if NewSonyflake(noMachineID) != nil { - t.Errorf("sonyflake with no machine id") - } - - var invalidMachineID Settings - invalidMachineID.CheckMachineID = func(uint16) bool { - return false - } - if NewSonyflake(invalidMachineID) != nil { - t.Errorf("sonyflake with invalid machine id") - } -} - func pseudoSleep(period time.Duration) { sf.startTime -= int64(period) / sonyflakeTimeUnit } From 9aa125baa2329f4f12233f4332d7d106664178c3 Mon Sep 17 00:00:00 2001 From: Yoshiyuki Mineo Date: Mon, 14 Aug 2023 00:22:43 +0900 Subject: [PATCH 4/6] gofmt --- sonyflake.go | 2 +- sonyflake_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sonyflake.go b/sonyflake.go index 8e84eb2..12df0ab 100644 --- a/sonyflake.go +++ b/sonyflake.go @@ -78,7 +78,7 @@ func New(st Settings) (*Sonyflake, error) { sf.startTime = toSonyflakeTime(st.StartTime) } - var err error + var err error if st.MachineID == nil { sf.machineID, err = lower16BitPrivateIP(defaultInterfaceAddrs) } else { diff --git a/sonyflake_test.go b/sonyflake_test.go index 19e0bed..0a7aa4f 100644 --- a/sonyflake_test.go +++ b/sonyflake_test.go @@ -1,8 +1,8 @@ package sonyflake import ( - "errors" "bytes" + "errors" "fmt" "net" "runtime" From e53d2e57bbae21646fdf119f68533a809575944e Mon Sep 17 00:00:00 2001 From: Yoshiyuki Mineo Date: Mon, 14 Aug 2023 00:56:40 +0900 Subject: [PATCH 5/6] Update error messages and comments --- sonyflake.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sonyflake.go b/sonyflake.go index 12df0ab..548b243 100644 --- a/sonyflake.go +++ b/sonyflake.go @@ -53,16 +53,19 @@ type Sonyflake struct { } var ( - ErrStartTimeAhead = errors.New("the start time cannot be in the future") - ErrNoPrivateAddress = errors.New("no private IP address") + ErrStartTimeAhead = errors.New("start time is ahead of now") + ErrNoPrivateAddress = errors.New("no private ip address") ErrOverTimeLimit = errors.New("over the time limit") - ErrInvalidMachineID = errors.New("invalid machine ID") + ErrInvalidMachineID = errors.New("invalid machine id") ) var defaultInterfaceAddrs = net.InterfaceAddrs -// New returns a new Sonyflake configured with the given Settings. If an error occurs, -// the *sonyflake.Sonyflake pointer will be nil and an error instance will be returned. +// New returns a new Sonyflake configured with the given Settings. +// New returns an error in the following cases: +// - Settings.StartTime is ahead of the current time. +// - Settings.MachineID returns an error. +// - Settings.CheckMachineID returns false. func New(st Settings) (*Sonyflake, error) { if st.StartTime.After(time.Now()) { return nil, ErrStartTimeAhead From 9279155fdf6d5b3a5ca6c5d07e2ca8b815d49950 Mon Sep 17 00:00:00 2001 From: Yoshiyuki Mineo Date: Mon, 14 Aug 2023 01:16:47 +0900 Subject: [PATCH 6/6] Introduce New function --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9c9ebeb..0e6cdab 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,10 @@ go get github.com/sony/sonyflake Usage ----- -The function NewSonyflake creates a new Sonyflake instance. +The function New creates a new Sonyflake instance. ```go -func NewSonyflake(st Settings) *Sonyflake +func New(st Settings) (*Sonyflake, error) ``` You can configure Sonyflake by the struct Settings: