From f9447f9fc66bbedac1ee310c66d9b546ab6674ad Mon Sep 17 00:00:00 2001 From: mikespook Date: Thu, 11 Apr 2013 13:28:00 +0800 Subject: [PATCH] add testing cases for both client and worker; #4, #8 closed --- gearman_test.go | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 gearman_test.go diff --git a/gearman_test.go b/gearman_test.go new file mode 100644 index 0000000..4c132bb --- /dev/null +++ b/gearman_test.go @@ -0,0 +1,96 @@ +// Copyright 2011 Xing Xing 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 ( + "fmt" + "time" + "errors" + "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." + UPPERSTR = "THE GEARMAN-GO IS A PURE GO IMPLEMENTED LIRBRARY." + 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) + 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 + } + go w.Work() + + c, err := client.New(GEARMAND) + if err != nil { + t.Error(err) + return + } + defer c.Close() + + c.ErrHandler = func(e error) { + panic(e) + } + + jobHandler := func(job *client.Job) { + if (string(job.Data) != UPPERSTR) { + panic(errors.New(fmt.Sprintf("%s expected, got %s", UPPERSTR, job.Data))) + } + } + + { + handle := c.Do("ToUpper", []byte(STR), client.JOB_NORMAL, jobHandler) + status := c.Status(handle) + + if !status.Known { + t.Errorf("%s should be known", status.Handle) + return + } + } + + { + handle := c.DoBg("Sleep", nil, client.JOB_NORMAL) + status := c.Status(handle) + + if !status.Known { + t.Errorf("%s should be known", status.Handle) + return + } + + if !status.Running { + t.Errorf("%s shouldn be running", status.Handle) + } + } +}