Permalink
Browse files

Merge pull request #1 from 0x6e6562/master

Replace panic with errors
  • Loading branch information...
2 parents 0279960 + 684868a commit 3a05c415e886bd8d06522975d2c3458eafe93f04 @sdming committed Apr 3, 2013
Showing with 72 additions and 57 deletions.
  1. +13 −10 README.md
  2. +8 −7 gosnow.go
  3. +51 −40 gosnow_test.go
View
23 README.md
@@ -1,26 +1,29 @@
gosnow
======
-snowflake in golang
+gosnow is a snowflake implementation in golang.
----how to use
+This is a fork of the upstream that replaces the the usage of panic() by returning errors instead.
~~~golang
package main
import (
- "github.com/sdming/gosnow"
- "fmt"
+ "github.com/sdming/gosnow"
+ "fmt"
)
func main() {
- v := gosnow.Default()
- //v := gosnow.NewSnowFlake(100)
- for i := 0; i < 10; i++ {
- id := v.Next()
- fmt.Println(id)
- }
+ v, err := gosnow.Default()
+
+ // Alternatively you can set the worker id if you are running multiple snowflakes
+ // v, err := gosnow.NewSnowFlake(100)
+
+ for i := 0; i < 10; i++ {
+ id, err := v.Next()
+ fmt.Println(id)
+ }
}
~~~
View
15 gosnow.go
@@ -10,6 +10,7 @@ import (
"net"
"sync"
"time"
+ "fmt"
)
const (
@@ -40,7 +41,7 @@ func (sf *SnowFlake) uint64() uint64 {
(uint64(sf.sequence))
}
-func (sf *SnowFlake) Next() uint64 {
+func (sf *SnowFlake) Next() (uint64, error) {
sf.lock.Lock()
defer sf.lock.Unlock()
@@ -55,21 +56,21 @@ func (sf *SnowFlake) Next() uint64 {
}
if ts < sf.lastTimestamp {
- panic("invalid timestamp")
+ return 0, fmt.Errorf("Invalid timestamp: %v - precedes %v", ts, sf)
}
sf.lastTimestamp = ts
- return sf.uint64()
+ return sf.uint64(), nil
}
-func Default() *SnowFlake {
+func Default() (*SnowFlake, error) {
return NewSnowFlake(DefaultWorkId())
}
-func NewSnowFlake(workerId uint32) *SnowFlake {
+func NewSnowFlake(workerId uint32) (*SnowFlake, error) {
if workerId < 0 || workerId > MaxWorkerId {
- panic("worker id is invalid")
+ return nil, fmt.Errorf("Worker id %v is invalid", workerId)
}
- return &SnowFlake{workerId: workerId}
+ return &SnowFlake{workerId: workerId}, nil
}
func timestamp() uint64 {
View
91 gosnow_test.go
@@ -5,56 +5,67 @@ github.com/twitter/snowflake in golang
package gosnow
import (
- "testing"
+ "testing"
+ "github.com/stretchrcom/testify/assert"
)
func TestDefaultWorkId(t *testing.T) {
- id := DefaultWorkId()
- id2 := DefaultWorkId()
- t.Logf("id %v, next id %v", id, id2)
+ id := DefaultWorkId()
+ id2 := DefaultWorkId()
+ t.Logf("id %v, next id %v", id, id2)
- if id != id2 {
- t.Errorf("different workd id, %v and %v", id, id2)
- }
+ if id != id2 {
+ t.Errorf("different workd id, %v and %v", id, id2)
+ }
}
func TestNext(t *testing.T) {
- sf := Default()
- id := sf.Next()
- id2 := sf.Next()
- t.Logf("id %v, next id %v", id, id2)
-
- if id > id2 {
- t.Errorf("id %v is smaller then previous one %v", id2, id)
- }
+ sf, err := Default()
+ assert.Nil(t, err)
+
+ id, err := sf.Next()
+ assert.Nil(t, err)
+
+ id2, err := sf.Next()
+ assert.Nil(t, err)
+
+ t.Logf("id %v, next id %v", id, id2)
+
+ if id > id2 {
+ t.Errorf("id %v is smaller then previous one %v", id2, id)
+ }
}
func TestDuplicate(t *testing.T) {
- total := 1000 * 1000
- data := make(map[uint64]int)
- sf := Default()
-
- var id, pre uint64
- for i := 0; i < total; i++ {
- id = sf.Next()
-
- if id < pre {
- t.Errorf("id %v is samller than previous one %v", id, pre)
- }
- pre = id
-
- count := data[id]
- if count > 0 {
- t.Errorf("duplicate id %v %d", id, count)
- }
- data[id] = count + 1
- }
-
- length := len(data)
- t.Logf("map length %v", length)
- if length != total {
- t.Errorf("legth does not match want %v actual %d", total, length)
- }
+ total := 1000 * 1000
+ data := make(map[uint64]int)
+
+ sf, err := Default()
+ assert.Nil(t, err)
+
+ var id, pre uint64
+ for i := 0; i < total; i++ {
+
+ id, err = sf.Next()
+ assert.Nil(t, err)
+
+ if id < pre {
+ t.Errorf("id %v is samller than previous one %v", id, pre)
+ }
+ pre = id
+
+ count := data[id]
+ if count > 0 {
+ t.Errorf("duplicate id %v %d", id, count)
+ }
+ data[id] = count + 1
+ }
+
+ length := len(data)
+ t.Logf("map length %v", length)
+ if length != total {
+ t.Errorf("legth does not match want %v actual %d", total, length)
+ }
}

0 comments on commit 3a05c41

Please sign in to comment.