From b9aaa11ea0d8c7976b63bb36426f6cd21b6290cc Mon Sep 17 00:00:00 2001 From: Simeone Date: Tue, 26 Mar 2019 10:07:58 +0100 Subject: [PATCH] errors coercion to pass the tests --- pipe.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/pipe.go b/pipe.go index d35a94e..33087df 100644 --- a/pipe.go +++ b/pipe.go @@ -138,22 +138,22 @@ func (s pipeAddress) String() string { return string(s) } +//helper function used to try to open the pipe multiple times. func tryDialPipe(ctx context.Context, path *string) (syscall.Handle, error) { for { select { case <-ctx.Done(): - err := ctx.Err() - if err == context.DeadlineExceeded { - err = ErrTimeout - } else { - err = &os.PathError{Op: "open", Path: *path, Err: err} - } - return syscall.Handle(0), err + return syscall.Handle(0), ctx.Err() default: h, err := createFile(*path, syscall.GENERIC_READ|syscall.GENERIC_WRITE, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_OVERLAPPED|cSECURITY_SQOS_PRESENT|cSECURITY_ANONYMOUS, 0) - if err != cERROR_PIPE_BUSY { - return h, &os.PathError{Op: "open", Path: *path, Err: err} + if err == nil { + return h, nil } + if err != cERROR_PIPE_BUSY { + return h, newOpenError(path, err) + } + // Wait 10 msec and try again. This is a rather simplistic + // view, as we always try each 10 milliseconds. time.Sleep(time.Millisecond * 10) } } @@ -171,9 +171,19 @@ func DialPipe(path string, timeout *time.Duration) (net.Conn, error) { } ctx, _ := context.WithDeadline(context.Background(), absTimeout) conn, err := DialPipeContext(ctx, path) + if err == context.DeadlineExceeded { + return nil, ErrTimeout + } return conn, err } +func newOpenError(path *string, err error) error { + if err != nil { + return &os.PathError{Err: err, Op: "open", Path: *path} + } + return nil +} + //DialPipeContext connects to a named pipe. ctx can be used to cancel or //expire the pending connection ( We do not use WaitNamedPipe.) func DialPipeContext(ctx context.Context, path string) (net.Conn, error) {