github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/sys/select_test.go (about)

     1  // +build !windows
     2  
     3  package sys
     4  
     5  import (
     6  	"testing"
     7  
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  func TestFdSet(t *testing.T) {
    12  	fs := NewFdSet(42, 233)
    13  	fs.Set(77)
    14  	fds := []int{42, 233, 77}
    15  	for _, i := range fds {
    16  		if !fs.IsSet(i) {
    17  			t.Errorf("fs.IsSet(%d) => false, want true", i)
    18  		}
    19  	}
    20  	fs.Clear(233)
    21  	if fs.IsSet(233) {
    22  		t.Errorf("fs.IsSet(233) => true, want false")
    23  	}
    24  	fs.Zero()
    25  	for _, i := range fds {
    26  		if fs.IsSet(i) {
    27  			t.Errorf("fs.IsSet(%d) => true, want false", i)
    28  		}
    29  	}
    30  }
    31  
    32  func TestSelect(t *testing.T) {
    33  	var p1, p2 [2]int
    34  	mustNil(unix.Pipe(p1[:]))
    35  	mustNil(unix.Pipe(p2[:]))
    36  	fs := NewFdSet(p1[0], p2[0])
    37  	var maxfd int
    38  	if p1[0] > p2[0] {
    39  		maxfd = p1[0] + 1
    40  	} else {
    41  		maxfd = p2[0] + 1
    42  	}
    43  	go func() {
    44  		unix.Write(p1[1], []byte("to p1"))
    45  		unix.Write(p2[1], []byte("to p2"))
    46  		unix.Close(p1[1])
    47  		unix.Close(p2[1])
    48  	}()
    49  	e := Select(maxfd+1, fs, nil, nil)
    50  	if e != nil {
    51  		t.Errorf("Select(%v, %v, nil, nil) => %v, want <nil>",
    52  			maxfd+1, fs, e)
    53  	}
    54  	unix.Close(p1[0])
    55  	unix.Close(p2[0])
    56  }