github.com/wfusion/gofusion@v1.1.14/test/config/cases/example_test.go (about) 1 package cases 2 3 import ( 4 "context" 5 "fmt" 6 "math/rand" 7 "os" 8 "path/filepath" 9 "sync" 10 "testing" 11 "time" 12 13 "github.com/stretchr/testify/suite" 14 15 "github.com/wfusion/gofusion/async" 16 "github.com/wfusion/gofusion/common/env" 17 "github.com/wfusion/gofusion/common/utils" 18 "github.com/wfusion/gofusion/cron" 19 "github.com/wfusion/gofusion/db" 20 "github.com/wfusion/gofusion/internal/configor" 21 "github.com/wfusion/gofusion/lock" 22 "github.com/wfusion/gofusion/log" 23 "github.com/wfusion/gofusion/metrics" 24 "github.com/wfusion/gofusion/mongo" 25 "github.com/wfusion/gofusion/mq" 26 "github.com/wfusion/gofusion/redis" 27 "github.com/wfusion/gofusion/test/config" 28 29 fusCfg "github.com/wfusion/gofusion/config" 30 31 _ "github.com/wfusion/gofusion/cache" 32 _ "github.com/wfusion/gofusion/http" 33 _ "github.com/wfusion/gofusion/i18n" 34 ) 35 36 func TestExample(t *testing.T) { 37 testingSuite := &Example{Test: new(config.Test)} 38 testingSuite.Init(testingSuite) 39 suite.Run(t, testingSuite) 40 } 41 42 type Example struct { 43 *config.Test 44 } 45 46 func (t *Example) BeforeTest(suiteName, testName string) { 47 t.Catch(func() { 48 log.Info(context.Background(), "right before %s %s", suiteName, testName) 49 }) 50 } 51 52 func (t *Example) AfterTest(suiteName, testName string) { 53 t.Catch(func() { 54 log.Info(context.Background(), "right after %s %s", suiteName, testName) 55 }) 56 } 57 58 func (t *Example) TestDefault() { 59 t.Catch(func() { 60 defer t.RawCopy(t.AllConfigFiles(), 1)() 61 62 appSetting := new(appConf) 63 defer fusCfg.Registry.Init(&appSetting)() 64 65 allConfigs := fusCfg.Registry.GetAllConfigs() 66 log.Info(context.Background(), "get all configs: %+v", allConfigs) 67 log.Info(context.Background(), "get all configs json: %s", utils.MustJsonMarshal(allConfigs)) 68 log.Info(context.Background(), "get app name: %s", fusCfg.Registry.AppName()) 69 log.Info(context.Background(), "get debug: %+v", fusCfg.Registry.Debug()) 70 }) 71 } 72 73 func (t *Example) TestRequired() { 74 t.Catch(func() { 75 files := []string{ 76 "app.required.local.yml", 77 "app.required.yml", 78 } 79 defer t.RawCopy(files, 1)() 80 81 for i := 0; i < len(files); i++ { 82 files[i] = filepath.Join(env.WorkDir, "configs", files[i]) 83 } 84 85 appSetting := new(appConf) 86 defer fusCfg.Registry.Init(&appSetting, fusCfg.Files(files))() 87 allConfigs := fusCfg.Registry.GetAllConfigs() 88 log.Info(context.Background(), "get all configs: %+v", allConfigs) 89 log.Info(context.Background(), "get all configs json: %s", utils.MustJsonMarshal(allConfigs)) 90 log.Info(context.Background(), "get app name: %s", fusCfg.Registry.AppName()) 91 log.Info(context.Background(), "get debug: %+v", fusCfg.Registry.Debug()) 92 93 ctx, cancel := context.WithCancel(context.Background()) 94 defer cancel() 95 lock.Use("default") 96 db.Use(ctx, "default") 97 mongo.Use("default") 98 redis.Use(ctx, "default") 99 log.Use("default") 100 mq.Use("default") 101 async.C("default") 102 async.P("default") 103 cron.Use("default") 104 metrics.Use("prometheus", "test_config_required") 105 }) 106 } 107 108 func (t *Example) TestWithoutFiles() { 109 t.Catch(func() { 110 appSetting := new(appConf) 111 defer fusCfg.Registry.Init(&appSetting, fusCfg.Files(nil))() 112 allConfigs := fusCfg.Registry.GetAllConfigs() 113 log.Info(context.Background(), "get all configs: %+v", allConfigs) 114 log.Info(context.Background(), "get all configs json: %s", utils.MustJsonMarshal(allConfigs)) 115 log.Info(context.Background(), "get app name: %s", fusCfg.Registry.AppName()) 116 log.Info(context.Background(), "get debug: %+v", fusCfg.Registry.Debug()) 117 }) 118 } 119 120 func (t *Example) TestLoadAppConfig() { 121 t.Catch(func() { 122 appSetting := new(appConf) 123 defer fusCfg.New(t.AppName()).Init(&appSetting, fusCfg.Files(t.ConfigFiles()))() 124 allConfigs := fusCfg.Use(t.AppName()).GetAllConfigs() 125 log.Info(context.Background(), "get all configs: %+v", allConfigs) 126 log.Info(context.Background(), "get all configs json: %s", utils.MustJsonMarshal(allConfigs)) 127 log.Info(context.Background(), "get app name: %s", fusCfg.Use(t.AppName()).AppName()) 128 log.Info(context.Background(), "get debug: %+v", fusCfg.Use(t.AppName()).Debug()) 129 }) 130 } 131 132 func (t *Example) TestLoadMultiTimes() { 133 t.Catch(func() { 134 appSetting := new(appConf) 135 fusCfg.New(t.AppName()).Init(&appSetting, fusCfg.Files(t.ConfigFiles()))() 136 137 appSetting = new(appConf) 138 defer fusCfg.New(t.AppName()).Init(&appSetting, fusCfg.Files(t.ConfigFiles()))() 139 allConfigs := fusCfg.Use(t.AppName()).GetAllConfigs() 140 log.Info(context.Background(), "get all configs json: %s", utils.MustJsonMarshal(allConfigs)) 141 log.Info(context.Background(), "get app name: %s", fusCfg.Use(t.AppName()).AppName()) 142 log.Info(context.Background(), "get debug: %+v", fusCfg.Use(t.AppName()).Debug()) 143 }) 144 } 145 146 func (t *Example) TestLoadWithContext() { 147 t.Catch(func() { 148 ctx, cancel := context.WithCancel(context.Background()) 149 appSetting := new(appConf) 150 defer fusCfg.New(t.AppName()).Init(&appSetting, fusCfg.Ctx(ctx), fusCfg.Files(t.ConfigFiles()))() 151 152 allConfigs := fusCfg.Use(t.AppName()).GetAllConfigs() 153 log.Info(context.Background(), "get all configs: %+v", allConfigs) 154 log.Info(context.Background(), "get all configs json: %s", utils.MustJsonMarshal(allConfigs)) 155 log.Info(context.Background(), "get app name: %s", fusCfg.Use(t.AppName()).AppName()) 156 log.Info(context.Background(), "get debug: %+v", fusCfg.Use(t.AppName()).Debug()) 157 158 cancel() 159 time.Sleep(time.Second) 160 }) 161 } 162 163 func (t *Example) TestLoadWithLoader() { 164 t.Catch(func() { 165 appSetting := new(appConf) 166 defer fusCfg.New(t.AppName()).Init(&appSetting, fusCfg.Files(t.ConfigFiles()), 167 fusCfg.Loader(func(a any, opts ...utils.OptionExtender) { 168 log.Info(context.Background(), "enter custom loader") 169 defer log.Info(context.Background(), "exit custom loader") 170 files := make([]string, 0, 2) 171 localConfPath := filepath.Join(env.WorkDir + "configs" + 172 fmt.Sprintf("%s.app.local.yml", t.AppName())) 173 defaultConfPath := filepath.Join(env.WorkDir + "configs" + 174 fmt.Sprintf("%s.app.yml", t.AppName())) 175 if _, err := os.Stat(localConfPath); err == nil { 176 files = append(files, localConfPath) 177 } 178 files = append(files, defaultConfPath) 179 t.Require().NoError(configor.New(&configor.Config{}).Load(a, files...)) 180 }))() 181 182 allConfigs := fusCfg.Use(t.AppName()).GetAllConfigs() 183 log.Info(context.Background(), "get all configs: %+v", allConfigs) 184 log.Info(context.Background(), "get all configs json: %s", utils.MustJsonMarshal(allConfigs)) 185 log.Info(context.Background(), "get app name: %s", fusCfg.Use(t.AppName()).AppName()) 186 log.Info(context.Background(), "get debug: %+v", fusCfg.Use(t.AppName()).Debug()) 187 }) 188 } 189 190 func (t *Example) TestConcurrency() { 191 t.Catch(func() { 192 defer t.RawCopy(t.AllConfigFiles(), 1)() 193 194 ctx, cancel := context.WithCancel(context.Background()) 195 defer cancel() 196 197 testComponentsFn := func(appName string) { 198 lock.Use("default", lock.AppName(appName)) 199 db.Use(ctx, "read", db.AppName(appName)) 200 db.Use(ctx, "write", db.AppName(appName)) 201 mongo.Use("default", mongo.AppName(appName)) 202 redis.Use(ctx, "default", redis.AppName(appName)) 203 log.Use("default", log.AppName(appName)) 204 mq.Use("mysql", mq.AppName(appName)) 205 async.C("default", async.AppName(appName)) 206 async.P("default", async.AppName(appName)) 207 cron.Use("default", cron.AppName(appName)) 208 metrics.Use("prometheus", "test_config_concurrency", metrics.AppName(appName)) 209 } 210 211 wg := new(sync.WaitGroup) 212 for i := 0; i < 1000; i++ { 213 wg.Add(1) 214 go func() { 215 defer wg.Done() 216 time.Sleep(time.Duration(rand.Intn(50)) * time.Millisecond) 217 218 appSetting := new(appConf) 219 defer fusCfg.New(t.AppName()).Init(&appSetting, fusCfg.Files(t.ConfigFiles()))() 220 testComponentsFn(t.AppName()) 221 }() 222 223 wg.Add(1) 224 go func() { 225 defer wg.Done() 226 227 time.Sleep(time.Duration(rand.Intn(50)) * time.Millisecond) 228 229 appSetting := new(appConf) 230 defer fusCfg.Registry.Init(&appSetting)() 231 testComponentsFn("") 232 }() 233 } 234 wg.Wait() 235 }) 236 }