Skip to content

Commit

Permalink
Update k6 0.37.0 and resolve deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Jun 23, 2022
1 parent c9b72f9 commit 84961e7
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 467 deletions.
20 changes: 6 additions & 14 deletions cable.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cable

import (
"context"
"crypto/tls"
"fmt"
"net"
Expand All @@ -14,28 +13,21 @@ import (
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib"
"go.k6.io/k6/stats"
)

func init() {
modules.Register("k6/x/cable", new(Cable))
}

// errCableInInitContext is returned when cable used in the init context
var errCableInInitContext = common.NewInitContextError("using cable in the init context is not supported")

type Cable struct{}

// Connect connects to the websocket, creates and starts client, and returns it to the js.
func (r *Cable) Connect(ctx context.Context, cableUrl string, opts goja.Value) (*Client, error) {
state := lib.GetState(ctx)
func (c *Cable) Connect(cableUrl string, opts goja.Value) (*Client, error) {
state := c.vu.State()
if state == nil {
return nil, errCableInInitContext
}

cOpts, err := parseOptions(ctx, opts)
cOpts, err := parseOptions(c.vu.Runtime(), opts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -79,7 +71,7 @@ func (r *Cable) Connect(ctx context.Context, cableUrl string, opts goja.Value) (

logger := state.Logger.WithField("source", "cable")

conn, httpResponse, connErr := wsd.DialContext(ctx, cableUrl, headers)
conn, httpResponse, connErr := wsd.DialContext(c.vu.Context(), cableUrl, headers)
connectionEnd := time.Now()

tags := cOpts.appendTags(state.CloneTags())
Expand All @@ -103,7 +95,7 @@ func (r *Cable) Connect(ctx context.Context, cableUrl string, opts goja.Value) (

sampleTags := stats.IntoSampleTags(&tags)

stats.PushIfNotDone(ctx, state.Samples, stats.ConnectedSamples{
stats.PushIfNotDone(c.vu.Context(), state.Samples, stats.ConnectedSamples{
Samples: []stats.Sample{
{Metric: state.BuiltinMetrics.WSSessions, Time: connectionStart, Tags: sampleTags, Value: 1},
{Metric: state.BuiltinMetrics.WSConnecting, Time: connectionStart, Tags: sampleTags, Value: stats.D(connectionEnd.Sub(connectionStart))},
Expand All @@ -118,7 +110,7 @@ func (r *Cable) Connect(ctx context.Context, cableUrl string, opts goja.Value) (
}

client := Client{
ctx: ctx,
vu: c.vu,
conn: conn,
codec: cOpts.codec(),
logger: logger,
Expand Down
9 changes: 4 additions & 5 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/dop251/goja"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/common"
)

type Channel struct {
Expand All @@ -24,7 +23,7 @@ type Channel struct {

// Perform sends passed action with additional data to the channel
func (ch *Channel) Perform(action string, attr goja.Value) error {
rt := common.GetRuntime(ch.client.ctx)
rt := ch.client.vu.Runtime()
obj := attr.ToObject(rt).Export().(map[string]interface{})
obj["action"] = action
data, err := json.Marshal(obj)
Expand Down Expand Up @@ -147,7 +146,7 @@ func (m *AttrMatcher) Match(msg interface{}) bool {

type PassthruMatcher struct{}

func (PassthruMatcher) Match(msg interface{}) bool {
func (PassthruMatcher) Match(_ interface{}) bool {
return true
}

Expand All @@ -168,11 +167,11 @@ func (ch *Channel) buildMatcher(cond goja.Value) (Matcher, error) {
userFunc, isFunc := goja.AssertFunction(cond)

if isFunc {
return &FuncMatcher{common.GetRuntime(ch.client.ctx), userFunc}, nil
return &FuncMatcher{ch.client.vu.Runtime(), userFunc}, nil
}

// we need to pass object through json unmarshalling to use same types for numbers
jsonAttr, err := cond.ToObject(common.GetRuntime(ch.client.ctx)).MarshalJSON()
jsonAttr, err := cond.ToObject(ch.client.vu.Runtime()).MarshalJSON()
if err != nil {
return nil, err
}
Expand Down
20 changes: 9 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package cable

import (
"context"
"encoding/json"
"go.k6.io/k6/js/modules"
"sync"
"time"

"go.k6.io/k6/js/common"
"go.k6.io/k6/lib"
"go.k6.io/k6/stats"

"github.com/dop251/goja"
Expand All @@ -24,7 +22,7 @@ type cableMsg struct {
}

type Client struct {
ctx context.Context
vu modules.VU
codec *Codec
conn *websocket.Conn
channels map[string]*Channel
Expand Down Expand Up @@ -98,17 +96,17 @@ func (c *Client) Subscribe(channelName string, paramsIn goja.Value) (*Channel, e
}

func (c *Client) Disconnect() {
c.conn.Close()
_ = c.conn.Close()
}

func (c *Client) send(msg *cableMsg) error {
state := lib.GetState(c.ctx)
state := c.vu.State()
if state == nil {
return errCableInInitContext
}

err := c.codec.Send(c.conn, msg)
stats.PushIfNotDone(c.ctx, c.samplesOutput, stats.Sample{
stats.PushIfNotDone(c.vu.Context(), c.samplesOutput, stats.Sample{
Metric: state.BuiltinMetrics.WSMessagesSent,
Time: time.Now(),
Tags: c.sampleTags,
Expand Down Expand Up @@ -143,7 +141,7 @@ func (c *Client) handleLoop() {
c.logger.Errorf("websocket error: %v", err)
continue
case <-c.closeCh:
case <-c.ctx.Done():
case <-c.vu.Context().Done():
_ = c.conn.Close()
c.logger.Debugln("connection closed")
return
Expand Down Expand Up @@ -179,11 +177,11 @@ func (c *Client) receiveLoop() {

select {
case c.readCh <- obj:
state := lib.GetState(c.ctx)
state := c.vu.State()
if state == nil {
continue
}
stats.PushIfNotDone(c.ctx, c.samplesOutput, stats.Sample{
stats.PushIfNotDone(c.vu.Context(), c.samplesOutput, stats.Sample{
Metric: state.BuiltinMetrics.WSMessagesReceived,
Time: time.Now(),
Tags: c.sampleTags,
Expand Down Expand Up @@ -231,7 +229,7 @@ func (c *Client) parseParams(in goja.Value) (map[string]interface{}, error) {
return params, nil
}

rt := common.GetRuntime(c.ctx)
rt := c.vu.Runtime()
data := in.ToObject(rt).Export().(map[string]interface{})

return data, nil
Expand Down
5 changes: 1 addition & 4 deletions connect_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package cable

import (
"bytes"
"context"
"encoding/json"
"net/http"
"time"

"github.com/dop251/goja"
"go.k6.io/k6/js/common"
)

type connectOptions struct {
Expand All @@ -27,14 +25,13 @@ const (
defaultReceiveTimeout = 1000
)

func parseOptions(ctx context.Context, inOpts goja.Value) (*connectOptions, error) {
func parseOptions(rt *goja.Runtime, inOpts goja.Value) (*connectOptions, error) {
var outOpts connectOptions

if inOpts == nil || goja.IsUndefined(inOpts) || goja.IsNull(inOpts) {
return &outOpts, nil
}

rt := common.GetRuntime(ctx)
data, err := json.Marshal(inOpts.ToObject(rt).Export())
if err != nil {
return nil, err
Expand Down
22 changes: 4 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,29 @@ module github.com/anycable/xk6-cable
go 1.17

require (
github.com/dop251/goja v0.0.0-20211121151210-cc28e89bfc81
github.com/dop251/goja v0.0.0-20220124171016-cfb079cdc7b4
github.com/golang/protobuf v1.5.2
github.com/gorilla/websocket v1.4.2
github.com/sirupsen/logrus v1.8.1
github.com/vmihailenco/msgpack/v5 v5.3.5
go.k6.io/k6 v0.35.0
go.k6.io/k6 v0.37.0
)

require (
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c // indirect
github.com/PuerkitoBio/goquery v1.6.1 // indirect
github.com/Soontao/goHttpDigestClient v0.0.0-20170320082612-6d28bb1415c5 // indirect
github.com/andybalholm/brotli v1.0.3 // indirect
github.com/andybalholm/cascadia v1.1.0 // indirect
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/kubernetes/helm v2.9.0+incompatible // indirect
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/tidwall/gjson v1.10.2 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e // indirect
golang.org/x/net v0.0.0-20211101194204-95aca89e93de // indirect
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 // indirect
golang.org/x/text v0.3.7-0.20210503195748-5c7c50ebbd4f // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/guregu/null.v3 v3.3.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 84961e7

Please sign in to comment.