Skip to content

Commit

Permalink
query tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Eygin Semen Leonidovich committed Nov 9, 2022
1 parent 2258616 commit 2fc4276
Showing 1 changed file with 41 additions and 0 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 @@ -1370,6 +1371,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 err != ErrorCancelConfirmation {
t.Error("processQueryResponse expected to fail with ErrorCancelConfirmation 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

0 comments on commit 2fc4276

Please sign in to comment.