Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Fixed `context` checking in `ydb.Open`

## v3.118.1
* Fixed connection timeout issue in topics writer
* Supported `sql.Null*` from `database/sql` as query params in `toValue` func
Expand Down
8 changes: 8 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ func (d *Driver) Topic() topic.Client {
//
//nolint:nonamedreturns
func Open(ctx context.Context, dsn string, opts ...Option) (_ *Driver, _ error) {
if ctx.Err() != nil {
return nil, xerrors.WithStackTrace(ctx.Err())
}

opts = append(append(make([]Option, 0, len(opts)+1), WithConnectionString(dsn)), opts...)

for parserIdx := range dsnParsers {
Expand Down Expand Up @@ -438,6 +442,10 @@ func (d *Driver) connect(ctx context.Context) error {
return xerrors.WithStackTrace(errors.New("configuration: empty database")) //nolint:err113
}

if ctx.Err() != nil {
return xerrors.WithStackTrace(ctx.Err())
}

if d.userInfo != nil {
d.config = d.config.With(config.WithCredentials(
credentials.NewStaticCredentials(
Expand Down
37 changes: 37 additions & 0 deletions driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ydb_test

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)

func TestOpen(t *testing.T) {
t.Run("context already canceled", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

_, err := ydb.Open(ctx, "grpc://localhost:2136/local")
require.ErrorIs(t, err, context.Canceled)
assert.Regexp(t, "^context canceled at", err.Error())
})

t.Run("context canceled at internal connect()", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

_, err := ydb.Open(ctx, "grpc://localhost:2136/local", ydb.WithTraceDriver(trace.Driver{
OnInit: func(disi trace.DriverInitStartInfo) func(trace.DriverInitDoneInfo) {
cancel()

return func(didi trace.DriverInitDoneInfo) {}
},
}))
require.ErrorIs(t, err, context.Canceled)
assert.Regexp(t, "^context canceled at", err.Error())
})
}
4 changes: 4 additions & 0 deletions internal/balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ func makeDiscoveryFunc(
func New(ctx context.Context, driverConfig *config.Config, pool *conn.Pool, opts ...discoveryConfig.Option) (
b *Balancer, finalErr error,
) {
if ctx.Err() != nil {
return nil, xerrors.WithStackTrace(ctx.Err())
}

onDone := trace.DriverOnBalancerInit(driverConfig.Trace(), &ctx,
stack.FunctionID("github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer.New"),
driverConfig.Balancer().String(),
Expand Down
12 changes: 12 additions & 0 deletions internal/balancer/balancer_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/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/resolver"
Expand Down Expand Up @@ -143,3 +144,14 @@ type mockResolver struct{}

func (r *mockResolver) ResolveNow(resolver.ResolveNowOptions) {}
func (r *mockResolver) Close() {}

func TestNew(t *testing.T) {
t.Run("context already canceled", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()

_, err := New(ctx, config.New(), nil)
require.ErrorIs(t, err, context.Canceled)
assert.Regexp(t, "^context canceled at", err.Error())
})
}
Loading