|
| 1 | +// Copyright 2023 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package wasi_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "bufio" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "math/rand" |
| 12 | + "os" |
| 13 | + "os/exec" |
| 14 | + "path/filepath" |
| 15 | + "syscall" |
| 16 | + "testing" |
| 17 | +) |
| 18 | + |
| 19 | +// This test creates a set of FIFOs and writes to them in reverse order. It |
| 20 | +// checks that the output order matches the write order. The test binary opens |
| 21 | +// the FIFOs in their original order and spawns a goroutine for each that reads |
| 22 | +// from the FIFO and writes the result to stderr. If I/O was blocking, all |
| 23 | +// goroutines would be blocked waiting for one read call to return, and the |
| 24 | +// output order wouldn't match. |
| 25 | + |
| 26 | +type fifo struct { |
| 27 | + file *os.File |
| 28 | + path string |
| 29 | +} |
| 30 | + |
| 31 | +func TestNonblock(t *testing.T) { |
| 32 | + if target != "wasip1/wasm" { |
| 33 | + t.Skip() |
| 34 | + } |
| 35 | + |
| 36 | + switch os.Getenv("GOWASIRUNTIME") { |
| 37 | + case "wazero", "": |
| 38 | + t.Skip("wazero does not support non-blocking I/O") |
| 39 | + case "wasmer": |
| 40 | + t.Skip("wasmer does not support non-blocking I/O") |
| 41 | + } |
| 42 | + |
| 43 | + args := []string{"run", "./testdata/nonblock.go"} |
| 44 | + |
| 45 | + fifos := make([]*fifo, 8) |
| 46 | + for i := range fifos { |
| 47 | + path := filepath.Join(t.TempDir(), fmt.Sprintf("wasip1-nonblock-fifo-%d-%d", rand.Uint32(), i)) |
| 48 | + if err := syscall.Mkfifo(path, 0666); err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + |
| 52 | + file, err := os.OpenFile(path, os.O_RDWR, 0) |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + defer file.Close() |
| 57 | + |
| 58 | + args = append(args, path) |
| 59 | + fifos[len(fifos)-i-1] = &fifo{file, path} |
| 60 | + } |
| 61 | + |
| 62 | + subProcess := exec.Command("go", args...) |
| 63 | + |
| 64 | + subProcess.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm") |
| 65 | + |
| 66 | + pr, pw := io.Pipe() |
| 67 | + defer pw.Close() |
| 68 | + |
| 69 | + subProcess.Stderr = pw |
| 70 | + |
| 71 | + if err := subProcess.Start(); err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + scanner := bufio.NewScanner(pr) |
| 76 | + if !scanner.Scan() { |
| 77 | + t.Fatal("expected line:", scanner.Err()) |
| 78 | + } else if scanner.Text() != "waiting" { |
| 79 | + t.Fatal("unexpected output:", scanner.Text()) |
| 80 | + } |
| 81 | + |
| 82 | + for _, fifo := range fifos { |
| 83 | + if _, err := fifo.file.WriteString(fifo.path + "\n"); err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + if !scanner.Scan() { |
| 87 | + t.Fatal("expected line:", scanner.Err()) |
| 88 | + } else if scanner.Text() != fifo.path { |
| 89 | + t.Fatal("unexpected line:", scanner.Text()) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if err := subProcess.Wait(); err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | +} |
0 commit comments