Skip to content

Commit

Permalink
test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
rauljordan committed Jul 15, 2024
1 parent a65870d commit 27298fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
8 changes: 7 additions & 1 deletion staker/challenge-cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ func (c *Cache) Prune(ctx context.Context, messageNumber uint64) error {
messageNumPattern := fmt.Sprintf(`%s-(\d+)-`, messageNumberPrefix)
pattern := regexp.MustCompile(messageNumPattern)
pathsToDelete := make([]string, 0)
if err := filepath.Walk(c.baseDir, func(path string, info os.FileInfo, err error) error {
if err := filepath.WalkDir(c.baseDir, func(path string, info os.DirEntry, err error) error {
if ctx.Err() != nil {
return ctx.Err()
}
if err != nil {
return err
}
Expand All @@ -201,6 +204,9 @@ func (c *Cache) Prune(ctx context.Context, messageNumber uint64) error {
// We delete separately from collecting the paths, as deleting while walking
// a dir can cause issues with the filepath.Walk function.
for _, path := range pathsToDelete {
if ctx.Err() != nil {
return ctx.Err()
}
if err := os.RemoveAll(path); err != nil {
return fmt.Errorf("could not prune directory with path %s: %w", path, err)
}
Expand Down
24 changes: 6 additions & 18 deletions staker/challenge-cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ import (
var _ HistoryCommitmentCacher = (*Cache)(nil)

func TestCache(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
basePath := t.TempDir()
if err := os.MkdirAll(basePath, os.ModePerm); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.RemoveAll(basePath); err != nil {
t.Fatal(err)
}
})
cache, err := New(basePath)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -75,16 +71,12 @@ func TestCache(t *testing.T) {
}

func TestPrune(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
basePath := t.TempDir()
if err := os.MkdirAll(basePath, os.ModePerm); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.RemoveAll(basePath); err != nil {
t.Fatal(err)
}
})
cache, err := New(basePath)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -429,17 +421,13 @@ func Test_determineFilePath(t *testing.T) {
}

func BenchmarkCache_Read_32Mb(b *testing.B) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
b.StopTimer()
basePath := os.TempDir()
if err := os.MkdirAll(basePath, os.ModePerm); err != nil {
b.Fatal(err)
}
b.Cleanup(func() {
if err := os.RemoveAll(basePath); err != nil {
b.Fatal(err)
}
})
cache, err := New(basePath)
if err != nil {
b.Fatal(err)
Expand Down

0 comments on commit 27298fe

Please sign in to comment.