Skip to content

Commit

Permalink
Bug fix: Mark connection as not good when error on cancellation confi…
Browse files Browse the repository at this point in the history
…rmation (#68)

* Mark connection as not good when error on cancellation confirmation

(cherry picked from commit c996b77)

* tests added

(cherry picked from commit 2258616)

* query tests added

(cherry picked from commit 2fc4276)

* return ServerError when failed to get cancelation confirmation

* fix ci tests

Co-authored-by: Eygin Semen Leonidovich <[email protected]>
  • Loading branch information
zcolleen and Eygin Semen Leonidovich authored Dec 5, 2022
1 parent e2afc1a commit eaf0b71
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
41 changes: 41 additions & 0 deletions queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -1387,6 +1388,46 @@ func TestProcessQueryErrors(t *testing.T) {
}
}

type mockReadWriteCloser struct {
io.ReadWriteCloser
}

func (*mockReadWriteCloser) Read([]byte) (int, error) { return 0, errors.New("fake err") }

func TestProcessQueryCancelConfirmationError(t *testing.T) {
tl := testLogger{t: t}
defer tl.StopLogging()
conn := internalConnection(t, &tl)
defer conn.Close()

stmt, err := conn.prepareContext(context.Background(), "select 1")
if err != nil {
t.Fatal("prepareContext expected to succeed, but it failed with", err)
}
err = stmt.sendQuery(context.Background(), []namedValue{})
if err != nil {
t.Fatal("sendQuery expected to succeed, but it failed with", err)
}
// mock real connection to imitate situation when you write but dont get response
conn.sess.buf.transport = &mockReadWriteCloser{ReadWriteCloser: conn.sess.buf.transport}
// canceling context to try to send attention request
ctx, cancel := context.WithCancel(context.Background())
cancel()

_, err = stmt.processQueryResponse(ctx)
if err == nil {
t.Error("processQueryResponse expected to fail but it succeeded")
}
// should not fail with ErrBadConn because query was successfully sent to server
if _, ok := err.(ServerError); !ok {
t.Error("processQueryResponse expected to fail with ServerError error but failed with other error: ", err)
}

if conn.connectionGood {
t.Fatal("Connection should be in a bad state")
}
}

func TestProcessQueryNextErrors(t *testing.T) {
tl := testLogger{t: t}
defer tl.StopLogging()
Expand Down
3 changes: 1 addition & 2 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mssql
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -967,7 +966,7 @@ func (t tokenProcessor) nextToken() (tokenStruct, error) {
}
// we did not get cancellation confirmation, something is not
// right, this connection is not usable anymore
return nil, errors.New("did not get cancellation confirmation from the server")
return nil, ServerError{Error{Message: "did not get cancellation confirmation from the server"}}
}
}

Expand Down

0 comments on commit eaf0b71

Please sign in to comment.