Map io.EOF errors to io.ErrUnexpectedEOF
io.EOF is never expected during valid usage. In addition, database/sql uses io.EOF as a sentinal value that all rows from a query have been received. See https://github.com/jackc/pgx/issues/662.
This commit is contained in:
+24
-2
@@ -1,10 +1,11 @@
|
||||
package pgproto3_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgproto3/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type interruptReader struct {
|
||||
@@ -13,7 +14,7 @@ type interruptReader struct {
|
||||
|
||||
func (ir *interruptReader) Read(p []byte) (n int, err error) {
|
||||
if len(ir.chunks) == 0 {
|
||||
return 0, errors.New("no data")
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
n = copy(p, ir.chunks[0])
|
||||
@@ -56,3 +57,24 @@ func TestFrontendReceiveInterrupted(t *testing.T) {
|
||||
t.Fatalf("unexpected msg: %v", msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrontendReceiveUnexpectedEOF(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
server := &interruptReader{}
|
||||
server.push([]byte{'Z', 0, 0, 0, 5})
|
||||
|
||||
frontend := pgproto3.NewFrontend(pgproto3.NewChunkReader(server), nil)
|
||||
|
||||
msg, err := frontend.Receive()
|
||||
if err == nil {
|
||||
t.Fatal("expected err")
|
||||
}
|
||||
if msg != nil {
|
||||
t.Fatalf("did not expect msg, but %v", msg)
|
||||
}
|
||||
|
||||
msg, err = frontend.Receive()
|
||||
assert.Nil(t, msg)
|
||||
assert.Equal(t, io.ErrUnexpectedEOF, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user