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: fix ws not support throw option #2616

Merged
merged 12 commits into from Jul 28, 2022
9 changes: 7 additions & 2 deletions js/modules/k6/ws/ws.go
Expand Up @@ -286,8 +286,13 @@ func (mi *WS) Connect(url string, args ...goja.Value) (*WSHTTPResponse, error) {
if connErr != nil {
// Pass the error to the user script before exiting immediately
socket.handleEvent("error", rt.ToValue(connErr))

return nil, connErr
if state.Options.Throw.Bool {
return nil, connErr
}
state.Logger.WithField("error", connErr).Warn("Ws Connection Failed")
return &WSHTTPResponse{
Error: connErr.Error(),
}, nil
}

// Run the user-provided set up function
Expand Down
64 changes: 64 additions & 0 deletions js/modules/k6/ws/ws_test.go
Expand Up @@ -34,6 +34,7 @@ import (

"github.com/dop251/goja"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"
Expand All @@ -42,6 +43,7 @@ import (
httpModule "go.k6.io/k6/js/modules/k6/http"
"go.k6.io/k6/js/modulestest"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/testutils/httpmultibin"
"go.k6.io/k6/metrics"
)
Expand Down Expand Up @@ -655,6 +657,7 @@ func TestErrors(t *testing.T) {
Dialer: tb.Dialer,
Options: lib.Options{
SystemTags: &metrics.DefaultSystemTagSet,
Throw: null.BoolFrom(true),
fatelei marked this conversation as resolved.
Show resolved Hide resolved
},
Samples: samples,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Expand Down Expand Up @@ -1288,3 +1291,64 @@ func TestCookieJar(t *testing.T) {

assertSessionMetricsEmitted(t, metrics.GetBufferedSamples(ts.samples), "", sr("WSBIN_URL/ws-echo-someheader"), statusProtocolSwitch, "")
}

func TestWSConnectWithThrowErrorOption(t *testing.T) {
tb := httpmultibin.NewHTTPMultiBin(t)
root, err := lib.NewGroup("", nil)
require.NoError(t, err)

logHook := &testutils.SimpleLogrusHook{HookedLevels: []logrus.Level{logrus.WarnLevel}}
testLog := logrus.New()
testLog.AddHook(logHook)
testLog.SetOutput(io.Discard)
fatelei marked this conversation as resolved.
Show resolved Hide resolved

rt := goja.New()
rt.SetFieldNameMapper(common.FieldNameMapper{})
samples := make(chan metrics.SampleContainer, 1000)
state := &lib.State{
Group: root,
Dialer: tb.Dialer,
Options: lib.Options{
SystemTags: &metrics.DefaultSystemTagSet,
},
Samples: samples,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Tags: lib.NewTagMap(nil),
Logger: testLog,
}

m := New().NewModuleInstance(&modulestest.VU{
CtxField: context.Background(),
InitEnvField: &common.InitEnvironment{},
RuntimeField: rt,
StateField: state,
})
require.NoError(t, rt.Set("ws", m.Exports().Default))

t.Run("ThrowEnabled", func(t *testing.T) {
state.Options.Throw = null.BoolFrom(true)
_, err = rt.RunString(`
var res = ws.connect("INVALID", function(socket){
socket.on("open", function() {
socket.close();
});
});
fatelei marked this conversation as resolved.
Show resolved Hide resolved
`)
assert.Error(t, err)
})

t.Run("ThrowDisabled", func(t *testing.T) {
state.Options.Throw = null.BoolFrom(false)
fatelei marked this conversation as resolved.
Show resolved Hide resolved
_, err = rt.RunString(`
var res = ws.connect("INVALID", function(socket){
socket.on("open", function() {
socket.close();
});
});
`)
entries := logHook.Drain()
require.Len(t, entries, 1)
assert.Contains(t, entries[0].Message, "Ws Connection Failed")
assert.NoError(t, err)
fatelei marked this conversation as resolved.
Show resolved Hide resolved
})
}