github.com/fisco-bcos/crypto@v0.0.0-20200202032121-bd8ab0b5d4f1/internal/poll/read_test.go (about)

     1  // Copyright 2019 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 poll_test
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"runtime"
    11  	"sync"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestRead(t *testing.T) {
    17  	t.Run("SpecialFile", func(t *testing.T) {
    18  		var wg sync.WaitGroup
    19  		for _, p := range specialFiles() {
    20  			for i := 0; i < 4; i++ {
    21  				wg.Add(1)
    22  				go func(p string) {
    23  					defer wg.Done()
    24  					for i := 0; i < 100; i++ {
    25  						if _, err := ioutil.ReadFile(p); err != nil {
    26  							t.Error(err)
    27  							return
    28  						}
    29  						time.Sleep(time.Nanosecond)
    30  					}
    31  				}(p)
    32  			}
    33  		}
    34  		wg.Wait()
    35  	})
    36  }
    37  
    38  func specialFiles() []string {
    39  	var ps []string
    40  	switch runtime.GOOS {
    41  	case "darwin", "dragonfly", "freebsd", "netbsd", "openbsd":
    42  		ps = []string{
    43  			"/dev/null",
    44  		}
    45  	case "linux":
    46  		ps = []string{
    47  			"/dev/null",
    48  			"/proc/stat",
    49  			"/sys/devices/system/cpu/online",
    50  		}
    51  	}
    52  	nps := ps[:0]
    53  	for _, p := range ps {
    54  		f, err := os.Open(p)
    55  		if err != nil {
    56  			continue
    57  		}
    58  		f.Close()
    59  		nps = append(nps, p)
    60  	}
    61  	return nps
    62  }