diff options
author | Valery Piashchynski <[email protected]> | 2020-12-16 10:40:22 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-12-16 10:40:22 +0300 |
commit | 9e692dd9996f4d100ff3d9c5a358aa6894a748a3 (patch) | |
tree | 2f4c51144a7b54ee41db8ca1e9771a64100c3cc1 /plugins | |
parent | 089a202aa716f3510402ff8baf47a3b9bfaefcb8 (diff) |
Add reload tests to the Makefile and CI. Update tests
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/reload/plugin.go | 34 | ||||
-rw-r--r-- | plugins/reload/tests/reload_plugin_test.go | 2 | ||||
-rw-r--r-- | plugins/reload/tests/watcher_test.go | 37 | ||||
-rw-r--r-- | plugins/reload/watcher.go | 17 |
4 files changed, 60 insertions, 30 deletions
diff --git a/plugins/reload/plugin.go b/plugins/reload/plugin.go index bad85b59..386c5e39 100644 --- a/plugins/reload/plugin.go +++ b/plugins/reload/plugin.go @@ -56,7 +56,7 @@ func (s *Plugin) Init(cfg config.Configurer, log log.Logger, res resetter.Resett return nil } } - return errors.E(op, errors.Skip, err) + return errors.E(op, errors.Skip) }, Files: make(map[string]os.FileInfo), Ignored: ignored, @@ -64,7 +64,7 @@ func (s *Plugin) Init(cfg config.Configurer, log log.Logger, res resetter.Resett }) } - s.watcher, err = NewWatcher(configs) + s.watcher, err = NewWatcher(configs, s.log) if err != nil { return errors.E(op, err) } @@ -91,15 +91,6 @@ func (s *Plugin) Serve() chan error { // use the same interval ticker := time.NewTicker(s.cfg.Interval) - // drain channel in case of leaved messages - defer func() { - go func() { - for range treshholdc { - - } - }() - }() - go func() { for e := range s.watcher.Event { treshholdc <- struct { @@ -118,14 +109,12 @@ func (s *Plugin) Serve() chan error { case cfg := <-treshholdc: // replace previous value in map by more recent without adding new one updated[cfg.service] = cfg.serviceConfig - // stop ticker - ticker.Stop() // restart // logic is following: - // if we getting a lot of events, we should't restart particular service on each of it (user doing bug move or very fast typing) + // if we getting a lot of events, we shouldn't restart particular service on each of it (user doing batch move or very fast typing) // instead, we are resetting the ticker and wait for Interval time // If there is no more events, we restart service only once - ticker = time.NewTicker(s.cfg.Interval) + ticker.Reset(s.cfg.Interval) case <-ticker.C: if len(updated) > 0 { for name := range updated { @@ -145,18 +134,21 @@ func (s *Plugin) Serve() chan error { } }() - err := s.watcher.StartPolling(s.cfg.Interval) - if err != nil { - errCh <- errors.E(op, err) - return errCh - } + go func() { + err := s.watcher.StartPolling(s.cfg.Interval) + if err != nil { + errCh <- errors.E(op, err) + return + } + }() return errCh } -func (s *Plugin) Stop() { +func (s *Plugin) Stop() error { s.watcher.Stop() s.stopc <- struct{}{} + return nil } func (s *Plugin) Name() string { diff --git a/plugins/reload/tests/reload_plugin_test.go b/plugins/reload/tests/reload_plugin_test.go index 376df9c8..3d50030f 100644 --- a/plugins/reload/tests/reload_plugin_test.go +++ b/plugins/reload/tests/reload_plugin_test.go @@ -51,7 +51,7 @@ func TestReloadInit(t *testing.T) { wg := &sync.WaitGroup{} wg.Add(1) - tt := time.NewTimer(time.Second * 5) + tt := time.NewTimer(time.Second * 10) go func() { defer wg.Done() diff --git a/plugins/reload/tests/watcher_test.go b/plugins/reload/tests/watcher_test.go index 6a4521a4..f1e0d737 100644 --- a/plugins/reload/tests/watcher_test.go +++ b/plugins/reload/tests/watcher_test.go @@ -12,7 +12,9 @@ import ( "time" "github.com/spiral/errors" + logPlugin "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/reload" + "go.uber.org/zap" ) var testServiceName = "test" @@ -46,7 +48,10 @@ func Test_Correct_Watcher_Init(t *testing.T) { FilePatterns: nil, } - w, err := reload.NewWatcher([]reload.WatcherConfig{wc}) + logger, _ := zap.NewDevelopment() + lg := logPlugin.NewZapAdapter(logger) + + w, err := reload.NewWatcher([]reload.WatcherConfig{wc}, lg) if err != nil { t.Fatal(err) } @@ -103,7 +108,10 @@ func Test_Get_FileEvent(t *testing.T) { FilePatterns: []string{"aaa", "txt"}, } - w, err := reload.NewWatcher([]reload.WatcherConfig{wc}) + logger, _ := zap.NewDevelopment() + lg := logPlugin.NewZapAdapter(logger) + + w, err := reload.NewWatcher([]reload.WatcherConfig{wc}, lg) if err != nil { t.Fatal(err) } @@ -195,7 +203,10 @@ func Test_FileExtensionFilter(t *testing.T) { FilePatterns: []string{"aaa", "bbb"}, } - w, err := reload.NewWatcher([]reload.WatcherConfig{wc}) + logger, _ := zap.NewDevelopment() + lg := logPlugin.NewZapAdapter(logger) + + w, err := reload.NewWatcher([]reload.WatcherConfig{wc}, lg) if err != nil { t.Fatal(err) } @@ -294,7 +305,10 @@ func Test_Recursive_Support(t *testing.T) { FilePatterns: []string{"aaa", "bbb"}, } - w, err := reload.NewWatcher([]reload.WatcherConfig{wc}) + logger, _ := zap.NewDevelopment() + lg := logPlugin.NewZapAdapter(logger) + + w, err := reload.NewWatcher([]reload.WatcherConfig{wc}, lg) if err != nil { t.Fatal(err) } @@ -351,7 +365,10 @@ func Test_Wrong_Dir(t *testing.T) { FilePatterns: []string{"aaa", "bbb"}, } - _, err := reload.NewWatcher([]reload.WatcherConfig{wc}) + logger, _ := zap.NewDevelopment() + lg := logPlugin.NewZapAdapter(logger) + + _, err := reload.NewWatcher([]reload.WatcherConfig{wc}, lg) if err == nil { t.Fatal(err) } @@ -420,7 +437,10 @@ func Test_Filter_Directory(t *testing.T) { FilePatterns: []string{"aaa", "bbb", "txt"}, } - w, err := reload.NewWatcher([]reload.WatcherConfig{wc}) + logger, _ := zap.NewDevelopment() + lg := logPlugin.NewZapAdapter(logger) + + w, err := reload.NewWatcher([]reload.WatcherConfig{wc}, lg) if err != nil { t.Fatal(err) } @@ -522,7 +542,10 @@ func Test_Copy_Directory(t *testing.T) { FilePatterns: []string{"aaa", "bbb", "txt"}, } - w, err := reload.NewWatcher([]reload.WatcherConfig{wc}) + logger, _ := zap.NewDevelopment() + lg := logPlugin.NewZapAdapter(logger) + + w, err := reload.NewWatcher([]reload.WatcherConfig{wc}, lg) if err != nil { t.Fatal(err) } diff --git a/plugins/reload/watcher.go b/plugins/reload/watcher.go index ba0b8e82..1829839b 100644 --- a/plugins/reload/watcher.go +++ b/plugins/reload/watcher.go @@ -8,6 +8,7 @@ import ( "time" "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2/interfaces/log" ) // SimpleHook is used to filter by simple criteria, CONTAINS @@ -60,17 +61,22 @@ type Watcher struct { // config for each service // need pointer here to assign files watcherConfigs map[string]WatcherConfig + + // logger + log log.Logger } // Options is used to set Watcher Options type Options func(*Watcher) // NewWatcher returns new instance of File Watcher -func NewWatcher(configs []WatcherConfig, options ...Options) (*Watcher, error) { +func NewWatcher(configs []WatcherConfig, log log.Logger, options ...Options) (*Watcher, error) { w := &Watcher{ Event: make(chan Event), mu: &sync.Mutex{}, + log: log, + close: make(chan struct{}), //workingDir: workDir, @@ -320,6 +326,7 @@ func (w *Watcher) pollEvents(serviceName string, files map[string]os.FileInfo) { for pth, info := range w.watcherConfigs[serviceName].Files { if _, found := files[pth]; !found { removes[pth] = info + w.log.Debug("file was removed", "path", pth, "name", info.Name(), "size", info.Size()) } } @@ -332,10 +339,12 @@ func (w *Watcher) pollEvents(serviceName string, files map[string]os.FileInfo) { if !found { // A file was created. creates[pth] = info + w.log.Debug("file was created", "path", pth, "name", info.Name(), "size", info.Size()) continue } if oldInfo.ModTime() != info.ModTime() { w.watcherConfigs[serviceName].Files[pth] = info + w.log.Debug("file was updated", "path", pth, "name", info.Name(), "size", info.Size()) w.Event <- Event{ Path: pth, Info: info, @@ -344,6 +353,7 @@ func (w *Watcher) pollEvents(serviceName string, files map[string]os.FileInfo) { } if oldInfo.Mode() != info.Mode() { w.watcherConfigs[serviceName].Files[pth] = info + w.log.Debug("file was updated", "path", pth, "name", info.Name(), "size", info.Size()) w.Event <- Event{ Path: pth, Info: info, @@ -367,6 +377,7 @@ func (w *Watcher) pollEvents(serviceName string, files map[string]os.FileInfo) { // update with new w.watcherConfigs[serviceName].Files[path2] = info2 + w.log.Debug("file was renamed/moved", "old path", path1, "new path", path2, "name", info2.Name(), "size", info2.Size()) w.Event <- e } } @@ -375,6 +386,8 @@ func (w *Watcher) pollEvents(serviceName string, files map[string]os.FileInfo) { // Send all the remaining create and remove events. for pth, info := range creates { w.watcherConfigs[serviceName].Files[pth] = info + w.log.Debug("file was created", "path", pth, "name", info.Name(), "size", info.Size()) + w.Event <- Event{ Path: pth, Info: info, @@ -383,6 +396,8 @@ func (w *Watcher) pollEvents(serviceName string, files map[string]os.FileInfo) { } for pth, info := range removes { delete(w.watcherConfigs[serviceName].Files, pth) + w.log.Debug("file was removed", "path", pth, "name", info.Name(), "size", info.Size()) + w.Event <- Event{ Path: pth, Info: info, |