Skip to content

Commit

Permalink
Drop internal 'modules' package
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 9, 2021
1 parent 2f13438 commit 28e4ba2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 90 deletions.
4 changes: 2 additions & 2 deletions js/init_and_modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

"github.com/loadimpact/k6/js"
"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/js/internal/modules"
"github.com/loadimpact/k6/js/modules"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/testutils"
"github.com/loadimpact/k6/loader"
Expand Down Expand Up @@ -66,7 +66,7 @@ var uniqueModuleNumber int64 //nolint:gochecknoglobals
func TestNewJSRunnerWithCustomModule(t *testing.T) {
t.Parallel()
checkModule := &CheckModule{t: t}
moduleName := fmt.Sprintf("k6/check-%d", atomic.AddInt64(&uniqueModuleNumber, 1))
moduleName := fmt.Sprintf("k6/x/check-%d", atomic.AddInt64(&uniqueModuleNumber, 1))
modules.Register(moduleName, checkModule)

script := fmt.Sprintf(`
Expand Down
2 changes: 1 addition & 1 deletion js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/js/compiler"
"github.com/loadimpact/k6/js/internal/modules"
"github.com/loadimpact/k6/js/modules"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/loader"
)
Expand Down
85 changes: 0 additions & 85 deletions js/internal/modules/modules.go

This file was deleted.

59 changes: 57 additions & 2 deletions js/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,28 @@ package modules
import (
"fmt"
"strings"
"sync"

"github.com/loadimpact/k6/js/internal/modules"
"github.com/loadimpact/k6/js/modules/k6"
"github.com/loadimpact/k6/js/modules/k6/crypto"
"github.com/loadimpact/k6/js/modules/k6/crypto/x509"
"github.com/loadimpact/k6/js/modules/k6/data"
"github.com/loadimpact/k6/js/modules/k6/encoding"
"github.com/loadimpact/k6/js/modules/k6/grpc"
"github.com/loadimpact/k6/js/modules/k6/html"
"github.com/loadimpact/k6/js/modules/k6/http"
"github.com/loadimpact/k6/js/modules/k6/metrics"
"github.com/loadimpact/k6/js/modules/k6/ws"
)

const extPrefix string = "k6/x/"

//nolint:gochecknoglobals
var (
modules = make(map[string]interface{})
mx sync.RWMutex
)

// Register the given mod as an external JavaScript module that can be imported
// by name. The name must be unique across all registered modules and must be
// prefixed with "k6/x/", otherwise this function will panic.
Expand All @@ -37,5 +53,44 @@ func Register(name string, mod interface{}) {
panic(fmt.Errorf("external module names must be prefixed with '%s', tried to register: %s", extPrefix, name))
}

modules.Register(name, mod)
mx.Lock()
defer mx.Unlock()

if _, ok := modules[name]; ok {
panic(fmt.Sprintf("module already registered: %s", name))
}
modules[name] = mod
}

// HasModuleInstancePerVU should be implemented by all native Golang modules that
// would require per-VU state. k6 will call their NewModuleInstancePerVU() methods
// every time a VU imports the module and use its result as the returned object.
type HasModuleInstancePerVU interface {
NewModuleInstancePerVU() interface{}
}

// checks that modules implement HasModuleInstancePerVU
// this is done here as otherwise there will be a loop if the module imports this package
var _ HasModuleInstancePerVU = http.New()

// GetJSModules returns a map of all js modules
func GetJSModules() map[string]interface{} {
result := map[string]interface{}{
"k6": k6.New(),
"k6/crypto": crypto.New(),
"k6/crypto/x509": x509.New(),
"k6/data": data.New(),
"k6/encoding": encoding.New(),
"k6/net/grpc": grpc.New(),
"k6/html": html.New(),
"k6/http": http.New(),
"k6/metrics": metrics.New(),
"k6/ws": ws.New(),
}

for name, module := range modules {
result[name] = module
}

return result
}

0 comments on commit 28e4ba2

Please sign in to comment.