diff options
author | blanet <bupt_xingxin@163.com> | 2021-04-23 12:02:08 +0300 |
---|---|---|
committer | blanet <bupt_xingxin@163.com> | 2021-04-23 18:06:37 +0300 |
commit | 9e1f75c6a4c8765e6a63bb078fd513a1afa3070a (patch) | |
tree | 50afb2d579610106795cf8e0872ee5cdcddac1f8 | |
parent | ac6e16eb6ce8c99e66cf06daa85010cd0001e7dc (diff) |
command: Add test to detect goroutine leaks
Add TestNewNonExistent to help detecting goroutine leaks when
spawning a non-existent command with nil stderr.
When we `New` a command with nil stderr, a default stderr would
be created to help logging possible cmd errors, and in the
meanwhile we use io.Pipe to implement log size and line size
limiting. However, if we encounter an error from the latter
`cmd.Start()`, the PipeWriter will be left unclosed and related
goroutine leaks.
Signed-off-by: blanet <bupt_xingxin@163.com>
-rw-r--r-- | NOTICE | 24 | ||||
-rw-r--r-- | internal/command/command_test.go | 15 | ||||
-rw-r--r-- | internal/testhelper/testhelper.go | 5 |
3 files changed, 43 insertions, 1 deletions
@@ -12226,6 +12226,30 @@ LICENSE - go.opencensus.io See the License for the specific language governing permissions and limitations under the License. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +LICENSE - go.uber.org/goleak +The MIT License (MIT) + +Copyright (c) 2018 Uber Technologies, Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ LICENSE - golang.org/x/crypto Copyright (c) 2009 The Go Authors. All rights reserved. diff --git a/internal/command/command_test.go b/internal/command/command_test.go index 74c250a6b..0916ea56d 100644 --- a/internal/command/command_test.go +++ b/internal/command/command_test.go @@ -17,8 +17,13 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.uber.org/goleak" ) +func TestMain(m *testing.M) { + goleak.VerifyTestMain(m) +} + func TestNewCommandTZEnv(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -220,12 +225,20 @@ func TestNewCommandNullInArg(t *testing.T) { require.EqualError(t, err, `detected null byte in command argument "hello\x00world"`) } +func TestNewNonExistent(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + cmd, err := New(ctx, exec.Command("command-non-existent"), nil, nil, nil) + require.Nil(t, cmd) + require.Error(t, err) +} + func TestCommandStdErr(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() var stdout bytes.Buffer - expectedMessage := `hello world\\nhello world\\nhello world\\nhello world\\nhello world\\n` r, w := io.Pipe() diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index 342dc9610..57fb4ccf8 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -1,5 +1,6 @@ package testhelper +//nolint: gci import ( "context" "crypto/ecdsa" @@ -33,6 +34,10 @@ import ( "gitlab.com/gitlab-org/gitaly/internal/storage" "gitlab.com/gitlab-org/gitaly/proto/go/gitalypb" "google.golang.org/grpc/metadata" + + // The goleak import only exists such that this test-only dependency is properly being + // attributed in our NOTICE file. + _ "go.uber.org/goleak" ) const ( |