gearman-go/client/pool_test.go

122 lines
2.2 KiB
Go
Raw Normal View History

2013-01-08 17:23:10 +08:00
package client
import (
2013-08-29 16:51:23 +08:00
"testing"
2013-01-08 17:23:10 +08:00
)
var (
2013-08-29 16:51:23 +08:00
pool = NewPool()
2013-01-08 17:23:10 +08:00
)
func TestPoolAdd(t *testing.T) {
2013-08-29 16:51:23 +08:00
t.Log("Add servers")
2013-09-22 22:02:05 +08:00
c := 2
2013-08-29 18:08:05 +08:00
if err := pool.Add("tcp4", "127.0.0.1:4730", 1); err != nil {
2013-09-22 22:02:05 +08:00
t.Fatal(err)
2013-08-29 16:51:23 +08:00
}
2013-08-30 11:20:51 +08:00
if err := pool.Add("tcp4", "127.0.1.1:4730", 1); err != nil {
2013-09-22 22:02:05 +08:00
t.Log(err)
c -= 1
2013-08-29 16:51:23 +08:00
}
2013-09-22 22:02:05 +08:00
if len(pool.clients) != c {
t.Errorf("%d servers expected, %d got.", c, len(pool.clients))
2013-08-29 16:51:23 +08:00
}
2013-01-08 17:23:10 +08:00
}
2013-01-23 17:25:38 +08:00
2013-01-08 17:23:10 +08:00
func TestPoolEcho(t *testing.T) {
2013-08-30 11:20:51 +08:00
echo, err := pool.Echo("", []byte(TestStr))
2013-08-29 16:51:23 +08:00
if err != nil {
t.Error(err)
return
}
2013-08-30 11:20:51 +08:00
if string(echo) != TestStr {
2013-08-29 16:51:23 +08:00
t.Errorf("Invalid echo data: %s", echo)
return
}
2013-01-25 15:16:11 +08:00
2013-08-30 11:20:51 +08:00
_, err = pool.Echo("not exists", []byte(TestStr))
2013-08-29 16:51:23 +08:00
if err != ErrNotFound {
t.Errorf("ErrNotFound expected, got %s", err)
}
2013-01-25 15:16:11 +08:00
}
func TestPoolDoBg(t *testing.T) {
2013-08-30 11:41:18 +08:00
addr, handle, err := pool.DoBg("ToUpper",
[]byte("abcdef"), JobLow)
2013-08-30 11:41:18 +08:00
if err != nil {
t.Error(err)
return
}
if handle == "" {
2013-08-29 16:51:23 +08:00
t.Error("Handle is empty.")
} else {
t.Log(addr, handle)
}
2013-01-25 15:16:11 +08:00
}
func TestPoolDo(t *testing.T) {
2013-08-29 18:08:05 +08:00
jobHandler := func(job *Response) {
2013-08-29 16:51:23 +08:00
str := string(job.Data)
if str == "ABCDEF" {
t.Log(str)
} else {
t.Errorf("Invalid data: %s", job.Data)
}
return
}
2013-08-30 11:41:18 +08:00
addr, handle, err := pool.Do("ToUpper",
[]byte("abcdef"), JobLow, jobHandler)
2013-08-30 11:41:18 +08:00
if err != nil {
t.Error(err)
}
if handle == "" {
2013-08-29 16:51:23 +08:00
t.Error("Handle is empty.")
} else {
t.Log(addr, handle)
}
2013-01-08 17:23:10 +08:00
}
2013-01-24 18:13:02 +08:00
2013-01-25 15:16:11 +08:00
func TestPoolStatus(t *testing.T) {
2013-08-30 11:20:51 +08:00
status, err := pool.Status("127.0.0.1:4730", "handle not exists")
2013-08-29 16:51:23 +08:00
if err != nil {
t.Error(err)
return
}
2013-08-30 11:20:51 +08:00
if status.Known {
t.Errorf("The job (%s) shouldn't be known.", status.Handle)
}
if status.Running {
t.Errorf("The job (%s) shouldn't be running.", status.Handle)
}
2013-08-30 11:41:18 +08:00
addr, handle, err := pool.Do("Delay5sec",
[]byte("abcdef"), JobLow, nil)
2013-08-30 11:41:18 +08:00
if err != nil {
t.Error(err)
return
}
2013-08-30 11:20:51 +08:00
status, err = pool.Status(addr, handle)
2013-08-29 16:51:23 +08:00
if err != nil {
t.Error(err)
return
}
2013-08-30 11:20:51 +08:00
if !status.Known {
t.Errorf("The job (%s) should be known.", status.Handle)
}
if status.Running {
t.Errorf("The job (%s) shouldn't be running.", status.Handle)
}
status, err = pool.Status("not exists", "not exists")
2013-08-29 16:51:23 +08:00
if err != ErrNotFound {
t.Error(err)
2013-08-30 11:20:51 +08:00
return
2013-08-29 16:51:23 +08:00
}
2013-01-08 17:23:10 +08:00
}
2013-01-24 18:13:02 +08:00
2013-01-08 17:23:10 +08:00
func TestPoolClose(t *testing.T) {
2013-08-29 16:51:23 +08:00
return
if err := pool.Close(); err != nil {
t.Error(err)
}
2013-01-08 17:23:10 +08:00
}