|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- // 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.
-
- /*
- This module is Gearman API for golang.
- The protocol was implemented by native way.
- */
-
- package gearman
-
- import (
- "time"
- "sync"
- "testing"
- "strings"
- "github.com/mikespook/gearman-go/client"
- "github.com/mikespook/gearman-go/worker"
- )
-
- const(
- STR = "The gearman-go is a pure go implemented library."
- GEARMAND = "127.0.0.1:4730"
- )
-
- func ToUpper(job *worker.Job) ([]byte, error) {
- data := []byte(strings.ToUpper(string(job.Data)))
- return data, nil
- }
-
- func Sleep(job *worker.Job) ([]byte, error) {
- time.Sleep(time.Second * 5)
- return nil, nil
- }
-
-
- func TestJobs(t *testing.T) {
- w := worker.New(worker.Unlimited)
- if err := w.AddServer(GEARMAND); err != nil {
- t.Error(err)
- return
- }
- defer w.Close()
- 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
- }
-
- w.ErrHandler = func(e error) {
- t.Error(e)
- }
- go w.Work()
-
- c, err := client.New(GEARMAND)
- if err != nil {
- t.Error(err)
- return
- }
- defer c.Close()
-
- c.ErrHandler = func(e error) {
- // t.Error(e)
- t.Log(e)
- }
-
- {
- var w sync.WaitGroup
- jobHandler := func(job *client.Job) {
- upper := strings.ToUpper(STR)
- if (string(job.Data) != upper) {
- t.Error("%s expected, got %s", []byte(upper), job.Data)
- }
- w.Done()
- }
-
- w.Add(1)
- handle := c.Do("ToUpper", []byte(STR), client.JOB_NORMAL, jobHandler)
- w.Wait()
- status, err := c.Status(handle, time.Second)
- 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 := c.DoBg("Sleep", nil, client.JOB_NORMAL)
- time.Sleep(time.Second)
- status, err := c.Status(handle, time.Second)
- if err != nil {
- t.Error(err)
- return
- }
-
- 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", time.Second)
- 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)
- }
- }
- }
|