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.
 
 
 

141 lines
2.6 KiB

  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. go w.Work()
  51. t.Log("Worker is running...")
  52. c, err := client.New("tcp4", GEARMAND)
  53. if err != nil {
  54. t.Error(err)
  55. return
  56. }
  57. defer c.Close()
  58. c.ErrorHandler = func(e error) {
  59. t.Log(e)
  60. }
  61. {
  62. var w sync.WaitGroup
  63. jobHandler := func(job *client.Response) {
  64. upper := strings.ToUpper(STR)
  65. if string(job.Data) != upper {
  66. t.Errorf("%s expected, got %s", upper, job.Data)
  67. }
  68. w.Done()
  69. }
  70. w.Add(1)
  71. handle, err := c.Do("ToUpper", []byte(STR), client.JOB_NORMAL, jobHandler)
  72. if err != nil {
  73. t.Error(err)
  74. return
  75. }
  76. w.Wait()
  77. status, err := c.Status(handle)
  78. if err != nil {
  79. t.Error(err)
  80. return
  81. }
  82. if status.Known {
  83. t.Errorf("%s shouldn't be known", status.Handle)
  84. return
  85. }
  86. if status.Running {
  87. t.Errorf("%s shouldn't be running", status.Handle)
  88. }
  89. }
  90. {
  91. handle, err := c.DoBg("Sleep", nil, client.JOB_NORMAL)
  92. if err != nil {
  93. t.Error(err)
  94. return
  95. }
  96. time.Sleep(time.Second)
  97. status, err := c.Status(handle)
  98. if err != nil {
  99. t.Error(err)
  100. return
  101. }
  102. if !status.Known {
  103. t.Errorf("%s should be known", status.Handle)
  104. return
  105. }
  106. if !status.Running {
  107. t.Errorf("%s should be running", status.Handle)
  108. }
  109. }
  110. {
  111. status, err := c.Status("not exists handle")
  112. if err != nil {
  113. t.Error(err)
  114. return
  115. }
  116. if status.Known {
  117. t.Errorf("%s shouldn't be known", status.Handle)
  118. return
  119. }
  120. if status.Running {
  121. t.Errorf("%s shouldn't be running", status.Handle)
  122. }
  123. }
  124. }