gearman-go/worker/worker_test.go

83 lines
1.5 KiB
Go
Raw Normal View History

package worker
2013-12-23 17:01:01 +08:00
import (
2013-12-23 17:05:42 +08:00
"sync"
"testing"
2013-12-23 17:01:01 +08:00
)
var worker *Worker
func init() {
2013-08-30 12:36:57 +08:00
worker = New(Unlimited)
}
2013-12-26 12:06:47 +08:00
func TestWorkerErrNoneAgents(t *testing.T) {
err := worker.Ready()
if err != ErrNoneAgents {
t.Error("ErrNoneAgents expected.")
}
}
func TestWorkerAddServer(t *testing.T) {
2013-08-30 12:36:57 +08:00
t.Log("Add local server 127.0.0.1:4730.")
2013-08-30 18:01:10 +08:00
if err := worker.AddServer("tcp4", "127.0.0.1:4730"); err != nil {
2013-08-30 12:36:57 +08:00
t.Error(err)
}
if l := len(worker.agents); l != 1 {
t.Log(worker.agents)
t.Error("The length of server list should be 1.")
}
}
2013-12-26 12:06:47 +08:00
func TestWorkerErrNoneFuncs(t *testing.T) {
err := worker.Ready()
if err != ErrNoneFuncs {
t.Error("ErrNoneFuncs expected.")
}
}
2013-08-30 18:01:10 +08:00
func foobar(job Job) ([]byte, error) {
2013-08-30 12:36:57 +08:00
return nil, nil
}
func TestWorkerAddFunction(t *testing.T) {
2013-08-30 12:36:57 +08:00
if err := worker.AddFunc("foobar", foobar, 0); err != nil {
t.Error(err)
}
if err := worker.AddFunc("timeout", foobar, 5); err != nil {
t.Error(err)
}
if l := len(worker.funcs); l != 2 {
t.Log(worker.funcs)
t.Errorf("The length of function map should be %d.", 2)
}
}
2012-05-24 19:21:30 +08:00
func TestWorkerRemoveFunc(t *testing.T) {
2013-08-30 12:36:57 +08:00
if err := worker.RemoveFunc("foobar"); err != nil {
t.Error(err)
}
}
2013-08-30 18:01:10 +08:00
func TestWork(t *testing.T) {
2013-12-23 17:01:01 +08:00
var wg sync.WaitGroup
worker.JobHandler = func(job Job) error {
t.Logf("%s", job.Data())
wg.Done()
return nil
}
if err := worker.Ready(); err != nil {
t.Error(err)
return
}
2013-08-30 18:01:10 +08:00
go worker.Work()
2013-12-23 17:01:01 +08:00
wg.Add(1)
worker.Echo([]byte("Hello"))
wg.Wait()
2013-08-30 18:01:10 +08:00
}
func TestWorkerClose(t *testing.T) {
worker.Close()
}