Skip to content

Commit

Permalink
Merge pull request #1 from nhatthm/fix-close-connector
Browse files Browse the repository at this point in the history
Fix an issue while closing a connector that does not implement `io.Closer`
  • Loading branch information
nhatthm committed Jan 26, 2022
2 parents 12d1f39 + 82b751b commit 746f28f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
7 changes: 4 additions & 3 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func wrapDriver(d driver.Driver, o driverOptions) driver.Driver {
drv := otDriver{
parent: d,
connConfig: newConnConfig(o),
close: func() error { return nil },
}

if _, ok := d.(driver.DriverContext); ok {
Expand Down Expand Up @@ -160,7 +161,7 @@ var _ driver.Driver = (*otDriver)(nil)
type otDriver struct {
parent driver.Driver
connector driver.Connector
closer io.Closer
close func() error

connConfig connConfig
}
Expand All @@ -175,7 +176,7 @@ func (d otDriver) Open(name string) (driver.Conn, error) {
}

func (d otDriver) Close() error {
return d.closer.Close()
return d.close()
}

func (d otDriver) OpenConnector(name string) (driver.Connector, error) {
Expand All @@ -187,7 +188,7 @@ func (d otDriver) OpenConnector(name string) (driver.Connector, error) {
}

if c, ok := d.connector.(io.Closer); ok {
d.closer = c
d.close = c.Close
}

return d, err
Expand Down
32 changes: 31 additions & 1 deletion driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -129,6 +130,35 @@ func TestWrap_DriverContext_ConnectError(t *testing.T) {
assert.Equal(t, expectedError, err)
}

func TestWrap_DriverContext_CloseBeforeOpenConnector(t *testing.T) {
t.Parallel()

parent := struct {
driver.Driver
driver.DriverContext
}{
DriverContext: driverOpenConnectorFunc(func(name string) (driver.Connector, error) {
return struct {
driverDriverFunc
driverConnectFunc
driverCloseFunc
}{}, nil
}),
}

drv, ok := otelsql.Wrap(parent).(struct {
driver.Driver
driver.DriverContext
})
require.True(t, ok, "unexpected driver implementation")

c, ok := drv.Driver.(io.Closer)
require.True(t, ok, "driver must implement io.Closer")

err := c.Close()
assert.NoError(t, err)
}

func TestWrap_DriverContext_CloseError(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2346,7 +2376,7 @@ func assertSpansHaveSameRoot(t assert.TestingT, actual []oteltest.Span) bool {
}

func getFixture(file string, args ...interface{}) string {
data, err := os.ReadFile(file) // nolint: gosec
data, err := os.ReadFile(filepath.Clean(file))
mustNotFail(err)

return fmt.Sprintf(string(data), args...)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package otelsql
// Version is the current release version of the otelsql instrumentation.
func Version() string {
// This string is updated by the pre_release.sh script during release
return "0.1.0"
return "0.1.1"
}

// SemVersion is the semantic version to be supplied to tracer/meter creation.
Expand Down

0 comments on commit 746f28f

Please sign in to comment.