Skip to content

Commit

Permalink
Change to using sobek instead of goja
Browse files Browse the repository at this point in the history
We are moving to a fork of goja under grafana org called sobek.

More info in:
- grafana/k6#3772
- grafana/k6#3773
  • Loading branch information
mstoykov authored and pmalhaire committed Jul 9, 2024
1 parent f88a6fe commit 54b2270
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 152 deletions.
54 changes: 27 additions & 27 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"os"
"time"

"github.com/dop251/goja"
paho "github.com/eclipse/paho.mqtt.golang"
"github.com/grafana/sobek"
"github.com/mstoykov/k6-taskqueue-lib/taskqueue"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
Expand All @@ -21,13 +21,13 @@ type client struct {
metrics *mqttMetrics
conf conf
pahoClient paho.Client
obj *goja.Object // the object that is given to js to interact with the WebSocket
obj *sobek.Object // the object that is given to js to interact with the WebSocket

// listeners
// this return goja.value *and* error in order to return error on exception instead of panic
// this return sobek.value *and* error in order to return error on exception instead of panic
// https://pkg.go.dev/github.com/dop251/goja#hdr-Functions
messageListener func(goja.Value) (goja.Value, error)
errorListener func(goja.Value) (goja.Value, error)
messageListener func(sobek.Value) (sobek.Value, error)
errorListener func(sobek.Value) (sobek.Value, error)
tq *taskqueue.TaskQueue
messageChan chan paho.Message
subRefCount int
Expand Down Expand Up @@ -63,10 +63,10 @@ const (
receivedMessagesCountLabel = "mqtt_received_messages_count"
)

func getLabels(labelsArg goja.Value, rt *goja.Runtime) mqttMetricsLabels {
func getLabels(labelsArg sobek.Value, rt *sobek.Runtime) mqttMetricsLabels {
labels := mqttMetricsLabels{}
metricsLabels := labelsArg
if metricsLabels == nil || goja.IsUndefined(metricsLabels) {
if metricsLabels == nil || sobek.IsUndefined(metricsLabels) {
// set default values
labels.SentBytesLabel = sentBytesLabel
labels.ReceivedBytesLabel = receivedBytesLabel
Expand Down Expand Up @@ -100,10 +100,10 @@ func getLabels(labelsArg goja.Value, rt *goja.Runtime) mqttMetricsLabels {
}

//nolint:nosnakecase // their choice not mine
func (m *MqttAPI) client(c goja.ConstructorCall) *goja.Object {
func (m *MqttAPI) client(c sobek.ConstructorCall) *sobek.Object {
serversArray := c.Argument(0)
rt := m.vu.Runtime()
if serversArray == nil || goja.IsUndefined(serversArray) {
if serversArray == nil || sobek.IsUndefined(serversArray) {
common.Throw(rt, errors.New("Client requires a server list"))
}
var servers []string
Expand All @@ -115,45 +115,45 @@ func (m *MqttAPI) client(c goja.ConstructorCall) *goja.Object {
}
clientConf.servers = servers
userValue := c.Argument(1)
if userValue == nil || goja.IsUndefined(userValue) {
if userValue == nil || sobek.IsUndefined(userValue) {
common.Throw(rt, errors.New("Client requires a user value"))
}
clientConf.user = userValue.String()
passwordValue := c.Argument(2)
if userValue == nil || goja.IsUndefined(passwordValue) {
if userValue == nil || sobek.IsUndefined(passwordValue) {
common.Throw(rt, errors.New("Client requires a password value"))
}
clientConf.password = passwordValue.String()
cleansessValue := c.Argument(3)
if cleansessValue == nil || goja.IsUndefined(cleansessValue) {
if cleansessValue == nil || sobek.IsUndefined(cleansessValue) {
common.Throw(rt, errors.New("Client requires a cleansess value"))
}
clientConf.cleansess = cleansessValue.ToBoolean()

clientIDValue := c.Argument(4)
if clientIDValue == nil || goja.IsUndefined(clientIDValue) {
if clientIDValue == nil || sobek.IsUndefined(clientIDValue) {
common.Throw(rt, errors.New("Client requires a clientID value"))
}
clientConf.clientid = clientIDValue.String()

timeoutValue := c.Argument(5)
if timeoutValue == nil || goja.IsUndefined(timeoutValue) {
if timeoutValue == nil || sobek.IsUndefined(timeoutValue) {
common.Throw(rt, errors.New("Client requires a timeout value"))
}
clientConf.timeout = uint(timeoutValue.ToInteger())

// optional args
if caRootPathValue := c.Argument(6); caRootPathValue == nil || goja.IsUndefined(caRootPathValue) {
if caRootPathValue := c.Argument(6); caRootPathValue == nil || sobek.IsUndefined(caRootPathValue) {
clientConf.caRootPath = ""
} else {
clientConf.caRootPath = caRootPathValue.String()
}
if clientCertPathValue := c.Argument(7); clientCertPathValue == nil || goja.IsUndefined(clientCertPathValue) {
if clientCertPathValue := c.Argument(7); clientCertPathValue == nil || sobek.IsUndefined(clientCertPathValue) {
clientConf.clientCertPath = ""
} else {
clientConf.clientCertPath = clientCertPathValue.String()
}
if clientCertKeyPathValue := c.Argument(8); clientCertKeyPathValue == nil || goja.IsUndefined(clientCertKeyPathValue) {
if clientCertKeyPathValue := c.Argument(8); clientCertKeyPathValue == nil || sobek.IsUndefined(clientCertKeyPathValue) {

Check failure on line 156 in client.go

View workflow job for this annotation

GitHub Actions / lint

line is 121 characters (lll)
clientConf.clientCertKeyPath = ""
} else {
clientConf.clientCertKeyPath = clientCertKeyPathValue.String()
Expand Down Expand Up @@ -181,20 +181,20 @@ func (m *MqttAPI) client(c goja.ConstructorCall) *goja.Object {

// TODO add onmessage,onclose and so on
must(client.obj.DefineDataProperty(
"addEventListener", rt.ToValue(client.AddEventListener), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
"addEventListener", rt.ToValue(client.AddEventListener), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
must(client.obj.DefineDataProperty(
"subContinue", rt.ToValue(client.SubContinue), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
"subContinue", rt.ToValue(client.SubContinue), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
must(client.obj.DefineDataProperty(
"connect", rt.ToValue(client.Connect), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
"connect", rt.ToValue(client.Connect), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
must(client.obj.DefineDataProperty(
"isConnected", rt.ToValue(client.IsConnected), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
"isConnected", rt.ToValue(client.IsConnected), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
must(client.obj.DefineDataProperty(
"publish", rt.ToValue(client.Publish), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
"publish", rt.ToValue(client.Publish), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
must(client.obj.DefineDataProperty(
"subscribe", rt.ToValue(client.Subscribe), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
"subscribe", rt.ToValue(client.Subscribe), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))

must(client.obj.DefineDataProperty(
"close", rt.ToValue(client.Close), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
"close", rt.ToValue(client.Close), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))

return client.obj
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func (c *client) IsConnected() bool {
// error event for async
//
//nolint:nosnakecase // their choice not mine
func (c *client) newErrorEvent(msg string) *goja.Object {
func (c *client) newErrorEvent(msg string) *sobek.Object {
rt := c.vu.Runtime()
o := rt.NewObject()
must := func(err error) {
Expand All @@ -301,7 +301,7 @@ func (c *client) newErrorEvent(msg string) *goja.Object {
}
}

must(o.DefineDataProperty("type", rt.ToValue("error"), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
must(o.DefineDataProperty("message", rt.ToValue(msg), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
must(o.DefineDataProperty("type", rt.ToValue("error"), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
must(o.DefineDataProperty("message", rt.ToValue(msg), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
return o
}
47 changes: 24 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
module github.com/pmalhaire/xk6-mqtt

go 1.21
go 1.20

require (
github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/grafana/sobek v0.0.0-20240607083612-4f0cd64f4e78
github.com/mstoykov/k6-taskqueue-lib v0.1.0
go.k6.io/k6 v0.49.0
go.k6.io/k6 v0.51.1-0.20240610082146-1f01a9bc2365
)

require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/dop251/goja v0.0.0-20240516125602-ccbae20bcec2 // indirect
github.com/evanw/esbuild v0.21.2 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sourcemap/sourcemap v2.1.4-0.20211119122758-180fcef48034+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // indirect
github.com/google/pprof v0.0.0-20231101202521-4ca4178f5c7a // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -30,22 +31,22 @@ require (
github.com/serenize/snaker v0.0.0-20201027110005-a7ad2135616e // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/afero v1.10.0 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.21.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect
google.golang.org/grpc v1.60.0 // indirect
google.golang.org/protobuf v1.31.1-0.20231027082548-f4a6c1f6e5c1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/guregu/null.v3 v3.5.0 // indirect
)
Loading

0 comments on commit 54b2270

Please sign in to comment.