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

Prevent goroutines getting blocked in js tests #2284

Merged
merged 1 commit into from
Dec 13, 2021
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
7 changes: 5 additions & 2 deletions js/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func TestConsoleContext(t *testing.T) {
rt := goja.New()
rt.SetFieldNameMapper(common.FieldNameMapper{})

ctxPtr := new(context.Context)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctxPtr := &ctx

logger, hook := logtest.NewNullLogger()
rt.Set("console", common.Bind(rt, &console{logger}, ctxPtr))

Expand All @@ -58,7 +61,7 @@ func TestConsoleContext(t *testing.T) {
assert.Equal(t, "a", entry.Message)
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel = context.WithCancel(context.Background())
*ctxPtr = ctx
_, err = rt.RunString(`console.log("b")`)
assert.NoError(t, err)
Expand Down
6 changes: 4 additions & 2 deletions js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ func TestRequestWithBinaryFile(t *testing.T) {
Tags: lib.NewTagMap(nil),
}

ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx = lib.WithState(ctx, state)
ctx = common.WithRuntime(ctx, bi.Runtime)
*bi.Context = ctx
Expand Down Expand Up @@ -557,7 +558,8 @@ func TestRequestWithMultipleBinaryFiles(t *testing.T) {
Tags: lib.NewTagMap(nil),
}

ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ctx = lib.WithState(ctx, state)
ctx = common.WithRuntime(ctx, bi.Runtime)
*bi.Context = ctx
Expand Down
36 changes: 25 additions & 11 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,15 @@ func testSetupDataHelper(t *testing.T, data string) {
r := r
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
samples := make(chan stats.SampleContainer, 100)

if !assert.NoError(t, r.Setup(context.Background(), samples)) {
if !assert.NoError(t, r.Setup(ctx, samples)) {
return
}
initVU, err := r.NewVU(1, 1, samples)
if assert.NoError(t, err) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
err := vu.RunOnce()
assert.NoError(t, err)
Expand Down Expand Up @@ -947,7 +947,10 @@ func TestVUIntegrationBlockHostnamesOption(t *testing.T) {
t.Parallel()
initVu, err := r.NewVU(1, 1, make(chan stats.SampleContainer, 100))
require.NoError(t, err)
vu := initVu.Activate(&lib.VUActivationParams{RunContext: context.Background()})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vu := initVu.Activate(&lib.VUActivationParams{RunContext: ctx})
err = vu.RunOnce()
require.Error(t, err)
assert.Contains(t, err.Error(), "hostname (k6.io) is in a blocked pattern (*.io)")
Expand Down Expand Up @@ -982,7 +985,9 @@ func TestVUIntegrationBlockHostnamesScript(t *testing.T) {
t.Parallel()
initVu, err := r.NewVU(0, 0, make(chan stats.SampleContainer, 100))
require.NoError(t, err)
vu := initVu.Activate(&lib.VUActivationParams{RunContext: context.Background()})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vu := initVu.Activate(&lib.VUActivationParams{RunContext: ctx})
err = vu.RunOnce()
require.Error(t, err)
assert.Contains(t, err.Error(), "hostname (k6.io) is in a blocked pattern (*.io)")
Expand Down Expand Up @@ -1585,11 +1590,14 @@ func TestArchiveRunningIntegrity(t *testing.T) {
t.Parallel()
var err error
ch := make(chan stats.SampleContainer, 100)
err = r.Setup(context.Background(), ch)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err = r.Setup(ctx, ch)
cancel()
require.NoError(t, err)
initVU, err := r.NewVU(1, 1, ch)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel = context.WithCancel(context.Background())
defer cancel()
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
err = vu.RunOnce()
Expand Down Expand Up @@ -1755,6 +1763,8 @@ func TestSystemTags(t *testing.T) {
num, tc := num, tc
t.Run(fmt.Sprintf("TC %d with only %s", num, tc.tag), func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
samples := make(chan stats.SampleContainer, 100)
r, err := getSimpleRunner(t, "/script.js", tb.Replacer.Replace(`
var http = require("k6/http");
Expand All @@ -1781,7 +1791,7 @@ func TestSystemTags(t *testing.T) {
vu, err := r.NewVU(uint64(num), 0, samples)
require.NoError(t, err)
activeVU := vu.Activate(&lib.VUActivationParams{
RunContext: context.Background(),
RunContext: ctx,
Exec: tc.exec,
Scenario: "default",
})
Expand Down Expand Up @@ -1972,9 +1982,13 @@ func TestComplicatedFileImportsForGRPC(t *testing.T) {

exports.default = function() {
client.connect('GRPCBIN_ADDR', {timeout: '3s'});
var resp = client.invoke('grpc.testing.TestService/UnaryCall', {})
if (!resp.message || resp.error || resp.message.username !== 'foo') {
throw new Error('unexpected response message: ' + JSON.stringify(resp.message))
try {
var resp = client.invoke('grpc.testing.TestService/UnaryCall', {})
if (!resp.message || resp.error || resp.message.username !== 'foo') {
throw new Error('unexpected response message: ' + JSON.stringify(resp.message))
}
} finally {
client.close();
}
}
`, loadCode))
Expand Down