2012-05-22 20:05:39 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2013-08-29 18:08:05 +08:00
|
|
|
"fmt"
|
2013-08-29 16:51:23 +08:00
|
|
|
"testing"
|
2012-05-22 20:05:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var client *Client
|
|
|
|
|
2013-08-29 18:08:05 +08:00
|
|
|
func printHandle(resp *Response) {
|
|
|
|
fmt.Printf("%V", resp)
|
|
|
|
}
|
|
|
|
|
2012-05-22 20:05:39 +08:00
|
|
|
func TestClientAddServer(t *testing.T) {
|
2013-08-29 16:51:23 +08:00
|
|
|
t.Log("Add local server 127.0.0.1:4730")
|
|
|
|
var err error
|
2013-08-29 18:08:05 +08:00
|
|
|
if client, err = New("tcp4", "127.0.0.1:4730"); err != nil {
|
2013-08-29 16:51:23 +08:00
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2013-08-29 18:08:05 +08:00
|
|
|
client.ErrorHandler = func(e error) {
|
2013-08-29 16:51:23 +08:00
|
|
|
t.Log(e)
|
|
|
|
}
|
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 TestClientEcho(t *testing.T) {
|
2013-08-29 18:08:05 +08:00
|
|
|
err := client.Echo([]byte("Hello world"), printHandle)
|
2013-08-29 16:51:23 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2013-01-14 17:59:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientDoBg(t *testing.T) {
|
2013-08-29 16:51:23 +08:00
|
|
|
if handle := client.DoBg("ToUpper", []byte("abcdef"),
|
|
|
|
JOB_LOW); handle == "" {
|
|
|
|
t.Error("Handle is empty.")
|
|
|
|
}
|
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-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
|
|
|
|
}
|
|
|
|
if handle := client.Do("ToUpper", []byte("abcdef"),
|
|
|
|
JOB_LOW, jobHandler); handle == "" {
|
|
|
|
t.Error("Handle is empty.")
|
|
|
|
} else {
|
|
|
|
t.Log(handle)
|
|
|
|
}
|
2012-05-22 20:05:39 +08:00
|
|
|
}
|
2013-01-14 17:59:48 +08:00
|
|
|
|
|
|
|
func TestClientStatus(t *testing.T) {
|
|
|
|
|
2013-08-29 18:08:05 +08:00
|
|
|
err := client.Status("handle not exists", printHandle)
|
2013-08-29 16:51:23 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2013-08-29 18:08:05 +08:00
|
|
|
/*
|
|
|
|
if s1.Known {
|
|
|
|
t.Errorf("The job (%s) shouldn't be known.", s1.Handle)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s1.Running {
|
|
|
|
t.Errorf("The job (%s) shouldn't be running.", s1.Handle)
|
|
|
|
return
|
|
|
|
}*/
|
2013-01-14 17:59:48 +08:00
|
|
|
|
2013-08-29 16:51:23 +08:00
|
|
|
handle := client.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil)
|
2013-08-29 18:08:05 +08:00
|
|
|
err = client.Status(handle, printHandle)
|
|
|
|
/*
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !s2.Known {
|
|
|
|
t.Errorf("The job (%s) should be known.", s2.Handle)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s2.Running {
|
|
|
|
t.Errorf("The job (%s) shouldn't be running.", s2.Handle)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*/
|
2013-01-14 17:59:48 +08:00
|
|
|
}
|
|
|
|
|
2012-05-22 20:05:39 +08:00
|
|
|
func TestClientClose(t *testing.T) {
|
2013-08-29 16:51:23 +08:00
|
|
|
if err := client.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2012-05-22 20:05:39 +08:00
|
|
|
}
|