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.

pool_test.go 2.2 KiB

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