Skip to content

Commit 4b9b201

Browse files
committed
api: add ability to mock connections in tests
TBA Closes #237
1 parent 7d73f6a commit 4b9b201

34 files changed

+1527
-1441
lines changed

box_error_test.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,11 @@ func TestErrorTypeEval(t *testing.T) {
306306
t.Run(name, func(t *testing.T) {
307307
resp, err := conn.Eval("return ...", []interface{}{&testcase.tuple.val})
308308
require.Nil(t, err)
309-
require.NotNil(t, resp.Data)
310-
require.Equal(t, len(resp.Data), 1)
311-
actual, ok := resp.Data[0].(*BoxError)
309+
data, err := resp.Decode()
310+
require.Nil(t, err)
311+
require.NotNil(t, data)
312+
require.Equal(t, len(data), 1)
313+
actual, ok := data[0].(*BoxError)
312314
require.Truef(t, ok, "Response data has valid type")
313315
require.Equal(t, testcase.tuple.val, *actual)
314316
})
@@ -436,15 +438,17 @@ func TestErrorTypeSelect(t *testing.T) {
436438
_, err := conn.Eval(insertEval, []interface{}{})
437439
require.Nilf(t, err, "Tuple has been successfully inserted")
438440

439-
var resp *Response
441+
var resp Response
440442
var offset uint32 = 0
441443
var limit uint32 = 1
442444
resp, err = conn.Select(space, index, offset, limit, IterEq,
443445
[]interface{}{testcase.tuple.pk})
444446
require.Nil(t, err)
445-
require.NotNil(t, resp.Data)
446-
require.Equalf(t, len(resp.Data), 1, "Exactly one tuple had been found")
447-
tpl, ok := resp.Data[0].([]interface{})
447+
data, err := resp.Decode()
448+
require.Nil(t, err)
449+
require.NotNil(t, data)
450+
require.Equalf(t, len(data), 1, "Exactly one tuple had been found")
451+
tpl, ok := data[0].([]interface{})
448452
require.Truef(t, ok, "Tuple has valid type")
449453
require.Equal(t, testcase.tuple.pk, tpl[0])
450454
actual, ok := tpl[1].(*BoxError)

connection.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interfac
9797
log.Printf("tarantool: last reconnect to %s failed: %s, giving it up",
9898
conn.Addr(), err)
9999
case LogUnexpectedResultId:
100-
resp := v[0].(*Response)
100+
resp := v[0].(*ConnResponse)
101101
log.Printf("tarantool: connection %s got unexpected resultId (%d) in response",
102-
conn.Addr(), resp.RequestId)
102+
conn.Addr(), resp.requestId)
103103
case LogWatchEventReadFailed:
104104
err := v[0].(error)
105105
log.Printf("tarantool: unable to parse watch event: %s", err)
@@ -807,7 +807,7 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
807807
conn.reconnect(err, c)
808808
return
809809
}
810-
resp := &Response{buf: smallBuf{b: respBytes}}
810+
resp := &ConnResponse{buf: smallBuf{b: respBytes}}
811811
err = resp.decodeHeader(conn.dec)
812812
if err != nil {
813813
err = ClientError{
@@ -819,7 +819,7 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
819819
}
820820

821821
var fut *Future = nil
822-
if iproto.Type(resp.Code) == iproto.IPROTO_EVENT {
822+
if iproto.Type(resp.code) == iproto.IPROTO_EVENT {
823823
if event, err := readWatchEvent(&resp.buf); err == nil {
824824
events <- event
825825
} else {
@@ -830,12 +830,12 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
830830
conn.opts.Logger.Report(LogWatchEventReadFailed, conn, err)
831831
}
832832
continue
833-
} else if resp.Code == PushCode {
834-
if fut = conn.peekFuture(resp.RequestId); fut != nil {
833+
} else if resp.code == PushCode {
834+
if fut = conn.peekFuture(resp.requestId); fut != nil {
835835
fut.AppendPush(resp)
836836
}
837837
} else {
838-
if fut = conn.fetchFuture(resp.RequestId); fut != nil {
838+
if fut = conn.fetchFuture(resp.requestId); fut != nil {
839839
fut.SetResponse(resp)
840840
conn.markDone(fut)
841841
}
@@ -1052,9 +1052,9 @@ func (conn *Connection) putFuture(fut *Future, req Request, streamId uint64) {
10521052

10531053
if req.Async() {
10541054
if fut = conn.fetchFuture(reqid); fut != nil {
1055-
resp := &Response{
1056-
RequestId: reqid,
1057-
Code: OkCode,
1055+
resp := &ConnResponse{
1056+
requestId: reqid,
1057+
code: OkCode,
10581058
}
10591059
fut.SetResponse(resp)
10601060
conn.markDone(fut)
@@ -1236,7 +1236,7 @@ func (conn *Connection) SetSchema(s Schema) {
12361236
// NewPrepared passes a sql statement to Tarantool for preparation synchronously.
12371237
func (conn *Connection) NewPrepared(expr string) (*Prepared, error) {
12381238
req := NewPrepareRequest(expr)
1239-
resp, err := conn.Do(req).Get()
1239+
resp, err := conn.Do(req).GetResponse()
12401240
if err != nil {
12411241
return nil, err
12421242
}

connector.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,41 @@ type Connector interface {
1313

1414
// Deprecated: the method will be removed in the next major version,
1515
// use a PingRequest object + Do() instead.
16-
Ping() (*Response, error)
16+
Ping() (Response, error)
1717
// Deprecated: the method will be removed in the next major version,
1818
// use a SelectRequest object + Do() instead.
1919
Select(space, index interface{}, offset, limit uint32, iterator Iter,
20-
key interface{}) (*Response, error)
20+
key interface{}) (Response, error)
2121
// Deprecated: the method will be removed in the next major version,
2222
// use an InsertRequest object + Do() instead.
23-
Insert(space interface{}, tuple interface{}) (*Response, error)
23+
Insert(space interface{}, tuple interface{}) (Response, error)
2424
// Deprecated: the method will be removed in the next major version,
2525
// use a ReplicaRequest object + Do() instead.
26-
Replace(space interface{}, tuple interface{}) (*Response, error)
26+
Replace(space interface{}, tuple interface{}) (Response, error)
2727
// Deprecated: the method will be removed in the next major version,
2828
// use a DeleteRequest object + Do() instead.
29-
Delete(space, index interface{}, key interface{}) (*Response, error)
29+
Delete(space, index interface{}, key interface{}) (Response, error)
3030
// Deprecated: the method will be removed in the next major version,
3131
// use a UpdateRequest object + Do() instead.
32-
Update(space, index interface{}, key interface{}, ops *Operations) (*Response, error)
32+
Update(space, index interface{}, key interface{}, ops *Operations) (Response, error)
3333
// Deprecated: the method will be removed in the next major version,
3434
// use a UpsertRequest object + Do() instead.
35-
Upsert(space interface{}, tuple interface{}, ops *Operations) (*Response, error)
35+
Upsert(space interface{}, tuple interface{}, ops *Operations) (Response, error)
3636
// Deprecated: the method will be removed in the next major version,
3737
// use a CallRequest object + Do() instead.
38-
Call(functionName string, args interface{}) (*Response, error)
38+
Call(functionName string, args interface{}) (Response, error)
3939
// Deprecated: the method will be removed in the next major version,
4040
// use a Call16Request object + Do() instead.
41-
Call16(functionName string, args interface{}) (*Response, error)
41+
Call16(functionName string, args interface{}) (Response, error)
4242
// Deprecated: the method will be removed in the next major version,
4343
// use a Call17Request object + Do() instead.
44-
Call17(functionName string, args interface{}) (*Response, error)
44+
Call17(functionName string, args interface{}) (Response, error)
4545
// Deprecated: the method will be removed in the next major version,
4646
// use an EvalRequest object + Do() instead.
47-
Eval(expr string, args interface{}) (*Response, error)
47+
Eval(expr string, args interface{}) (Response, error)
4848
// Deprecated: the method will be removed in the next major version,
4949
// use an ExecuteRequest object + Do() instead.
50-
Execute(expr string, args interface{}) (*Response, error)
50+
Execute(expr string, args interface{}) (Response, error)
5151

5252
// Deprecated: the method will be removed in the next major version,
5353
// use a SelectRequest object + Do() instead.

crud/tarantool_test.go

+32-25
Original file line numberDiff line numberDiff line change
@@ -517,20 +517,17 @@ func testSelectGeneratedData(t *testing.T, conn tarantool.Connector,
517517
Limit(20).
518518
Iterator(tarantool.IterGe).
519519
Key([]interface{}{uint(1010)})
520-
resp, err := conn.Do(req).Get()
520+
data, err := conn.Do(req).Get()
521521
if err != nil {
522522
t.Fatalf("Failed to Select: %s", err.Error())
523523
}
524-
if resp == nil {
525-
t.Fatalf("Response is nil after Select")
526-
}
527-
if len(resp.Data) != expectedTuplesCount {
528-
t.Fatalf("Response Data len %d != %d", len(resp.Data), expectedTuplesCount)
524+
if len(data) != expectedTuplesCount {
525+
t.Fatalf("Response Data len %d != %d", len(data), expectedTuplesCount)
529526
}
530527
}
531528

532529
func testCrudRequestCheck(t *testing.T, req tarantool.Request,
533-
resp *tarantool.Response, err error, expectedLen int) {
530+
resp tarantool.Response, err error, expectedLen int) {
534531
t.Helper()
535532

536533
if err != nil {
@@ -541,15 +538,20 @@ func testCrudRequestCheck(t *testing.T, req tarantool.Request,
541538
t.Fatalf("Response is nil after Do CRUD request")
542539
}
543540

544-
if len(resp.Data) < expectedLen {
541+
data, err := resp.Decode()
542+
if err != nil {
543+
t.Fatalf("Failed to Decode: %s", err.Error())
544+
}
545+
546+
if len(data) < expectedLen {
545547
t.Fatalf("Response Body len < %#v, actual len %#v",
546-
expectedLen, len(resp.Data))
548+
expectedLen, len(data))
547549
}
548550

549551
// resp.Data[0] - CRUD res.
550552
// resp.Data[1] - CRUD err.
551-
if expectedLen >= 2 && resp.Data[1] != nil {
552-
if crudErr, err := getCrudError(req, resp.Data[1]); err != nil {
553+
if expectedLen >= 2 && data[1] != nil {
554+
if crudErr, err := getCrudError(req, data[1]); err != nil {
553555
t.Fatalf("Failed to get CRUD error: %#v", err)
554556
} else if crudErr != nil {
555557
t.Fatalf("Failed to perform CRUD request on CRUD side: %#v", crudErr)
@@ -569,7 +571,7 @@ func TestCrudGenerateData(t *testing.T) {
569571
conn.Do(req).Get()
570572
}
571573

572-
resp, err := conn.Do(testCase.req).Get()
574+
resp, err := conn.Do(testCase.req).GetResponse()
573575
testCrudRequestCheck(t, testCase.req, resp,
574576
err, testCase.expectedRespLen)
575577

@@ -591,7 +593,7 @@ func TestCrudProcessData(t *testing.T) {
591593
for _, testCase := range testProcessDataCases {
592594
t.Run(testCase.name, func(t *testing.T) {
593595
testCrudRequestPrepareData(t, conn)
594-
resp, err := conn.Do(testCase.req).Get()
596+
resp, err := conn.Do(testCase.req).GetResponse()
595597
testCrudRequestCheck(t, testCase.req, resp,
596598
err, testCase.expectedRespLen)
597599
for i := 1010; i < 1020; i++ {
@@ -623,7 +625,7 @@ func TestCrudUpdateSplice(t *testing.T) {
623625
Opts(simpleOperationOpts)
624626

625627
testCrudRequestPrepareData(t, conn)
626-
resp, err := conn.Do(req).Get()
628+
resp, err := conn.Do(req).GetResponse()
627629
testCrudRequestCheck(t, req, resp,
628630
err, 2)
629631
}
@@ -648,7 +650,7 @@ func TestCrudUpsertSplice(t *testing.T) {
648650
Opts(simpleOperationOpts)
649651

650652
testCrudRequestPrepareData(t, conn)
651-
resp, err := conn.Do(req).Get()
653+
resp, err := conn.Do(req).GetResponse()
652654
testCrudRequestCheck(t, req, resp,
653655
err, 2)
654656
}
@@ -673,7 +675,7 @@ func TestCrudUpsertObjectSplice(t *testing.T) {
673675
Opts(simpleOperationOpts)
674676

675677
testCrudRequestPrepareData(t, conn)
676-
resp, err := conn.Do(req).Get()
678+
resp, err := conn.Do(req).GetResponse()
677679
testCrudRequestCheck(t, req, resp,
678680
err, 2)
679681
}
@@ -719,11 +721,16 @@ func TestUnflattenRows(t *testing.T) {
719721
req := crud.MakeReplaceRequest(spaceName).
720722
Tuple(tuple).
721723
Opts(simpleOperationOpts)
722-
resp, err := conn.Do(req).Get()
724+
resp, err := conn.Do(req).GetResponse()
723725
testCrudRequestCheck(t, req, resp, err, 2)
724726

725-
if res, ok = resp.Data[0].(map[interface{}]interface{}); !ok {
726-
t.Fatalf("Unexpected CRUD result: %#v", resp.Data[0])
727+
data, err := resp.Decode()
728+
if err != nil {
729+
t.Fatalf("Failed to Decode: %s", err.Error())
730+
}
731+
732+
if res, ok = data[0].(map[interface{}]interface{}); !ok {
733+
t.Fatalf("Unexpected CRUD result: %#v", data[0])
727734
}
728735

729736
if rawMetadata, ok := res["metadata"]; !ok {
@@ -1293,21 +1300,21 @@ func TestNoreturnOption(t *testing.T) {
12931300
conn.Do(req).Get()
12941301
}
12951302

1296-
resp, err := conn.Do(testCase.req).Get()
1303+
data, err := conn.Do(testCase.req).Get()
12971304
if err != nil {
12981305
t.Fatalf("Failed to Do CRUD request: %s", err)
12991306
}
13001307

1301-
if len(resp.Data) == 0 {
1308+
if len(data) == 0 {
13021309
t.Fatalf("Expected explicit nil")
13031310
}
13041311

1305-
if resp.Data[0] != nil {
1306-
t.Fatalf("Expected nil result, got %v", resp.Data[0])
1312+
if data[0] != nil {
1313+
t.Fatalf("Expected nil result, got %v", data[0])
13071314
}
13081315

1309-
if len(resp.Data) >= 2 && resp.Data[1] != nil {
1310-
t.Fatalf("Expected no returned errors, got %v", resp.Data[1])
1316+
if len(data) >= 2 && data[1] != nil {
1317+
t.Fatalf("Expected no returned errors, got %v", data[1])
13111318
}
13121319

13131320
for i := 1010; i < 1020; i++ {

0 commit comments

Comments
 (0)