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.

140 lines
2.7 KiB

  1. package client
  2. import (
  3. "testing"
  4. )
  5. var (
  6. pool = NewPool()
  7. )
  8. func TestPoolAdd(t *testing.T) {
  9. if !runIntegrationTests {
  10. t.Skip("To run this test, use: go test -integration")
  11. }
  12. t.Log("Add servers")
  13. c := 2
  14. if err := pool.Add("tcp4", "127.0.0.1:4730", 1); err != nil {
  15. t.Fatal(err)
  16. }
  17. if err := pool.Add("tcp4", "127.0.1.1:4730", 1); err != nil {
  18. t.Log(err)
  19. c -= 1
  20. }
  21. if len(pool.Clients) != c {
  22. t.Errorf("%d servers expected, %d got.", c, len(pool.Clients))
  23. }
  24. }
  25. func TestPoolEcho(t *testing.T) {
  26. if !runIntegrationTests {
  27. t.Skip("To run this test, use: go test -integration")
  28. }
  29. echo, err := pool.Echo("", []byte(TestStr))
  30. if err != nil {
  31. t.Error(err)
  32. return
  33. }
  34. if string(echo) != TestStr {
  35. t.Errorf("Invalid echo data: %s", echo)
  36. return
  37. }
  38. _, err = pool.Echo("not exists", []byte(TestStr))
  39. if err != ErrNotFound {
  40. t.Errorf("ErrNotFound expected, got %s", err)
  41. }
  42. }
  43. func TestPoolDoBg(t *testing.T) {
  44. if !runIntegrationTests {
  45. t.Skip("To run this test, use: go test -integration")
  46. }
  47. addr, handle, err := pool.DoBg("ToUpper",
  48. []byte("abcdef"), JobLow)
  49. if err != nil {
  50. t.Error(err)
  51. return
  52. }
  53. if handle == "" {
  54. t.Error("Handle is empty.")
  55. } else {
  56. t.Log(addr, handle)
  57. }
  58. }
  59. func TestPoolDo(t *testing.T) {
  60. if !runIntegrationTests {
  61. t.Skip("To run this test, use: go test -integration")
  62. }
  63. jobHandler := func(job *Response) {
  64. str := string(job.Data)
  65. if str == "ABCDEF" {
  66. t.Log(str)
  67. } else {
  68. t.Errorf("Invalid data: %s", job.Data)
  69. }
  70. return
  71. }
  72. addr, handle, err := pool.Do("ToUpper",
  73. []byte("abcdef"), JobLow, jobHandler)
  74. if err != nil {
  75. t.Error(err)
  76. }
  77. if handle == "" {
  78. t.Error("Handle is empty.")
  79. } else {
  80. t.Log(addr, handle)
  81. }
  82. }
  83. func TestPoolStatus(t *testing.T) {
  84. if !runIntegrationTests {
  85. t.Skip("To run this test, use: go test -integration")
  86. }
  87. status, err := pool.Status("127.0.0.1:4730", "handle not exists")
  88. if err != nil {
  89. t.Error(err)
  90. return
  91. }
  92. if status.Known {
  93. t.Errorf("The job (%s) shouldn't be known.", status.Handle)
  94. }
  95. if status.Running {
  96. t.Errorf("The job (%s) shouldn't be running.", status.Handle)
  97. }
  98. addr, handle, err := pool.Do("Delay5sec",
  99. []byte("abcdef"), JobLow, nil)
  100. if err != nil {
  101. t.Error(err)
  102. return
  103. }
  104. status, err = pool.Status(addr, handle)
  105. if err != nil {
  106. t.Error(err)
  107. return
  108. }
  109. if !status.Known {
  110. t.Errorf("The job (%s) should be known.", status.Handle)
  111. }
  112. if status.Running {
  113. t.Errorf("The job (%s) shouldn't be running.", status.Handle)
  114. }
  115. status, err = pool.Status("not exists", "not exists")
  116. if err != ErrNotFound {
  117. t.Error(err)
  118. return
  119. }
  120. }
  121. func TestPoolClose(t *testing.T) {
  122. if !runIntegrationTests {
  123. t.Skip("To run this test, use: go test -integration")
  124. }
  125. return
  126. if err := pool.Close(); err != nil {
  127. t.Error(err)
  128. }
  129. }