Minor comments updates

This commit is contained in:
Bruce Marriner 2019-04-11 20:34:59 -05:00
parent 7dcf86e66a
commit 0684ace7ee
2 changed files with 26 additions and 7 deletions

View File

@ -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()

View File

@ -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) {