github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/data-usage-cache_test.go (about)

     1  // Copyright (c) 2015-2023 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/dustin/go-humanize"
    25  )
    26  
    27  func TestSizeHistogramToMap(t *testing.T) {
    28  	tests := []struct {
    29  		sizes []int64
    30  		want  map[string]uint64
    31  	}{
    32  		{
    33  			sizes: []int64{100, 1000, 72_000, 100_000},
    34  			want: map[string]uint64{
    35  				"LESS_THAN_1024_B":         2,
    36  				"BETWEEN_64_KB_AND_256_KB": 2,
    37  				"BETWEEN_1024B_AND_1_MB":   2,
    38  			},
    39  		},
    40  		{
    41  			sizes: []int64{100, 1000, 2000, 100_000, 13 * humanize.MiByte},
    42  			want: map[string]uint64{
    43  				"LESS_THAN_1024_B":         2,
    44  				"BETWEEN_1024_B_AND_64_KB": 1,
    45  				"BETWEEN_64_KB_AND_256_KB": 1,
    46  				"BETWEEN_1024B_AND_1_MB":   2,
    47  				"BETWEEN_10_MB_AND_64_MB":  1,
    48  			},
    49  		},
    50  	}
    51  	for i, test := range tests {
    52  		t.Run(fmt.Sprintf("Test-%d", i), func(t *testing.T) {
    53  			var h sizeHistogram
    54  			for _, sz := range test.sizes {
    55  				h.add(sz)
    56  			}
    57  			got := h.toMap()
    58  			exp := test.want
    59  			// what is in exp is in got
    60  			for k := range exp {
    61  				if exp[k] != got[k] {
    62  					t.Fatalf("interval %s: Expected %d values but got %d values\n", k, exp[k], got[k])
    63  				}
    64  			}
    65  			// what is absent in exp is absent in got too
    66  			for k := range got {
    67  				if _, ok := exp[k]; !ok && got[k] > 0 {
    68  					t.Fatalf("Unexpected interval: %s has value %d\n", k, got[k])
    69  				}
    70  			}
    71  		})
    72  	}
    73  }
    74  
    75  func TestMigrateSizeHistogramFromV1(t *testing.T) {
    76  	tests := []struct {
    77  		v    sizeHistogramV1
    78  		want sizeHistogram
    79  	}{
    80  		{
    81  			v:    sizeHistogramV1{0: 10, 1: 20, 2: 3},
    82  			want: sizeHistogram{0: 10, 5: 20, 6: 3},
    83  		},
    84  		{
    85  			v:    sizeHistogramV1{0: 10, 1: 20, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7},
    86  			want: sizeHistogram{0: 10, 5: 20, 6: 3, 7: 4, 8: 5, 9: 6, 10: 7},
    87  		},
    88  	}
    89  	for i, test := range tests {
    90  		t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) {
    91  			var got sizeHistogram
    92  			got.mergeV1(test.v)
    93  			if got != test.want {
    94  				t.Fatalf("Expected %v but got %v", test.want, got)
    95  			}
    96  		})
    97  	}
    98  }