storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/disk-cache-utils_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2019 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package cmd 18 19 import ( 20 "net/http" 21 "reflect" 22 "testing" 23 "time" 24 ) 25 26 func TestGetCacheControlOpts(t *testing.T) { 27 expiry, _ := time.Parse(http.TimeFormat, "Wed, 21 Oct 2015 07:28:00 GMT") 28 29 testCases := []struct { 30 cacheControlHeaderVal string 31 expiryHeaderVal time.Time 32 expectedCacheControl *cacheControl 33 expectedErr bool 34 }{ 35 {"", timeSentinel, nil, false}, 36 {"max-age=2592000, public", timeSentinel, &cacheControl{maxAge: 2592000, sMaxAge: 0, minFresh: 0, expiry: time.Time{}}, false}, 37 {"max-age=2592000, no-store", timeSentinel, &cacheControl{maxAge: 2592000, sMaxAge: 0, noStore: true, minFresh: 0, expiry: time.Time{}}, false}, 38 {"must-revalidate, max-age=600", timeSentinel, &cacheControl{maxAge: 600, sMaxAge: 0, minFresh: 0, expiry: time.Time{}}, false}, 39 {"s-maxAge=2500, max-age=600", timeSentinel, &cacheControl{maxAge: 600, sMaxAge: 2500, minFresh: 0, expiry: time.Time{}}, false}, 40 {"s-maxAge=2500, max-age=600", expiry, &cacheControl{maxAge: 600, sMaxAge: 2500, minFresh: 0, expiry: time.Date(2015, time.October, 21, 07, 28, 00, 00, time.UTC)}, false}, 41 {"s-maxAge=2500, max-age=600s", timeSentinel, &cacheControl{maxAge: 600, sMaxAge: 2500, minFresh: 0, expiry: time.Time{}}, true}, 42 } 43 44 for _, testCase := range testCases { 45 t.Run("", func(t *testing.T) { 46 m := make(map[string]string) 47 m["cache-control"] = testCase.cacheControlHeaderVal 48 if !testCase.expiryHeaderVal.Equal(timeSentinel) { 49 m["expires"] = testCase.expiryHeaderVal.String() 50 } 51 c := cacheControlOpts(ObjectInfo{UserDefined: m, Expires: testCase.expiryHeaderVal}) 52 if testCase.expectedErr && (c != nil) { 53 t.Errorf("expected err, got <nil>") 54 } 55 if !testCase.expectedErr && !reflect.DeepEqual(c, testCase.expectedCacheControl) { 56 t.Errorf("expected %v, got %v", testCase.expectedCacheControl, c) 57 } 58 }) 59 } 60 } 61 62 func TestIsMetadataSame(t *testing.T) { 63 64 testCases := []struct { 65 m1 map[string]string 66 m2 map[string]string 67 expected bool 68 }{ 69 {nil, nil, true}, 70 {nil, map[string]string{}, false}, 71 {map[string]string{"k": "v"}, map[string]string{"k": "v"}, true}, 72 {map[string]string{"k": "v"}, map[string]string{"a": "b"}, false}, 73 {map[string]string{"k1": "v1", "k2": "v2"}, map[string]string{"k1": "v1", "k2": "v1"}, false}, 74 {map[string]string{"k1": "v1", "k2": "v2"}, map[string]string{"k1": "v1", "k2": "v2"}, true}, 75 {map[string]string{"K1": "v1", "k2": "v2"}, map[string]string{"k1": "v1", "k2": "v2"}, false}, 76 {map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}, map[string]string{"k1": "v1", "k2": "v2"}, false}, 77 } 78 79 for i, testCase := range testCases { 80 actual := isMetadataSame(testCase.m1, testCase.m2) 81 if testCase.expected != actual { 82 t.Errorf("test %d expected %v, got %v", i, testCase.expected, actual) 83 } 84 } 85 } 86 87 func TestNewFileScorer(t *testing.T) { 88 fs, err := newFileScorer(1000, time.Now().Unix(), 10) 89 if err != nil { 90 t.Fatal(err) 91 } 92 if len(fs.fileNames()) != 0 { 93 t.Fatal("non zero files??") 94 } 95 now := time.Now() 96 fs.addFile("recent", now.Add(-time.Minute), 1000, 10) 97 fs.addFile("older", now.Add(-time.Hour), 1000, 10) 98 if !reflect.DeepEqual(fs.fileNames(), []string{"older"}) { 99 t.Fatal("unexpected file list", fs.queueString()) 100 } 101 fs.reset() 102 fs.addFile("bigger", now.Add(-time.Minute), 2000, 10) 103 fs.addFile("recent", now.Add(-time.Minute), 1000, 10) 104 if !reflect.DeepEqual(fs.fileNames(), []string{"bigger"}) { 105 t.Fatal("unexpected file list", fs.queueString()) 106 } 107 fs.reset() 108 fs.addFile("less", now.Add(-time.Minute), 1000, 5) 109 fs.addFile("recent", now.Add(-time.Minute), 1000, 10) 110 if !reflect.DeepEqual(fs.fileNames(), []string{"less"}) { 111 t.Fatal("unexpected file list", fs.queueString()) 112 } 113 fs.reset() 114 fs.addFile("small", now.Add(-time.Minute), 200, 10) 115 fs.addFile("medium", now.Add(-time.Minute), 300, 10) 116 if !reflect.DeepEqual(fs.fileNames(), []string{"medium", "small"}) { 117 t.Fatal("unexpected file list", fs.queueString()) 118 } 119 fs.addFile("large", now.Add(-time.Minute), 700, 10) 120 fs.addFile("xsmol", now.Add(-time.Minute), 7, 10) 121 if !reflect.DeepEqual(fs.fileNames(), []string{"large", "medium"}) { 122 t.Fatal("unexpected file list", fs.queueString()) 123 } 124 125 fs.reset() 126 fs.addFile("less", now.Add(-time.Minute), 500, 5) 127 fs.addFile("recent", now.Add(-time.Minute), 500, 10) 128 if !fs.adjustSaveBytes(-500) { 129 t.Fatal("we should still need more bytes, got false") 130 } 131 // We should only need 500 bytes now. 132 if !reflect.DeepEqual(fs.fileNames(), []string{"less"}) { 133 t.Fatal("unexpected file list", fs.queueString()) 134 } 135 if fs.adjustSaveBytes(-500) { 136 t.Fatal("we shouldn't need any more bytes, got true") 137 } 138 fs, err = newFileScorer(1000, time.Now().Unix(), 10) 139 if err != nil { 140 t.Fatal(err) 141 } 142 fs.addFile("bigger", now.Add(-time.Minute), 50, 10) 143 // sorting should be consistent after adjusting savebytes. 144 fs.adjustSaveBytes(-800) 145 fs.addFile("smaller", now.Add(-time.Minute), 40, 10) 146 if !reflect.DeepEqual(fs.fileNames(), []string{"bigger", "smaller"}) { 147 t.Fatal("unexpected file list", fs.queueString()) 148 } 149 } 150 func TestBytesToClear(t *testing.T) { 151 testCases := []struct { 152 total int64 153 free int64 154 quotaPct uint64 155 watermarkLow uint64 156 watermarkHigh uint64 157 expected uint64 158 }{ 159 {total: 1000, free: 800, quotaPct: 40, watermarkLow: 90, watermarkHigh: 90, expected: 0}, 160 {total: 1000, free: 200, quotaPct: 40, watermarkLow: 90, watermarkHigh: 90, expected: 400}, 161 {total: 1000, free: 400, quotaPct: 40, watermarkLow: 90, watermarkHigh: 90, expected: 240}, 162 {total: 1000, free: 600, quotaPct: 40, watermarkLow: 90, watermarkHigh: 90, expected: 40}, 163 {total: 1000, free: 600, quotaPct: 40, watermarkLow: 70, watermarkHigh: 70, expected: 120}, 164 {total: 1000, free: 1000, quotaPct: 90, watermarkLow: 70, watermarkHigh: 70, expected: 0}, 165 166 // High not yet reached.. 167 {total: 1000, free: 250, quotaPct: 100, watermarkLow: 50, watermarkHigh: 90, expected: 0}, 168 {total: 1000, free: 250, quotaPct: 100, watermarkLow: 50, watermarkHigh: 90, expected: 0}, 169 } 170 for i, tc := range testCases { 171 toClear := bytesToClear(tc.total, tc.free, tc.quotaPct, tc.watermarkLow, tc.watermarkHigh) 172 if tc.expected != toClear { 173 t.Errorf("test %d expected %v, got %v", i, tc.expected, toClear) 174 } 175 } 176 }