Skip to content

Commit

Permalink
Merge pull request #8698 from pierreca/fix-iserreof
Browse files Browse the repository at this point in the history
Use errors.Is() in IsErrEOF()
  • Loading branch information
dnephin authored Mar 16, 2021
2 parents a1d5e1f + f85fec6 commit 931023f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
10 changes: 9 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,10 @@ func IsErrEOF(err error) bool {
return true
}

var serverError rpc.ServerError
if errors.As(err, &serverError) {
return strings.HasSuffix(err.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())))},
}

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

0 comments on commit 931023f

Please sign in to comment.