Skip to content

Commit

Permalink
fixed context issue
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <wangxiaoxuan119@gmail.com>
  • Loading branch information
wangxiaoxuan273 committed Jan 4, 2024
1 parent 942cc4d commit 2d454e2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
6 changes: 6 additions & 0 deletions content/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ func (s *Store) traverseIndex(ctx context.Context) (set.Set[digest.Digest], erro
queue := []ocispec.Descriptor{}
queue = append(queue, manifests...)
for len(queue) > 0 {
if err := isContextDone(ctx); err != nil {
return nil, err
}

Check warning on line 470 in content/oci/oci.go

View check run for this annotation

Codecov / codecov/patch

content/oci/oci.go#L469-L470

Added lines #L469 - L470 were not covered by tests
head := queue[0]
queue = queue[1:]
if visited.Contains(head.Digest) {
Expand Down Expand Up @@ -521,6 +524,9 @@ func (s *Store) GC(ctx context.Context) error {
return err
}

Check warning on line 525 in content/oci/oci.go

View check run for this annotation

Codecov / codecov/patch

content/oci/oci.go#L524-L525

Added lines #L524 - L525 were not covered by tests
for _, dgstDir := range dgstDirs {
if err := isContextDone(ctx); err != nil {
return err
}

Check warning on line 529 in content/oci/oci.go

View check run for this annotation

Codecov / codecov/patch

content/oci/oci.go#L528-L529

Added lines #L528 - L529 were not covered by tests
dgst := dgstDir.Name()
blobDigest := digest.NewDigestFromEncoded(digest.Algorithm(alg), dgst)
err := blobDigest.Validate()
Expand Down
39 changes: 11 additions & 28 deletions content/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2914,20 +2914,11 @@ func TestStore_GC(t *testing.T) {
generateManifest(descs[0], &descs[13], descs[1]) // Blob 16, referrer of a garbage manifest

// push blobs 0 - blobs 10 into s
eg, egCtx := errgroup.WithContext(ctx)
for i := 0; i <= 10; i++ {
eg.Go(func(i int) func() error {
return func() error {
err := s.Push(egCtx, descs[i], bytes.NewReader(blobs[i]))
if err != nil {
return fmt.Errorf("failed to push test content to src: %d: %v", i, err)
}
return nil
}
}(i))
}
if err := eg.Wait(); err != nil {
t.Fatal(err)
err := s.Push(ctx, descs[i], bytes.NewReader(blobs[i]))
if err != nil {
t.Errorf("failed to push test content to src: %d: %v", i, err)
}
}

// remove blobs 4 - blobs 10 from index.json
Expand All @@ -2939,23 +2930,15 @@ func TestStore_GC(t *testing.T) {
// push blobs 11 - blobs 16 into s.storage, making them garbage as their metadata
// doesn't exist in s
for i := 11; i < len(blobs); i++ {
eg.Go(func(i int) func() error {
return func() error {
err := s.storage.Push(egCtx, descs[i], bytes.NewReader(blobs[i]))
if err != nil {
return fmt.Errorf("failed to push test content to src: %d: %v", i, err)
}
return nil
}
}(i))
}
if err := eg.Wait(); err != nil {
t.Fatal(err)
err := s.storage.Push(ctx, descs[i], bytes.NewReader(blobs[i]))
if err != nil {
t.Errorf("failed to push test content to src: %d: %v", i, err)
}
}

// confirm that all the blobs are in the storage
for i := 11; i < len(blobs); i++ {
exists, err := s.Exists(egCtx, descs[i])
exists, err := s.Exists(ctx, descs[i])
if err != nil {
t.Fatal(err)
}
Expand All @@ -2965,14 +2948,14 @@ func TestStore_GC(t *testing.T) {
}

// perform GC
if err = s.GC(egCtx); err != nil {
if err = s.GC(ctx); err != nil {
t.Fatal(err)
}

// verify existence
wantExistence := []bool{true, true, false, true, true, false, false, false, false, false, false, false, false, false, false, false, false}
for i, wantValue := range wantExistence {
exists, err := s.Exists(egCtx, descs[i])
exists, err := s.Exists(ctx, descs[i])
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 2d454e2

Please sign in to comment.