Accept nil ctx in NewRuntime (#752)

Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
This commit is contained in:
Anuraag Agrawal
2022-08-19 12:35:15 +09:00
committed by GitHub
parent 7f8e629c65
commit b93fd89637
2 changed files with 31 additions and 9 deletions

View File

@@ -127,6 +127,9 @@ func NewRuntime(ctx context.Context) Runtime {
// NewRuntimeWithConfig returns a runtime with the given configuration.
func NewRuntimeWithConfig(ctx context.Context, rConfig RuntimeConfig) Runtime {
if ctx == nil {
ctx = context.Background()
}
if v := ctx.Value(version.WazeroVersionKey{}); v == nil {
ctx = context.WithValue(ctx, version.WazeroVersionKey{}, wazeroVersion)
}

View File

@@ -22,6 +22,23 @@ var (
)
func TestNewRuntimeWithConfig_version(t *testing.T) {
// Make sure nil ctx doesn't panic
tests := []struct {
name string
ctx context.Context
}{
{
name: "not nil",
ctx: testCtx,
},
{
name: "nil",
ctx: nil,
},
}
for _, tc := range tests {
tt := tc
t.Run(tt.name, func(t *testing.T) {
cfg := NewRuntimeConfig().(*runtimeConfig)
oldNewEngine := cfg.newEngine
cfg.newEngine = func(ctx context.Context, features wasm.Features) wasm.Engine {
@@ -31,7 +48,9 @@ func TestNewRuntimeWithConfig_version(t *testing.T) {
require.Equal(t, wazeroVersion, v.(string))
return oldNewEngine(ctx, features)
}
_ = NewRuntimeWithConfig(testCtx, cfg)
_ = NewRuntimeWithConfig(tt.ctx, cfg)
})
}
}
func TestRuntime_CompileModule(t *testing.T) {