Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Merge pull request #1 from dokshina/fix-calling-exec-typing-context-w…
Browse files Browse the repository at this point in the history
…ithout-timeout-error

Fix calling ExecTypedContext w/o timeout
  • Loading branch information
FZambia authored Jan 20, 2021
2 parents 8fb4aa6 + 3c49b93 commit 5d49000
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 2 deletions.
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func (conn *Connection) ExecContext(ctx context.Context, req *Request) (*Respons

// Exec Request with context.Context and decode it to typed result.
func (conn *Connection) ExecTypedContext(ctx context.Context, req *Request, result interface{}) error {
if _, ok := ctx.Deadline(); !ok {
if _, ok := ctx.Deadline(); !ok && conn.opts.RequestTimeout > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, conn.opts.RequestTimeout)
defer cancel()
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/FZambia/tarantool

go 1.15

require github.com/vmihailenco/msgpack/v5 v5.1.0
require (
github.com/stretchr/testify v1.6.1
github.com/vmihailenco/msgpack/v5 v5.1.0
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
113 changes: 113 additions & 0 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/vmihailenco/msgpack/v5"
)

Expand Down Expand Up @@ -1081,3 +1082,115 @@ func TestComplexStructs(t *testing.T) {
return
}
}

func TestExecContext(t *testing.T) {
assert := assert.New(t)

var err error
var connWithTimeout *Connection
var connNoTimeout *Connection
var result []interface{}

var ctx context.Context
var cancel context.CancelFunc

// long request
req := Eval("require('fiber').sleep(0.5)", []interface{}{})

// connection w/o request timeout
connNoTimeout, err = Connect(server, Opts{
User: opts.User,
Password: opts.Password,
})
assert.Nil(err)
assert.NotNil(connNoTimeout)

defer connNoTimeout.Close()

// exec without timeout - shouldn't fail
err = connNoTimeout.ExecTypedContext(
context.Background(),
req,
&result,
)
assert.Nil(err)

_, err = connNoTimeout.ExecContext(
context.Background(),
req,
)
assert.Nil(err)

// exec with timeout - should fail
ctx, cancel = context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()

err = connNoTimeout.ExecTypedContext(
ctx,
req,
&result,
)

assert.NotNil(err)
assert.Contains(err.Error(), "context deadline exceeded")

ctx, cancel = context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()

_, err = connNoTimeout.ExecContext(
ctx,
req,
)
assert.NotNil(err)
assert.Contains(err.Error(), "context deadline exceeded")

// connection w/ request timeout
connWithTimeout, err = Connect(server, Opts{
User: opts.User,
Password: opts.Password,
RequestTimeout: 200 * time.Millisecond,
})
assert.Nil(err)
assert.NotNil(connWithTimeout)

defer connWithTimeout.Close()

// exec without timeout - should fail
err = connWithTimeout.ExecTypedContext(
context.Background(),
req,
&result,
)
assert.NotNil(err)
assert.Contains(err.Error(), "context deadline exceeded")

_, err = connWithTimeout.ExecContext(
context.Background(),
req,
)
assert.NotNil(err)
assert.Contains(err.Error(), "context deadline exceeded")

// exec with timeout - should fail
ctx, cancel = context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()

err = connWithTimeout.ExecTypedContext(
ctx,
req,
&result,
)

assert.NotNil(err)
assert.Contains(err.Error(), "context deadline exceeded")

ctx, cancel = context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()

_, err = connWithTimeout.ExecContext(
ctx,
req,
)
assert.NotNil(err)
assert.Contains(err.Error(), "context deadline exceeded")
}

0 comments on commit 5d49000

Please sign in to comment.