manager_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package connection
  2. import (
  3. "github.com/lf-edge/ekuiper/internal/conf"
  4. "reflect"
  5. "testing"
  6. )
  7. const TestClient = "MockClient"
  8. var GetCalledNumber int
  9. var CloseCalledNumber int
  10. type MockClient struct {
  11. selector *conf.ConSelector
  12. mockCon string
  13. }
  14. func (m *MockClient) CfgValidate(m2 map[string]interface{}) error {
  15. return nil
  16. }
  17. func (m *MockClient) GetClient() (interface{}, error) {
  18. GetCalledNumber = 1
  19. m.mockCon = "MockClient"
  20. return m.mockCon, nil
  21. }
  22. func (m *MockClient) CloseClient() error {
  23. CloseCalledNumber = 1
  24. m.mockCon = ""
  25. return nil
  26. }
  27. func TestManager(t *testing.T) {
  28. registerClientFactory("mqtt", func(super *conf.ConSelector) Client {
  29. return &MockClient{selector: super}
  30. })
  31. conSelector := "mqtt.localConnection"
  32. connection, err := GetConnection(conSelector)
  33. if err != nil {
  34. t.Errorf("GetConnection Error")
  35. }
  36. value := connection.(string)
  37. if !reflect.DeepEqual(value, TestClient) {
  38. t.Errorf("Error")
  39. }
  40. connection, err = GetConnection(conSelector)
  41. if err != nil {
  42. t.Errorf("GetConnection Error")
  43. }
  44. wrapper := m.clientMap[conSelector]
  45. if !reflect.DeepEqual(wrapper.refCnt, uint32(2)) {
  46. t.Errorf("Error, ectual=%v, want=%v", wrapper.refCnt, 2)
  47. }
  48. connection, err = GetConnection(conSelector)
  49. if err != nil {
  50. t.Errorf("GetConnection Error")
  51. }
  52. if !reflect.DeepEqual(wrapper.refCnt, uint32(3)) {
  53. t.Errorf("Error")
  54. }
  55. ReleaseConnection(conSelector)
  56. if !reflect.DeepEqual(wrapper.refCnt, uint32(2)) {
  57. t.Errorf("Error")
  58. }
  59. ReleaseConnection(conSelector)
  60. if !reflect.DeepEqual(wrapper.refCnt, uint32(1)) {
  61. t.Errorf("Error")
  62. }
  63. ReleaseConnection(conSelector)
  64. if !reflect.DeepEqual(wrapper.refCnt, uint32(0)) {
  65. t.Errorf("Error")
  66. }
  67. if _, ok := m.clientMap[conSelector]; ok {
  68. t.Errorf("Error")
  69. }
  70. if !reflect.DeepEqual(GetCalledNumber, CloseCalledNumber) || !reflect.DeepEqual(GetCalledNumber, 1) {
  71. t.Errorf("Error")
  72. }
  73. }