From 82b751bcd051b1172da94960b669b721ec21bae7 Mon Sep 17 00:00:00 2001 From: nhatthm Date: Wed, 26 Jan 2022 19:24:22 +0100 Subject: [PATCH] Fix an issue while closing a connector that does not implement io.Closer --- driver.go | 7 ++++--- driver_test.go | 32 +++++++++++++++++++++++++++++++- version.go | 2 +- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/driver.go b/driver.go index aaa9f9e..a2cf239 100644 --- a/driver.go +++ b/driver.go @@ -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 { @@ -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 } @@ -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) { @@ -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 diff --git a/driver_test.go b/driver_test.go index ef60fc5..dfa2546 100644 --- a/driver_test.go +++ b/driver_test.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -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() @@ -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...) diff --git a/version.go b/version.go index 673881c..ebcb2a3 100644 --- a/version.go +++ b/version.go @@ -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.