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.
 
 
 

113 lines
2.0 KiB

  1. package client
  2. import (
  3. "testing"
  4. )
  5. const (
  6. TestStr = "Hello world"
  7. )
  8. var client *Client
  9. func TestClientAddServer(t *testing.T) {
  10. t.Log("Add local server 127.0.0.1:4730")
  11. var err error
  12. if client, err = New("tcp4", "127.0.0.1:4730"); err != nil {
  13. t.Error(err)
  14. return
  15. }
  16. client.ErrorHandler = func(e error) {
  17. t.Log(e)
  18. }
  19. }
  20. func TestClientEcho(t *testing.T) {
  21. echo, err := client.Echo([]byte(TestStr))
  22. if err != nil {
  23. t.Error(err)
  24. return
  25. }
  26. if string(echo) != TestStr {
  27. t.Errorf("Echo error, %s expected, %s got", TestStr, echo)
  28. return
  29. }
  30. }
  31. func TestClientDoBg(t *testing.T) {
  32. handle, err := client.DoBg("ToUpper", []byte("abcdef"), JOB_LOW)
  33. if err != nil {
  34. t.Error(err)
  35. return
  36. }
  37. if handle == "" {
  38. t.Error("Handle is empty.")
  39. } else {
  40. t.Log(handle)
  41. }
  42. }
  43. func TestClientDo(t *testing.T) {
  44. jobHandler := func(job *Response) {
  45. str := string(job.Data)
  46. if str == "ABCDEF" {
  47. t.Log(str)
  48. } else {
  49. t.Errorf("Invalid data: %s", job.Data)
  50. }
  51. return
  52. }
  53. handle, err := client.Do("ToUpper", []byte("abcdef"),
  54. JOB_LOW, jobHandler)
  55. if err != nil {
  56. t.Error(err)
  57. return
  58. }
  59. if handle == "" {
  60. t.Error("Handle is empty.")
  61. } else {
  62. t.Log(handle)
  63. }
  64. }
  65. func TestClientStatus(t *testing.T) {
  66. status, err := client.Status("handle not exists")
  67. if err != nil {
  68. t.Error(err)
  69. return
  70. }
  71. if status.Known {
  72. t.Errorf("The job (%s) shouldn't be known.", status.Handle)
  73. return
  74. }
  75. if status.Running {
  76. t.Errorf("The job (%s) shouldn't be running.", status.Handle)
  77. return
  78. }
  79. handle, err := client.Do("Delay5sec", []byte("abcdef"), JOB_LOW, nil)
  80. if err != nil {
  81. t.Error(err)
  82. return
  83. }
  84. status, err = client.Status(handle)
  85. if err != nil {
  86. t.Error(err)
  87. return
  88. }
  89. if !status.Known {
  90. t.Errorf("The job (%s) should be known.", status.Handle)
  91. return
  92. }
  93. if status.Running {
  94. t.Errorf("The job (%s) shouldn't be running.", status.Handle)
  95. return
  96. }
  97. }
  98. func TestClientClose(t *testing.T) {
  99. if err := client.Close(); err != nil {
  100. t.Error(err)
  101. }
  102. }