2011-05-16 17:26:32 +08:00
|
|
|
package gearman
|
2011-03-15 20:21:42 +08:00
|
|
|
|
|
|
|
import (
|
2011-05-16 17:26:32 +08:00
|
|
|
"testing"
|
2011-05-18 19:40:03 +08:00
|
|
|
"os"
|
2011-03-15 20:21:42 +08:00
|
|
|
)
|
|
|
|
|
2011-05-17 18:12:40 +08:00
|
|
|
var worker *Worker
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
worker = NewWorker()
|
|
|
|
}
|
|
|
|
|
2011-05-19 20:10:53 +08:00
|
|
|
func TestWorkerAddServer(t *testing.T) {
|
2011-05-16 17:26:32 +08:00
|
|
|
t.Log("Add local server 127.0.0.1:4730.")
|
|
|
|
if err := worker.AddServer("127.0.0.1:4730"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2011-05-17 18:22:12 +08:00
|
|
|
|
2011-05-18 19:40:03 +08:00
|
|
|
if l := len(worker.clients); l != 1 {
|
|
|
|
t.Log(worker.clients)
|
2011-05-16 17:26:32 +08:00
|
|
|
t.Error("The length of server list should be 1.")
|
|
|
|
}
|
2011-03-15 20:21:42 +08:00
|
|
|
}
|
2011-05-17 18:12:40 +08:00
|
|
|
|
2011-05-19 20:10:53 +08:00
|
|
|
func foobar(job *WorkerJob) ([]byte, os.Error) {
|
2011-05-18 19:40:03 +08:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2011-05-17 18:12:40 +08:00
|
|
|
|
2011-05-19 20:10:53 +08:00
|
|
|
func TestWorkerAddFunction(t *testing.T) {
|
2011-05-18 19:40:03 +08:00
|
|
|
if err := worker.AddFunction("foobar", foobar, 0); err != nil {
|
2011-05-17 18:12:40 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
2011-05-18 19:40:03 +08:00
|
|
|
if err := worker.AddFunction("timeout", foobar, 5); err != nil {
|
2011-05-17 20:32:36 +08:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if l := len(worker.functions); l != 2 {
|
2011-05-17 18:12:40 +08:00
|
|
|
t.Log(worker.functions)
|
2011-05-17 20:32:36 +08:00
|
|
|
t.Errorf("The length of function map should be %d.", 2)
|
2011-05-17 18:12:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-19 20:10:53 +08:00
|
|
|
func TestWorkerEcho(t * testing.T) {
|
2011-05-17 18:12:40 +08:00
|
|
|
if err := worker.Echo([]byte("Hello World")); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2011-05-18 19:40:03 +08:00
|
|
|
/*
|
2011-05-19 20:10:53 +08:00
|
|
|
func TestWorkerResult(t *testing.T) {
|
|
|
|
if job := worker.LastResult(); job == nil {
|
2011-05-18 19:40:03 +08:00
|
|
|
t.Error("Nothing in result.")
|
2011-05-17 18:22:12 +08:00
|
|
|
} else {
|
|
|
|
t.Log(job)
|
|
|
|
}
|
|
|
|
}
|
2011-05-18 19:40:03 +08:00
|
|
|
*/
|
|
|
|
|
2011-05-19 20:10:53 +08:00
|
|
|
func TestWorkerRemoveFunction(t * testing.T) {
|
2011-05-18 19:40:03 +08:00
|
|
|
if err := worker.RemoveFunction("foobar"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-19 20:10:53 +08:00
|
|
|
func TestWorkerReset(t * testing.T) {
|
2011-05-18 19:40:03 +08:00
|
|
|
if err := worker.Reset(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2011-05-17 18:22:12 +08:00
|
|
|
|
2011-05-19 20:10:53 +08:00
|
|
|
func TestWorkerClose(t *testing.T) {
|
2011-05-17 18:12:40 +08:00
|
|
|
if err := worker.Close(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|