gearman-go/client/client_test.go
2013-01-14 17:59:48 +08:00

94 lines
2.2 KiB
Go

package client
import (
"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)
}
}
func TestClientEcho(t *testing.T) {
echoHandler = func(job *Job) {
echo := string(job.Data)
if echo == "Hello world" {
t.Log(echo)
} else {
t.Errorf("Invalid echo data: %s", job.Data)
}
return
}
client.Echo([]byte("Hello world"), echoHandler)
}
func TestClientDoBg(t *testing.T) {
if handle, err := client.DoBg("ToUpper", []byte("abcdef"),
JOB_LOW); err != nil {
t.Error(err)
} else {
t.Log(handle)
}
}
func TestClientDo(t *testing.T) {
jobHandler := func(job *Job) {
str := string(job.Data)
if str == "ABCDEF" {
t.Log(str)
} else {
t.Errorf("Invalid data: %s", job.Data)
}
return
}
if handle, err := client.Do("ToUpper", []byte("abcdef"),
JOB_LOW, jobHandler); err != nil {
t.Error(err)
} else {
t.Log(handle)
}
}
func TestClientStatus(t *testing.T) {
statusHandler = func(handler string, known bool,
running bool, numerator uint64, denominator uint64) {
if known {
t.Errorf("The job (%s) shouldn't be known.", handler)
}
if running {
t.Errorf("The job (%s) shouldn't be running.", handler)
}
}
client.Status("handle not exists", statusHandler)
if handle, err := client.Do("Delay5sec", []byte("abcdef"),
JOB_LOW, nil); err != nil {
t.Error(err)
} else {
t.Log(handle)
statusHandler = func(handler string, known bool,
running bool, numerator uint64, denominator uint64) {
if !known {
t.Errorf("The job (%s) shouldn be known.", handler)
}
if !running {
t.Errorf("The job (%s) shouldn be running.", handler)
}
}
client.Status(handle, statusHandler)
}
}
func TestClientClose(t *testing.T) {
if err := client.Close(); err != nil {
t.Error(err)
}
}