diff options
Diffstat (limited to 'tests/plugins/config')
-rwxr-xr-x | tests/plugins/config/.rr.yaml | 21 | ||||
-rwxr-xr-x | tests/plugins/config/config_test.go | 64 | ||||
-rwxr-xr-x | tests/plugins/config/plugin1.go | 96 |
3 files changed, 181 insertions, 0 deletions
diff --git a/tests/plugins/config/.rr.yaml b/tests/plugins/config/.rr.yaml new file mode 100755 index 00000000..bad2846a --- /dev/null +++ b/tests/plugins/config/.rr.yaml @@ -0,0 +1,21 @@ +rpc: + listen: tcp://localhost:6060 + +reload: + enabled: true + interval: 1s + patterns: [".php"] + services: + http: + recursive: true + ignore: ["vendor"] + patterns: [".php", ".go",".md",] + dirs: ["."] + jobs: + recursive: false + ignore: ["service/metrics"] + dirs: ["./jobs"] + rpc: + recursive: true + patterns: [".json"] + dirs: [""] diff --git a/tests/plugins/config/config_test.go b/tests/plugins/config/config_test.go new file mode 100755 index 00000000..6d95ba70 --- /dev/null +++ b/tests/plugins/config/config_test.go @@ -0,0 +1,64 @@ +package config + +import ( + "os" + "os/signal" + "testing" + "time" + + "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/plugins/config" + "github.com/stretchr/testify/assert" +) + +func TestViperProvider_Init(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.ErrorLevel)) + if err != nil { + t.Fatal(err) + } + vp := &config.Viper{} + vp.Path = ".rr.yaml" + vp.Prefix = "rr" + err = container.Register(vp) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&Foo{}) + if err != nil { + t.Fatal(err) + } + + err = container.Init() + if err != nil { + t.Fatal(err) + } + + errCh, err := container.Serve() + if err != nil { + t.Fatal(err) + } + + // stop by CTRL+C + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + + tt := time.NewTicker(time.Second * 2) + defer tt.Stop() + + for { + select { + case e := <-errCh: + assert.NoError(t, e.Error) + assert.NoError(t, container.Stop()) + return + case <-c: + er := container.Stop() + assert.NoError(t, er) + return + case <-tt.C: + assert.NoError(t, container.Stop()) + return + } + } +} diff --git a/tests/plugins/config/plugin1.go b/tests/plugins/config/plugin1.go new file mode 100755 index 00000000..a6c06aec --- /dev/null +++ b/tests/plugins/config/plugin1.go @@ -0,0 +1,96 @@ +package config + +import ( + "time" + + "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2/plugins/config" +) + +type AllConfig struct { + RPC struct { + Listen string `yaml:"listen"` + } `yaml:"rpc"` + Reload struct { + Enabled bool `yaml:"enabled"` + Interval string `yaml:"interval"` + Patterns []string `yaml:"patterns"` + Services struct { + HTTP struct { + Recursive bool `yaml:"recursive"` + Ignore []string `yaml:"ignore"` + Patterns []string `yaml:"patterns"` + Dirs []string `yaml:"dirs"` + } `yaml:"http"` + Jobs struct { + Recursive bool `yaml:"recursive"` + Ignore []string `yaml:"ignore"` + Dirs []string `yaml:"dirs"` + } `yaml:"jobs"` + RPC struct { + Recursive bool `yaml:"recursive"` + Patterns []string `yaml:"patterns"` + Dirs []string `yaml:"dirs"` + } `yaml:"rpc"` + } `yaml:"services"` + } `yaml:"reload"` +} + +// ReloadConfig is a Reload configuration point. +type ReloadConfig struct { + Interval time.Duration + Patterns []string + Services map[string]ServiceConfig +} + +type ServiceConfig struct { + Enabled bool + Recursive bool + Patterns []string + Dirs []string + Ignore []string +} + +type Foo struct { + configProvider config.Configurer +} + +// Depends on S2 and DB (S3 in the current case) +func (f *Foo) Init(p config.Configurer) error { + f.configProvider = p + return nil +} + +func (f *Foo) Serve() chan error { + const op = errors.Op("foo serve") + errCh := make(chan error, 1) + + r := &ReloadConfig{} + err := f.configProvider.UnmarshalKey("reload", r) + if err != nil { + errCh <- err + } + + if len(r.Patterns) == 0 { + errCh <- errors.E(op, errors.Str("should be at least one pattern, but got 0")) + return errCh + } + + var allCfg AllConfig + err = f.configProvider.Unmarshal(&allCfg) + if err != nil { + errCh <- errors.E(op, errors.Str("should be at least one pattern, but got 0")) + return errCh + } + + if allCfg.RPC.Listen != "tcp://localhost:6060" { + errCh <- errors.E(op, errors.Str("RPC.Listen should be parsed")) + return errCh + } + + return errCh +} + +func (f *Foo) Stop() error { + return nil +} |