summaryrefslogtreecommitdiffstats
path: root/src/runtime/internal/wasitest/testdata/nonblock.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
commitf6ad4dcef54c5ce997a4bad5a6d86de229015700 (patch)
tree7cfa4e31ace5c2bd95c72b154d15af494b2bcbef /src/runtime/internal/wasitest/testdata/nonblock.go
parentInitial commit. (diff)
downloadgolang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.tar.xz
golang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.zip
Adding upstream version 1.22.1.upstream/1.22.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/runtime/internal/wasitest/testdata/nonblock.go')
-rw-r--r--src/runtime/internal/wasitest/testdata/nonblock.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/runtime/internal/wasitest/testdata/nonblock.go b/src/runtime/internal/wasitest/testdata/nonblock.go
new file mode 100644
index 0000000..8cbf21b
--- /dev/null
+++ b/src/runtime/internal/wasitest/testdata/nonblock.go
@@ -0,0 +1,65 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "os"
+ "sync"
+ "syscall"
+)
+
+func main() {
+ if len(os.Args) < 2 {
+ panic("usage: nonblock <MODE> [PATH...]")
+ }
+ mode := os.Args[1]
+
+ ready := make(chan struct{})
+
+ var wg sync.WaitGroup
+ for _, path := range os.Args[2:] {
+ f, err := os.Open(path)
+ if err != nil {
+ panic(err)
+ }
+ switch mode {
+ case "os.OpenFile":
+ case "os.NewFile":
+ fd := f.Fd()
+ if err := syscall.SetNonblock(int(fd), true); err != nil {
+ panic(err)
+ }
+ f = os.NewFile(fd, path)
+ default:
+ panic("invalid test mode")
+ }
+
+ spawnWait := make(chan struct{})
+
+ wg.Add(1)
+ go func(f *os.File) {
+ defer f.Close()
+ defer wg.Done()
+
+ close(spawnWait)
+
+ <-ready
+
+ var buf [256]byte
+ n, err := f.Read(buf[:])
+ if err != nil {
+ panic(err)
+ }
+ os.Stderr.Write(buf[:n])
+ }(f)
+
+ // Spawn one goroutine at a time.
+ <-spawnWait
+ }
+
+ println("waiting")
+ close(ready)
+ wg.Wait()
+}