Files
wazero/internal/platform/fdset_test.go
Edoardo Vacchi ea336061c2
Some checks failed
Release CLI / Pre-release build (push) Has been cancelled
Release CLI / Pre-release test (macos-12) (push) Has been cancelled
Release CLI / Pre-release test (ubuntu-22.04) (push) Has been cancelled
Release CLI / Pre-release test (windows-2022) (push) Has been cancelled
Release CLI / Release (push) Has been cancelled
wasi: introduce platform.Select and use it for poll_oneoff (#1346)
The PR introduces the `platform.Select()` API, wrapping `select(2)` on POSIX and emulated in some cases on Windows. RATIONALE.md contains a full explanation of the approach followed in `poll_oneoff` to handle Stdin and the other types of file descriptors, and the clock subscriptions.

It also introduces an abstraction (`StdioFilePoller`) to allow the simulation of different scenarios (waiting for input, input ready, timeout expired, etc.) when unit-testing interactive input.

This closes #1317.

Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
2023-04-18 16:31:34 +02:00

110 lines
2.1 KiB
Go

package platform
import (
"runtime"
"testing"
"github.com/tetratelabs/wazero/internal/testing/require"
)
func TestFdSet(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skip("not supported")
}
allBitsSetAtIndex0 := FdSet{}
allBitsSetAtIndex0.Bits[0] = -1
tests := []struct {
name string
init FdSet
exec func(fdSet *FdSet)
expected FdSet
}{
{
name: "all bits set",
exec: func(fdSet *FdSet) {
for fd := 0; fd < nfdbits; fd++ {
fdSet.Set(fd)
}
},
expected: allBitsSetAtIndex0,
},
{
name: "all bits cleared",
init: allBitsSetAtIndex0,
exec: func(fdSet *FdSet) {
for fd := 0; fd < nfdbits; fd++ {
fdSet.Clear(fd)
}
},
expected: FdSet{},
},
{
name: "zero should clear all bits",
init: allBitsSetAtIndex0,
exec: func(fdSet *FdSet) {
fdSet.Zero()
},
expected: FdSet{},
},
{
name: "is-set should return true for all bits",
init: allBitsSetAtIndex0,
exec: func(fdSet *FdSet) {
for i := range fdSet.Bits {
require.True(t, fdSet.IsSet(i))
}
},
expected: allBitsSetAtIndex0,
},
{
name: "is-set should return true for all odd bits",
init: FdSet{},
exec: func(fdSet *FdSet) {
for fd := 1; fd < nfdbits; fd += 2 {
fdSet.Set(fd)
}
for fd := 0; fd < nfdbits; fd++ {
isSet := fdSet.IsSet(fd)
if fd&0x1 == 0x1 {
require.True(t, isSet)
} else {
require.False(t, isSet)
}
}
fdSet.Zero()
},
expected: FdSet{},
},
{
name: "should clear all even bits",
init: allBitsSetAtIndex0,
exec: func(fdSet *FdSet) {
for fd := 0; fd < nfdbits; fd += 2 {
fdSet.Clear(fd)
}
for fd := 0; fd < nfdbits; fd++ {
isSet := fdSet.IsSet(fd)
if fd&0x1 == 0x1 {
require.True(t, isSet)
} else {
require.False(t, isSet)
}
}
fdSet.Zero()
},
expected: FdSet{},
},
}
for _, tt := range tests {
tc := tt
t.Run(tc.name, func(t *testing.T) {
x := tc.init
tc.exec(&x)
require.Equal(t, tc.expected, x)
})
}
}