Fix "PR c++/92804 ICE trying to use concept as a nested-name-specifier"
[official-gcc.git] / libgo / go / net / write_unix_test.go
blob6d8cb6a6f81666443e4fba43484573401f519e68
1 // Copyright 2017 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.
5 // +build darwin dragonfly freebsd linux netbsd openbsd solaris
7 package net
9 import (
10 "bytes"
11 "syscall"
12 "testing"
13 "time"
16 // Test that a client can't trigger an endless loop of write system
17 // calls on the server by shutting down the write side on the client.
18 // Possibility raised in the discussion of https://golang.org/cl/71973.
19 func TestEndlessWrite(t *testing.T) {
20 t.Parallel()
21 c := make(chan bool)
22 server := func(cs *TCPConn) error {
23 cs.CloseWrite()
24 <-c
25 return nil
27 client := func(ss *TCPConn) error {
28 // Tell the server to return when we return.
29 defer close(c)
31 // Loop writing to the server. The server is not reading
32 // anything, so this will eventually block, and then time out.
33 b := bytes.Repeat([]byte{'a'}, 8192)
34 cagain := 0
35 for {
36 n, err := ss.conn.fd.pfd.WriteOnce(b)
37 if n > 0 {
38 cagain = 0
40 switch err {
41 case nil:
42 case syscall.EAGAIN:
43 if cagain == 0 {
44 // We've written enough data to
45 // start blocking. Set a deadline
46 // so that we will stop.
47 ss.SetWriteDeadline(time.Now().Add(5 * time.Millisecond))
49 cagain++
50 if cagain > 20 {
51 t.Error("looping on EAGAIN")
52 return nil
54 if err = ss.conn.fd.pfd.WaitWrite(); err != nil {
55 t.Logf("client WaitWrite: %v", err)
56 return nil
58 default:
59 // We expect to eventually get an error.
60 t.Logf("client WriteOnce: %v", err)
61 return nil
65 withTCPConnPair(t, client, server)