github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/vfs/vfstest/read_unix.go (about)

     1  // +build linux darwin freebsd
     2  
     3  package vfstest
     4  
     5  import (
     6  	"syscall"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // TestReadFileDoubleClose tests double close on read
    13  func TestReadFileDoubleClose(t *testing.T) {
    14  	run.skipIfVFS(t)
    15  	run.skipIfNoFUSE(t)
    16  
    17  	run.createFile(t, "testdoubleclose", "hello")
    18  
    19  	in, err := run.os.Open(run.path("testdoubleclose"))
    20  	assert.NoError(t, err)
    21  	fd := in.Fd()
    22  
    23  	fd1, err := syscall.Dup(int(fd))
    24  	assert.NoError(t, err)
    25  
    26  	fd2, err := syscall.Dup(int(fd))
    27  	assert.NoError(t, err)
    28  
    29  	// close one of the dups - should produce no error
    30  	err = syscall.Close(fd1)
    31  	assert.NoError(t, err)
    32  
    33  	// read from the file
    34  	buf := make([]byte, 1)
    35  	_, err = in.Read(buf)
    36  	assert.NoError(t, err)
    37  
    38  	// close it
    39  	err = in.Close()
    40  	assert.NoError(t, err)
    41  
    42  	// read from the other dup - should produce no error as this
    43  	// file is now buffered
    44  	n, err := syscall.Read(fd2, buf)
    45  	assert.NoError(t, err)
    46  	assert.Equal(t, 1, n)
    47  
    48  	// close the dup - should not produce an error
    49  	err = syscall.Close(fd2)
    50  	assert.NoError(t, err, "input/output error")
    51  
    52  	run.rm(t, "testdoubleclose")
    53  }