Files
wazero/internal/logging/logging_test.go
Crypt Keeper da99a7f5c0 logging: adds exit scope and fixes mtim bug (#1074)
This allows you to specify the exit scope amongst existing logging scopes, both in API and the CLI.

e.g for the CLI.
```bash
$ wazero run --hostlogging=exit,filesystem --mount=.:/:ro cat.wasm
```

e.g. for Go
```go
loggingCtx := context.WithValue(testCtx, experimental.FunctionListenerFactoryKey{},
	logging.NewHostLoggingListenerFactory(&log, logging.LogScopeExit|logging.LogScopeFilesystem))
```

This is helpful to know if the wasm called exit or if it exited
implicitly. This is one of the few host functions that exists in three
places: assemblyscript, gojs and wasi.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-01-29 08:43:14 +02:00

70 lines
1.6 KiB
Go

package logging
import (
"fmt"
"testing"
"github.com/tetratelabs/wazero/internal/testing/require"
)
// TestLogScopes tests the bitset works as expected
func TestLogScopes(t *testing.T) {
tests := []struct {
name string
scopes LogScopes
}{
{
name: "one is the smallest flag",
scopes: 1,
},
{
name: "63 is the largest feature flag", // because uint64
scopes: 1 << 2,
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
f := LogScopes(0)
// Defaults to false
require.False(t, f.IsEnabled(tc.scopes))
// Set true makes it true
f = f | tc.scopes
require.True(t, f.IsEnabled(tc.scopes))
// Set false makes it false again
f = f ^ tc.scopes
require.False(t, f.IsEnabled(tc.scopes))
})
}
}
func TestLogScopes_String(t *testing.T) {
tests := []struct {
name string
scopes LogScopes
expected string
}{
{name: "none", scopes: LogScopeNone, expected: ""},
{name: "any", scopes: LogScopeAll, expected: "all"},
{name: "clock", scopes: LogScopeClock, expected: "clock"},
{name: "exit", scopes: LogScopeExit, expected: "exit"},
{name: "filesystem", scopes: LogScopeFilesystem, expected: "filesystem"},
{name: "poll", scopes: LogScopePoll, expected: "poll"},
{name: "random", scopes: LogScopeRandom, expected: "random"},
{name: "filesystem|random", scopes: LogScopeFilesystem | LogScopeRandom, expected: "filesystem|random"},
{name: "undefined", scopes: 1 << 14, expected: fmt.Sprintf("<unknown=%d>", 1<<14)},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expected, tc.scopes.String())
})
}
}