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.

init_test.go 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 TestInitRegistry(t *testing.T) {
  23. type args struct {
  24. serviceConfig *ServiceConfig
  25. registryConfig *RegistryConfig
  26. }
  27. tests := []struct {
  28. name string
  29. args args
  30. hasPanic bool
  31. expectedType string
  32. }{
  33. {
  34. name: "normal",
  35. args: args{
  36. registryConfig: &RegistryConfig{
  37. Type: FILE,
  38. },
  39. serviceConfig: &ServiceConfig{},
  40. },
  41. expectedType: "FileRegistryService",
  42. },
  43. {
  44. name: "unknown type",
  45. args: args{
  46. registryConfig: &RegistryConfig{
  47. Type: "unknown",
  48. },
  49. serviceConfig: &ServiceConfig{},
  50. },
  51. hasPanic: true,
  52. },
  53. }
  54. for _, tt := range tests {
  55. t.Run(tt.name, func(t *testing.T) {
  56. defer func() {
  57. if r := recover(); r != nil {
  58. if !tt.hasPanic {
  59. t.Errorf("panic is not expected!")
  60. }
  61. } else if tt.hasPanic {
  62. t.Errorf("Expected a panic but did not receive one")
  63. }
  64. }()
  65. InitRegistry(tt.args.serviceConfig, tt.args.registryConfig)
  66. instance := GetRegistry()
  67. if !tt.hasPanic {
  68. actualType := reflect.TypeOf(instance).Elem().Name()
  69. if actualType != tt.expectedType {
  70. t.Errorf("type = %v, want %v", actualType, tt.expectedType)
  71. }
  72. }
  73. })
  74. }
  75. }