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.
 
 
 

108 lines
2.4 KiB

  1. package client
  2. import (
  3. "time"
  4. "testing"
  5. )
  6. var (
  7. pool = NewPool()
  8. )
  9. func TestPoolAdd(t *testing.T) {
  10. t.Log("Add servers")
  11. if err := pool.Add("127.0.0.1:4730", 1); err != nil {
  12. t.Error(err)
  13. }
  14. if err := pool.Add("127.0.0.1:4730", 1); err != nil {
  15. t.Error(err)
  16. }
  17. if len(pool.clients) != 2 {
  18. t.Errorf("2 servers expected, %d got.", len(pool.clients))
  19. }
  20. }
  21. func TestPoolEcho(t *testing.T) {
  22. echo, err := pool.Echo("", []byte("Hello pool"), time.Second)
  23. if err != nil {
  24. t.Error(err)
  25. return
  26. }
  27. if string(echo) != "Hello pool" {
  28. t.Errorf("Invalid echo data: %s", echo)
  29. return
  30. }
  31. _, err = pool.Echo("not exists", []byte("Hello pool"), time.Second)
  32. if err != ErrNotFound {
  33. t.Errorf("ErrNotFound expected, got %s", err)
  34. }
  35. }
  36. func TestPoolDoBg(t *testing.T) {
  37. if addr, handle := pool.DoBg("ToUpper", []byte("abcdef"),
  38. JOB_LOW); handle == "" {
  39. t.Error("Handle is empty.")
  40. } else {
  41. t.Log(addr, handle)
  42. }
  43. }
  44. func TestPoolDo(t *testing.T) {
  45. jobHandler := func(job *Job) {
  46. str := string(job.Data)
  47. if str == "ABCDEF" {
  48. t.Log(str)
  49. } else {
  50. t.Errorf("Invalid data: %s", job.Data)
  51. }
  52. return
  53. }
  54. if addr, handle := pool.Do("ToUpper", []byte("abcdef"),
  55. JOB_LOW, jobHandler); handle == "" {
  56. t.Error("Handle is empty.")
  57. } else {
  58. t.Log(addr, handle)
  59. }
  60. }
  61. func TestPoolStatus(t *testing.T) {
  62. s1, err := pool.Status("127.0.0.1:4730", "handle not exists", time.Second)
  63. if err != nil {
  64. t.Error(err)
  65. return
  66. }
  67. if s1.Known {
  68. t.Errorf("The job (%s) shouldn't be known.", s1.Handle)
  69. }
  70. if s1.Running {
  71. t.Errorf("The job (%s) shouldn't be running.", s1.Handle)
  72. }
  73. addr, handle := pool.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil);
  74. s2, err := pool.Status(addr, handle, time.Second)
  75. if err != nil {
  76. t.Error(err)
  77. return
  78. }
  79. if !s2.Known {
  80. t.Errorf("The job (%s) should be known.", s2.Handle)
  81. }
  82. if s2.Running {
  83. t.Errorf("The job (%s) shouldn't be running.", s2.Handle)
  84. }
  85. _, err = pool.Status("not exists", "not exists", time.Second)
  86. if err != ErrNotFound {
  87. t.Error(err)
  88. }
  89. }
  90. func TestPoolClose(t *testing.T) {
  91. return
  92. if err := pool.Close(); err != nil {
  93. t.Error(err)
  94. }
  95. }