github.com/Jeffail/benthos/v3@v3.65.0/lib/buffer/single/mmap_cache_test.go (about) 1 package single 2 3 import ( 4 "testing" 5 6 "github.com/Jeffail/benthos/v3/lib/log" 7 "github.com/Jeffail/benthos/v3/lib/metrics" 8 ) 9 10 func TestMmapCacheTracker(t *testing.T) { 11 t.Skip("DEPRECATED") 12 13 dir := t.TempDir() 14 15 conf := NewMmapCacheConfig() 16 conf.FileSize = 1000 17 conf.Path = dir 18 19 cache, err := NewMmapCache(conf, log.Noop(), metrics.Noop()) 20 if err != nil { 21 t.Error(err) 22 return 23 } 24 cache.L.Lock() 25 26 trackerBytes := cache.GetTracker() 27 if trackerBytes == nil { 28 t.Errorf("Tracker bytes were nil") 29 return 30 } 31 32 if len(trackerBytes) != 16 { 33 t.Errorf("Tracker was wrong length: %v != %v", len(trackerBytes), 16) 34 return 35 } 36 37 if exp, act := trackerBytes[0], byte(0); exp != act { 38 t.Errorf("Wrong byte from tracker: %v != %v", exp, act) 39 } 40 if exp, act := trackerBytes[1], byte(0); exp != act { 41 t.Errorf("Wrong byte from tracker: %v != %v", exp, act) 42 } 43 if exp, act := trackerBytes[2], byte(0); exp != act { 44 t.Errorf("Wrong byte from tracker: %v != %v", exp, act) 45 } 46 if exp, act := trackerBytes[3], byte(0); exp != act { 47 t.Errorf("Wrong byte from tracker: %v != %v", exp, act) 48 } 49 50 trackerBytes[0] = byte(0) 51 trackerBytes[1] = byte(1) 52 trackerBytes[2] = byte(2) 53 trackerBytes[3] = byte(3) 54 55 if err = cache.RemoveAll(); err != nil { 56 t.Error(err) 57 return 58 } 59 60 cache.L.Unlock() 61 cache, err = NewMmapCache(conf, log.Noop(), metrics.Noop()) 62 if err != nil { 63 t.Error(err) 64 return 65 } 66 cache.L.Lock() 67 defer cache.L.Unlock() 68 69 trackerBytes = cache.GetTracker() 70 if trackerBytes == nil { 71 t.Errorf("Tracker bytes were nil") 72 return 73 } 74 75 if len(trackerBytes) != 16 { 76 t.Errorf("Tracker was wrong length: %v != %v", len(trackerBytes), 16) 77 return 78 } 79 80 if exp, act := trackerBytes[0], byte(0); exp != act { 81 t.Errorf("Wrong byte from tracker: %v != %v", exp, act) 82 } 83 if exp, act := trackerBytes[1], byte(1); exp != act { 84 t.Errorf("Wrong byte from tracker: %v != %v", exp, act) 85 } 86 if exp, act := trackerBytes[2], byte(2); exp != act { 87 t.Errorf("Wrong byte from tracker: %v != %v", exp, act) 88 } 89 if exp, act := trackerBytes[3], byte(3); exp != act { 90 t.Errorf("Wrong byte from tracker: %v != %v", exp, act) 91 } 92 } 93 94 func TestMmapCacheIndexes(t *testing.T) { 95 t.Skip("DEPRECATED") 96 97 dir := t.TempDir() 98 99 conf := NewMmapCacheConfig() 100 conf.FileSize = 1000 101 conf.Path = dir 102 103 cache, err := NewMmapCache(conf, log.Noop(), metrics.Noop()) 104 if err != nil { 105 t.Error(err) 106 return 107 } 108 cache.L.Lock() 109 110 if err := cache.EnsureCached(20); err != nil { 111 t.Error(err) 112 return 113 } 114 115 bytes := cache.Get(20) 116 if bytes == nil { 117 t.Errorf("Index bytes were nil") 118 return 119 } 120 121 if len(bytes) != conf.FileSize { 122 t.Errorf("Index was wrong length: %v != %v", len(bytes), conf.FileSize) 123 return 124 } 125 126 if exp, act := bytes[0], byte(0); exp != act { 127 t.Errorf("Wrong byte from index: %v != %v", exp, act) 128 } 129 if exp, act := bytes[1], byte(0); exp != act { 130 t.Errorf("Wrong byte from index: %v != %v", exp, act) 131 } 132 if exp, act := bytes[2], byte(0); exp != act { 133 t.Errorf("Wrong byte from index: %v != %v", exp, act) 134 } 135 if exp, act := bytes[3], byte(0); exp != act { 136 t.Errorf("Wrong byte from index: %v != %v", exp, act) 137 } 138 139 bytes[0] = byte(0) 140 bytes[1] = byte(1) 141 bytes[2] = byte(2) 142 bytes[3] = byte(3) 143 144 if err = cache.RemoveAll(); err != nil { 145 t.Error(err) 146 return 147 } 148 149 cache.L.Unlock() 150 cache, err = NewMmapCache(conf, log.Noop(), metrics.Noop()) 151 if err != nil { 152 t.Error(err) 153 return 154 } 155 cache.L.Lock() 156 defer cache.L.Unlock() 157 158 if err := cache.EnsureCached(20); err != nil { 159 t.Error(err) 160 return 161 } 162 163 bytes = cache.Get(20) 164 if bytes == nil { 165 t.Errorf("Index bytes were nil") 166 return 167 } 168 169 if len(bytes) != conf.FileSize { 170 t.Errorf("Index was wrong length: %v != %v", len(bytes), conf.FileSize) 171 return 172 } 173 174 if exp, act := bytes[0], byte(0); exp != act { 175 t.Errorf("Wrong byte from index: %v != %v", exp, act) 176 } 177 if exp, act := bytes[1], byte(1); exp != act { 178 t.Errorf("Wrong byte from index: %v != %v", exp, act) 179 } 180 if exp, act := bytes[2], byte(2); exp != act { 181 t.Errorf("Wrong byte from index: %v != %v", exp, act) 182 } 183 if exp, act := bytes[3], byte(3); exp != act { 184 t.Errorf("Wrong byte from index: %v != %v", exp, act) 185 } 186 } 187 188 func TestMmapCacheRaces(t *testing.T) { 189 t.Skip("DEPRECATED") 190 191 dir := t.TempDir() 192 193 conf := NewMmapCacheConfig() 194 conf.FileSize = 10 195 conf.Path = dir 196 197 cache, err := NewMmapCache(conf, log.Noop(), metrics.Noop()) 198 if err != nil { 199 t.Error(err) 200 return 201 } 202 cache.L.Lock() 203 defer cache.L.Unlock() 204 205 for i := 0; i < 100; i++ { 206 if err := cache.EnsureCached(i); err != nil { 207 t.Error(err) 208 return 209 } 210 } 211 212 for i := 0; i < 1000; i++ { 213 bytes := cache.Get(20) 214 if len(bytes) == 0 { 215 t.Errorf("Index %v bytes were nil or empty", i) 216 return 217 } 218 219 bytes[0] = byte(1) 220 bytes[1] = byte(2) 221 bytes[2] = byte(3) 222 bytes[3] = byte(4) 223 } 224 225 }