diff options
author | Valery Piashchynski <[email protected]> | 2020-12-20 18:28:46 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-12-20 18:28:46 +0300 |
commit | f4a36c7f684216fb408693a6c494486144df57cf (patch) | |
tree | e1b61bf7e74cb63aa45f9ca0284a4cffe8e06b0e /plugins/reload | |
parent | fbd5adde5abae6f7adb7fcdafc226bcd3480d498 (diff) | |
parent | a10d20d20e910ed8fcfbc3bc690aaf17ee338ff3 (diff) |
Merge remote-tracking branch 'origin/2.0' into plugin/redis
# Conflicts:
# go.sum
# pkg/pipe/pipe_factory_test.go
# pkg/pool/static_pool.go
# plugins/rpc/plugin.go
Diffstat (limited to 'plugins/reload')
-rw-r--r-- | plugins/reload/tests/reload_plugin_test.go | 249 |
1 files changed, 119 insertions, 130 deletions
diff --git a/plugins/reload/tests/reload_plugin_test.go b/plugins/reload/tests/reload_plugin_test.go index d2fad28d..2ba9e256 100644 --- a/plugins/reload/tests/reload_plugin_test.go +++ b/plugins/reload/tests/reload_plugin_test.go @@ -4,7 +4,6 @@ import ( "io" "io/ioutil" "math/rand" - "net/http" "os" "os/signal" "path/filepath" @@ -14,11 +13,12 @@ import ( "testing" "time" + "github.com/golang/mock/gomock" "github.com/spiral/endure" "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2/mocks" "github.com/spiral/roadrunner/v2/plugins/config" httpPlugin "github.com/spiral/roadrunner/v2/plugins/http" - "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/reload" "github.com/spiral/roadrunner/v2/plugins/resetter" "github.com/spiral/roadrunner/v2/plugins/server" @@ -27,7 +27,8 @@ import ( const testDir string = "unit_tests" const testCopyToDir string = "unit_tests_copied" -const hugeNumberOfFiles uint = 5000 +const dir1 string = "dir1" +const hugeNumberOfFiles uint = 500 func TestReloadInit(t *testing.T) { cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) @@ -43,13 +44,20 @@ func TestReloadInit(t *testing.T) { err = os.Mkdir(testDir, 0755) assert.NoError(t, err) - defer func() { - assert.NoError(t, freeResources(testDir)) - }() + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + + mockLogger.EXPECT().Debug("http handler response received", "elapsed", gomock.Any(), "remote address", "127.0.0.1").Times(1) + mockLogger.EXPECT().Debug("file was created", "path", gomock.Any(), "name", "file.txt", "size", gomock.Any()).Times(2) + mockLogger.EXPECT().Debug("file was added to watcher", "path", gomock.Any(), "name", "file.txt", "size", gomock.Any()).Times(2) + mockLogger.EXPECT().Info("HTTP plugin got restart request. Restarting...").Times(1) + mockLogger.EXPECT().Info("HTTP workers Pool successfully restarted").Times(1) + mockLogger.EXPECT().Info("HTTP listeners successfully re-added").Times(1) + mockLogger.EXPECT().Info("HTTP plugin successfully restarted").Times(1) err = cont.RegisterAll( cfg, - &logger.ZapLogger{}, + mockLogger, &server.Plugin{}, &httpPlugin.Plugin{}, &reload.Plugin{}, @@ -58,9 +66,7 @@ func TestReloadInit(t *testing.T) { assert.NoError(t, err) err = cont.Init() - if err != nil { - t.Fatal(err) - } + assert.NoError(t, err) ch, err := cont.Serve() assert.NoError(t, err) @@ -101,9 +107,9 @@ func TestReloadInit(t *testing.T) { }() t.Run("ReloadTestInit", reloadTestInit) - reloadHTTPLiveAfterReset(t, "22388") wg.Wait() + assert.NoError(t, freeResources(testDir)) } func reloadTestInit(t *testing.T) { @@ -124,19 +130,26 @@ func TestReloadHugeNumberOfFiles(t *testing.T) { // try to remove, skip error assert.NoError(t, freeResources(testDir)) assert.NoError(t, freeResources(testCopyToDir)) - err = os.Mkdir(testDir, 0755) - assert.NoError(t, err) - err = os.Mkdir(testCopyToDir, 0755) - assert.NoError(t, err) - defer func() { - assert.NoError(t, freeResources(testDir)) - assert.NoError(t, freeResources(testCopyToDir)) - }() + assert.NoError(t, os.Mkdir(testDir, 0755)) + assert.NoError(t, os.Mkdir(testCopyToDir, 0755)) + + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + + mockLogger.EXPECT().Debug("file added to the list of removed files", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).AnyTimes() + mockLogger.EXPECT().Debug("http handler response received", "elapsed", gomock.Any(), "remote address", "127.0.0.1").Times(1) + mockLogger.EXPECT().Debug("file was created", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(1) + mockLogger.EXPECT().Debug("file was updated", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(1) + mockLogger.EXPECT().Debug("file was added to watcher", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(1) + mockLogger.EXPECT().Info("HTTP plugin got restart request. Restarting...").MinTimes(1) + mockLogger.EXPECT().Info("HTTP workers Pool successfully restarted").MinTimes(1) + mockLogger.EXPECT().Info("HTTP listeners successfully re-added").MinTimes(1) + mockLogger.EXPECT().Info("HTTP plugin successfully restarted").MinTimes(1) err = cont.RegisterAll( cfg, - &logger.ZapLogger{}, + mockLogger, &server.Plugin{}, &httpPlugin.Plugin{}, &reload.Plugin{}, @@ -145,9 +158,7 @@ func TestReloadHugeNumberOfFiles(t *testing.T) { assert.NoError(t, err) err = cont.Init() - if err != nil { - t.Fatal(err) - } + assert.NoError(t, err) ch, err := cont.Serve() assert.NoError(t, err) @@ -158,7 +169,7 @@ func TestReloadHugeNumberOfFiles(t *testing.T) { wg := &sync.WaitGroup{} wg.Add(1) - tt := time.NewTimer(time.Second * 100) + tt := time.NewTimer(time.Second * 60) go func() { defer wg.Done() @@ -188,22 +199,20 @@ func TestReloadHugeNumberOfFiles(t *testing.T) { }() t.Run("ReloadTestHugeNumberOfFiles", reloadHugeNumberOfFiles) - ttt := time.Now() t.Run("ReloadRandomlyChangeFile", randomlyChangeFile) - if time.Since(ttt).Seconds() > 80 { - t.Fatal("spend too much time on reloading") - } - reloadHTTPLiveAfterReset(t, "22388") wg.Wait() + + assert.NoError(t, freeResources(testDir)) + assert.NoError(t, freeResources(testCopyToDir)) } func randomlyChangeFile(t *testing.T) { - // we know, that directory contains 5000 files (0-4999) + // we know, that directory contains 500 files (0-499) // let's try to randomly change it - for i := 0; i < 100; i++ { + for i := 0; i < 10; i++ { // rand sleep - rSleep := rand.Int63n(1000) // nolint:gosec + rSleep := rand.Int63n(500) // nolint:gosec time.Sleep(time.Millisecond * time.Duration(rSleep)) rNum := rand.Int63n(int64(hugeNumberOfFiles)) // nolint:gosec err := ioutil.WriteFile(filepath.Join(testDir, "file_"+strconv.Itoa(int(rNum))+".txt"), []byte("Hello, Gophers!"), 0755) // nolint:gosec @@ -229,16 +238,23 @@ func TestReloadFilterFileExt(t *testing.T) { // try to remove, skip error assert.NoError(t, freeResources(testDir)) - err = os.Mkdir(testDir, 0755) - assert.NoError(t, err) + assert.NoError(t, os.Mkdir(testDir, 0755)) - defer func() { - assert.NoError(t, freeResources(testDir)) - }() + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + + mockLogger.EXPECT().Debug("http handler response received", "elapsed", gomock.Any(), "remote address", "127.0.0.1").Times(1) + mockLogger.EXPECT().Debug("file was created", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(100) + mockLogger.EXPECT().Debug("file was added to watcher", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(1) + mockLogger.EXPECT().Debug("file added to the list of removed files", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).AnyTimes() + mockLogger.EXPECT().Info("HTTP plugin got restart request. Restarting...").Times(1) + mockLogger.EXPECT().Info("HTTP workers Pool successfully restarted").Times(1) + mockLogger.EXPECT().Info("HTTP listeners successfully re-added").Times(1) + mockLogger.EXPECT().Info("HTTP plugin successfully restarted").Times(1) err = cont.RegisterAll( cfg, - &logger.ZapLogger{}, + mockLogger, &server.Plugin{}, &httpPlugin.Plugin{}, &reload.Plugin{}, @@ -247,9 +263,7 @@ func TestReloadFilterFileExt(t *testing.T) { assert.NoError(t, err) err = cont.Init() - if err != nil { - t.Fatal(err) - } + assert.NoError(t, err) ch, err := cont.Serve() assert.NoError(t, err) @@ -260,7 +274,7 @@ func TestReloadFilterFileExt(t *testing.T) { wg := &sync.WaitGroup{} wg.Add(1) - tt := time.NewTimer(time.Second * 40) + tt := time.NewTimer(time.Second * 60) go func() { defer wg.Done() @@ -290,15 +304,11 @@ func TestReloadFilterFileExt(t *testing.T) { }() t.Run("ReloadMakeFiles", reloadMakeFiles) - ttt := time.Now() t.Run("ReloadFilteredExt", reloadFilteredExt) - if time.Since(ttt).Seconds() > 20 { - t.Fatal("spend too much time on reloading") - } - - reloadHTTPLiveAfterReset(t, "27388") wg.Wait() + + assert.NoError(t, freeResources(testDir)) } func reloadMakeFiles(t *testing.T) { @@ -336,7 +346,7 @@ func reloadFilteredExt(t *testing.T) { } // Should be events only about creating files with txt ext -func TestReloadCopy3k(t *testing.T) { +func TestReloadCopy500(t *testing.T) { cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) assert.NoError(t, err) @@ -348,23 +358,29 @@ func TestReloadCopy3k(t *testing.T) { // try to remove, skip error assert.NoError(t, freeResources(testDir)) assert.NoError(t, freeResources(testCopyToDir)) - assert.NoError(t, freeResources("dir1")) - err = os.Mkdir(testDir, 0755) - assert.NoError(t, err) - err = os.Mkdir(testCopyToDir, 0755) - assert.NoError(t, err) - err = os.Mkdir("dir1", 0755) - assert.NoError(t, err) - - defer func() { - assert.NoError(t, freeResources(testDir)) - assert.NoError(t, freeResources(testCopyToDir)) - assert.NoError(t, freeResources("dir1")) - }() + assert.NoError(t, freeResources(dir1)) + + assert.NoError(t, os.Mkdir(testDir, 0755)) + assert.NoError(t, os.Mkdir(testCopyToDir, 0755)) + assert.NoError(t, os.Mkdir(dir1, 0755)) + + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + // + mockLogger.EXPECT().Debug("http handler response received", "elapsed", gomock.Any(), "remote address", "127.0.0.1").Times(1) + mockLogger.EXPECT().Debug("file was created", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(50) + mockLogger.EXPECT().Debug("file was added to watcher", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(50) + mockLogger.EXPECT().Debug("file added to the list of removed files", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(50) + mockLogger.EXPECT().Debug("file was removed from watcher", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(50) + mockLogger.EXPECT().Debug("file was updated", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(50) + mockLogger.EXPECT().Info("HTTP plugin got restart request. Restarting...").MinTimes(1) + mockLogger.EXPECT().Info("HTTP workers Pool successfully restarted").MinTimes(1) + mockLogger.EXPECT().Info("HTTP listeners successfully re-added").MinTimes(1) + mockLogger.EXPECT().Info("HTTP plugin successfully restarted").MinTimes(1) err = cont.RegisterAll( cfg, - &logger.ZapLogger{}, + mockLogger, &server.Plugin{}, &httpPlugin.Plugin{}, &reload.Plugin{}, @@ -373,9 +389,7 @@ func TestReloadCopy3k(t *testing.T) { assert.NoError(t, err) err = cont.Init() - if err != nil { - t.Fatal(err) - } + assert.NoError(t, err) ch, err := cont.Serve() assert.NoError(t, err) @@ -386,7 +400,7 @@ func TestReloadCopy3k(t *testing.T) { wg := &sync.WaitGroup{} wg.Add(1) - tt := time.NewTimer(time.Second * 220) + tt := time.NewTimer(time.Second * 120) go func() { defer wg.Done() @@ -398,6 +412,7 @@ func TestReloadCopy3k(t *testing.T) { if err != nil { assert.FailNow(t, "error", err.Error()) } + return case <-sig: err = cont.Stop() if err != nil { @@ -426,19 +441,16 @@ func TestReloadCopy3k(t *testing.T) { // 3 // Recursive - t.Run("ReloadMake3kFiles", reloadMake3kFiles) - ttt := time.Now() + t.Run("ReloadMake300Files", reloadMake300Files) t.Run("ReloadCopyFiles", reloadCopyFiles) - if time.Since(ttt).Seconds() > 120 { - t.Fatal("spend too much time on copy") - } - t.Run("ReloadRecursiveDirsSupport", copyFilesRecursive) t.Run("RandomChangesInRecursiveDirs", randomChangesInRecursiveDirs) t.Run("RemoveFilesSupport", removeFilesSupport) t.Run("ReloadMoveSupport", reloadMoveSupport) - reloadHTTPLiveAfterReset(t, "37388") + assert.NoError(t, freeResources(testDir)) + assert.NoError(t, freeResources(testCopyToDir)) + assert.NoError(t, freeResources(dir1)) wg.Wait() } @@ -446,11 +458,11 @@ func TestReloadCopy3k(t *testing.T) { func reloadMoveSupport(t *testing.T) { t.Run("MoveSupportCopy", copyFilesRecursive) // move some files - for i := 0; i < 50; i++ { + for i := 0; i < 10; i++ { // rand sleep - rSleep := rand.Int63n(1000) // nolint:gosec + rSleep := rand.Int63n(500) // nolint:gosec time.Sleep(time.Millisecond * time.Duration(rSleep)) - rNum := rand.Int63n(int64(200)) // nolint:gosec + rNum := rand.Int63n(int64(100)) // nolint:gosec rDir := rand.Int63n(9) // nolint:gosec rExt := rand.Int63n(3) // nolint:gosec @@ -482,11 +494,11 @@ func reloadMoveSupport(t *testing.T) { func removeFilesSupport(t *testing.T) { // remove some files - for i := 0; i < 50; i++ { + for i := 0; i < 10; i++ { // rand sleep - rSleep := rand.Int63n(1000) // nolint:gosec + rSleep := rand.Int63n(500) // nolint:gosec time.Sleep(time.Millisecond * time.Duration(rSleep)) - rNum := rand.Int63n(int64(200)) // nolint:gosec + rNum := rand.Int63n(int64(100)) // nolint:gosec rDir := rand.Int63n(10) // nolint:gosec rExt := rand.Int63n(3) // nolint:gosec @@ -509,8 +521,8 @@ func removeFilesSupport(t *testing.T) { "dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9", "dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10", } - err := os.Remove(filepath.Join(dirs[rDir], "file_"+strconv.Itoa(int(rNum))+ext[rExt])) - assert.NoError(t, err) + // here can be a situation, when file already deleted + _ = os.Remove(filepath.Join(dirs[rDir], "file_"+strconv.Itoa(int(rNum))+ext[rExt])) } } @@ -540,11 +552,11 @@ func randomChangesInRecursiveDirs(t *testing.T) { "foo_", // should be created "bar_", // should be created } - for i := 0; i < 50; i++ { + for i := 0; i < 10; i++ { // rand sleep - rSleep := rand.Int63n(1000) // nolint:gosec + rSleep := rand.Int63n(500) // nolint:gosec time.Sleep(time.Millisecond * time.Duration(rSleep)) - rNum := rand.Int63n(int64(200)) // nolint:gosec + rNum := rand.Int63n(int64(100)) // nolint:gosec rDir := rand.Int63n(10) // nolint:gosec rExt := rand.Int63n(3) // nolint:gosec rName := rand.Int63n(3) // nolint:gosec @@ -583,19 +595,18 @@ func reloadCopyFiles(t *testing.T) { assert.NoError(t, freeResources(testDir)) assert.NoError(t, freeResources(testCopyToDir)) - err = os.Mkdir(testDir, 0755) - assert.NoError(t, err) - err = os.Mkdir(testCopyToDir, 0755) - assert.NoError(t, err) + + assert.NoError(t, os.Mkdir(testDir, 0755)) + assert.NoError(t, os.Mkdir(testCopyToDir, 0755)) // recreate files - for i := uint(0); i < 200; i++ { + for i := uint(0); i < 100; i++ { assert.NoError(t, makeFile("file_"+strconv.Itoa(int(i))+".txt")) } - for i := uint(0); i < 200; i++ { + for i := uint(0); i < 100; i++ { assert.NoError(t, makeFile("file_"+strconv.Itoa(int(i))+".abc")) } - for i := uint(0); i < 200; i++ { + for i := uint(0); i < 100; i++ { assert.NoError(t, makeFile("file_"+strconv.Itoa(int(i))+".def")) } @@ -603,7 +614,7 @@ func reloadCopyFiles(t *testing.T) { assert.NoError(t, err) } -func reloadMake3kFiles(t *testing.T) { +func reloadMake300Files(t *testing.T) { for i := uint(0); i < 100; i++ { assert.NoError(t, makeFile("file_"+strconv.Itoa(int(i))+".txt")) } @@ -627,25 +638,21 @@ func TestReloadNoRecursion(t *testing.T) { // try to remove, skip error assert.NoError(t, freeResources(testDir)) assert.NoError(t, freeResources(testCopyToDir)) - assert.NoError(t, freeResources("dir1")) - err = os.Mkdir(testDir, 0755) - assert.NoError(t, err) + assert.NoError(t, freeResources(dir1)) - err = os.Mkdir("dir1", 0755) - assert.NoError(t, err) + assert.NoError(t, os.Mkdir(testDir, 0755)) + assert.NoError(t, os.Mkdir(dir1, 0755)) + assert.NoError(t, os.Mkdir(testCopyToDir, 0755)) - err = os.Mkdir(testCopyToDir, 0755) - assert.NoError(t, err) + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) - defer func() { - assert.NoError(t, freeResources(testDir)) - assert.NoError(t, freeResources(testCopyToDir)) - assert.NoError(t, freeResources("dir1")) - }() + // http server should not be restarted. all event from wrong file extensions should be skipped + mockLogger.EXPECT().Debug("http handler response received", "elapsed", gomock.Any(), "remote address", "127.0.0.1").Times(1) err = cont.RegisterAll( cfg, - &logger.ZapLogger{}, + mockLogger, &server.Plugin{}, &httpPlugin.Plugin{}, &reload.Plugin{}, @@ -654,9 +661,7 @@ func TestReloadNoRecursion(t *testing.T) { assert.NoError(t, err) err = cont.Init() - if err != nil { - t.Fatal(err) - } + assert.NoError(t, err) ch, err := cont.Serve() assert.NoError(t, err) @@ -695,35 +700,19 @@ func TestReloadNoRecursion(t *testing.T) { } } }() + t.Run("ReloadMakeFiles", reloadMakeFiles) // make files in the testDir t.Run("ReloadCopyFilesRecursive", reloadCopyFiles) - reloadHTTPLiveAfterReset(t, "22766") - wg.Wait() + + assert.NoError(t, freeResources(testDir)) + assert.NoError(t, freeResources(testCopyToDir)) + assert.NoError(t, freeResources(dir1)) } // ======================================================================== -func reloadHTTPLiveAfterReset(t *testing.T, port string) { - req, err := http.NewRequest("GET", "http://localhost:"+port, nil) - assert.NoError(t, err) - - r, err := http.DefaultClient.Do(req) - if err != nil { - t.Fatal(err) - } - - b, err := ioutil.ReadAll(r.Body) - assert.NoError(t, err) - - assert.Equal(t, 200, r.StatusCode) - assert.Equal(t, "hello world", string(b)) - - err = r.Body.Close() - assert.NoError(t, err) -} - func freeResources(path string) error { return os.RemoveAll(path) } |