Skip to content

Commit

Permalink
facilitating java matcher mocks to imitate the external-sources abort…
Browse files Browse the repository at this point in the history
…-after functionality

Signed-off-by: Pouyan Khodabakhsh <[email protected]>
  • Loading branch information
pouyan021 authored and wagoodman committed May 16, 2024
1 parent 7043535 commit db426d2
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions grype/matcher/java/matcher_mocks_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package java

import (
"context"
"fmt"
"time"

"github.com/anchore/grype/grype/distro"
"github.com/anchore/grype/grype/pkg"
"github.com/anchore/grype/grype/version"
Expand Down Expand Up @@ -52,13 +56,22 @@ type mockMavenSearcher struct {
pkg pkg.Package
}

func (m mockMavenSearcher) GetMavenPackageBySha(string) (*pkg.Package, error) {
return &m.pkg, nil
func (m mockMavenSearcher) GetMavenPackageBySha(ctx context.Context, sha1 string) (*pkg.Package, error) {
deadline, ok := ctx.Deadline()
fmt.Println("GetMavenPackageBySha called with deadline:", deadline, "deadline set:", ok)
// Sleep for a duration longer than the context's deadline
select {
case <-time.After(10 * time.Second):
return &m.pkg, nil
case <-ctx.Done():
// If the context is done before the sleep is over, return a context.DeadlineExceeded error
return nil, ctx.Err()
}
}

func newMockSearcher(pkg pkg.Package) MavenSearcher {
return mockMavenSearcher{
pkg,
pkg: pkg,
}
}

Expand Down

0 comments on commit db426d2

Please sign in to comment.