Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use vu Context() instead of background Context #126

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions releases/v1.0.4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
xk6-sql `v1.0.4` is here 🎉!

This release includes:

## Bugfixes

- [Use VU Context()](https://github.com/grafana/xk6-sql/issues/124): VU Context() is now used in `query()` and `exec()` functions instead of background context. Using background context is a potential problem if SQL operations are still running after the VU context is invalidated.



15 changes: 10 additions & 5 deletions sql/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package sql

import (
"context"
"database/sql"
"errors"
"fmt"
Expand All @@ -24,20 +25,23 @@ type rootModule struct{}

// NewModuleInstance implements the modules.Module interface to return
// a new instance for each VU.
func (*rootModule) NewModuleInstance(_ modules.VU) modules.Instance {
func (*rootModule) NewModuleInstance(vu modules.VU) modules.Instance {
instance := &module{}

instance.exports.Default = instance
instance.exports.Named = map[string]interface{}{
"open": instance.Open,
}

instance.vu = vu

return instance
}

// module represents an instance of the JavaScript module for every VU.
type module struct {
exports modules.Exports
vu modules.VU
}

// Exports is representation of ESM exports of a module.
Expand Down Expand Up @@ -97,17 +101,18 @@ func (mod *module) Open(driverID sobek.Value, connectionString string, opts *opt
return nil, err
}

return &Database{db: db}, nil
return &Database{db: db, ctx: mod.vu.Context}, nil
}

// Database is a database handle representing a pool of zero or more underlying connections.
type Database struct {
db *sql.DB
db *sql.DB
ctx func() context.Context
}

// Query executes a query that returns rows, typically a SELECT.
func (dbase *Database) Query(query string, args ...interface{}) ([]KeyValue, error) {
rows, err := dbase.db.Query(query, args...)
rows, err := dbase.db.QueryContext(dbase.ctx(), query, args...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -152,7 +157,7 @@ func (dbase *Database) Query(query string, args ...interface{}) ([]KeyValue, err

// Exec a query without returning any rows.
func (dbase *Database) Exec(query string, args ...interface{}) (sql.Result, error) {
return dbase.db.Exec(query, args...)
return dbase.db.ExecContext(dbase.ctx(), query, args...)
}

// Close the database and prevents new queries from starting.
Expand Down
5 changes: 4 additions & 1 deletion sql/sql_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (

"github.com/grafana/sobek"
"github.com/stretchr/testify/require"
"go.k6.io/k6/js/modulestest"
)

func TestOpen(t *testing.T) { //nolint: paralleltest
mod, ok := New().NewModuleInstance(nil).(*module)
rt := modulestest.NewRuntime(t)

mod, ok := New().NewModuleInstance(rt.VU).(*module)

require.True(t, ok)

Expand Down
Loading