github.com/hanwen/go-fuse@v1.0.0/splice/copy_test.go (about)

     1  // Copyright 2016 the Go-FUSE 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 splice
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"testing"
    11  )
    12  
    13  func check(err error) {
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  }
    18  
    19  func TestCopyFile(t *testing.T) {
    20  	src, _ := ioutil.TempFile("", "termite")
    21  	err := ioutil.WriteFile(src.Name(), []byte("hello"), 0644)
    22  	if err != nil {
    23  		t.Error(err)
    24  	}
    25  	dst, _ := ioutil.TempFile("", "termite")
    26  	err = CopyFile(dst.Name(), src.Name(), 0755)
    27  	if err != nil {
    28  		t.Error(err)
    29  	}
    30  
    31  	c, err := ioutil.ReadFile(dst.Name())
    32  	if err != nil {
    33  		t.Error(err)
    34  	}
    35  	if string(c) != "hello" {
    36  		t.Error("mismatch", string(c))
    37  	}
    38  }
    39  
    40  func TestSpliceCopy(t *testing.T) {
    41  	src, err := ioutil.TempFile("", "termite")
    42  	check(err)
    43  	bs := make([]byte, 2*1024*1024)
    44  	for i := range bs {
    45  		bs[i] = byte(i % 256)
    46  	}
    47  	_, err = src.Write(bs)
    48  	check(err)
    49  	err = src.Close()
    50  	check(err)
    51  	src, err = os.Open(src.Name())
    52  	check(err)
    53  	dst, err := ioutil.TempFile("", "termite")
    54  	check(err)
    55  
    56  	if maxPipeSize%4096 != 0 || maxPipeSize < 4096 {
    57  		t.Error("pipe size should be page size multiple", maxPipeSize)
    58  	}
    59  	pool := newSplicePairPool()
    60  	p, err := pool.get()
    61  	if p != nil {
    62  		p.MaxGrow()
    63  		t.Logf("Splice size %d", p.size)
    64  		SpliceCopy(dst, src, p)
    65  		dst.Close()
    66  		src.Close()
    67  		p.Close()
    68  	} else {
    69  		t.Error("Could not open splice: ", err)
    70  	}
    71  }