github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/operations/multithread_test.go (about)

     1  package operations
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/ncw/rclone/fs"
     9  	"github.com/ncw/rclone/fstest"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestMultithreadCalculateChunks(t *testing.T) {
    15  	for _, test := range []struct {
    16  		size         int64
    17  		streams      int
    18  		wantPartSize int64
    19  		wantStreams  int
    20  	}{
    21  		{size: 1, streams: 10, wantPartSize: multithreadChunkSize, wantStreams: 1},
    22  		{size: 1 << 20, streams: 1, wantPartSize: 1 << 20, wantStreams: 1},
    23  		{size: 1 << 20, streams: 2, wantPartSize: 1 << 19, wantStreams: 2},
    24  		{size: (1 << 20) + 1, streams: 2, wantPartSize: (1 << 19) + multithreadChunkSize, wantStreams: 2},
    25  		{size: (1 << 20) - 1, streams: 2, wantPartSize: (1 << 19), wantStreams: 2},
    26  	} {
    27  		t.Run(fmt.Sprintf("%+v", test), func(t *testing.T) {
    28  			mc := &multiThreadCopyState{
    29  				size:    test.size,
    30  				streams: test.streams,
    31  			}
    32  			mc.calculateChunks()
    33  			assert.Equal(t, test.wantPartSize, mc.partSize)
    34  			assert.Equal(t, test.wantStreams, mc.streams)
    35  		})
    36  	}
    37  }
    38  
    39  func TestMultithreadCopy(t *testing.T) {
    40  	r := fstest.NewRun(t)
    41  	defer r.Finalise()
    42  
    43  	for _, test := range []struct {
    44  		size    int
    45  		streams int
    46  	}{
    47  		{size: multithreadChunkSize*2 - 1, streams: 2},
    48  		{size: multithreadChunkSize * 2, streams: 2},
    49  		{size: multithreadChunkSize*2 + 1, streams: 2},
    50  	} {
    51  		t.Run(fmt.Sprintf("%+v", test), func(t *testing.T) {
    52  			contents := fstest.RandomString(test.size)
    53  			t1 := fstest.Time("2001-02-03T04:05:06.499999999Z")
    54  			file1 := r.WriteObject(context.Background(), "file1", contents, t1)
    55  			fstest.CheckItems(t, r.Fremote, file1)
    56  			fstest.CheckItems(t, r.Flocal)
    57  
    58  			src, err := r.Fremote.NewObject(context.Background(), "file1")
    59  			require.NoError(t, err)
    60  
    61  			dst, err := multiThreadCopy(context.Background(), r.Flocal, "file1", src, 2)
    62  			require.NoError(t, err)
    63  			assert.Equal(t, src.Size(), dst.Size())
    64  			assert.Equal(t, "file1", dst.Remote())
    65  
    66  			fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, nil, fs.ModTimeNotSupported)
    67  			require.NoError(t, dst.Remove(context.Background()))
    68  		})
    69  	}
    70  
    71  }