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.
 
 
 

226 lines
4.3 KiB

  1. package worker
  2. import (
  3. "bytes"
  4. "sync"
  5. "testing"
  6. "time"
  7. )
  8. var worker *Worker
  9. func init() {
  10. worker = New(Unlimited)
  11. }
  12. func TestWorkerErrNoneAgents(t *testing.T) {
  13. err := worker.Ready()
  14. if err != ErrNoneAgents {
  15. t.Error("ErrNoneAgents expected.")
  16. }
  17. }
  18. func TestWorkerAddServer(t *testing.T) {
  19. t.Log("Add local server 127.0.0.1:4730.")
  20. if err := worker.AddServer(Network, "127.0.0.1:4730"); err != nil {
  21. t.Error(err)
  22. }
  23. if l := len(worker.agents); l != 1 {
  24. t.Log(worker.agents)
  25. t.Error("The length of server list should be 1.")
  26. }
  27. }
  28. func TestWorkerErrNoneFuncs(t *testing.T) {
  29. err := worker.Ready()
  30. if err != ErrNoneFuncs {
  31. t.Error("ErrNoneFuncs expected.")
  32. }
  33. }
  34. func foobar(job Job) ([]byte, error) {
  35. return nil, nil
  36. }
  37. func TestWorkerAddFunction(t *testing.T) {
  38. if err := worker.AddFunc("foobar", foobar, 0); err != nil {
  39. t.Error(err)
  40. }
  41. if err := worker.AddFunc("timeout", foobar, 5); err != nil {
  42. t.Error(err)
  43. }
  44. if l := len(worker.funcs); l != 2 {
  45. t.Log(worker.funcs)
  46. t.Errorf("The length of function map should be %d.", 2)
  47. }
  48. }
  49. func TestWorkerRemoveFunc(t *testing.T) {
  50. if err := worker.RemoveFunc("foobar"); err != nil {
  51. t.Error(err)
  52. }
  53. }
  54. func TestWork(t *testing.T) {
  55. var wg sync.WaitGroup
  56. worker.JobHandler = func(job Job) error {
  57. t.Logf("%s", job.Data())
  58. wg.Done()
  59. return nil
  60. }
  61. if err := worker.Ready(); err != nil {
  62. t.Error(err)
  63. return
  64. }
  65. go worker.Work()
  66. wg.Add(1)
  67. worker.Echo([]byte("Hello"))
  68. wg.Wait()
  69. }
  70. func TestLargeDataWork(t *testing.T) {
  71. worker := New(Unlimited)
  72. defer worker.Close()
  73. if err := worker.AddServer(Network, "127.0.0.1:4730"); err != nil {
  74. t.Error(err)
  75. }
  76. worker.Ready()
  77. l := 5714
  78. var wg sync.WaitGroup
  79. bigdataHandler := func(job Job) error {
  80. defer wg.Done()
  81. if len(job.Data()) != l {
  82. t.Errorf("expected length %d. got %d.", l, len(job.Data()))
  83. }
  84. return nil
  85. }
  86. if err := worker.AddFunc("bigdata", foobar, 0); err != nil {
  87. defer wg.Done()
  88. t.Error(err)
  89. }
  90. worker.JobHandler = bigdataHandler
  91. worker.ErrorHandler = func(err error) {
  92. t.Fatal("shouldn't have received an error")
  93. }
  94. if err := worker.Ready(); err != nil {
  95. t.Error(err)
  96. return
  97. }
  98. go worker.Work()
  99. wg.Add(1)
  100. // var cli *client.Client
  101. // var err error
  102. // if cli, err = client.New(client.Network, "127.0.0.1:4730"); err != nil {
  103. // t.Fatal(err)
  104. // }
  105. // cli.ErrorHandler = func(e error) {
  106. // t.Error(e)
  107. // }
  108. // _, err = cli.Do("bigdata", bytes.Repeat([]byte("a"), l), client.JobLow, func(res *client.Response) {
  109. // })
  110. // if err != nil {
  111. // t.Error(err)
  112. // }
  113. worker.Echo(bytes.Repeat([]byte("a"), l))
  114. wg.Wait()
  115. }
  116. func TestWorkerClose(t *testing.T) {
  117. worker.Close()
  118. }
  119. func TestWorkWithoutReady(t * testing.T){
  120. other_worker := New(Unlimited)
  121. if err := other_worker.AddServer(Network, "127.0.0.1:4730"); err != nil {
  122. t.Error(err)
  123. }
  124. if err := other_worker.AddFunc("gearman-go-workertest", foobar, 0); err != nil {
  125. t.Error(err)
  126. }
  127. timeout := make(chan bool, 1)
  128. done := make( chan bool, 1)
  129. other_worker.JobHandler = func( j Job ) error {
  130. if( ! other_worker.ready ){
  131. t.Error("Worker not ready as expected");
  132. }
  133. done <-true
  134. return nil
  135. }
  136. go func() {
  137. time.Sleep(5 * time.Second)
  138. timeout <- true
  139. }()
  140. go func(){
  141. other_worker.Work();
  142. }()
  143. // With the all-in-one Work() we don't know if the
  144. // worker is ready at this stage so we may have to wait a sec:
  145. go func(){
  146. tries := 5
  147. for( tries > 0 ){
  148. if other_worker.ready {
  149. other_worker.Echo([]byte("Hello"))
  150. break
  151. }
  152. // still waiting for it to be ready..
  153. time.Sleep(250 * time.Millisecond)
  154. tries--
  155. }
  156. }()
  157. // determine if we've finished or timed out:
  158. select{
  159. case <- timeout:
  160. t.Error("Test timed out waiting for the worker")
  161. case <- done:
  162. }
  163. }
  164. func TestWorkWithoutReadyWithPanic(t * testing.T){
  165. other_worker := New(Unlimited)
  166. timeout := make(chan bool, 1)
  167. done := make( chan bool, 1)
  168. // Going to work with no worker setup.
  169. // when Work (hopefully) calls Ready it will get an error which should cause it to panic()
  170. go func(){
  171. defer func() {
  172. if err := recover(); err != nil {
  173. done <- true
  174. return
  175. }
  176. t.Error("Work should raise a panic.")
  177. done <- true
  178. }()
  179. other_worker.Work();
  180. }()
  181. go func() {
  182. time.Sleep(2 * time.Second)
  183. timeout <- true
  184. }()
  185. select{
  186. case <- timeout:
  187. t.Error("Test timed out waiting for the worker")
  188. case <- done:
  189. }
  190. }