add watch test

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-11-24 01:21:55 +03:00
parent 7447bd5ad2
commit 70a3109b31
3 changed files with 51 additions and 5 deletions

View File

@ -4,8 +4,10 @@ import (
"context"
"os"
"testing"
"time"
"go.unistack.org/micro/v3/config"
rutil "go.unistack.org/micro/v3/util/reflect"
)
type Config struct {
@ -17,6 +19,52 @@ type Config struct {
MapIntValue map[string]int `env:"MAP_INT"`
}
func TestMerge(t *testing.T) {
ctx := context.Background()
type Nested struct {
Name string `env:"NAME_VALUE"`
}
type Cfg struct {
Name string `env:"NAME_VALUE"`
Nested Nested
}
conf := &Cfg{}
cfg := NewConfig(config.Struct(conf))
if err := cfg.Init(); err != nil {
t.Fatal(err)
}
if err := cfg.Load(ctx, config.LoadOverride(true), config.LoadAppend(true)); err != nil {
t.Fatal(err)
}
w, err := cfg.Watch(ctx, config.WatchInterval(50*time.Millisecond, 500*time.Millisecond))
defer func() {
if err := w.Stop(); err != nil {
t.Fatal(err)
}
}()
os.Setenv("NAME_VALUE", "after")
changes, err := w.Next()
if err != nil {
t.Fatal(err)
}
for k, v := range changes {
if err := rutil.SetFieldByPath(conf, v, k); err != nil {
t.Fatal(err)
}
}
if conf.Name != "after" || conf.Nested.Name != "after" {
t.Fatalf("changes %#+v not applied to %#+v", changes, conf)
}
}
func TestLoad(t *testing.T) {
ctx := context.Background()
conf := &Config{StringValue: "before_load"}
@ -63,8 +111,6 @@ func TestLoad(t *testing.T) {
if len(conf.MapIntValue) != 2 {
t.Fatalf("something wrong with env config: %#+v", conf.MapIntValue)
}
t.Logf("cfg %#+v", conf)
}
func TestSave(t *testing.T) {

2
go.mod
View File

@ -4,5 +4,5 @@ go 1.16
require (
github.com/imdario/mergo v0.3.12
go.unistack.org/micro/v3 v3.8.10
go.unistack.org/micro/v3 v3.8.11
)

4
go.sum
View File

@ -9,8 +9,8 @@ github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTK
github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
go.unistack.org/micro-proto/v3 v3.1.0 h1:q39FwjFiRZn+Ux/tt+d3bJTmDtsQQWa+3SLYVo1vLfA=
go.unistack.org/micro-proto/v3 v3.1.0/go.mod h1:DpRhYCBXlmSJ/AAXTmntvlh7kQkYU6eFvlmYAx4BQS8=
go.unistack.org/micro/v3 v3.8.10 h1:ZMKGGalqkZc677S5BmSPTAMZAE4q3u2FuqCrCd1TO7g=
go.unistack.org/micro/v3 v3.8.10/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA=
go.unistack.org/micro/v3 v3.8.11 h1:Wv1YopcYNcsN3bW8Mv8v6AF99s0uKBtWQ1M/Ag8QLec=
go.unistack.org/micro/v3 v3.8.11/go.mod h1:KMMmOmbgo/D52/rCAbqeKbBsgEEbSKM69he54J3ZIuA=
golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=