Skip to content

Commit

Permalink
api: add ability to mock connections in tests
Browse files Browse the repository at this point in the history
TBA

Closes #237
  • Loading branch information
DerekBum committed Dec 7, 2023
1 parent 7d73f6a commit 0ee8687
Show file tree
Hide file tree
Showing 34 changed files with 1,526 additions and 1,441 deletions.
18 changes: 11 additions & 7 deletions box_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,11 @@ func TestErrorTypeEval(t *testing.T) {
t.Run(name, func(t *testing.T) {
resp, err := conn.Eval("return ...", []interface{}{&testcase.tuple.val})
require.Nil(t, err)
require.NotNil(t, resp.Data)
require.Equal(t, len(resp.Data), 1)
actual, ok := resp.Data[0].(*BoxError)
data, err := resp.Decode()
require.Nil(t, err)
require.NotNil(t, data)
require.Equal(t, len(data), 1)
actual, ok := data[0].(*BoxError)
require.Truef(t, ok, "Response data has valid type")
require.Equal(t, testcase.tuple.val, *actual)
})
Expand Down Expand Up @@ -436,15 +438,17 @@ func TestErrorTypeSelect(t *testing.T) {
_, err := conn.Eval(insertEval, []interface{}{})
require.Nilf(t, err, "Tuple has been successfully inserted")

var resp *Response
var resp Response
var offset uint32 = 0
var limit uint32 = 1
resp, err = conn.Select(space, index, offset, limit, IterEq,
[]interface{}{testcase.tuple.pk})
require.Nil(t, err)
require.NotNil(t, resp.Data)
require.Equalf(t, len(resp.Data), 1, "Exactly one tuple had been found")
tpl, ok := resp.Data[0].([]interface{})
data, err := resp.Decode()
require.Nil(t, err)
require.NotNil(t, data)
require.Equalf(t, len(data), 1, "Exactly one tuple had been found")
tpl, ok := data[0].([]interface{})
require.Truef(t, ok, "Tuple has valid type")
require.Equal(t, testcase.tuple.pk, tpl[0])
actual, ok := tpl[1].(*BoxError)
Expand Down
22 changes: 11 additions & 11 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ func (d defaultLogger) Report(event ConnLogKind, conn *Connection, v ...interfac
log.Printf("tarantool: last reconnect to %s failed: %s, giving it up",
conn.Addr(), err)
case LogUnexpectedResultId:
resp := v[0].(*Response)
resp := v[0].(*ConnResponse)
log.Printf("tarantool: connection %s got unexpected resultId (%d) in response",
conn.Addr(), resp.RequestId)
conn.Addr(), resp.requestId)
case LogWatchEventReadFailed:
err := v[0].(error)
log.Printf("tarantool: unable to parse watch event: %s", err)
Expand Down Expand Up @@ -807,7 +807,7 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
conn.reconnect(err, c)
return
}
resp := &Response{buf: smallBuf{b: respBytes}}
resp := &ConnResponse{buf: smallBuf{b: respBytes}}
err = resp.decodeHeader(conn.dec)
if err != nil {
err = ClientError{
Expand All @@ -819,7 +819,7 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
}

var fut *Future = nil
if iproto.Type(resp.Code) == iproto.IPROTO_EVENT {
if iproto.Type(resp.code) == iproto.IPROTO_EVENT {
if event, err := readWatchEvent(&resp.buf); err == nil {
events <- event
} else {
Expand All @@ -830,12 +830,12 @@ func (conn *Connection) reader(r io.Reader, c Conn) {
conn.opts.Logger.Report(LogWatchEventReadFailed, conn, err)
}
continue
} else if resp.Code == PushCode {
if fut = conn.peekFuture(resp.RequestId); fut != nil {
} else if resp.code == PushCode {
if fut = conn.peekFuture(resp.requestId); fut != nil {
fut.AppendPush(resp)
}
} else {
if fut = conn.fetchFuture(resp.RequestId); fut != nil {
if fut = conn.fetchFuture(resp.requestId); fut != nil {
fut.SetResponse(resp)
conn.markDone(fut)
}
Expand Down Expand Up @@ -1052,9 +1052,9 @@ func (conn *Connection) putFuture(fut *Future, req Request, streamId uint64) {

if req.Async() {
if fut = conn.fetchFuture(reqid); fut != nil {
resp := &Response{
RequestId: reqid,
Code: OkCode,
resp := &ConnResponse{
requestId: reqid,
code: OkCode,
}
fut.SetResponse(resp)
conn.markDone(fut)
Expand Down Expand Up @@ -1236,7 +1236,7 @@ func (conn *Connection) SetSchema(s Schema) {
// NewPrepared passes a sql statement to Tarantool for preparation synchronously.
func (conn *Connection) NewPrepared(expr string) (*Prepared, error) {
req := NewPrepareRequest(expr)
resp, err := conn.Do(req).Get()
resp, err := conn.Do(req).GetResponse()
if err != nil {
return nil, err
}
Expand Down
24 changes: 12 additions & 12 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,41 @@ type Connector interface {

// Deprecated: the method will be removed in the next major version,
// use a PingRequest object + Do() instead.
Ping() (*Response, error)
Ping() (Response, error)
// Deprecated: the method will be removed in the next major version,
// use a SelectRequest object + Do() instead.
Select(space, index interface{}, offset, limit uint32, iterator Iter,
key interface{}) (*Response, error)
key interface{}) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use an InsertRequest object + Do() instead.
Insert(space interface{}, tuple interface{}) (*Response, error)
Insert(space interface{}, tuple interface{}) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use a ReplicaRequest object + Do() instead.
Replace(space interface{}, tuple interface{}) (*Response, error)
Replace(space interface{}, tuple interface{}) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use a DeleteRequest object + Do() instead.
Delete(space, index interface{}, key interface{}) (*Response, error)
Delete(space, index interface{}, key interface{}) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use a UpdateRequest object + Do() instead.
Update(space, index interface{}, key interface{}, ops *Operations) (*Response, error)
Update(space, index interface{}, key interface{}, ops *Operations) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use a UpsertRequest object + Do() instead.
Upsert(space interface{}, tuple interface{}, ops *Operations) (*Response, error)
Upsert(space interface{}, tuple interface{}, ops *Operations) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use a CallRequest object + Do() instead.
Call(functionName string, args interface{}) (*Response, error)
Call(functionName string, args interface{}) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use a Call16Request object + Do() instead.
Call16(functionName string, args interface{}) (*Response, error)
Call16(functionName string, args interface{}) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use a Call17Request object + Do() instead.
Call17(functionName string, args interface{}) (*Response, error)
Call17(functionName string, args interface{}) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use an EvalRequest object + Do() instead.
Eval(expr string, args interface{}) (*Response, error)
Eval(expr string, args interface{}) (Response, error)
// Deprecated: the method will be removed in the next major version,
// use an ExecuteRequest object + Do() instead.
Execute(expr string, args interface{}) (*Response, error)
Execute(expr string, args interface{}) (Response, error)

// Deprecated: the method will be removed in the next major version,
// use a SelectRequest object + Do() instead.
Expand Down
57 changes: 32 additions & 25 deletions crud/tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,20 +517,17 @@ func testSelectGeneratedData(t *testing.T, conn tarantool.Connector,
Limit(20).
Iterator(tarantool.IterGe).
Key([]interface{}{uint(1010)})
resp, err := conn.Do(req).Get()
data, err := conn.Do(req).Get()
if err != nil {
t.Fatalf("Failed to Select: %s", err.Error())
}
if resp == nil {
t.Fatalf("Response is nil after Select")
}
if len(resp.Data) != expectedTuplesCount {
t.Fatalf("Response Data len %d != %d", len(resp.Data), expectedTuplesCount)
if len(data) != expectedTuplesCount {
t.Fatalf("Response Data len %d != %d", len(data), expectedTuplesCount)
}
}

func testCrudRequestCheck(t *testing.T, req tarantool.Request,
resp *tarantool.Response, err error, expectedLen int) {
resp tarantool.Response, err error, expectedLen int) {
t.Helper()

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

if len(resp.Data) < expectedLen {
data, err := resp.Decode()
if err != nil {
t.Fatalf("Failed to Decode: %s", err.Error())
}

if len(data) < expectedLen {
t.Fatalf("Response Body len < %#v, actual len %#v",
expectedLen, len(resp.Data))
expectedLen, len(data))
}

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

resp, err := conn.Do(testCase.req).Get()
resp, err := conn.Do(testCase.req).GetResponse()
testCrudRequestCheck(t, testCase.req, resp,
err, testCase.expectedRespLen)

Expand All @@ -591,7 +593,7 @@ func TestCrudProcessData(t *testing.T) {
for _, testCase := range testProcessDataCases {
t.Run(testCase.name, func(t *testing.T) {
testCrudRequestPrepareData(t, conn)
resp, err := conn.Do(testCase.req).Get()
resp, err := conn.Do(testCase.req).GetResponse()
testCrudRequestCheck(t, testCase.req, resp,
err, testCase.expectedRespLen)
for i := 1010; i < 1020; i++ {
Expand Down Expand Up @@ -623,7 +625,7 @@ func TestCrudUpdateSplice(t *testing.T) {
Opts(simpleOperationOpts)

testCrudRequestPrepareData(t, conn)
resp, err := conn.Do(req).Get()
resp, err := conn.Do(req).GetResponse()
testCrudRequestCheck(t, req, resp,
err, 2)
}
Expand All @@ -648,7 +650,7 @@ func TestCrudUpsertSplice(t *testing.T) {
Opts(simpleOperationOpts)

testCrudRequestPrepareData(t, conn)
resp, err := conn.Do(req).Get()
resp, err := conn.Do(req).GetResponse()
testCrudRequestCheck(t, req, resp,
err, 2)
}
Expand All @@ -673,7 +675,7 @@ func TestCrudUpsertObjectSplice(t *testing.T) {
Opts(simpleOperationOpts)

testCrudRequestPrepareData(t, conn)
resp, err := conn.Do(req).Get()
resp, err := conn.Do(req).GetResponse()
testCrudRequestCheck(t, req, resp,
err, 2)
}
Expand Down Expand Up @@ -719,11 +721,16 @@ func TestUnflattenRows(t *testing.T) {
req := crud.MakeReplaceRequest(spaceName).
Tuple(tuple).
Opts(simpleOperationOpts)
resp, err := conn.Do(req).Get()
resp, err := conn.Do(req).GetResponse()
testCrudRequestCheck(t, req, resp, err, 2)

if res, ok = resp.Data[0].(map[interface{}]interface{}); !ok {
t.Fatalf("Unexpected CRUD result: %#v", resp.Data[0])
data, err := resp.Decode()
if err != nil {
t.Fatalf("Failed to Decode: %s", err.Error())
}

if res, ok = data[0].(map[interface{}]interface{}); !ok {
t.Fatalf("Unexpected CRUD result: %#v", data[0])
}

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

resp, err := conn.Do(testCase.req).Get()
data, err := conn.Do(testCase.req).Get()
if err != nil {
t.Fatalf("Failed to Do CRUD request: %s", err)
}

if len(resp.Data) == 0 {
if len(data) == 0 {
t.Fatalf("Expected explicit nil")
}

if resp.Data[0] != nil {
t.Fatalf("Expected nil result, got %v", resp.Data[0])
if data[0] != nil {
t.Fatalf("Expected nil result, got %v", data[0])
}

if len(resp.Data) >= 2 && resp.Data[1] != nil {
t.Fatalf("Expected no returned errors, got %v", resp.Data[1])
if len(data) >= 2 && data[1] != nil {
t.Fatalf("Expected no returned errors, got %v", data[1])
}

for i := 1010; i < 1020; i++ {
Expand Down
Loading

0 comments on commit 0ee8687

Please sign in to comment.