From ad39466afb39fac49977cfb97f20f682f54bf35e Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Fri, 18 Dec 2020 22:57:33 +0300 Subject: Move roadrunner payload out of internal to pkg --- plugins/http/request.go | 10 +++++----- plugins/http/response.go | 4 ++-- plugins/http/tests/response_test.go | 16 ++++++++-------- plugins/server/tests/plugin_pipes.go | 4 ++-- plugins/server/tests/plugin_sockets.go | 4 ++-- plugins/server/tests/plugin_tcp.go | 4 ++-- 6 files changed, 21 insertions(+), 21 deletions(-) (limited to 'plugins') diff --git a/plugins/http/request.go b/plugins/http/request.go index 5df79b7d..d613bcf6 100644 --- a/plugins/http/request.go +++ b/plugins/http/request.go @@ -10,7 +10,7 @@ import ( j "github.com/json-iterator/go" "github.com/spiral/roadrunner/v2/interfaces/log" - "github.com/spiral/roadrunner/v2/internal" + "github.com/spiral/roadrunner/v2/pkg/payload" "github.com/spiral/roadrunner/v2/plugins/http/attributes" ) @@ -136,17 +136,17 @@ func (r *Request) Close(log log.Logger) { // Payload request marshaled RoadRunner payload based on PSR7 data. values encode method is JSON. Make sure to open // files prior to calling this method. -func (r *Request) Payload() (internal.Payload, error) { - p := internal.Payload{} +func (r *Request) Payload() (payload.Payload, error) { + p := payload.Payload{} var err error if p.Context, err = json.Marshal(r); err != nil { - return internal.Payload{}, err + return payload.Payload{}, err } if r.Parsed { if p.Body, err = json.Marshal(r.body); err != nil { - return internal.Payload{}, err + return payload.Payload{}, err } } else if r.body != nil { p.Body = r.body.([]byte) diff --git a/plugins/http/response.go b/plugins/http/response.go index 9700a16c..17049ce1 100644 --- a/plugins/http/response.go +++ b/plugins/http/response.go @@ -6,7 +6,7 @@ import ( "strings" "sync" - "github.com/spiral/roadrunner/v2/internal" + "github.com/spiral/roadrunner/v2/pkg/payload" ) // Response handles PSR7 response logic. @@ -23,7 +23,7 @@ type Response struct { } // NewResponse creates new response based on given pool payload. -func NewResponse(p internal.Payload) (*Response, error) { +func NewResponse(p payload.Payload) (*Response, error) { r := &Response{Body: p.Body} if err := json.Unmarshal(p.Context, r); err != nil { return nil, err diff --git a/plugins/http/tests/response_test.go b/plugins/http/tests/response_test.go index a526fe03..7901a0d1 100644 --- a/plugins/http/tests/response_test.go +++ b/plugins/http/tests/response_test.go @@ -6,7 +6,7 @@ import ( "net/http" "testing" - "github.com/spiral/roadrunner/v2/internal" + "github.com/spiral/roadrunner/v2/pkg/payload" http2 "github.com/spiral/roadrunner/v2/plugins/http" "github.com/stretchr/testify/assert" ) @@ -45,13 +45,13 @@ func (tw *testWriter) Push(target string, opts *http.PushOptions) error { } func TestNewResponse_Error(t *testing.T) { - r, err := http2.NewResponse(internal.Payload{Context: []byte(`invalid payload`)}) + r, err := http2.NewResponse(payload.Payload{Context: []byte(`invalid payload`)}) assert.Error(t, err) assert.Nil(t, r) } func TestNewResponse_Write(t *testing.T) { - r, err := http2.NewResponse(internal.Payload{ + r, err := http2.NewResponse(payload.Payload{ Context: []byte(`{"headers":{"key":["value"]},"status": 301}`), Body: []byte(`sample body`), }) @@ -68,7 +68,7 @@ func TestNewResponse_Write(t *testing.T) { } func TestNewResponse_Stream(t *testing.T) { - r, err := http2.NewResponse(internal.Payload{ + r, err := http2.NewResponse(payload.Payload{ Context: []byte(`{"headers":{"key":["value"]},"status": 301}`), }) @@ -92,7 +92,7 @@ func TestNewResponse_Stream(t *testing.T) { } func TestNewResponse_StreamError(t *testing.T) { - r, err := http2.NewResponse(internal.Payload{ + r, err := http2.NewResponse(payload.Payload{ Context: []byte(`{"headers":{"key":["value"]},"status": 301}`), }) @@ -112,7 +112,7 @@ func TestNewResponse_StreamError(t *testing.T) { } func TestWrite_HandlesPush(t *testing.T) { - r, err := http2.NewResponse(internal.Payload{ + r, err := http2.NewResponse(payload.Payload{ Context: []byte(`{"headers":{"Http2-Push":["/test.js"],"content-type":["text/html"]},"status": 200}`), }) @@ -127,7 +127,7 @@ func TestWrite_HandlesPush(t *testing.T) { } func TestWrite_HandlesTrailers(t *testing.T) { - r, err := http2.NewResponse(internal.Payload{ + r, err := http2.NewResponse(payload.Payload{ Context: []byte(`{"headers":{"Trailer":["foo, bar", "baz"],"foo":["test"],"bar":["demo"]},"status": 200}`), }) @@ -146,7 +146,7 @@ func TestWrite_HandlesTrailers(t *testing.T) { } func TestWrite_HandlesHandlesWhitespacesInTrailer(t *testing.T) { - r, err := http2.NewResponse(internal.Payload{ + r, err := http2.NewResponse(payload.Payload{ Context: []byte( `{"headers":{"Trailer":["foo\t,bar , baz"],"foo":["a"],"bar":["b"],"baz":["c"]},"status": 200}`), }) diff --git a/plugins/server/tests/plugin_pipes.go b/plugins/server/tests/plugin_pipes.go index 61c9a8f9..9a8a630c 100644 --- a/plugins/server/tests/plugin_pipes.go +++ b/plugins/server/tests/plugin_pipes.go @@ -7,7 +7,7 @@ import ( "github.com/spiral/errors" "github.com/spiral/roadrunner/v2/interfaces/pool" "github.com/spiral/roadrunner/v2/interfaces/server" - "github.com/spiral/roadrunner/v2/internal" + "github.com/spiral/roadrunner/v2/pkg/payload" poolImpl "github.com/spiral/roadrunner/v2/pkg/pool" "github.com/spiral/roadrunner/v2/pkg/worker" "github.com/spiral/roadrunner/v2/plugins/config" @@ -47,7 +47,7 @@ func (f *Foo) Serve() chan error { const op = errors.Op("serve") // test payload for echo - r := internal.Payload{ + r := payload.Payload{ Context: nil, Body: []byte(Response), } diff --git a/plugins/server/tests/plugin_sockets.go b/plugins/server/tests/plugin_sockets.go index 3b97efff..b1545718 100644 --- a/plugins/server/tests/plugin_sockets.go +++ b/plugins/server/tests/plugin_sockets.go @@ -6,7 +6,7 @@ import ( "github.com/spiral/errors" "github.com/spiral/roadrunner/v2/interfaces/pool" "github.com/spiral/roadrunner/v2/interfaces/server" - "github.com/spiral/roadrunner/v2/internal" + "github.com/spiral/roadrunner/v2/pkg/payload" "github.com/spiral/roadrunner/v2/pkg/worker" "github.com/spiral/roadrunner/v2/plugins/config" plugin "github.com/spiral/roadrunner/v2/plugins/server" @@ -31,7 +31,7 @@ func (f *Foo2) Serve() chan error { conf := &plugin.Config{} // test payload for echo - r := internal.Payload{ + r := payload.Payload{ Context: nil, Body: []byte(Response), } diff --git a/plugins/server/tests/plugin_tcp.go b/plugins/server/tests/plugin_tcp.go index 2857dadc..da92288a 100644 --- a/plugins/server/tests/plugin_tcp.go +++ b/plugins/server/tests/plugin_tcp.go @@ -6,7 +6,7 @@ import ( "github.com/spiral/errors" "github.com/spiral/roadrunner/v2/interfaces/pool" "github.com/spiral/roadrunner/v2/interfaces/server" - "github.com/spiral/roadrunner/v2/internal" + "github.com/spiral/roadrunner/v2/pkg/payload" "github.com/spiral/roadrunner/v2/pkg/worker" "github.com/spiral/roadrunner/v2/plugins/config" plugin "github.com/spiral/roadrunner/v2/plugins/server" @@ -31,7 +31,7 @@ func (f *Foo3) Serve() chan error { conf := &plugin.Config{} // test payload for echo - r := internal.Payload{ + r := payload.Payload{ Context: nil, Body: []byte(Response), } -- cgit v1.2.3 From 9d2bb93b3341471a084482f7bd41cac543f95cb2 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sun, 20 Dec 2020 15:35:03 +0300 Subject: Reload tests stabilization --- plugins/reload/tests/reload_plugin_test.go | 227 +++++++++++++++-------------- 1 file changed, 121 insertions(+), 106 deletions(-) (limited to 'plugins') diff --git a/plugins/reload/tests/reload_plugin_test.go b/plugins/reload/tests/reload_plugin_test.go index d2fad28d..4d60c146 100644 --- a/plugins/reload/tests/reload_plugin_test.go +++ b/plugins/reload/tests/reload_plugin_test.go @@ -14,11 +14,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 +28,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 +45,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 +67,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) @@ -104,6 +111,7 @@ func TestReloadInit(t *testing.T) { reloadHTTPLiveAfterReset(t, "22388") wg.Wait() + assert.NoError(t, freeResources(testDir)) } func reloadTestInit(t *testing.T) { @@ -124,19 +132,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 +160,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 +171,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 +201,21 @@ 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") + 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 +241,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 +266,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 +277,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 +307,12 @@ 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 +350,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 +362,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 +393,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 +404,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 +416,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,13 +445,8 @@ 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) @@ -440,17 +454,21 @@ func TestReloadCopy3k(t *testing.T) { reloadHTTPLiveAfterReset(t, "37388") + assert.NoError(t, freeResources(testDir)) + assert.NoError(t, freeResources(testCopyToDir)) + assert.NoError(t, freeResources(dir1)) + wg.Wait() } 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 +500,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 +527,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 +558,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 +601,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 +620,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 +644,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 +667,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,12 +706,16 @@ 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)) } // ======================================================================== -- cgit v1.2.3 From 349088db2081704e15699ce1e6f6b1f8898bd336 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sun, 20 Dec 2020 16:48:49 +0300 Subject: Remove reloadLiveAfterHTTP --- plugins/reload/tests/reload_plugin_test.go | 26 -------------------------- 1 file changed, 26 deletions(-) (limited to 'plugins') diff --git a/plugins/reload/tests/reload_plugin_test.go b/plugins/reload/tests/reload_plugin_test.go index 4d60c146..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" @@ -108,7 +107,6 @@ func TestReloadInit(t *testing.T) { }() t.Run("ReloadTestInit", reloadTestInit) - reloadHTTPLiveAfterReset(t, "22388") wg.Wait() assert.NoError(t, freeResources(testDir)) @@ -203,7 +201,6 @@ func TestReloadHugeNumberOfFiles(t *testing.T) { t.Run("ReloadTestHugeNumberOfFiles", reloadHugeNumberOfFiles) t.Run("ReloadRandomlyChangeFile", randomlyChangeFile) - reloadHTTPLiveAfterReset(t, "22388") wg.Wait() assert.NoError(t, freeResources(testDir)) @@ -308,7 +305,6 @@ func TestReloadFilterFileExt(t *testing.T) { t.Run("ReloadMakeFiles", reloadMakeFiles) t.Run("ReloadFilteredExt", reloadFilteredExt) - reloadHTTPLiveAfterReset(t, "27388") wg.Wait() @@ -452,8 +448,6 @@ func TestReloadCopy500(t *testing.T) { 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)) @@ -710,7 +704,6 @@ 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)) @@ -720,25 +713,6 @@ func TestReloadNoRecursion(t *testing.T) { // ======================================================================== -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) } -- cgit v1.2.3 From fe0109a3b19ee82bd7bfbb57eae6b5b5166d7068 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Sun, 20 Dec 2020 17:42:56 +0300 Subject: Latest goridge version support --- plugins/checker/tests/plugin_test.go | 4 ++-- plugins/http/tests/http_test.go | 6 +++--- plugins/informer/tests/informer_test.go | 4 ++-- plugins/metrics/tests/metrics_test.go | 28 ++++++++++++++-------------- plugins/resetter/tests/resetter_test.go | 4 ++-- plugins/rpc/plugin.go | 6 +++--- plugins/rpc/tests/plugin2.go | 4 ++-- 7 files changed, 28 insertions(+), 28 deletions(-) (limited to 'plugins') diff --git a/plugins/checker/tests/plugin_test.go b/plugins/checker/tests/plugin_test.go index 02a7f953..38b751ff 100644 --- a/plugins/checker/tests/plugin_test.go +++ b/plugins/checker/tests/plugin_test.go @@ -13,7 +13,7 @@ import ( "time" "github.com/spiral/endure" - "github.com/spiral/goridge/v3" + goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" "github.com/spiral/roadrunner/v2/interfaces/status" "github.com/spiral/roadrunner/v2/plugins/checker" "github.com/spiral/roadrunner/v2/plugins/config" @@ -178,7 +178,7 @@ func TestStatusRPC(t *testing.T) { func checkRPCStatus(t *testing.T) { conn, err := net.Dial("tcp", "127.0.0.1:6005") assert.NoError(t, err) - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) st := &status.Status{} diff --git a/plugins/http/tests/http_test.go b/plugins/http/tests/http_test.go index 1a61597c..d7818981 100644 --- a/plugins/http/tests/http_test.go +++ b/plugins/http/tests/http_test.go @@ -17,7 +17,7 @@ import ( "github.com/golang/mock/gomock" "github.com/spiral/endure" - "github.com/spiral/goridge/v3" + goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" "github.com/spiral/roadrunner/v2" "github.com/spiral/roadrunner/v2/interfaces/events" "github.com/spiral/roadrunner/v2/mocks" @@ -193,7 +193,7 @@ func echoHTTP(t *testing.T) { func resetTest(t *testing.T) { conn, err := net.Dial("tcp", "127.0.0.1:6001") assert.NoError(t, err) - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) // WorkerList contains list of workers. var ret bool @@ -213,7 +213,7 @@ func resetTest(t *testing.T) { func informerTest(t *testing.T) { conn, err := net.Dial("tcp", "127.0.0.1:6001") assert.NoError(t, err) - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) // WorkerList contains list of workers. list := struct { // Workers is list of workers. diff --git a/plugins/informer/tests/informer_test.go b/plugins/informer/tests/informer_test.go index 193e84bb..dd06f1c4 100644 --- a/plugins/informer/tests/informer_test.go +++ b/plugins/informer/tests/informer_test.go @@ -10,7 +10,7 @@ import ( "time" "github.com/spiral/endure" - "github.com/spiral/goridge/v3" + goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" "github.com/spiral/roadrunner/v2" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/informer" @@ -84,7 +84,7 @@ func TestInformerInit(t *testing.T) { func informerRPCTest(t *testing.T) { conn, err := net.Dial("tcp", "127.0.0.1:6001") assert.NoError(t, err) - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) // WorkerList contains list of workers. list := struct { // Workers is list of workers. diff --git a/plugins/metrics/tests/metrics_test.go b/plugins/metrics/tests/metrics_test.go index 2d3a3c27..4572bc3f 100644 --- a/plugins/metrics/tests/metrics_test.go +++ b/plugins/metrics/tests/metrics_test.go @@ -12,7 +12,7 @@ import ( "time" "github.com/spiral/endure" - "github.com/spiral/goridge/v3" + goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/metrics" @@ -298,7 +298,7 @@ func observeMetricNotEnoughLabels(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -331,7 +331,7 @@ func observeMetric(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -364,7 +364,7 @@ func counterMetric(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -398,7 +398,7 @@ func registerHistogram(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -434,7 +434,7 @@ func subVector(t *testing.T) { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -480,7 +480,7 @@ func subMetric(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -523,7 +523,7 @@ func setOnHistogram(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -558,7 +558,7 @@ func setWithoutLabels(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -593,7 +593,7 @@ func missingSection(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -629,7 +629,7 @@ func vectorMetric(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -665,7 +665,7 @@ func setMetric(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ @@ -698,7 +698,7 @@ func addMetricsTest(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool m := metrics.Metric{ @@ -718,7 +718,7 @@ func declareMetricsTest(t *testing.T) { defer func() { _ = conn.Close() }() - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret bool nc := metrics.NamedCollector{ diff --git a/plugins/resetter/tests/resetter_test.go b/plugins/resetter/tests/resetter_test.go index 45de67e3..95c3a6b4 100644 --- a/plugins/resetter/tests/resetter_test.go +++ b/plugins/resetter/tests/resetter_test.go @@ -10,7 +10,7 @@ import ( "time" "github.com/spiral/endure" - "github.com/spiral/goridge/v3" + goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/resetter" @@ -83,7 +83,7 @@ func TestResetterInit(t *testing.T) { func resetterRPCTest(t *testing.T) { conn, err := net.Dial("tcp", "127.0.0.1:6001") assert.NoError(t, err) - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) // WorkerList contains list of workers. var ret bool diff --git a/plugins/rpc/plugin.go b/plugins/rpc/plugin.go index c8e63496..98242ade 100755 --- a/plugins/rpc/plugin.go +++ b/plugins/rpc/plugin.go @@ -7,7 +7,7 @@ import ( "github.com/spiral/endure" "github.com/spiral/errors" - "github.com/spiral/goridge/v3" + goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" "github.com/spiral/roadrunner/v2/interfaces/log" rpc_ "github.com/spiral/roadrunner/v2/interfaces/rpc" "github.com/spiral/roadrunner/v2/plugins/config" @@ -100,7 +100,7 @@ func (s *Plugin) Serve() chan error { return } - go s.rpc.ServeCodec(goridge.NewCodec(conn)) + go s.rpc.ServeCodec(goridgeRpc.NewCodec(conn)) } }() @@ -161,5 +161,5 @@ func (s *Plugin) Client() (*rpc.Client, error) { return nil, err } - return rpc.NewClientWithCodec(goridge.NewClientCodec(conn)), nil + return rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)), nil } diff --git a/plugins/rpc/tests/plugin2.go b/plugins/rpc/tests/plugin2.go index 347e0330..411b9c54 100644 --- a/plugins/rpc/tests/plugin2.go +++ b/plugins/rpc/tests/plugin2.go @@ -6,7 +6,7 @@ import ( "time" "github.com/spiral/errors" - "github.com/spiral/goridge/v3" + goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" ) // plugin2 makes a call to the plugin1 via RPC @@ -30,7 +30,7 @@ func (p2 *Plugin2) Serve() chan error { errCh <- errors.E(errors.Serve, err) return } - client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn)) + client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn)) var ret string err = client.Call("rpc_test.plugin1.Hello", "Valery", &ret) if err != nil { -- cgit v1.2.3