github.com/divyam234/rclone@v1.64.1/fs/chunksize/chunksize_test.go (about)

     1  package chunksize
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/divyam234/rclone/fs"
     7  )
     8  
     9  func TestComputeChunkSize(t *testing.T) {
    10  	for _, test := range []struct {
    11  		name             string
    12  		size             fs.SizeSuffix
    13  		maxParts         int
    14  		defaultChunkSize fs.SizeSuffix
    15  		want             fs.SizeSuffix
    16  	}{
    17  		{
    18  			name:             "streaming file",
    19  			size:             -1,
    20  			maxParts:         10000,
    21  			defaultChunkSize: toSizeSuffixMiB(10),
    22  			want:             toSizeSuffixMiB(10),
    23  		}, {
    24  			name:             "default size returned when file size is small enough",
    25  			size:             1000,
    26  			maxParts:         10000,
    27  			defaultChunkSize: toSizeSuffixMiB(10),
    28  			want:             toSizeSuffixMiB(10),
    29  		}, {
    30  			name:             "default size returned when file size is just 1 byte small enough",
    31  			size:             toSizeSuffixMiB(100000) - 1,
    32  			maxParts:         10000,
    33  			defaultChunkSize: toSizeSuffixMiB(10),
    34  			want:             toSizeSuffixMiB(10),
    35  		}, {
    36  			name:             "no rounding up when everything divides evenly",
    37  			size:             toSizeSuffixMiB(1000000),
    38  			maxParts:         10000,
    39  			defaultChunkSize: toSizeSuffixMiB(100),
    40  			want:             toSizeSuffixMiB(100),
    41  		}, {
    42  			name:             "rounding up to nearest MiB when not quite enough parts",
    43  			size:             toSizeSuffixMiB(1000000),
    44  			maxParts:         9999,
    45  			defaultChunkSize: toSizeSuffixMiB(100),
    46  			want:             toSizeSuffixMiB(101),
    47  		}, {
    48  			name:             "rounding up to nearest MiB when one extra byte",
    49  			size:             toSizeSuffixMiB(1000000) + 1,
    50  			maxParts:         10000,
    51  			defaultChunkSize: toSizeSuffixMiB(100),
    52  			want:             toSizeSuffixMiB(101),
    53  		}, {
    54  			name:             "expected MiB value when rounding sets to absolute minimum",
    55  			size:             toSizeSuffixMiB(1) - 1,
    56  			maxParts:         1,
    57  			defaultChunkSize: toSizeSuffixMiB(1),
    58  			want:             toSizeSuffixMiB(1),
    59  		}, {
    60  			name:             "expected MiB value when rounding to absolute min with extra",
    61  			size:             toSizeSuffixMiB(1) + 1,
    62  			maxParts:         1,
    63  			defaultChunkSize: toSizeSuffixMiB(1),
    64  			want:             toSizeSuffixMiB(2),
    65  		}, {
    66  			name:             "issue from forum #1",
    67  			size:             120864818840,
    68  			maxParts:         10000,
    69  			defaultChunkSize: 5 * 1024 * 1024,
    70  			want:             toSizeSuffixMiB(12),
    71  		},
    72  	} {
    73  		t.Run(test.name, func(t *testing.T) {
    74  			got := Calculator(test.name, int64(test.size), test.maxParts, test.defaultChunkSize)
    75  			if got != test.want {
    76  				t.Fatalf("expected: %v, got: %v", test.want, got)
    77  			}
    78  			if test.size < 0 {
    79  				return
    80  			}
    81  			parts := func(result fs.SizeSuffix) int {
    82  				n := test.size / result
    83  				r := test.size % result
    84  				if r != 0 {
    85  					n++
    86  				}
    87  				return int(n)
    88  			}
    89  			// Check this gives the parts in range
    90  			if parts(got) > test.maxParts {
    91  				t.Fatalf("too many parts %d", parts(got))
    92  			}
    93  			// Check that setting chunk size smaller gave too many parts
    94  			if got > test.defaultChunkSize {
    95  				if parts(got-toSizeSuffixMiB(1)) <= test.maxParts {
    96  					t.Fatalf("chunk size %v too big as %v only gives %d parts", got, got-toSizeSuffixMiB(1), parts(got-toSizeSuffixMiB(1)))
    97  				}
    98  
    99  			}
   100  		})
   101  	}
   102  }
   103  
   104  func toSizeSuffixMiB(size int64) fs.SizeSuffix {
   105  	return fs.SizeSuffix(size * int64(fs.Mebi))
   106  }