diff --git a/snowflake.go b/snowflake.go index abe4846..c4dc536 100644 --- a/snowflake.go +++ b/snowflake.go @@ -48,7 +48,14 @@ func (j JSONSyntaxError) Error() string { return fmt.Sprintf("invalid snowflake ID %q", string(j.original)) } -// Create a map for decoding Base58. This speeds up the process tremendously. +// ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte +var ErrInvalidBase58 = errors.New("invalid base58") + +// ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte +var ErrInvalidBase32 = errors.New("invalid base32") + +// Create maps for decoding Base58/Base32. +// This speeds up the process tremendously. func init() { for i := 0; i < len(encodeBase58Map); i++ { @@ -68,12 +75,6 @@ func init() { } } -// ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte -var ErrInvalidBase58 = errors.New("invalid base58") - -// ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte -var ErrInvalidBase32 = errors.New("invalid base32") - // A Node struct holds the basic information needed for a snowflake generator // node type Node struct { @@ -128,6 +129,9 @@ func NewNode(node int64) (*Node, error) { } // Generate creates and returns a unique snowflake ID +// To help guarantee uniqueness +// - Make sure your system is keeping accurate system time +// - Make sure you never have multiple nodes running with the same node ID func (n *Node) Generate() ID { n.mu.Lock() diff --git a/snowflake_test.go b/snowflake_test.go index 3d84763..cf857b4 100644 --- a/snowflake_test.go +++ b/snowflake_test.go @@ -6,6 +6,21 @@ import ( "testing" ) +// check if Generate will create duplicate IDs +func TestGenerateDuplicateID(t *testing.T) { + + node, _ := NewNode(1) + + var x, y ID + for i := 0; i < 100000000; i++ { + y = node.Generate() + if x == y { + t.Errorf("x(%d) & y(%d) are the same", x, y) + } + x = y + } +} + // I feel like there's probably a better way func TestRace(t *testing.T) {