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