removed outside dependency

This commit is contained in:
Xing Xing 2013-08-30 11:41:18 +08:00
commit 9daac76f67
5 changed files with 93 additions and 14 deletions

View File

@ -9,7 +9,6 @@ import (
"io"
"net"
"sync"
"github.com/mikespook/golib/idgen"
)
/*
@ -30,7 +29,6 @@ type Client struct {
mutex sync.RWMutex
ErrorHandler ErrorHandler
IdGen idgen.IdGen
}
// Create a new client.
@ -44,7 +42,6 @@ func New(net, addr string) (client *Client, err error) {
respHandler: make(map[string]ResponseHandler, QUEUE_SIZE),
innerHandler: make(map[string]ResponseHandler, QUEUE_SIZE),
in: make(chan []byte, QUEUE_SIZE),
IdGen: idgen.NewObjectId(),
}
if err = client.connect(); err != nil {
return
@ -191,7 +188,7 @@ func (client *Client) handleInner(key string, resp *Response) {
// Internal do
func (client *Client) do(funcname string, data []byte,
flag uint32) (handle string, err error) {
id := client.IdGen.Id().(string)
id := IdGen.Id()
req := getJob(id, []byte(funcname), data)
req.DataType = flag
client.write(req)

View File

@ -35,8 +35,12 @@ func TestClientEcho(t *testing.T) {
}
func TestClientDoBg(t *testing.T) {
if handle := client.DoBg("ToUpper", []byte("abcdef"),
JOB_LOW); handle == "" {
handle, err := client.DoBg("ToUpper", []byte("abcdef"), JOB_LOW)
if err != nil {
t.Error(err)
return
}
if handle == "" {
t.Error("Handle is empty.")
} else {
t.Log(handle)
@ -53,8 +57,13 @@ func TestClientDo(t *testing.T) {
}
return
}
if handle := client.Do("ToUpper", []byte("abcdef"),
JOB_LOW, jobHandler); handle == "" {
handle, err := client.Do("ToUpper", []byte("abcdef"),
JOB_LOW, jobHandler)
if err != nil {
t.Error(err)
return
}
if handle == "" {
t.Error("Handle is empty.")
} else {
t.Log(handle)
@ -76,7 +85,11 @@ func TestClientStatus(t *testing.T) {
return
}
handle := client.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil)
handle, err := client.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil)
if err != nil {
t.Error(err)
return
}
status, err = client.Status(handle)
if err != nil {
t.Error(err)

37
client/id.go Normal file
View File

@ -0,0 +1,37 @@
package client
import (
"time"
"strconv"
"sync/atomic"
)
var (
IdGen IdGenerator
)
func init() {
IdGen = NewAutoIncId()
}
type IdGenerator interface {
Id() string
}
// AutoIncId
type autoincId struct {
value int64
}
func (ai *autoincId) Id() string {
next := atomic.AddInt64(&ai.value, 1)
return strconv.FormatInt(next, 10)
}
func NewAutoIncId() IdGenerator {
// we'll consider the nano fraction of a second at startup unique
// and count up from there.
return &autoincId{
value: int64(time.Now().Nanosecond()) << 32,
}
}

18
client/id_test.go Normal file
View File

@ -0,0 +1,18 @@
package client
import (
"testing"
)
func TestAutoInc(t *testing.T) {
ai := NewAutoIncId()
previous := ai.Id()
for i := 0; i < 10; i++ {
id := ai.Id()
if id == previous {
t.Errorf("Id not unique, previous and current %s", id)
}
previous = id
}
}

View File

@ -39,8 +39,13 @@ func TestPoolEcho(t *testing.T) {
}
func TestPoolDoBg(t *testing.T) {
if addr, handle := pool.DoBg("ToUpper", []byte("abcdef"),
JOB_LOW); handle == "" {
addr, handle, err := pool.DoBg("ToUpper",
[]byte("abcdef"), JOB_LOW);
if err != nil {
t.Error(err)
return
}
if handle == "" {
t.Error("Handle is empty.")
} else {
t.Log(addr, handle)
@ -57,8 +62,12 @@ func TestPoolDo(t *testing.T) {
}
return
}
if addr, handle := pool.Do("ToUpper", []byte("abcdef"),
JOB_LOW, jobHandler); handle == "" {
addr, handle, err := pool.Do("ToUpper",
[]byte("abcdef"), JOB_LOW, jobHandler)
if err != nil {
t.Error(err)
}
if handle == "" {
t.Error("Handle is empty.")
} else {
t.Log(addr, handle)
@ -77,7 +86,12 @@ func TestPoolStatus(t *testing.T) {
if status.Running {
t.Errorf("The job (%s) shouldn't be running.", status.Handle)
}
addr, handle := pool.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil)
addr, handle, err := pool.Do("Delay5sec",
[]byte("abcdef"), JOB_LOW, nil)
if err != nil {
t.Error(err)
return
}
status, err = pool.Status(addr, handle)
if err != nil {
t.Error(err)