func TestPartWriter(t *testing.T) { t.Parallel() test := func(start, length, partSize uint64) { cz := &types.CacheZone{ ID: "TestCZ", Storage: mock.NewStorage(partSize), Algorithm: mock.NewCacheAlgorithm(&mock.CacheAlgorithmRepliers{ ShouldKeep: func(*types.ObjectIndex) bool { return true }, }), } if err := cz.Storage.SaveMetadata(oMeta); err != nil { t.Fatalf("Unexpected error: %s", err) } write(t, start, length, cz, oid) checkParts(t, start, length, cz, oid) } // Test all variants for partSize := uint64(1); partSize <= inputSize+3; partSize++ { for start := uint64(0); start < inputSize; start++ { for length := uint64(1); length+start <= inputSize; length++ { test(start, length, partSize) } } } }
func storageWithObjects(t *testing.T, objs ...*types.ObjectID) types.Storage { var st = mock.NewStorage(10) for _, obj := range objs { testutils.ShouldntFail(t, st.SaveMetadata(&types.ObjectMetadata{ID: obj}), st.SavePart( &types.ObjectIndex{ObjID: obj, Part: 2}, bytes.NewReader([]byte("test bytes"))), st.SavePart( &types.ObjectIndex{ObjID: obj, Part: 4}, bytes.NewReader([]byte("more bytes"))), ) } return st }
func TestAlgorithmCompliance(t *testing.T) { t.Parallel() partSize := uint64(5) totalParts := inputSize/partSize + 1 state := make([]struct{ added, promoted bool }, totalParts) expectedParts := make([]bool, totalParts) algo := mock.NewCacheAlgorithm(&mock.CacheAlgorithmRepliers{ AddObject: func(idx *types.ObjectIndex) error { state[idx.Part].added = true return nil }, PromoteObject: func(idx *types.ObjectIndex) { state[idx.Part].promoted = true }, }) for i := uint64(0); i < totalParts; i++ { if rand.Intn(2) == 1 { expectedParts[i] = true algo.SetFakeReplies(&types.ObjectIndex{ObjID: oid, Part: uint32(i)}, &mock.CacheAlgorithmRepliers{ ShouldKeep: func(*types.ObjectIndex) bool { return true }, }) } } cz := &types.CacheZone{ID: "TestCZ", Storage: mock.NewStorage(partSize), Algorithm: algo} if err := cz.Storage.SaveMetadata(oMeta); err != nil { t.Fatalf("Unexpected error: %s", err) } write(t, 0, inputSize, cz, oid) for i := uint64(0); i < totalParts; i++ { idx := &types.ObjectIndex{ObjID: oid, Part: uint32(i)} if expectedParts[i] { expected := input[partSize*i : umin(inputSize, (i+1)*partSize)] checkPart(t, fmt.Sprintf("part %d", i), expected, cz.Storage, idx) if !state[i].added || state[i].promoted { t.Errorf("Wrong cache state for part %d: %v", i, state[i]) } } else { checkPartIsMissing(t, fmt.Sprintf("part %d", i), cz.Storage, idx) if state[i].added || state[i].promoted { t.Errorf("Wrong cache state for expected missing part %d: %v", i, state[i]) } } } }