MCPcopy Create free account
hub / github.com/Azure/peerd / TestGetOrCreate

Function TestGetOrCreate

pkg/cache/filecache_test.go:154–230  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

152}
153
154func TestGetOrCreate(t *testing.T) {
155 zerolog.TimeFieldFormat = time.RFC3339
156 //c := New(zerolog.New(os.Stdout).With().Timestamp().Logger().WithContext(context.Background()))
157 c := NewCache(context.Background(), cacheBlockSize, testFileCachePath)
158 var eg errgroup.Group
159
160 fileNames := new(sync.Map)
161 fileContents := new(sync.Map)
162 fileSizes := new(sync.Map)
163
164 for i := 0; i < 100; i++ {
165
166 fileNames.Store(i, newRandomStringN(10))
167 b := []byte(newRandomString())
168 size := int64(len(b))
169 fileContents.Store(i, b)
170 fileSizes.Store(i, size)
171
172 for j := 0; j < 10; j++ {
173 segs, err := math.NewSegments(0, 1024*1024, size, size)
174 if err != nil {
175 t.Fatal(err)
176 }
177 for seg := range segs.All() {
178
179 s := seg
180 fileIndex := i
181
182 eg.Go(func() error {
183 offset := s.Index
184 count := s.Count
185 fc, ok := fileContents.Load(fileIndex)
186 if !ok {
187 t.Fatalf("could not load fileContent from sync map: %v", fileIndex)
188 }
189 fcBytes, _ := fc.([]byte)
190 expected := make([]byte, count)
191 copy(expected, fcBytes[offset:offset+int64(count)])
192
193 fn, ok := fileNames.Load(fileIndex)
194 if !ok {
195 t.Fatalf("could not load fileName from sync map: %v", fileIndex)
196 }
197 name, _ := fn.(string)
198
199 got, err := c.GetOrCreate(name, offset, count, func() ([]byte, error) {
200 return expected, nil
201 })
202 if err != nil {
203 return fmt.Errorf("failed to get or create: %v -- %v", name, err)
204 }
205
206 l := len(got)
207 if count != l {
208 return fmt.Errorf("size mismatch, expected %v, got %v, offset: %v, fileName: %v", count, len(got), offset, name)
209 }
210
211 validationLen := math.Min(100, count)

Callers

nothing calls this directly

Calls 7

AllMethod · 0.95
GetOrCreateMethod · 0.95
NewSegmentsFunction · 0.92
MinFunction · 0.92
NewCacheFunction · 0.85
newRandomStringFunction · 0.85
newRandomStringNFunction · 0.70

Tested by

no test coverage detected