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.
 
 
 

170 lines
3.2 KiB

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