dubbo.apache.org/dubbo-go/v3@v3.1.1/config_center/file/impl_test.go (about) 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 18 package file 19 20 import ( 21 "fmt" 22 "os" 23 "testing" 24 "time" 25 ) 26 27 import ( 28 "github.com/stretchr/testify/assert" 29 ) 30 31 import ( 32 "dubbo.apache.org/dubbo-go/v3/common" 33 "dubbo.apache.org/dubbo-go/v3/common/extension" 34 "dubbo.apache.org/dubbo-go/v3/config_center" 35 ) 36 37 const ( 38 key = "com.dubbo.go" 39 ) 40 41 func initFileData(t *testing.T) (*FileSystemDynamicConfiguration, error) { 42 urlString := "registry://127.0.0.1:2181" 43 regurl, err := common.NewURL(urlString) 44 assert.NoError(t, err) 45 factory, err := extension.GetConfigCenterFactory("file") 46 assert.NoError(t, err) 47 dc, err := factory.GetDynamicConfiguration(regurl) 48 assert.NoError(t, err) 49 50 return dc.(*FileSystemDynamicConfiguration), err 51 } 52 53 func TestPublishAndGetConfig(t *testing.T) { 54 file, err := initFileData(t) 55 assert.NoError(t, err) 56 err = file.PublishConfig(key, "", "A") 57 assert.NoError(t, err) 58 59 prop, err := file.GetProperties(key) 60 assert.NoError(t, err) 61 assert.Equal(t, "A", prop) 62 63 defer destroy(file.rootPath, file) 64 } 65 66 func TestAddListener(t *testing.T) { 67 file, err := initFileData(t) 68 assert.Nil(t, err) 69 group := "dubbogo" 70 value := "Test Value" 71 err = file.PublishConfig(key, group, value) 72 assert.NoError(t, err) 73 74 listener := &mockDataListener{} 75 file.AddListener(key, listener, config_center.WithGroup(group)) 76 77 value = "Test Value 2" 78 err = file.PublishConfig(key, group, value) 79 assert.NoError(t, err) 80 // remove need wait a moment 81 time.Sleep(time.Second) 82 defer destroy(file.rootPath, file) 83 } 84 85 func TestRemoveListener(t *testing.T) { 86 file, err := initFileData(t) 87 assert.NoError(t, err) 88 group := "dubbogo" 89 value := "Test Value" 90 err = file.PublishConfig(key, group, value) 91 assert.NoError(t, err) 92 93 listener := &mockDataListener{} 94 file.AddListener(key, listener, config_center.WithGroup(group)) 95 96 value = "Test Value 2" 97 err = file.PublishConfig(key, group, value) 98 assert.NoError(t, err) 99 100 // make sure callback before RemoveListener 101 time.Sleep(time.Second) 102 file.RemoveListener(key, listener, config_center.WithGroup(group)) 103 value = "Test Value 3" 104 err = file.PublishConfig(key, group, value) 105 assert.NoError(t, err) 106 // remove need wait a moment 107 time.Sleep(time.Second) 108 defer destroy(file.rootPath, file) 109 } 110 111 func TestGetConfigKeysByGroup(t *testing.T) { 112 file, err := initFileData(t) 113 assert.Nil(t, err) 114 group := "dubbogo" 115 value := "Test Value" 116 err = file.PublishConfig(key, group, value) 117 assert.NoError(t, err) 118 gs, err := file.GetConfigKeysByGroup(group) 119 assert.NoError(t, err) 120 assert.Equal(t, 1, gs.Size()) 121 assert.Equal(t, key, gs.Values()[0]) 122 // remove need wait a moment 123 time.Sleep(time.Second) 124 defer destroy(file.rootPath, file) 125 } 126 127 func TestGetConfig(t *testing.T) { 128 file, err := initFileData(t) 129 assert.NoError(t, err) 130 group := "dubbogo" 131 value := "Test Value" 132 err = file.PublishConfig(key, group, value) 133 assert.NoError(t, err) 134 prop, err := file.GetProperties(key, config_center.WithGroup(group)) 135 assert.NoError(t, err) 136 assert.Equal(t, value, prop) 137 defer destroy(file.rootPath, file) 138 } 139 140 func TestPublishConfig(t *testing.T) { 141 file, err := initFileData(t) 142 assert.NoError(t, err) 143 group := "dubbogo" 144 value := "Test Value" 145 err = file.PublishConfig(key, group, value) 146 assert.NoError(t, err) 147 prop, err := file.GetInternalProperty(key, config_center.WithGroup(group)) 148 assert.NoError(t, err) 149 assert.Equal(t, value, prop) 150 defer destroy(file.rootPath, file) 151 } 152 153 func destroy(path string, fdc *FileSystemDynamicConfiguration) { 154 fdc.Close() 155 os.RemoveAll(path) 156 } 157 158 type mockDataListener struct{} 159 160 func (l *mockDataListener) Process(configType *config_center.ConfigChangeEvent) { 161 fmt.Printf("process!!!!! %v", configType) 162 }