Skip to content

Commit

Permalink
pkg/signals: Improved test coverage 60% to 100%
Browse files Browse the repository at this point in the history
Expanded tests on signals_test.go to cover more lines of code. 'go test' won't show 100% coverage (only 66.7%), because one test need to spawn a new
process (since it is testing a function that calls os.Exit(1)).

Fixes: #256

Signed-off-by: Eduardo Berrocal <[email protected]>
  • Loading branch information
eberroca committed Apr 25, 2023
1 parent 7fdaab4 commit 1c1ee80
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/runtime/pkg/signals/signals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package signals

import (
"bytes"
"errors"
"os"
"os/exec"
"reflect"
goruntime "runtime"
"sort"
Expand Down Expand Up @@ -135,3 +137,69 @@ func TestSignalBacktrace(t *testing.T) {
assert.True(strings.Contains(b, "contention:"))
assert.True(strings.Contains(b, `level=error`))
}

func TestSignalHandlePanic(t *testing.T) {
assert := assert.New(t)

savedLog := signalLog
defer func() {
signalLog = savedLog
}()

signalLog = logrus.WithFields(logrus.Fields{
"name": "name",
"pid": os.Getpid(),
"source": "throttler",
"test-logger": true})

// Create buffer to save logger output.
buf := &bytes.Buffer{}

savedOut := signalLog.Logger.Out
defer func() {
signalLog.Logger.Out = savedOut
}()

// Capture output to buffer.
signalLog.Logger.Out = buf

HandlePanic(nil)

b := buf.String()
assert.True(len(b) == 0)
}

func TestSignalHandlePanicWithError(t *testing.T) {
assert := assert.New(t)

if os.Getenv("CALL_EXIT") != "1" {
cmd := exec.Command(os.Args[0], "-test.run=TestSignalHandlePanicWithError")
cmd.Env = append(os.Environ(), "CALL_EXIT=1")

err := cmd.Run()
assert.True(err != nil)

exitError, ok := err.(*exec.ExitError)
assert.True(ok)
assert.True(exitError.ExitCode() == 1)

return
}

signalLog = logrus.WithFields(logrus.Fields{
"name": "name",
"pid": os.Getpid(),
"source": "throttler",
"test-logger": true})

// Create buffer to save logger output.
buf := &bytes.Buffer{}

// Capture output to buffer.
signalLog.Logger.Out = buf

dieCallBack := func() {}
defer HandlePanic(dieCallBack)
e := errors.New("test-panic")
panic(e)
}

0 comments on commit 1c1ee80

Please sign in to comment.