mirror of
https://github.com/sony/sonyflake.git
synced 2026-12-03 18:49:29 +00:00
Increase coverage (#42)
* Introduce mocking framework and increase coverage (#22) This change introduces two common golang patterns: - types: this will allow fine-tuned control over imported types by defining where they will be used and how - mock: this allows the generation of mock constructors, which allows for testing any individual path in a method by "injecting" a mock method which matches the expected type This change also increases test coverage to 100% Co-authored-by: Yoshiyuki Mineo <Yoshiyuki.Mineo@jp.sony.com> * gofmt --------- Co-authored-by: Bradley Boutcher <btboutcher@icloud.com>
This commit is contained in:
1 parent
cc94b60628
commit
597171da2e
4 files changed
+145
-6
No files matched your search
+9
-5
@@ -12,6 +12,8 @@ import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sony/sonyflake/types"
|
||||
)
|
||||
|
||||
// These constants are the bit lengths of Sonyflake ID parts.
|
||||
@@ -50,6 +52,8 @@ type Sonyflake struct {
|
||||
machineID uint16
|
||||
}
|
||||
|
||||
var defaultInterfaceAddrs = net.InterfaceAddrs
|
||||
|
||||
// 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.
|
||||
@@ -71,7 +75,7 @@ func NewSonyflake(st Settings) *Sonyflake {
|
||||
|
||||
var err error
|
||||
if st.MachineID == nil {
|
||||
sf.machineID, err = lower16BitPrivateIP()
|
||||
sf.machineID, err = lower16BitPrivateIP(defaultInterfaceAddrs)
|
||||
} else {
|
||||
sf.machineID, err = st.MachineID()
|
||||
}
|
||||
@@ -131,8 +135,8 @@ func (sf *Sonyflake) toID() (uint64, error) {
|
||||
uint64(sf.machineID), nil
|
||||
}
|
||||
|
||||
func privateIPv4() (net.IP, error) {
|
||||
as, err := net.InterfaceAddrs()
|
||||
func privateIPv4(interfaceAddrs types.InterfaceAddrs) (net.IP, error) {
|
||||
as, err := interfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -156,8 +160,8 @@ func isPrivateIPv4(ip net.IP) bool {
|
||||
(ip[0] == 10 || ip[0] == 172 && (ip[1] >= 16 && ip[1] < 32) || ip[0] == 192 && ip[1] == 168)
|
||||
}
|
||||
|
||||
func lower16BitPrivateIP() (uint16, error) {
|
||||
ip, err := privateIPv4()
|
||||
func lower16BitPrivateIP(interfaceAddrs types.InterfaceAddrs) (uint16, error) {
|
||||
ip, err := privateIPv4(interfaceAddrs)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user