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

Change to using sobek instead of goja #31

Merged
merged 1 commit into from
Jul 9, 2024
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
54 changes: 27 additions & 27 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"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 @@
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 @@ -61,10 +61,10 @@
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 @@ -98,10 +98,10 @@
}

//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 @@ -113,50 +113,50 @@
}
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)

Check failure on line 120 in client.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 2, in <argument> detected (mnd)
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)

Check failure on line 125 in client.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 3, in <argument> detected (mnd)
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)

Check failure on line 131 in client.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 4, in <argument> detected (mnd)
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)

Check failure on line 137 in client.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 5, in <argument> detected (mnd)
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) {

Check failure on line 144 in client.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 6, in <argument> detected (mnd)
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) {

Check failure on line 149 in client.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 7, in <argument> detected (mnd)
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 154 in client.go

View workflow job for this annotation

GitHub Actions / lint

line is 121 characters (lll)
clientConf.clientCertKeyPath = ""
} else {
clientConf.clientCertKeyPath = clientCertKeyPathValue.String()
}
labels := getLabels(c.Argument(9), rt)

Check failure on line 159 in client.go

View workflow job for this annotation

GitHub Actions / lint

Magic number: 9, in <argument> detected (mnd)
metrics, err := registerMetrics(m.vu, labels)
if err != nil {
common.Throw(m.vu.Runtime(), err)
Expand All @@ -175,20 +175,20 @@

// 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 @@ -278,7 +278,7 @@
// 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 @@ -287,7 +287,7 @@
}
}

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
Loading