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.
 
 
 

97 lines
2.0 KiB

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