Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use errors.Is() in IsErrEOF() #8698

Merged
merged 5 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/eof.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package lib

import (
"errors"
"fmt"
"io"
"net/rpc"
"strings"

"github.com/hashicorp/yamux"
Expand All @@ -13,7 +16,7 @@ var yamuxSessionShutdown = yamux.ErrSessionShutdown.Error()
// IsErrEOF returns true if we get an EOF error from the socket itself, or
// an EOF equivalent error from yamux.
func IsErrEOF(err error) bool {
if err == io.EOF {
if errors.Is(err, io.EOF) {
return true
}

Expand All @@ -23,5 +26,13 @@ func IsErrEOF(err error) bool {
return true
}

if srvErr, ok := err.(rpc.ServerError); ok {
return strings.HasSuffix(srvErr.Error(), fmt.Sprintf(": %s", io.EOF.Error()))
}

if srvErr, ok := errors.Unwrap(err).(rpc.ServerError); ok {
pierreca marked this conversation as resolved.
Show resolved Hide resolved
return strings.HasSuffix(srvErr.Error(), fmt.Sprintf(": %s", io.EOF.Error()))
}

return false
}
31 changes: 31 additions & 0 deletions lib/eof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package lib

import (
"fmt"
"io"
"net/rpc"
"testing"

"github.com/hashicorp/yamux"
"github.com/stretchr/testify/require"
)

func TestErrIsEOF(t *testing.T) {
var tests = []struct {
name string
err error
}{
{name: "EOF", err: io.EOF},
{name: "Wrapped EOF", err: fmt.Errorf("test: %w", io.EOF)},
{name: "yamuxStreamClosed", err: yamux.ErrStreamClosed},
{name: "yamuxSessionShutdown", err: yamux.ErrSessionShutdown},
{name: "ServerError(___: EOF)", err: rpc.ServerError(fmt.Sprintf("rpc error: %s", io.EOF.Error()))},
{name: "Wrapped ServerError(___: EOF)", err: fmt.Errorf("rpc error: %w", rpc.ServerError(fmt.Sprintf("rpc error: %s", io.EOF.Error())))},
Comment on lines +18 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple tests that are expected to return false might be good as well, to check that errors which are not EOF are not matched.

}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.True(t, IsErrEOF(tt.err))
})
}
}