Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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