Source file src/internal/poll/error_test.go

     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  	"fmt"
     9  	"io/fs"
    10  	"net"
    11  	"os"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestReadError(t *testing.T) {
    17  	t.Run("ErrNotPollable", func(t *testing.T) {
    18  		f, err := badStateFile()
    19  		if err != nil {
    20  			t.Skip(err)
    21  		}
    22  		defer f.Close()
    23  
    24  		// Give scheduler a chance to have two separated
    25  		// goroutines: an event poller and an event waiter.
    26  		time.Sleep(100 * time.Millisecond)
    27  
    28  		var b [1]byte
    29  		_, err = f.Read(b[:])
    30  		if perr := parseReadError(err, isBadStateFileError); perr != nil {
    31  			t.Fatal(perr)
    32  		}
    33  	})
    34  }
    35  
    36  func parseReadError(nestedErr error, verify func(error) (string, bool)) error {
    37  	err := nestedErr
    38  	if nerr, ok := err.(*net.OpError); ok {
    39  		err = nerr.Err
    40  	}
    41  	if nerr, ok := err.(*fs.PathError); ok {
    42  		err = nerr.Err
    43  	}
    44  	if nerr, ok := err.(*os.SyscallError); ok {
    45  		err = nerr.Err
    46  	}
    47  	if s, ok := verify(err); !ok {
    48  		return fmt.Errorf("got %v; want %s", nestedErr, s)
    49  	}
    50  	return nil
    51  }
    52  

View as plain text