From 3d2d2dc7b3c25e5159debd0d4935ec8fd23ade31 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Fri, 3 Dec 2021 16:37:15 +0200 Subject: [PATCH] Prevent goroutines getting blocked in js tests The predominant reason was the use of context.Background() for a context, which leads to a goroutine waiting on it ending, to block indefinitely. The other case were gRPC tests which did *not* close their connections. --- js/console_test.go | 7 +++++-- js/initcontext_test.go | 6 ++++-- js/runner_test.go | 36 +++++++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/js/console_test.go b/js/console_test.go index 2c8ccf48734..f816adb74d6 100644 --- a/js/console_test.go +++ b/js/console_test.go @@ -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)) @@ -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) diff --git a/js/initcontext_test.go b/js/initcontext_test.go index 09d70858179..e2c93f40ad8 100644 --- a/js/initcontext_test.go +++ b/js/initcontext_test.go @@ -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 @@ -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 diff --git a/js/runner_test.go b/js/runner_test.go index 3b2060ae88e..4613d65f86a 100644 --- a/js/runner_test.go +++ b/js/runner_test.go @@ -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) @@ -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)") @@ -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)") @@ -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() @@ -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"); @@ -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", }) @@ -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))