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.6 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "github.com/mikespook/gearman-go/client"
  11. "github.com/mikespook/gearman-go/worker"
  12. "strings"
  13. "sync"
  14. "testing"
  15. "time"
  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 := job.Data()
  23. data = []byte(strings.ToUpper(string(data)))
  24. return data, nil
  25. }
  26. func Sleep(job worker.Job) ([]byte, error) {
  27. time.Sleep(time.Second * 5)
  28. return nil, nil
  29. }
  30. func TestJobs(t *testing.T) {
  31. w := worker.New(worker.Unlimited)
  32. if err := w.AddServer("tcp4", GEARMAND); err != nil {
  33. t.Error(err)
  34. return
  35. }
  36. defer w.Close()
  37. t.Log("Servers added...")
  38. if err := w.AddFunc("ToUpper", ToUpper, 0); err != nil {
  39. t.Error(err)
  40. return
  41. }
  42. if err := w.AddFunc("Sleep", Sleep, 0); err != nil {
  43. t.Error(err)
  44. return
  45. }
  46. t.Log("Functions added...")
  47. w.ErrorHandler = func(e error) {
  48. t.Error(e)
  49. }
  50. if err := w.Ready(); err != nil {
  51. t.Error(err)
  52. return
  53. }
  54. go w.Work()
  55. t.Log("Worker is running...")
  56. c, err := client.New("tcp4", GEARMAND)
  57. if err != nil {
  58. t.Error(err)
  59. return
  60. }
  61. defer c.Close()
  62. c.ErrorHandler = func(e error) {
  63. t.Log(e)
  64. }
  65. {
  66. var w sync.WaitGroup
  67. jobHandler := func(job *client.Response) {
  68. upper := strings.ToUpper(STR)
  69. if string(job.Data) != upper {
  70. t.Errorf("%s expected, got %s", upper, job.Data)
  71. }
  72. w.Done()
  73. }
  74. w.Add(1)
  75. handle, err := c.Do("ToUpper", []byte(STR), client.JOB_NORMAL, jobHandler)
  76. if err != nil {
  77. t.Error(err)
  78. return
  79. }
  80. w.Wait()
  81. status, err := c.Status(handle)
  82. if err != nil {
  83. t.Error(err)
  84. return
  85. }
  86. if status.Known {
  87. t.Errorf("%s shouldn't be known", status.Handle)
  88. return
  89. }
  90. if status.Running {
  91. t.Errorf("%s shouldn't be running", status.Handle)
  92. }
  93. }
  94. {
  95. handle, err := c.DoBg("Sleep", nil, client.JOB_NORMAL)
  96. if err != nil {
  97. t.Error(err)
  98. return
  99. }
  100. time.Sleep(time.Second)
  101. status, err := c.Status(handle)
  102. if err != nil {
  103. t.Error(err)
  104. return
  105. }
  106. if !status.Known {
  107. t.Errorf("%s should be known", status.Handle)
  108. return
  109. }
  110. if !status.Running {
  111. t.Errorf("%s should be running", status.Handle)
  112. }
  113. }
  114. {
  115. status, err := c.Status("not exists handle")
  116. if err != nil {
  117. t.Error(err)
  118. return
  119. }
  120. if status.Known {
  121. t.Errorf("%s shouldn't be known", status.Handle)
  122. return
  123. }
  124. if status.Running {
  125. t.Errorf("%s shouldn't be running", status.Handle)
  126. }
  127. }
  128. }