Files
wazero/internal/platform/sync_test.go
Crypt Keeper aecb8e9cdb Implements fd_datasync in WASI and sync in GOOS=js (#1128)
This implements `fd_datasync` in WASI, falling back to normal
`File.Sync` when unsupported. This also backfills missing usage of sync
in GOOS=js. Finally, this updates the WASI status chart based on what's
implemented.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-02-15 12:48:48 -10:00

45 lines
1.0 KiB
Go

package platform
import (
"bytes"
"io"
"os"
"syscall"
"testing"
"github.com/tetratelabs/wazero/internal/testing/require"
)
// Test_Fdatasync doesn't guarantee sync works because the operating system may
// sync anyway. There is no test in Go for syscall.Fdatasync, but closest is
// similar to below. Effectively, this only tests that things don't error.
func Test_Fdatasync(t *testing.T) {
f, err := os.CreateTemp("", t.Name())
require.NoError(t, err)
defer f.Close()
expected := "hello world!"
// Write the expected data
_, err = f.Write([]byte(expected))
require.NoError(t, err)
// Sync the data.
if err = Fdatasync(f.Fd()); err == syscall.ENOSYS {
return // don't continue if it isn't supported.
}
require.NoError(t, err)
// Rewind while the file is still open.
_, err = f.Seek(0, io.SeekStart)
require.NoError(t, err)
// Read data from the file
var buf bytes.Buffer
_, err = io.Copy(&buf, f)
require.NoError(t, err)
// It may be the case that sync worked.
require.Equal(t, expected, buf.String())
}