|
5 | 5 | "context"
|
6 | 6 | "database/sql"
|
7 | 7 | "database/sql/driver"
|
| 8 | + "errors" |
8 | 9 | "fmt"
|
9 | 10 | "io"
|
10 | 11 | "log"
|
@@ -1387,6 +1388,46 @@ func TestProcessQueryErrors(t *testing.T) {
|
1387 | 1388 | }
|
1388 | 1389 | }
|
1389 | 1390 |
|
| 1391 | +type mockReadWriteCloser struct { |
| 1392 | + io.ReadWriteCloser |
| 1393 | +} |
| 1394 | + |
| 1395 | +func (*mockReadWriteCloser) Read([]byte) (int, error) { return 0, errors.New("fake err") } |
| 1396 | + |
| 1397 | +func TestProcessQueryCancelConfirmationError(t *testing.T) { |
| 1398 | + tl := testLogger{t: t} |
| 1399 | + defer tl.StopLogging() |
| 1400 | + conn := internalConnection(t, &tl) |
| 1401 | + defer conn.Close() |
| 1402 | + |
| 1403 | + stmt, err := conn.prepareContext(context.Background(), "select 1") |
| 1404 | + if err != nil { |
| 1405 | + t.Fatal("prepareContext expected to succeed, but it failed with", err) |
| 1406 | + } |
| 1407 | + err = stmt.sendQuery(context.Background(), []namedValue{}) |
| 1408 | + if err != nil { |
| 1409 | + t.Fatal("sendQuery expected to succeed, but it failed with", err) |
| 1410 | + } |
| 1411 | + // mock real connection to imitate situation when you write but dont get response |
| 1412 | + conn.sess.buf.transport = &mockReadWriteCloser{ReadWriteCloser: conn.sess.buf.transport} |
| 1413 | + // canceling context to try to send attention request |
| 1414 | + ctx, cancel := context.WithCancel(context.Background()) |
| 1415 | + cancel() |
| 1416 | + |
| 1417 | + _, err = stmt.processQueryResponse(ctx) |
| 1418 | + if err == nil { |
| 1419 | + t.Error("processQueryResponse expected to fail but it succeeded") |
| 1420 | + } |
| 1421 | + // should not fail with ErrBadConn because query was successfully sent to server |
| 1422 | + if _, ok := err.(ServerError); !ok { |
| 1423 | + t.Error("processQueryResponse expected to fail with ServerError error but failed with other error: ", err) |
| 1424 | + } |
| 1425 | + |
| 1426 | + if conn.connectionGood { |
| 1427 | + t.Fatal("Connection should be in a bad state") |
| 1428 | + } |
| 1429 | +} |
| 1430 | + |
1390 | 1431 | func TestProcessQueryNextErrors(t *testing.T) {
|
1391 | 1432 | tl := testLogger{t: t}
|
1392 | 1433 | defer tl.StopLogging()
|
|
0 commit comments