2012-05-22 20:05:39 +08:00
|
|
|
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
|
2012-05-23 15:22:29 +08:00
|
|
|
if client, err = New("127.0.0.1:4730"); err != nil {
|
2012-05-22 20:05:39 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 17:59:48 +08:00
|
|
|
|
2012-05-22 20:05:39 +08:00
|
|
|
func TestClientEcho(t *testing.T) {
|
2013-01-14 17:59:48 +08:00
|
|
|
echoHandler = func(job *Job) {
|
2012-05-22 20:05:39 +08:00
|
|
|
echo := string(job.Data)
|
|
|
|
if echo == "Hello world" {
|
|
|
|
t.Log(echo)
|
|
|
|
} else {
|
|
|
|
t.Errorf("Invalid echo data: %s", job.Data)
|
|
|
|
}
|
2013-01-14 17:59:48 +08:00
|
|
|
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)
|
2012-05-22 20:05:39 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-14 17:59:48 +08:00
|
|
|
|
2012-05-22 20:05:39 +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
|
|
|
|
}
|
|
|
|
if handle, err := client.Do("ToUpper", []byte("abcdef"),
|
|
|
|
JOB_LOW, jobHandler); err != nil {
|
2012-05-22 20:05:39 +08:00
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Log(handle)
|
|
|
|
}
|
|
|
|
}
|
2013-01-14 17:59:48 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-22 20:05:39 +08:00
|
|
|
func TestClientClose(t *testing.T) {
|
|
|
|
if err := client.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|