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