2
0

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:
Jack Christensen
2020-01-17 16:55:05 -06:00
parent eca1e51822
commit 595780be0f
4 changed files with 44 additions and 4 deletions
+24 -2
View File
@@ -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)
}