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.
 
 
 

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