gearman-go/client/client_test.go

97 lines
2.0 KiB
Go
Raw Permalink Normal View History

package client
import (
2013-04-24 16:58:06 +08:00
"time"
"testing"
)
var client *Client
func TestClientAddServer(t *testing.T) {
t.Log("Add local server 127.0.0.1:4730")
var err error
if client, err = New("127.0.0.1:4730"); err != nil {
t.Error(err)
2013-01-23 17:25:38 +08:00
return
}
client.ErrHandler = func(e error) {
t.Log(e)
}
}
2013-01-14 17:59:48 +08:00
func TestClientEcho(t *testing.T) {
2013-04-24 16:58:06 +08:00
echo, err := client.Echo([]byte("Hello world"), time.Second)
if err != nil {
t.Error(err)
return
}
if string(echo) != "Hello world" {
2013-01-15 17:55:44 +08:00
t.Errorf("Invalid echo data: %s", echo)
2013-04-24 16:58:06 +08:00
return
2013-01-14 17:59:48 +08:00
}
}
func TestClientDoBg(t *testing.T) {
2013-01-15 17:55:44 +08:00
if handle := client.DoBg("ToUpper", []byte("abcdef"),
JOB_LOW); handle == "" {
t.Error("Handle is empty.")
}
}
2013-01-14 17:59:48 +08:00
func TestClientDo(t *testing.T) {
2013-01-14 17:59:48 +08:00
jobHandler := func(job *Job) {
str := string(job.Data)
if str == "ABCDEF" {
t.Log(str)
} else {
t.Errorf("Invalid data: %s", job.Data)
}
return
}
2013-01-15 17:55:44 +08:00
if handle := client.Do("ToUpper", []byte("abcdef"),
JOB_LOW, jobHandler); handle == "" {
t.Error("Handle is empty.")
} else {
t.Log(handle)
}
}
2013-01-14 17:59:48 +08:00
func TestClientStatus(t *testing.T) {
2013-04-24 16:58:06 +08:00
s1, err := client.Status("handle not exists", time.Second)
if err != nil {
t.Error(err)
return
}
2013-01-15 17:55:44 +08:00
if s1.Known {
t.Errorf("The job (%s) shouldn't be known.", s1.Handle)
2013-04-24 16:58:06 +08:00
return
2013-01-15 17:55:44 +08:00
}
if s1.Running {
t.Errorf("The job (%s) shouldn't be running.", s1.Handle)
2013-04-24 16:58:06 +08:00
return
2013-01-15 17:55:44 +08:00
}
2013-01-14 17:59:48 +08:00
2013-01-15 17:55:44 +08:00
handle := client.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil);
2013-04-24 16:58:06 +08:00
s2, err := client.Status(handle, time.Second)
if err != nil {
t.Error(err)
return
}
2013-01-15 17:55:44 +08:00
if !s2.Known {
t.Errorf("The job (%s) should be known.", s2.Handle)
2013-04-24 16:58:06 +08:00
return
2013-01-15 17:55:44 +08:00
}
if s2.Running {
t.Errorf("The job (%s) shouldn't be running.", s2.Handle)
2013-04-24 16:58:06 +08:00
return
2013-01-14 17:59:48 +08:00
}
}
func TestClientClose(t *testing.T) {
if err := client.Close(); err != nil {
t.Error(err)
}
}