gearman-go/gearman_test.go

145 lines
2.6 KiB
Go
Raw Normal View History

// Copyright 2011 Xing Xing <mikespook@gmail.com> All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
/*
2013-08-30 12:36:57 +08:00
This module is Gearman API for golang.
The protocol was implemented by native way.
*/
package gearman
import (
2013-08-30 12:36:57 +08:00
"github.com/mikespook/gearman-go/client"
"github.com/mikespook/gearman-go/worker"
"strings"
"sync"
"testing"
"time"
)
2013-08-30 12:36:57 +08:00
const (
STR = "The gearman-go is a pure go implemented library."
GEARMAND = "127.0.0.1:4730"
)
2013-08-30 18:01:10 +08:00
func ToUpper(job worker.Job) ([]byte, error) {
data := job.Data()
data = []byte(strings.ToUpper(string(data)))
2013-08-30 12:36:57 +08:00
return data, nil
}
2013-08-30 18:01:10 +08:00
func Sleep(job worker.Job) ([]byte, error) {
2013-08-30 12:36:57 +08:00
time.Sleep(time.Second * 5)
return nil, nil
}
func TestJobs(t *testing.T) {
2013-08-30 12:36:57 +08:00
w := worker.New(worker.Unlimited)
2013-08-30 18:01:10 +08:00
if err := w.AddServer("tcp4", GEARMAND); err != nil {
2013-08-30 12:36:57 +08:00
t.Error(err)
return
}
defer w.Close()
2013-08-30 11:20:51 +08:00
t.Log("Servers added...")
2013-08-30 12:36:57 +08:00
if err := w.AddFunc("ToUpper", ToUpper, 0); err != nil {
t.Error(err)
return
}
if err := w.AddFunc("Sleep", Sleep, 0); err != nil {
t.Error(err)
return
}
2013-08-30 11:20:51 +08:00
t.Log("Functions added...")
2013-08-30 18:01:10 +08:00
w.ErrorHandler = func(e error) {
2013-08-30 12:36:57 +08:00
t.Error(e)
}
2013-12-24 16:37:20 +08:00
if err := w.Ready(); err != nil {
t.Error(err)
return
}
2013-08-30 12:36:57 +08:00
go w.Work()
2013-08-30 11:20:51 +08:00
t.Log("Worker is running...")
2013-08-30 11:20:51 +08:00
c, err := client.New("tcp4", GEARMAND)
2013-08-30 12:36:57 +08:00
if err != nil {
t.Error(err)
return
}
defer c.Close()
c.ErrorHandler = func(e error) {
t.Log(e)
}
{
var w sync.WaitGroup
jobHandler := func(job *client.Response) {
upper := strings.ToUpper(STR)
if string(job.Data) != upper {
t.Errorf("%s expected, got %s", upper, job.Data)
}
w.Done()
}
w.Add(1)
handle, err := c.Do("ToUpper", []byte(STR), client.JOB_NORMAL, jobHandler)
if err != nil {
t.Error(err)
return
}
w.Wait()
status, err := c.Status(handle)
if err != nil {
t.Error(err)
return
}
if status.Known {
t.Errorf("%s shouldn't be known", status.Handle)
return
}
if status.Running {
t.Errorf("%s shouldn't be running", status.Handle)
}
}
{
handle, err := c.DoBg("Sleep", nil, client.JOB_NORMAL)
2013-08-30 11:20:51 +08:00
if err != nil {
t.Error(err)
return
}
2013-08-30 12:36:57 +08:00
time.Sleep(time.Second)
status, err := c.Status(handle)
2013-08-30 11:20:51 +08:00
if err != nil {
t.Error(err)
return
}
2013-08-30 12:36:57 +08:00
if !status.Known {
t.Errorf("%s should be known", status.Handle)
return
}
if !status.Running {
t.Errorf("%s should be running", status.Handle)
}
}
{
status, err := c.Status("not exists handle")
if err != nil {
t.Error(err)
return
}
if status.Known {
t.Errorf("%s shouldn't be known", status.Handle)
return
}
if status.Running {
t.Errorf("%s shouldn't be running", status.Handle)
}
}
}