2013-01-08 17:23:10 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2013-04-24 16:58:06 +08:00
|
|
|
"time"
|
2013-01-24 18:13:02 +08:00
|
|
|
"testing"
|
2013-01-08 17:23:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pool = NewPool()
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPoolAdd(t *testing.T) {
|
|
|
|
t.Log("Add servers")
|
|
|
|
if err := pool.Add("127.0.0.1:4730", 1); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-06-09 12:46:47 +08:00
|
|
|
if err := pool.Add("127.0.0.1:4730", 1); err != nil {
|
2013-01-08 17:23:10 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2013-01-24 18:13:02 +08:00
|
|
|
if len(pool.clients) != 2 {
|
|
|
|
t.Errorf("2 servers expected, %d got.", len(pool.clients))
|
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-04-24 16:58:06 +08:00
|
|
|
echo, err := pool.Echo("", []byte("Hello pool"), time.Second)
|
2013-01-25 15:16:11 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2013-04-24 16:58:06 +08:00
|
|
|
if string(echo) != "Hello pool" {
|
2013-01-25 15:16:11 +08:00
|
|
|
t.Errorf("Invalid echo data: %s", echo)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-04-24 16:58:06 +08:00
|
|
|
_, err = pool.Echo("not exists", []byte("Hello pool"), time.Second)
|
2013-01-25 15:16:11 +08:00
|
|
|
if err != ErrNotFound {
|
|
|
|
t.Errorf("ErrNotFound expected, got %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPoolDoBg(t *testing.T) {
|
|
|
|
if addr, handle := pool.DoBg("ToUpper", []byte("abcdef"),
|
|
|
|
JOB_LOW); handle == "" {
|
|
|
|
t.Error("Handle is empty.")
|
|
|
|
} else {
|
|
|
|
t.Log(addr, handle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPoolDo(t *testing.T) {
|
|
|
|
jobHandler := func(job *Job) {
|
|
|
|
str := string(job.Data)
|
|
|
|
if str == "ABCDEF" {
|
|
|
|
t.Log(str)
|
2013-01-08 17:23:10 +08:00
|
|
|
} else {
|
2013-01-25 15:16:11 +08:00
|
|
|
t.Errorf("Invalid data: %s", job.Data)
|
2013-01-08 17:23:10 +08:00
|
|
|
}
|
2013-01-25 15:16:11 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if addr, handle := pool.Do("ToUpper", []byte("abcdef"),
|
|
|
|
JOB_LOW, jobHandler); handle == "" {
|
|
|
|
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-04-24 16:58:06 +08:00
|
|
|
s1, err := pool.Status("127.0.0.1:4730", "handle not exists", time.Second)
|
2013-01-25 15:16:11 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s1.Known {
|
|
|
|
t.Errorf("The job (%s) shouldn't be known.", s1.Handle)
|
|
|
|
}
|
|
|
|
if s1.Running {
|
|
|
|
t.Errorf("The job (%s) shouldn't be running.", s1.Handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, handle := pool.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil);
|
2013-04-24 16:58:06 +08:00
|
|
|
s2, err := pool.Status(addr, handle, time.Second)
|
2013-01-25 15:16:11 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !s2.Known {
|
|
|
|
t.Errorf("The job (%s) should be known.", s2.Handle)
|
|
|
|
}
|
|
|
|
if s2.Running {
|
|
|
|
t.Errorf("The job (%s) shouldn't be running.", s2.Handle)
|
|
|
|
}
|
|
|
|
|
2013-04-24 16:58:06 +08:00
|
|
|
_, err = pool.Status("not exists", "not exists", time.Second)
|
2013-01-25 15:16:11 +08:00
|
|
|
if err != ErrNotFound {
|
2013-01-08 17:23:10 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 18:13:02 +08:00
|
|
|
|
2013-01-08 17:23:10 +08:00
|
|
|
func TestPoolClose(t *testing.T) {
|
2013-01-18 17:03:45 +08:00
|
|
|
return
|
2013-01-08 17:23:10 +08:00
|
|
|
if err := pool.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|