Skip to content

Commit

Permalink
chore: enhance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fatelei committed Jul 25, 2022
1 parent 120befd commit f081ebe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
11 changes: 6 additions & 5 deletions js/modules/k6/ws/ws.go
Expand Up @@ -288,12 +288,13 @@ func (mi *WS) Connect(url string, args ...goja.Value) (*WSHTTPResponse, error) {
socket.handleEvent("error", rt.ToValue(connErr))
if state.Options.Throw.Bool {
return nil, connErr
} else {
if state.Logger != nil {
state.Logger.WithField("error", connErr).Warn("Ws Connection Failed")
}
return &WSHTTPResponse{}, nil
}
if state.Logger != nil {
state.Logger.WithField("error", connErr).Warn("Ws Connection Failed")
}
return &WSHTTPResponse{
Error: connErr.Error(),
}, nil
}

// Run the user-provided set up function
Expand Down
27 changes: 23 additions & 4 deletions js/modules/k6/ws/ws_test.go
Expand Up @@ -1290,7 +1290,8 @@ func TestCookieJar(t *testing.T) {
assertSessionMetricsEmitted(t, metrics.GetBufferedSamples(ts.samples), "", sr("WSBIN_URL/ws-echo-someheader"), statusProtocolSwitch, "")
}

func TestWithoutThrowError(t *testing.T) {
func TestWsWithThrowOptionError(t *testing.T) {
t.Parallel()
tb := httpmultibin.NewHTTPMultiBin(t)
root, err := lib.NewGroup("", nil)
require.NoError(t, err)
Expand All @@ -1303,7 +1304,6 @@ func TestWithoutThrowError(t *testing.T) {
Dialer: tb.Dialer,
Options: lib.Options{
SystemTags: &metrics.DefaultSystemTagSet,
Throw: null.BoolFrom(false),
},
Samples: samples,
BuiltinMetrics: metrics.RegisterBuiltinMetrics(metrics.NewRegistry()),
Expand All @@ -1317,12 +1317,31 @@ func TestWithoutThrowError(t *testing.T) {
StateField: state,
})
require.NoError(t, rt.Set("ws", m.Exports().Default))
_, err = rt.RunString(`

t.Run("enable_throw_error", 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();
});
});
`)
assert.Error(t, err)
})

t.Run("disable_throw_error", func(t *testing.T) {
state.Options.Throw = null.BoolFrom(false)
_, err = rt.RunString(`
var res = ws.connect("INVALID", function(socket){
socket.on("open", function() {
socket.close();
});
});
if (res && res.error) {
throw new Error(res.error);
}
`)
assert.NoError(t, err)
assert.Error(t, err)
})
}

0 comments on commit f081ebe

Please sign in to comment.