Skip to content

Commit

Permalink
Copy __ENV so that 2 VUs don't share the same one
Browse files Browse the repository at this point in the history
as in #799(which was about the same issue with setup data), we
don't actually have problems with a VU seeing changes from
previous iterations. It will be too expensive to constantly copy, it
won't panic and there are no issues with distributed execution.
  • Loading branch information
mstoykov committed Feb 11, 2020
1 parent 74f11a6 commit 33513be
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ func (b *Bundle) instantiate(rt *goja.Runtime, init *InitContext) error {
_ = module.Set("exports", exports)
rt.Set("module", module)

rt.Set("__ENV", b.Env)
env := make(map[string]string, len(b.Env))
for key, value := range b.Env {
env[key] = value
}
rt.Set("__ENV", env)
rt.Set("console", common.Bind(rt, newConsole(), init.ctxPtr))

*init.ctxPtr = common.WithRuntime(context.Background(), rt)
Expand Down
42 changes: 42 additions & 0 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,48 @@ func TestBundleEnv(t *testing.T) {
}
}

func TestBundleNotSharable(t *testing.T) {
data := `
export default function() {
if (__ITER == 0) {
if (typeof __ENV.something !== "undefined") {
throw new Error("invalid something: " + __ENV.something + " should be undefined");
}
__ENV.something = __VU;
} else if (__ENV.something != __VU) {
throw new Error("invalid something: " + __ENV.something+ " should be "+ __VU);
}
}
`
b1, err := getSimpleBundle("/script.js", data)
if !assert.NoError(t, err) {
return
}

b2, err := NewBundleFromArchive(b1.makeArchive(), lib.RuntimeOptions{})
if !assert.NoError(t, err) {
return
}

bundles := map[string]*Bundle{"Source": b1, "Archive": b2}
vus, iters := 10, 1000
for name, b := range bundles {
b := b
t.Run(name, func(t *testing.T) {
for i := 0; i < vus; i++ {
bi, err := b.Instantiate()
bi.Runtime.Set("__VU", i)
require.NoError(t, err)
for j := 0; j < iters; j++ {
bi.Runtime.Set("__ITER", j)
_, err := bi.Default(goja.Undefined())
assert.NoError(t, err)
}
}
})
}
}

func TestBundleMakeArchive(t *testing.T) {
testCases := []struct {
cm compiler.CompatibilityMode
Expand Down

0 comments on commit 33513be

Please sign in to comment.