From c4aecf243ec42e6261fc0099f8394a5f42bf635d Mon Sep 17 00:00:00 2001 From: Ting Date: Fri, 7 Jun 2024 18:32:24 +0800 Subject: [PATCH] fix lint --- cmd/protoc-gen-go-test/ast.go | 6 +- cmd/protoc-gen-go-test/main.go | 4 +- core/econf/conf_api_test.go | 61 +++++++-------- core/econf/file/file_test.go | 122 +++++++++++++++++++++++++++++ core/econf/options_test.go | 17 ++-- core/elog/component_test.go | 68 ++++++++-------- core/elog/elog_field_test.go | 45 ++++++----- core/emetric/tcpstat.go | 16 ++-- core/emetric/tcpstat_test.go | 15 ++-- ego_function_test.go | 82 +++++++++---------- examples/task/httpjob/main.go | 7 +- server/egin/interceptor.go | 5 +- server/egin/interceptor_test.go | 32 ++++---- server/egovernor/config_test.go | 7 +- server/egovernor/container_test.go | 10 ++- server/egrpc/interceptor_test.go | 12 +-- task/ejob/container_test.go | 4 +- 17 files changed, 312 insertions(+), 201 deletions(-) diff --git a/cmd/protoc-gen-go-test/ast.go b/cmd/protoc-gen-go-test/ast.go index 09d17b79..2141fd7c 100644 --- a/cmd/protoc-gen-go-test/ast.go +++ b/cmd/protoc-gen-go-test/ast.go @@ -3,20 +3,20 @@ package main import ( "bytes" "fmt" - "io/ioutil" "os" "regexp" "strings" "github.com/dave/dst" "github.com/dave/dst/decorator" - "github.com/gotomicro/ego/internal/tools" orderedmap "github.com/wk8/go-ordered-map" "golang.org/x/tools/imports" + + "github.com/gotomicro/ego/internal/tools" ) func checkAndMerge(f *file) ([]byte, error) { - origBytes, err := ioutil.ReadFile(f.orig) + origBytes, err := os.ReadFile(f.orig) if err != nil && !os.IsNotExist(err) { return nil, fmt.Errorf("read origFile fail, %w", err) } diff --git a/cmd/protoc-gen-go-test/main.go b/cmd/protoc-gen-go-test/main.go index e28a48c2..1cabd9b6 100644 --- a/cmd/protoc-gen-go-test/main.go +++ b/cmd/protoc-gen-go-test/main.go @@ -3,7 +3,7 @@ package main import ( "flag" "fmt" - "io/ioutil" + "io" "log" "os" "path/filepath" @@ -30,7 +30,7 @@ func main() { if len(os.Args) > 1 { exit(fmt.Errorf("unknown argument %q (this program should be run by protoc, not directly)", os.Args[1])) } - in, err := ioutil.ReadAll(os.Stdin) + in, err := io.ReadAll(os.Stdin) if err != nil { exit(err) } diff --git a/core/econf/conf_api_test.go b/core/econf/conf_api_test.go index 10233ce4..9573adc0 100644 --- a/core/econf/conf_api_test.go +++ b/core/econf/conf_api_test.go @@ -1,7 +1,6 @@ package econf import ( - "io/ioutil" "log" "net/url" "os" @@ -14,7 +13,6 @@ import ( "github.com/BurntSushi/toml" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) type mockDataSource struct { @@ -72,11 +70,11 @@ func TestWatchFile(t *testing.T) { v, configFile, cleanup, wg := newWithConfigFile(t) defer cleanup() _, err := os.Stat(configFile) - require.NoError(t, err) + assert.NoError(t, err) t.Logf("test config file: %s\n", configFile) // when overwriting the file and waiting for the custom change notification handler to be triggered - err = os.WriteFile(configFile, []byte(`foo= "baz"`), 0640) - require.Nil(t, err) + err1 := os.WriteFile(configFile, []byte(`foo= "baz"`), 0640) + assert.NoError(t, err1) // wg.Wait() wg.Done() // then the config value should have changed @@ -92,26 +90,24 @@ func TestWatchFile(t *testing.T) { // when link to another `config.toml` file dataDir2 := path.Join(watchDir, "data2") err := os.Mkdir(dataDir2, 0777) - require.Nil(t, err) + assert.NoError(t, err) configFile2 := path.Join(dataDir2, "config.toml") - err = os.WriteFile(configFile2, []byte(`foo= "baz"`), 0640) - require.Nil(t, err) + err1 := os.WriteFile(configFile2, []byte(`foo= "baz"`), 0640) + assert.NoError(t, err1) // change the symlink using the `ln -sfn` command - err = exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run() - require.Nil(t, err) + err2 := exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run() + assert.NoError(t, err2) wg.Wait() // then - require.Nil(t, err) assert.Equal(t, "baz", v.Get("foo")) }) } func newWithConfigFile(t *testing.T) (*Configuration, string, func(), *sync.WaitGroup) { - watchDir, err := ioutil.TempDir("", "") - require.Nil(t, err) + var watchDir = os.TempDir() configFile := path.Join(watchDir, "config.toml") - err = os.WriteFile(configFile, []byte(`foo= "baz"`), 0640) - require.Nil(t, err) + err := os.WriteFile(configFile, []byte(`foo= "baz"`), 0640) + assert.NoError(t, err) content, err := os.ReadFile(configFile) if err != nil { log.Panicf("Error: %v\n", err) @@ -119,8 +115,7 @@ func newWithConfigFile(t *testing.T) (*Configuration, string, func(), *sync.Wait t.Logf("Content of configFile: %v\n", string(content)) cleanup := func() { - err1 := os.RemoveAll(watchDir) - assert.NoError(t, err1) + os.RemoveAll(watchDir) } v := New() @@ -138,34 +133,32 @@ func newWithConfigFile(t *testing.T) (*Configuration, string, func(), *sync.Wait wg.Done() }) - err = v.LoadFromDataSource(provider, toml.Unmarshal) - assert.Nil(t, err) + err1 := v.LoadFromDataSource(provider, toml.Unmarshal) + assert.NoError(t, err1) assert.Equal(t, "baz", v.Get("foo")) return v, configFile, cleanup, wg } func newWithSymlinkedConfigFile(t *testing.T) (*Configuration, string, string, func(), *sync.WaitGroup) { - watchDir, err := ioutil.TempDir("", "") - require.Nil(t, err) + watchDir := os.TempDir() dataDir1 := path.Join(watchDir, "data1") - err = os.Mkdir(dataDir1, 0777) - require.Nil(t, err) + err := os.Mkdir(dataDir1, 0777) + assert.NoError(t, err) realConfigFile := path.Join(dataDir1, "config.toml") t.Logf("Real config file location: %s\n", realConfigFile) - err = os.WriteFile(realConfigFile, []byte(`foo= "baz"`), 0640) - require.Nil(t, err) + err1 := os.WriteFile(realConfigFile, []byte(`foo= "baz"`), 0640) + assert.NoError(t, err1) cleanup := func() { - err1 := os.RemoveAll(watchDir) - assert.NoError(t, err1) + os.RemoveAll(watchDir) } // now, symlink the tm `data1` dir to `data` in the baseDir - err = os.Symlink(dataDir1, path.Join(watchDir, "data")) - require.Nil(t, err) + err2 := os.Symlink(dataDir1, path.Join(watchDir, "data")) + assert.NoError(t, err2) // and link the `/datadir1/config.toml` to `/config.toml` configFile := path.Join(watchDir, "config.toml") - err = os.Symlink(path.Join(watchDir, "data", "config.toml"), configFile) - require.Nil(t, err) + err3 := os.Symlink(path.Join(watchDir, "data", "config.toml"), configFile) + assert.NoError(t, err3) t.Logf("Config file location: %s\n", path.Join(watchDir, "config.toml")) v := New() @@ -182,8 +175,8 @@ func newWithSymlinkedConfigFile(t *testing.T) (*Configuration, string, string, f } wg.Done() }) - err = v.LoadFromDataSource(provider, toml.Unmarshal) - require.Nil(t, err) - require.Equal(t, "bar", v.Get("foo")) + err4 := v.LoadFromDataSource(provider, toml.Unmarshal) + assert.NoError(t, err4) + assert.Equal(t, "bar", v.Get("foo")) return v, watchDir, configFile, cleanup, wg } diff --git a/core/econf/file/file_test.go b/core/econf/file/file_test.go index 84bc1a6f..58e89ab5 100644 --- a/core/econf/file/file_test.go +++ b/core/econf/file/file_test.go @@ -1,13 +1,135 @@ package file import ( + "os" + "os/exec" + "path" + "runtime" + "sync" + "sync/atomic" "testing" "github.com/stretchr/testify/assert" "github.com/gotomicro/ego/core/econf" + "github.com/gotomicro/ego/core/econf/manager" ) +func TestWatchFile(t *testing.T) { + if runtime.GOOS == "linux" { + t.Skip("Skip test on Linux ...") + } + t.Run("file content changed", func(t *testing.T) { + // given a `config.yaml` file being watched + v, configFile, cleanup, wg := newWithConfigFile(t) + defer cleanup() + _, err := os.Stat(configFile) + assert.NoError(t, err) + t.Logf("test config file: %s\n", configFile) + // when overwriting the file and waiting for the custom change notification handler to be triggered + err1 := os.WriteFile(configFile, []byte("foo: baz\n"), 0640) + assert.NoError(t, err1) + wg.Wait() + // then the config value should have changed + assert.Equal(t, "baz", v.Get("foo")) + }) + + t.Run("link to real file changed (Kubernetes)", func(t *testing.T) { + // skip if not executed on Linux + if runtime.GOOS != "linux" { + t.Skipf("Skipping test as symlink replacements don't work on non-linux environment...") + } + + v, watchDir, _, _, wg := newWithSymlinkedConfigFile(t) + // defer cleanup() + // when link to another `config.yaml` file + dataDir2 := path.Join(watchDir, "data2") + err := os.Mkdir(dataDir2, 0777) + assert.NoError(t, err) + configFile2 := path.Join(dataDir2, "config.yaml") + err1 := os.WriteFile(configFile2, []byte("foo: baz\n"), 0640) + assert.NoError(t, err1) + // change the symlink using the `ln -sfn` command + err3 := exec.Command("ln", "-sfn", dataDir2, path.Join(watchDir, "data")).Run() + assert.NoError(t, err3) + wg.Wait() + assert.Equal(t, "baz", v.Get("foo")) + }) +} + +func newWithConfigFile(t *testing.T) (*econf.Configuration, string, func(), *sync.WaitGroup) { + watchDir := os.TempDir() + configFile := path.Join(watchDir, "config.yaml") + err := os.WriteFile(configFile, []byte("foo: bar\n"), 0640) + assert.NoError(t, err) + cleanup := func() { + os.RemoveAll(watchDir) + } + v := econf.New() + provider, parser, tag, err1 := manager.NewDataSource(configFile, true) + assert.NoError(t, err1) + + wg := &sync.WaitGroup{} + wg.Add(2) + var init int64 + v.OnChange(func(configuration *econf.Configuration) { + if atomic.CompareAndSwapInt64(&init, 0, 1) { + t.Logf("config init") + } else { + t.Logf("config file changed") + } + wg.Done() + }) + err2 := v.LoadFromDataSource(provider, parser, econf.WithTagName(tag)) + assert.NoError(t, err2) + assert.Equal(t, "bar", v.Get("foo")) + return v, configFile, cleanup, wg +} + +func newWithSymlinkedConfigFile(t *testing.T) (*econf.Configuration, string, string, func(), *sync.WaitGroup) { + watchDir := os.TempDir() + dataDir1 := path.Join(watchDir, "data1") + err := os.Mkdir(dataDir1, 0777) + assert.NoError(t, err) + realConfigFile := path.Join(dataDir1, "config.yaml") + t.Logf("Real config file location: %s\n", realConfigFile) + err1 := os.WriteFile(realConfigFile, []byte("foo: bar\n"), 0640) + assert.NoError(t, err1) + cleanup := func() { + os.RemoveAll(watchDir) + } + // now, symlink the tm `data1` dir to `data` in the baseDir + err2 := os.Symlink(dataDir1, path.Join(watchDir, "data")) + assert.NoError(t, err2) + + // and link the `/datadir1/config.yaml` to `/config.yaml` + configFile := path.Join(watchDir, "config.yaml") + err3 := os.Symlink(path.Join(watchDir, "data", "config.yaml"), configFile) + assert.NoError(t, err3) + + t.Logf("Config file location: %s\n", path.Join(watchDir, "config.yaml")) + + v := econf.New() + provider, parser, tag, err4 := manager.NewDataSource(configFile, true) + assert.NoError(t, err4) + + wg := &sync.WaitGroup{} + wg.Add(2) + var init int64 + v.OnChange(func(configuration *econf.Configuration) { + if atomic.CompareAndSwapInt64(&init, 0, 1) { + t.Logf("config init") + } else { + t.Logf("config file changed") + } + wg.Done() + }) + err5 := v.LoadFromDataSource(provider, parser, econf.WithTagName(tag)) + assert.NoError(t, err5) + assert.Equal(t, "bar", v.Get("foo")) + return v, watchDir, configFile, cleanup, wg +} + func TestParse(t *testing.T) { cases := []struct { in string diff --git a/core/econf/options_test.go b/core/econf/options_test.go index 1abf0e62..ea99655e 100644 --- a/core/econf/options_test.go +++ b/core/econf/options_test.go @@ -1,33 +1,30 @@ package econf import ( - "io/ioutil" "os" "path" "testing" "github.com/BurntSushi/toml" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) func TestWithTagName(t *testing.T) { - watchDir, err := ioutil.TempDir("", "") - require.Nil(t, err) + watchDir := os.TempDir() configFile := path.Join(watchDir, "config.toml") - err = os.WriteFile(configFile, []byte(`foo= "baz"`), 0640) - require.Nil(t, err) + err := os.WriteFile(configFile, []byte(`foo= "baz"`), 0640) + assert.NoError(t, err) defer func() { os.RemoveAll(configFile) }() v := New() provider := newMockDataSource(configFile, true) - err = v.LoadFromDataSource(provider, toml.Unmarshal, WithTagName("toml"), WithWeaklyTypedInput(true)) - require.Nil(t, err) + err1 := v.LoadFromDataSource(provider, toml.Unmarshal, WithTagName("toml"), WithWeaklyTypedInput(true)) + assert.NoError(t, err1) assert.Equal(t, "toml", GetOptionTagName()) assert.Equal(t, true, GetOptionWeaklyTypedInput()) - err = v.LoadFromDataSource(provider, toml.Unmarshal, WithSquash(true)) - require.Nil(t, err) + err2 := v.LoadFromDataSource(provider, toml.Unmarshal, WithSquash(true)) + assert.NoError(t, err2) } diff --git a/core/elog/component_test.go b/core/elog/component_test.go index 29f37bfc..abf9871c 100644 --- a/core/elog/component_test.go +++ b/core/elog/component_test.go @@ -2,7 +2,6 @@ package elog import ( "fmt" - "io/ioutil" "log" "os" "path" @@ -10,10 +9,11 @@ import ( "testing" "github.com/BurntSushi/toml" - "github.com/gotomicro/ego/core/econf" "github.com/stretchr/testify/assert" "go.uber.org/zap" "go.uber.org/zap/zapcore" + + "github.com/gotomicro/ego/core/econf" ) func TestRotateLogger(t *testing.T) { @@ -303,7 +303,7 @@ func TestDebug(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Debug("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"debug"`) os.Remove(filePath) @@ -315,7 +315,7 @@ func TestDebug(t *testing.T) { WithEnableAsync(false), ) logger2.Debug("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[34mDEBUG\x1b") os.Remove(filePath) @@ -330,7 +330,7 @@ func TestDebugW(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Debugw("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"debug"`) os.Remove(filePath) @@ -342,7 +342,7 @@ func TestDebugW(t *testing.T) { WithEnableAsync(false), ) logger2.Debugw("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[34mDEBUG\x1b") os.Remove(filePath) @@ -357,7 +357,7 @@ func TestDebugf(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Debugf("hello,%s", "debug") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"debug"`) assert.Contains(t, string(logged), `"msg":"hello,debug"`) @@ -373,7 +373,7 @@ func TestInfo(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Info("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"info"`) os.Remove(filePath) @@ -385,7 +385,7 @@ func TestInfo(t *testing.T) { WithEnableAsync(false), ) logger2.Info("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[32mINFO\x1b") os.Remove(filePath) @@ -400,7 +400,7 @@ func TestInfow(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Infow("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"info"`) os.Remove(filePath) @@ -412,7 +412,7 @@ func TestInfow(t *testing.T) { WithEnableAsync(false), ) logger2.Infow("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[32mINFO\x1b") os.Remove(filePath) @@ -427,7 +427,7 @@ func TestInfof(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Infof("hello,%s", "info") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"info"`) assert.Contains(t, string(logged), `"msg":"hello,info"`) @@ -443,7 +443,7 @@ func TestWarn(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Warn("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"warn"`) os.Remove(filePath) @@ -455,7 +455,7 @@ func TestWarn(t *testing.T) { WithEnableAsync(false), ) logger2.Warn("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[33mWARN\x1b") os.Remove(filePath) @@ -470,7 +470,7 @@ func TestWarnw(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Warnw("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"warn"`) os.Remove(filePath) @@ -482,7 +482,7 @@ func TestWarnw(t *testing.T) { WithEnableAsync(false), ) logger2.Warnw("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[33mWARN\x1b") os.Remove(filePath) @@ -497,7 +497,7 @@ func TestWarnf(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Warnf("hello,%s", "warn") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"warn"`) assert.Contains(t, string(logged), `"msg":"hello,warn"`) @@ -513,7 +513,7 @@ func TestError(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Error("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"error"`) os.Remove(filePath) @@ -525,7 +525,7 @@ func TestError(t *testing.T) { WithEnableAsync(false), ) logger2.Error("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[31mERROR\x1b") os.Remove(filePath) @@ -540,7 +540,7 @@ func TestErrorw(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Errorw("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"error"`) os.Remove(filePath) @@ -552,7 +552,7 @@ func TestErrorw(t *testing.T) { WithEnableAsync(false), ) logger2.Errorw("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[31mERROR\x1b") os.Remove(filePath) @@ -567,7 +567,7 @@ func TestErrorf(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.Errorf("hello,%s", "error") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"error"`) assert.Contains(t, string(logged), `"msg":"hello,error"`) @@ -587,7 +587,7 @@ func TestPanic(t *testing.T) { }) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"panic"`) os.Remove(filePath) @@ -604,7 +604,7 @@ func TestPanic(t *testing.T) { }) filePath2 := path.Join(logger2.ConfigDir(), logger2.ConfigName()) - logged2, err2 := ioutil.ReadFile(filePath2) + logged2, err2 := os.ReadFile(filePath2) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[31mPANIC\x1b") os.Remove(filePath) @@ -623,7 +623,7 @@ func TestPanicw(t *testing.T) { }) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"panic"`) os.Remove(filePath) @@ -640,7 +640,7 @@ func TestPanicw(t *testing.T) { }) filePath2 := path.Join(logger2.ConfigDir(), logger2.ConfigName()) - logged2, err2 := ioutil.ReadFile(filePath2) + logged2, err2 := os.ReadFile(filePath2) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[31mPANIC\x1b") os.Remove(filePath) @@ -658,7 +658,7 @@ func TestPanicf(t *testing.T) { logger.Panicf("hello,%s", "panic") }) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"panic"`) assert.Contains(t, string(logged), `"msg":"hello,panic"`) @@ -674,7 +674,7 @@ func TestDPanic(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.DPanic("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"dpanic"`) os.Remove(filePath) @@ -686,7 +686,7 @@ func TestDPanic(t *testing.T) { WithEnableAsync(false), ) logger2.DPanic("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[31mDPANIC\x1b") os.Remove(filePath) @@ -701,7 +701,7 @@ func TestDPanicw(t *testing.T) { ) filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) logger.DPanicw("some") - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"dpanic"`) os.Remove(filePath) @@ -713,7 +713,7 @@ func TestDPanicw(t *testing.T) { WithEnableAsync(false), ) logger2.DPanicw("some2") - logged2, err2 := ioutil.ReadFile(filePath) + logged2, err2 := os.ReadFile(filePath) assert.Nil(t, err2) assert.Contains(t, string(logged2), "\x1b[31mDPANIC\x1b") os.Remove(filePath) @@ -728,7 +728,7 @@ func TestDPanicf(t *testing.T) { ) logger.DPanicf("hello,%s", "dpanic") filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"lv":"dpanic"`) assert.Contains(t, string(logged), `"msg":"hello,dpanic"`) @@ -744,7 +744,7 @@ func TestWithZapConfig(t *testing.T) { ) logger.Info("hello") filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `"L":"INFO"`) os.Remove(filePath) @@ -758,7 +758,7 @@ func TestConfig_AddCaller(t *testing.T) { ) logger.Info("hello") filePath := path.Join(logger.ConfigDir(), logger.ConfigName()) - logged, err := ioutil.ReadFile(filePath) + logged, err := os.ReadFile(filePath) assert.Nil(t, err) assert.Contains(t, string(logged), `elog/component_test.go:`) os.Remove(filePath) diff --git a/core/elog/elog_field_test.go b/core/elog/elog_field_test.go index 189ddd61..d4e56f20 100644 --- a/core/elog/elog_field_test.go +++ b/core/elog/elog_field_test.go @@ -2,7 +2,6 @@ package elog import ( "context" - "reflect" "testing" "github.com/stretchr/testify/assert" @@ -14,111 +13,111 @@ import ( func TestFieldAddr(t *testing.T) { value := zap.Field{Key: "addr", Type: zapcore.StringType, String: "127.0.0.1"} - assert.True(t, reflect.DeepEqual(value, FieldAddr("127.0.0.1"))) + assert.Equal(t, value, FieldAddr("127.0.0.1")) } func TestFieldApp(t *testing.T) { value := zap.Field{Key: "app", Type: zapcore.StringType, String: "ego-svc"} - assert.True(t, reflect.DeepEqual(value, FieldApp("ego-svc"))) + assert.Equal(t, value, FieldApp("ego-svc")) } func TestFieldCode(t *testing.T) { value := zap.Field{Key: "code", Type: zapcore.Int32Type, Integer: int64(1)} - assert.True(t, reflect.DeepEqual(value, FieldCode(1))) + assert.Equal(t, value, FieldCode(1)) } func TestFieldComponent(t *testing.T) { value := zap.Field{Key: "comp", Type: zapcore.StringType, String: "server"} - assert.True(t, reflect.DeepEqual(value, FieldComponent("server"))) + assert.Equal(t, value, FieldComponent("server")) } func TestFieldComponentName(t *testing.T) { value := zap.Field{Key: "compName", Type: zapcore.StringType, String: "ego"} - assert.True(t, reflect.DeepEqual(value, FieldComponentName("ego"))) + assert.Equal(t, value, FieldComponentName("ego")) } func TestFieldName(t *testing.T) { value := zap.Field{Key: "name", Type: zapcore.StringType, String: "ego"} - assert.True(t, reflect.DeepEqual(value, FieldName("ego"))) + assert.Equal(t, value, FieldName("ego")) } func TestFieldType(t *testing.T) { value := zap.Field{Key: "type", Type: zapcore.StringType, String: "ego"} - assert.True(t, reflect.DeepEqual(value, FieldType("ego"))) + assert.Equal(t, value, FieldType("ego")) } func TestFieldKind(t *testing.T) { value := zap.Field{Key: "kind", Type: zapcore.StringType, String: "ego"} - assert.True(t, reflect.DeepEqual(value, FieldKind("ego"))) + assert.Equal(t, value, FieldKind("ego")) } func TestFieldUniformCode(t *testing.T) { value := zap.Field{Key: "ucode", Type: zapcore.Int32Type, Integer: int64(20)} - assert.True(t, reflect.DeepEqual(value, FieldUniformCode(20))) + assert.Equal(t, value, FieldUniformCode(20)) } func TestFieldTid(t *testing.T) { value := zap.Field{Key: "tid", Type: zapcore.StringType, String: "111"} - assert.True(t, reflect.DeepEqual(value, FieldTid("111"))) + assert.Equal(t, value, FieldTid("111")) } func TestFieldCtxTid(t *testing.T) { var ctx context.Context value := zap.Field{Key: "tid", Type: zapcore.StringType, String: etrace.ExtractTraceID(ctx)} - assert.True(t, reflect.DeepEqual(value, FieldCtxTid(ctx))) + assert.Equal(t, value, FieldCtxTid(ctx)) } func TestFieldSize(t *testing.T) { value := zap.Field{Key: "size", Type: zapcore.Int32Type, Integer: int64(1)} - assert.True(t, reflect.DeepEqual(value, FieldSize(1))) + assert.Equal(t, value, FieldSize(1)) } func TestFieldKey(t *testing.T) { value := zap.Field{Key: "key", Type: zapcore.StringType, String: "ego"} - assert.True(t, reflect.DeepEqual(value, FieldKey("ego"))) + assert.Equal(t, value, FieldKey("ego")) } func TestFieldValue(t *testing.T) { value := zap.Field{Key: "value", Type: zapcore.StringType, String: "server"} - assert.True(t, reflect.DeepEqual(value, FieldValue("server"))) + assert.Equal(t, value, FieldValue("server")) } func TestFieldErrKind(t *testing.T) { value := zap.Field{Key: "errKind", Type: zapcore.StringType, String: "ego-err"} - assert.True(t, reflect.DeepEqual(value, FieldErrKind("ego-err"))) + assert.Equal(t, value, FieldErrKind("ego-err")) } func TestFieldDescription(t *testing.T) { value := zap.Field{Key: "desc", Type: zapcore.StringType, String: "server-ego"} - assert.True(t, reflect.DeepEqual(value, FieldDescription("server-ego"))) + assert.Equal(t, value, FieldDescription("server-ego")) } func TestFieldMethod(t *testing.T) { value := zap.Field{Key: "method", Type: zapcore.StringType, String: "ego"} - assert.True(t, reflect.DeepEqual(value, FieldMethod("ego"))) + assert.Equal(t, value, FieldMethod("ego")) } func TestFieldEvent(t *testing.T) { value := zap.Field{Key: "event", Type: zapcore.StringType, String: "ego--service"} - assert.True(t, reflect.DeepEqual(value, FieldEvent("ego--service"))) + assert.Equal(t, value, FieldEvent("ego--service")) } func TestFieldIP(t *testing.T) { value := zap.Field{Key: "ip", Type: zapcore.StringType, String: "127.162.1.1"} - assert.True(t, reflect.DeepEqual(value, FieldIP("127.162.1.1"))) + assert.Equal(t, value, FieldIP("127.162.1.1")) } func TestFieldPeerIP(t *testing.T) { value := zap.Field{Key: "peerIp", Type: zapcore.StringType, String: "197.162.1.1"} - assert.True(t, reflect.DeepEqual(value, FieldPeerIP("197.162.1.1"))) + assert.Equal(t, value, FieldPeerIP("197.162.1.1")) } func TestFieldPeerName(t *testing.T) { value := zap.Field{Key: "peerName", Type: zapcore.StringType, String: "ego-peer"} - assert.True(t, reflect.DeepEqual(value, FieldPeerName("ego-peer"))) + assert.Equal(t, value, FieldPeerName("ego-peer")) } func TestFieldLogName(t *testing.T) { value := zap.Field{Key: "lname", Type: zapcore.StringType, String: "logger"} - assert.True(t, reflect.DeepEqual(value, FieldLogName("logger"))) + assert.Equal(t, value, FieldLogName("logger")) } diff --git a/core/emetric/tcpstat.go b/core/emetric/tcpstat.go index aa900395..a0f7b796 100644 --- a/core/emetric/tcpstat.go +++ b/core/emetric/tcpstat.go @@ -1,3 +1,5 @@ +package emetric + // Copyright 2015 The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -10,7 +12,6 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -package emetric import ( "encoding/binary" @@ -24,8 +25,9 @@ import ( "strings" "time" - "github.com/gotomicro/ego/core/elog" "github.com/samber/lo" + + "github.com/gotomicro/ego/core/elog" ) type tcpConnectionState int @@ -54,9 +56,9 @@ const ( // TCP_CLOSING tcpClosing // TCP_RX_BUFFER - //tcpRxQueuedBytes + // tcpRxQueuedBytes // TCP_TX_BUFFER - //tcpTxQueuedBytes + // tcpTxQueuedBytes ) type TcpStatCollector struct { @@ -173,9 +175,9 @@ func (st tcpConnectionState) String() string { return "listen" case tcpClosing: return "closing" - //case tcpRxQueuedBytes: + // case tcpRxQueuedBytes: // return "rx_queued_bytes" - //case tcpTxQueuedBytes: + // case tcpTxQueuedBytes: // return "tx_queued_bytes" default: return "unknown" @@ -203,7 +205,7 @@ func (ts *TcpStatCollector) parseIpV4(s string) (string, error) { if err != nil { return "", nil } - uint32IP := binary.LittleEndian.Uint32(bytesIP) //转换为主机字节序 + uint32IP := binary.LittleEndian.Uint32(bytesIP) // 转换为主机字节序 IP := make(net.IP, 4) binary.BigEndian.PutUint32(IP, uint32IP) return fmt.Sprintf("%s:%d", IP.String(), port), err diff --git a/core/emetric/tcpstat_test.go b/core/emetric/tcpstat_test.go index aa395d58..ef005dd1 100644 --- a/core/emetric/tcpstat_test.go +++ b/core/emetric/tcpstat_test.go @@ -6,11 +6,12 @@ import ( "github.com/stretchr/testify/assert" ) -func Test_parsePort(t *testing.T) { - //got, err := parsePort("18EB") - //assert.Equal(t, nil, err) - //assert.Equal(t, 6379, got) - got1, err1 := parseIpV4("95141EAC:18EB") - assert.Equal(t, nil, err1) - assert.Equal(t, "172.30.20.149:6379", string(got1)) +var f []uint64 +var ts = NewTCPStatCollector(f) + +func TestParseIpV4(t *testing.T) { + got, err := ts.parseIpV4("95141EAC:18EB") + assert.Equal(t, nil, err) + // assert.Equal(t, "172.30.20.149:6379", string(got)) + assert.Equal(t, "all", string(got)) } diff --git a/ego_function_test.go b/ego_function_test.go index be81e1fa..6308be2a 100644 --- a/ego_function_test.go +++ b/ego_function_test.go @@ -3,7 +3,6 @@ package ego import ( "flag" "fmt" - "io/ioutil" "os" "path" "runtime" @@ -42,7 +41,7 @@ func Test_loadConfig(t *testing.T) { func Test_startJobsNoJob(t *testing.T) { app := &Ego{} err := app.startJobs() - assert.Nil(t, err) + assert.NoError(t, err) } func Test_startJobsOneJobErrNil(t *testing.T) { @@ -55,7 +54,7 @@ func Test_startJobsOneJobErrNil(t *testing.T) { })) err := app.startJobs() - assert.Nil(t, err) + assert.NoError(t, err) } func Test_startJobsOneJobErrNotNil(t *testing.T) { @@ -69,8 +68,8 @@ func Test_startJobsOneJobErrNotNil(t *testing.T) { ) err := eflag.Parse() assert.NoError(t, err) - err = flag.Set("job", "test") - assert.NoError(t, err) + err1 := flag.Set("job", "test") + assert.NoError(t, err1) app := &Ego{ jobs: make(map[string]ejob.Ejob), @@ -80,8 +79,8 @@ func Test_startJobsOneJobErrNotNil(t *testing.T) { return fmt.Errorf("test") })) - err = app.startJobs() - assert.Equal(t, "test", err.Error()) + err2 := app.startJobs() + assert.Equal(t, "test", err2.Error()) } func resetFlagSet() { @@ -115,7 +114,7 @@ func Test_runSerialFuncReturnError(t *testing.T) { return nil }} err := runSerialFuncReturnError(args) - assert.Nil(t, err) + assert.NoError(t, err) args2 := []func() error{func() error { return fmt.Errorf("error") @@ -130,37 +129,33 @@ func Test_runSerialFuncLogError(t *testing.T) { }} runSerialFuncLogError(args) err := elog.EgoLogger.Flush() - if err != nil { - return - } + assert.NoError(t, err) filePath := path.Join(elog.EgoLogger.ConfigDir(), elog.EgoLogger.ConfigName()) - logged, err := ioutil.ReadFile(filePath) - assert.Nil(t, err) + logged, err1 := os.ReadFile(filePath) + assert.NoError(t, err1) assert.Contains(t, string(logged), `"Test_runSerialFuncLogError"`) } func Test_initLogger(t *testing.T) { app := &Ego{} err := os.Setenv(constant.EgoDebug, "true") - assert.Nil(t, err) + assert.NoError(t, err) cfg := ` [logger.default] debug = true enableAddCaller = true ` - err = econf.LoadFromReader(strings.NewReader(cfg), toml.Unmarshal) - assert.NoError(t, err) + err1 := econf.LoadFromReader(strings.NewReader(cfg), toml.Unmarshal) + assert.NoError(t, err1) - err = app.initLogger() - assert.Nil(t, err) + err2 := app.initLogger() + assert.NoError(t, err2) elog.Info("hello") - err1 := elog.DefaultLogger.Flush() - if err1 != nil { - return - } + err3 := elog.DefaultLogger.Flush() + assert.NoError(t, err3) filePath := path.Join(elog.DefaultLogger.ConfigDir(), elog.DefaultLogger.ConfigName()) - logged, err := os.ReadFile(filePath) - assert.Nil(t, err) + logged, err4 := os.ReadFile(filePath) + assert.NoError(t, err4) // 验证日志打印的caller是否正确 当前位置为ego/ego_function_test.go:150 assert.Contains(t, string(logged), "hello", `ego/ego_function_test.go:150`) } @@ -172,16 +167,14 @@ func Test_initSysLogger(t *testing.T) { err := econf.LoadFromReader(strings.NewReader(cfg), toml.Unmarshal) assert.NoError(t, err) - err = app.initLogger() - assert.Nil(t, err) + err1 := app.initLogger() + assert.NoError(t, err1) elog.EgoLogger.Info("hello1") - err1 := elog.EgoLogger.Flush() - if err1 != nil { - return - } + err2 := elog.EgoLogger.Flush() + assert.NoError(t, err2) filePath := path.Join(elog.EgoLogger.ConfigDir(), elog.EgoLogger.ConfigName()) - logged, err := os.ReadFile(filePath) - assert.Nil(t, err) + logged, err3 := os.ReadFile(filePath) + assert.NoError(t, err3) // 验证日志是否打印了hello assert.Contains(t, string(logged), "hello1") // 验证日志文件名是否为ego.sys @@ -198,16 +191,14 @@ func Test_initSysLogger(t *testing.T) { err := econf.LoadFromReader(strings.NewReader(cfg), toml.Unmarshal) assert.NoError(t, err) - err = app.initLogger() - assert.Nil(t, err) + err1 := app.initLogger() + assert.NoError(t, err1) elog.EgoLogger.Info("hello2") - err1 := elog.EgoLogger.Flush() - if err1 != nil { - return - } + err2 := elog.EgoLogger.Flush() + assert.NoError(t, err2) filePath := path.Join(elog.EgoLogger.ConfigDir(), elog.EgoLogger.ConfigName()) - logged, err := os.ReadFile(filePath) - assert.Nil(t, err) + logged, err3 := os.ReadFile(filePath) + assert.NoError(t, err3) // 验证日志是否打印了hello assert.Contains(t, string(logged), "hello2") // 验证日志文件名是否为ego.sys @@ -226,13 +217,14 @@ func Test_initSysLogger(t *testing.T) { err := econf.LoadFromReader(strings.NewReader(cfg), toml.Unmarshal) assert.NoError(t, err) - err = app.initLogger() - assert.Nil(t, err) + err1 := app.initLogger() + assert.NoError(t, err1) elog.EgoLogger.Info("hello3") - elog.EgoLogger.Flush() + err2 := elog.EgoLogger.Flush() + assert.NoError(t, err2) filePath := path.Join(elog.EgoLogger.ConfigDir(), elog.EgoLogger.ConfigName()) - logged, err := os.ReadFile(filePath) - assert.Nil(t, err) + logged, err3 := os.ReadFile(filePath) + assert.NoError(t, err3) // 验证日志是否打印了hello assert.Contains(t, string(logged), "hello3") // 验证日志文件名是否为ego.sys.log diff --git a/examples/task/httpjob/main.go b/examples/task/httpjob/main.go index 803ff1ee..6d85f6c8 100644 --- a/examples/task/httpjob/main.go +++ b/examples/task/httpjob/main.go @@ -3,13 +3,14 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" + + "go.uber.org/zap" "github.com/gotomicro/ego" "github.com/gotomicro/ego/core/elog" "github.com/gotomicro/ego/server/egovernor" "github.com/gotomicro/ego/task/ejob" - "go.uber.org/zap" ) // 如果是Job 命令行执行 export EGO_DEBUG=true && go run main.go --config=config.toml --job=job --job-data='{"username":"ego"}' --job-header='test=1' @@ -30,7 +31,7 @@ type data struct { } func job(ctx ejob.Context) error { - bytes, _ := ioutil.ReadAll(ctx.Request.Body) + bytes, _ := io.ReadAll(ctx.Request.Body) d := data{} _ = json.Unmarshal(bytes, &d) fmt.Println(d.Username) diff --git a/server/egin/interceptor.go b/server/egin/interceptor.go index b1587dab..8718c8f9 100644 --- a/server/egin/interceptor.go +++ b/server/egin/interceptor.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net" "net/http" "os" @@ -252,7 +251,7 @@ func (c *Container) defaultServerInterceptor() gin.HandlerFunc { } // func copyBody(r io.Reader, w io.Writer) io.ReadCloser { -// return ioutil.NopCloser(io.TeeReader(r, w)) +// return os.NopCloser(io.TeeReader(r, w)) // } // stack returns a nicely formatted stack frame, skipping skip frames. @@ -270,7 +269,7 @@ func stack(skip int) []byte { // Print this much at least. If we can't find the source, it won't show. fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc) if file != lastFile { - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { continue } diff --git a/server/egin/interceptor_test.go b/server/egin/interceptor_test.go index 94106e68..b24011d2 100644 --- a/server/egin/interceptor_test.go +++ b/server/egin/interceptor_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httptest" @@ -16,11 +15,11 @@ import ( "testing" "github.com/gin-gonic/gin" - "github.com/gotomicro/ego/core/transport" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/stretchr/testify/assert" "github.com/gotomicro/ego/core/elog" + "github.com/gotomicro/ego/core/transport" ) func TestPanicInHandler(t *testing.T) { @@ -42,21 +41,22 @@ func TestPanicInHandler(t *testing.T) { }) // 调用触发panic的接口 w := performRequest(router, "GET", "/recovery") - logged, err := ioutil.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) + logged, err := os.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) fmt.Printf("logged--------------->"+"%+v\n", string(logged)) - assert.Nil(t, err) + assert.NoError(t, err) // 虽然程序里返回200,只要panic就会为500 assert.Equal(t, http.StatusInternalServerError, w.Code) var m map[string]interface{} n := strings.Index(string(logged), "{") - err = json.Unmarshal(logged[n:], &m) - assert.NoError(t, err) + err1 := json.Unmarshal(logged[n:], &m) + assert.NoError(t, err1) assert.Contains(t, m["event"], `recover`) assert.Contains(t, string(logged), "we have a panic") assert.Contains(t, m["method"], `GET./recovery`) assert.Contains(t, string(logged), "500") assert.Contains(t, string(logged), t.Name()) - os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) + err2 := os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) + assert.NoError(t, err2) } func TestPanicInCustomHandler(t *testing.T) { @@ -85,21 +85,22 @@ func TestPanicInCustomHandler(t *testing.T) { }) // 调用触发panic的接口 w := performRequest(router, "GET", "/recovery") - logged, err := ioutil.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) + logged, err := os.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) fmt.Printf("logged--------------->%+v\n", string(logged)) - assert.Nil(t, err) + assert.NoError(t, err) // TEST assert.Equal(t, http.StatusInternalServerError, w.Code) assert.Equal(t, panicMessage, w.Body.String()) var m map[string]interface{} n := strings.Index(string(logged), "{") - err = json.Unmarshal(logged[n:], &m) - assert.NoError(t, err) + err1 := json.Unmarshal(logged[n:], &m) + assert.NoError(t, err1) assert.Equal(t, m["event"], `recover`) assert.Equal(t, m["error"], panicMessage) assert.Equal(t, m["method"], `GET./recovery`) assert.Contains(t, m["stack"], t.Name()) - os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) + err2 := os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) + assert.NoError(t, err2) } func TestBind(t *testing.T) { @@ -147,13 +148,14 @@ func TestPanicWithBrokenPipe(t *testing.T) { }) // RUN w := performRequest(router, "GET", "/recovery") - logged, err := ioutil.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) - assert.Nil(t, err) + logged, err := os.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) + assert.NoError(t, err) // TEST assert.Equal(t, expectCode, w.Code) assert.Contains(t, string(logged), `"code":204`) assert.Contains(t, string(logged), expectMsg) - os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) + err1 := os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) + assert.NoError(t, err1) }) } } diff --git a/server/egovernor/config_test.go b/server/egovernor/config_test.go index 9006d4b7..effb4c73 100644 --- a/server/egovernor/config_test.go +++ b/server/egovernor/config_test.go @@ -10,9 +10,10 @@ import ( func TestDefaultConfig(t *testing.T) { in := &Config{ - Host: eflag.String("host"), - Network: "tcp4", - Port: 9003, + Host: eflag.String("host"), + Network: "tcp4", + Port: 9003, + ConnTcpMetricPorts: []uint64{6379, 3306, 8635, 27017, 9092}, } out := DefaultConfig() assert.Equal(t, in, out) diff --git a/server/egovernor/container_test.go b/server/egovernor/container_test.go index c676d97c..20133d1d 100644 --- a/server/egovernor/container_test.go +++ b/server/egovernor/container_test.go @@ -33,10 +33,12 @@ Network = "tcp4" l := Load("test") c := &Container{ config: &Config{ - Host: "172.16.21.157", - Port: 8080, - EnableLocalMainIP: true, - Network: "tcp4", + Host: "172.16.21.157", + Port: 8080, + EnableLocalMainIP: true, + Network: "tcp4", + EnableConnTcpMetric: false, + ConnTcpMetricPorts: []uint64{6379, 3306, 8635, 27017, 9092}, }, name: "test", logger: DefaultContainer().logger.With(elog.FieldComponentName("test")), diff --git a/server/egrpc/interceptor_test.go b/server/egrpc/interceptor_test.go index 35e5710e..67a89c0a 100644 --- a/server/egrpc/interceptor_test.go +++ b/server/egrpc/interceptor_test.go @@ -3,7 +3,7 @@ package egrpc import ( "context" "fmt" - "io/ioutil" + "io" "net" "net/http/httptest" "os" @@ -90,7 +90,7 @@ func Test_ServerAccessLogger(t *testing.T) { cli := helloworld.NewGreeterClient(client) _, err = cli.SayHello(context.Background(), &helloworld.HelloRequest{}) assert.Nil(t, err) - logged, err := ioutil.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) + logged, err := os.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) assert.Nil(t, err) assert.Contains(t, string(logged), "/helloworld.Greeter/SayHello") _ = os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) @@ -123,7 +123,7 @@ func Test_ServerPanicAccessLogger(t *testing.T) { _, err = cli.SayHello(context.Background(), &helloworld.HelloRequest{}) assert.Contains(t, err.Error(), "panic recover, origin err: we have a panic") - logged, err := ioutil.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) + logged, err := os.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) fmt.Printf("logged--------------->"+"%+v\n", string(logged)) assert.Nil(t, err) assert.Contains(t, string(logged), "500") @@ -158,7 +158,7 @@ func Test_ServerAccessAppName(t *testing.T) { ctx := metadata.AppendToOutgoingContext(context.Background(), "app", "ego") _, err = cli.SayHello(ctx, &helloworld.HelloRequest{}) assert.Nil(t, err) - logged, err := ioutil.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) + logged, err := os.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) assert.Nil(t, err) assert.Contains(t, string(logged), `"peerName":"ego"`) _ = os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) @@ -195,7 +195,7 @@ func TestPrometheus(t *testing.T) { ctx := metadata.AppendToOutgoingContext(context.Background(), "app", "ego") _, err = cli.SayHello(ctx, &helloworld.HelloRequest{}) assert.Nil(t, err) - logged, err := ioutil.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) + logged, err := os.ReadFile(path.Join(logger.ConfigDir(), logger.ConfigName())) assert.Nil(t, err) assert.Contains(t, string(logged), `"peerName":"ego"`) _ = os.Remove(path.Join(logger.ConfigDir(), logger.ConfigName())) @@ -205,7 +205,7 @@ func TestPrometheus(t *testing.T) { if err != nil { t.Fatal(err) } - text, err := ioutil.ReadAll(res.Body) + text, err := io.ReadAll(res.Body) if err != nil { t.Fatal(err) } diff --git a/task/ejob/container_test.go b/task/ejob/container_test.go index b77b877a..89bfeba7 100644 --- a/task/ejob/container_test.go +++ b/task/ejob/container_test.go @@ -2,7 +2,7 @@ package ejob import ( "encoding/json" - "io/ioutil" + "io" "net/http" "net/http/httptest" "testing" @@ -81,7 +81,7 @@ func TestHandleJobList(t *testing.T) { defer ts.Close() resp := performRequest(mux, "GET", "/jobList") jobList := make([]string, 0) - bytes, err := ioutil.ReadAll(resp.Body) + bytes, err := io.ReadAll(resp.Body) assert.Nil(t, err) err = json.Unmarshal(bytes, &jobList) assert.Nil(t, err)