You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gearman_test.go 2.9 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2011 Xing Xing <mikespook@gmail.com> All rights reserved.
  2. // Use of this source code is governed by a MIT
  3. // license that can be found in the LICENSE file.
  4. /*
  5. This module is Gearman API for golang.
  6. The protocol was implemented by native way.
  7. */
  8. package gearman
  9. import (
  10. "time"
  11. "sync"
  12. "testing"
  13. "strings"
  14. "github.com/mikespook/gearman-go/client"
  15. "github.com/mikespook/gearman-go/worker"
  16. )
  17. const(
  18. STR = "The gearman-go is a pure go implemented library."
  19. GEARMAND = "127.0.0.1:4730"
  20. )
  21. func ToUpper(job *worker.Job) ([]byte, error) {
  22. data := []byte(strings.ToUpper(string(job.Data)))
  23. return data, nil
  24. }
  25. func Sleep(job *worker.Job) ([]byte, error) {
  26. time.Sleep(time.Second * 5)
  27. return nil, nil
  28. }
  29. func TestJobs(t *testing.T) {
  30. w := worker.New(worker.Unlimited)
  31. if err := w.AddServer(GEARMAND); err != nil {
  32. t.Error(err)
  33. return
  34. }
  35. defer w.Close()
  36. if err := w.AddFunc("ToUpper", ToUpper, 0); err != nil {
  37. t.Error(err)
  38. return
  39. }
  40. if err := w.AddFunc("Sleep", Sleep, 0); err != nil {
  41. t.Error(err)
  42. return
  43. }
  44. w.ErrHandler = func(e error) {
  45. t.Error(e)
  46. }
  47. go w.Work()
  48. c, err := client.New(GEARMAND)
  49. if err != nil {
  50. t.Error(err)
  51. return
  52. }
  53. defer c.Close()
  54. c.ErrHandler = func(e error) {
  55. // t.Error(e)
  56. t.Log(e)
  57. }
  58. {
  59. var w sync.WaitGroup
  60. jobHandler := func(job *client.Job) {
  61. upper := strings.ToUpper(STR)
  62. if (string(job.Data) != upper) {
  63. t.Error("%s expected, got %s", []byte(upper), job.Data)
  64. }
  65. w.Done()
  66. }
  67. w.Add(1)
  68. handle := c.Do("ToUpper", []byte(STR), client.JOB_NORMAL, jobHandler)
  69. w.Wait()
  70. status, err := c.Status(handle, time.Second)
  71. if err != nil {
  72. t.Error(err)
  73. return
  74. }
  75. if status.Known {
  76. t.Errorf("%s shouldn't be known", status.Handle)
  77. return
  78. }
  79. if status.Running {
  80. t.Errorf("%s shouldn't be running", status.Handle)
  81. }
  82. }
  83. {
  84. handle := c.DoBg("Sleep", nil, client.JOB_NORMAL)
  85. time.Sleep(time.Second)
  86. status, err := c.Status(handle, time.Second)
  87. if err != nil {
  88. t.Error(err)
  89. return
  90. }
  91. if !status.Known {
  92. t.Errorf("%s should be known", status.Handle)
  93. return
  94. }
  95. if !status.Running {
  96. t.Errorf("%s should be running", status.Handle)
  97. }
  98. }
  99. {
  100. status, err := c.Status("not exists handle", time.Second)
  101. if err != nil {
  102. t.Error(err)
  103. return
  104. }
  105. if status.Known {
  106. t.Errorf("%s shouldn't be known", status.Handle)
  107. return
  108. }
  109. if status.Running {
  110. t.Errorf("%s shouldn't be running", status.Handle)
  111. }
  112. }
  113. }