You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

file_test.go 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package discovery
  18. import (
  19. "reflect"
  20. "testing"
  21. )
  22. func TestFileRegistryService_Lookup(t *testing.T) {
  23. type fields struct {
  24. serviceConfig *ServiceConfig
  25. }
  26. type args struct {
  27. key string
  28. }
  29. tests := []struct {
  30. name string
  31. fields fields
  32. args args
  33. want []*ServiceInstance
  34. wantErr bool
  35. wantErrMsg string
  36. }{
  37. {
  38. name: "normal single endpoint.",
  39. args: args{
  40. key: "default_tx_group",
  41. },
  42. fields: fields{
  43. serviceConfig: &ServiceConfig{
  44. VgroupMapping: map[string]string{
  45. "default_tx_group": "default",
  46. },
  47. Grouplist: map[string]string{
  48. "default": "127.0.0.1:8091",
  49. },
  50. },
  51. },
  52. want: []*ServiceInstance{
  53. {
  54. Addr: "127.0.0.1",
  55. Port: 8091,
  56. },
  57. },
  58. wantErr: false,
  59. },
  60. {
  61. name: "normal multi endpoints.",
  62. args: args{
  63. key: "default_tx_group",
  64. },
  65. fields: fields{
  66. serviceConfig: &ServiceConfig{
  67. VgroupMapping: map[string]string{
  68. "default_tx_group": "default",
  69. },
  70. Grouplist: map[string]string{
  71. "default": "127.0.0.1:8091;192.168.0.1:8092",
  72. },
  73. },
  74. },
  75. want: []*ServiceInstance{
  76. {
  77. Addr: "127.0.0.1",
  78. Port: 8091,
  79. },
  80. {
  81. Addr: "192.168.0.1",
  82. Port: 8092,
  83. },
  84. },
  85. wantErr: false,
  86. },
  87. {
  88. name: "vgroup is empty.",
  89. args: args{
  90. key: "default_tx_group",
  91. },
  92. fields: fields{
  93. serviceConfig: &ServiceConfig{
  94. VgroupMapping: map[string]string{
  95. "default_tx_group": "",
  96. },
  97. },
  98. },
  99. want: nil,
  100. wantErr: true,
  101. wantErrMsg: "vgroup is empty. key: default_tx_group",
  102. },
  103. {
  104. name: "endpoint is empty.",
  105. args: args{
  106. key: "default_tx_group",
  107. },
  108. fields: fields{
  109. serviceConfig: &ServiceConfig{
  110. VgroupMapping: map[string]string{
  111. "default_tx_group": "default",
  112. },
  113. },
  114. },
  115. want: nil,
  116. wantErr: true,
  117. wantErrMsg: "endpoint is empty. key: default_tx_group group: default",
  118. },
  119. {
  120. name: "format is not ip:port",
  121. args: args{
  122. key: "default_tx_group",
  123. },
  124. fields: fields{
  125. serviceConfig: &ServiceConfig{
  126. VgroupMapping: map[string]string{
  127. "default_tx_group": "default",
  128. },
  129. Grouplist: map[string]string{
  130. "default": "127.0.0.18091",
  131. },
  132. },
  133. },
  134. want: nil,
  135. wantErr: true,
  136. wantErrMsg: "endpoint format should like ip:port. endpoint: 127.0.0.18091",
  137. },
  138. {
  139. name: "port is not number",
  140. args: args{
  141. key: "default_tx_group",
  142. },
  143. fields: fields{
  144. serviceConfig: &ServiceConfig{
  145. VgroupMapping: map[string]string{
  146. "default_tx_group": "default",
  147. },
  148. Grouplist: map[string]string{
  149. "default": "127.0.0.1:abc",
  150. },
  151. },
  152. },
  153. want: nil,
  154. wantErr: true,
  155. wantErrMsg: "strconv.Atoi: parsing \"abc\": invalid syntax",
  156. },
  157. }
  158. for _, tt := range tests {
  159. t.Run(tt.name, func(t *testing.T) {
  160. s := &FileRegistryService{
  161. serviceConfig: tt.fields.serviceConfig,
  162. }
  163. got, err := s.Lookup(tt.args.key)
  164. if (err != nil) != tt.wantErr {
  165. t.Errorf("Lookup() error = %v, wantErr = %v", err, tt.wantErr)
  166. }
  167. if tt.wantErr && err.Error() != tt.wantErrMsg {
  168. t.Errorf("Lookup() errMsg = %v, wantErrMsg = %v", err.Error(), tt.wantErrMsg)
  169. }
  170. if !tt.wantErr && !reflect.DeepEqual(got, tt.want) {
  171. t.Errorf("Lookup() got = %v, want = %v", got, tt.want)
  172. }
  173. })
  174. }
  175. }