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

Copy __ENV so that 2 VUs don't share the same one #1329

Merged
merged 1 commit into from
Feb 17, 2020
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
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