...
Run Format

Source file src/runtime/race/testdata/io_test.go

Documentation: runtime/race/testdata

     1  // Copyright 2012 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 race_test
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"net"
    11  	"net/http"
    12  	"os"
    13  	"path/filepath"
    14  	"sync"
    15  	"testing"
    16  	"time"
    17  )
    18  
    19  func TestNoRaceIOFile(t *testing.T) {
    20  	x := 0
    21  	path, _ := ioutil.TempDir("", "race_test")
    22  	fname := filepath.Join(path, "data")
    23  	go func() {
    24  		x = 42
    25  		f, _ := os.Create(fname)
    26  		f.Write([]byte("done"))
    27  		f.Close()
    28  	}()
    29  	for {
    30  		f, err := os.Open(fname)
    31  		if err != nil {
    32  			time.Sleep(1e6)
    33  			continue
    34  		}
    35  		buf := make([]byte, 100)
    36  		count, err := f.Read(buf)
    37  		if count == 0 {
    38  			time.Sleep(1e6)
    39  			continue
    40  		}
    41  		break
    42  	}
    43  	_ = x
    44  }
    45  
    46  var (
    47  	regHandler  sync.Once
    48  	handlerData int
    49  )
    50  
    51  func TestNoRaceIOHttp(t *testing.T) {
    52  	regHandler.Do(func() {
    53  		http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    54  			handlerData++
    55  			fmt.Fprintf(w, "test")
    56  			handlerData++
    57  		})
    58  	})
    59  	ln, err := net.Listen("tcp", "127.0.0.1:0")
    60  	if err != nil {
    61  		t.Fatalf("net.Listen: %v", err)
    62  	}
    63  	defer ln.Close()
    64  	go http.Serve(ln, nil)
    65  	handlerData++
    66  	_, err = http.Get("http://" + ln.Addr().String())
    67  	if err != nil {
    68  		t.Fatalf("http.Get: %v", err)
    69  	}
    70  	handlerData++
    71  	_, err = http.Get("http://" + ln.Addr().String())
    72  	if err != nil {
    73  		t.Fatalf("http.Get: %v", err)
    74  	}
    75  	handlerData++
    76  }
    77  

View as plain text