1
0
mirror of https://github.com/golang/go synced 2024-11-24 13:30:09 -07:00
go/src/os/fifo_test.go
Russ Cox f229e7031a all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor)
When these packages are released as part of Go 1.18,
Go 1.16 will no longer be supported, so we can remove
the +build tags in these files.

Ran go fix -fix=buildtag std cmd and then reverted the bootstrapDirs
as defined in src/cmd/dist/buildtool.go, which need to continue
to build with Go 1.4 for now.

Also reverted src/vendor and src/cmd/vendor, which will need
to be updated in their own repos first.

Manual changes in runtime/pprof/mprof_test.go to adjust line numbers.

For #41184.

Change-Id: Ic0f93f7091295b6abc76ed5cd6e6746e1280861e
Reviewed-on: https://go-review.googlesource.com/c/go/+/344955
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-10-28 18:17:57 +00:00

104 lines
1.8 KiB
Go

// Copyright 2015 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.
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
package os_test
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sync"
"syscall"
"testing"
"time"
)
// Issue 24164.
func TestFifoEOF(t *testing.T) {
switch runtime.GOOS {
case "android":
t.Skip("skipping on Android; mkfifo syscall not available")
}
dir := t.TempDir()
fifoName := filepath.Join(dir, "fifo")
if err := syscall.Mkfifo(fifoName, 0600); err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
w, err := os.OpenFile(fifoName, os.O_WRONLY, 0)
if err != nil {
t.Error(err)
return
}
defer func() {
if err := w.Close(); err != nil {
t.Errorf("error closing writer: %v", err)
}
}()
for i := 0; i < 3; i++ {
time.Sleep(10 * time.Millisecond)
_, err := fmt.Fprintf(w, "line %d\n", i)
if err != nil {
t.Errorf("error writing to fifo: %v", err)
return
}
}
time.Sleep(10 * time.Millisecond)
}()
defer wg.Wait()
r, err := os.Open(fifoName)
if err != nil {
t.Fatal(err)
}
done := make(chan bool)
go func() {
defer close(done)
defer func() {
if err := r.Close(); err != nil {
t.Errorf("error closing reader: %v", err)
}
}()
rbuf := bufio.NewReader(r)
for {
b, err := rbuf.ReadBytes('\n')
if err == io.EOF {
break
}
if err != nil {
t.Error(err)
return
}
t.Logf("%s\n", bytes.TrimSpace(b))
}
}()
select {
case <-done:
// Test succeeded.
case <-time.After(time.Second):
t.Error("timed out waiting for read")
// Close the reader to force the read to complete.
r.Close()
}
}