From 5e164330b5aa8656029ca5a147876d239aaae456 Mon Sep 17 00:00:00 2001 From: Acha Bill Date: Wed, 31 Jan 2024 06:05:23 +0100 Subject: [PATCH] refactor: test --- .../internal/upload/uploadstore_test.go | 99 ++++++++++++------- 1 file changed, 63 insertions(+), 36 deletions(-) diff --git a/pkg/storer/internal/upload/uploadstore_test.go b/pkg/storer/internal/upload/uploadstore_test.go index 182e7188c1a..95ec4d26b39 100644 --- a/pkg/storer/internal/upload/uploadstore_test.go +++ b/pkg/storer/internal/upload/uploadstore_test.go @@ -574,45 +574,72 @@ func TestChunkPutter(t *testing.T) { t.Fatalf("Get(...): unexpected TagItem (-want +have):\n%s", diff) } - var uploadItemsCount int - err = ts.IndexStore().Iterate( - storage.Query{ - Factory: func() storage.Item { return &upload.UploadItem{} }, - }, - func(res storage.Result) (bool, error) { - pi := res.Entry.(*upload.UploadItem) - if pi.TagID == tag.TagID { - uploadItemsCount++ - } - return false, nil - }, - ) - if err != nil { - t.Fatalf("iterate upload items: %v", err) - } - if uploadItemsCount != 0 { - t.Fatalf("want %d upload items. got %d", 0, uploadItemsCount) - } + t.Run("cleanup upload items", func(t *testing.T) { + var uploadItemsCount int + err = ts.IndexStore().Iterate( + storage.Query{ + Factory: func() storage.Item { return &upload.UploadItem{} }, + }, + func(res storage.Result) (bool, error) { + pi := res.Entry.(*upload.UploadItem) + if pi.TagID == tag.TagID { + uploadItemsCount++ + } + return false, nil + }, + ) + if err != nil { + t.Fatalf("iterate upload items: %v", err) + } + if uploadItemsCount != 0 { + t.Fatalf("want %d upload items. got %d", 0, uploadItemsCount) + } - var pushItemsCount int - err = ts.IndexStore().Iterate( - storage.Query{ - Factory: func() storage.Item { return &upload.PushItem{} }, - }, - func(res storage.Result) (bool, error) { - pi := res.Entry.(*upload.PushItem) - if pi.TagID == tag.TagID { - pushItemsCount++ + }) + t.Run("cleanup push items", func(t *testing.T) { + var pushItemsCount int + err = ts.IndexStore().Iterate( + storage.Query{ + Factory: func() storage.Item { return &upload.PushItem{} }, + }, + func(res storage.Result) (bool, error) { + pi := res.Entry.(*upload.PushItem) + if pi.TagID == tag.TagID { + pushItemsCount++ + } + return false, nil + }, + ) + if err != nil { + t.Fatalf("iterate push items: %v", err) + } + if pushItemsCount != 0 { + t.Fatalf("want %d push items. got %d", 0, pushItemsCount) + } + }) + t.Run("cleanup chunks", func(t *testing.T) { + count := 0 + err := upload.IterateAll(ts.IndexStore(), func(addr swarm.Address, synced bool) (bool, error) { + count++ + if synced { + t.Fatal("expected synced to be false") + } + has, err := ts.ChunkStore().Has(context.Background(), addr) + if err != nil { + t.Fatalf("unexpected error in Has(...): %v", err) + } + if !has { + t.Fatalf("expected chunk to be present %s", addr.String()) } return false, nil - }, - ) - if err != nil { - t.Fatalf("iterate push items: %v", err) - } - if pushItemsCount != 0 { - t.Fatalf("want %d push items. got %d", 0, pushItemsCount) - } + }) + if err != nil { + t.Fatalf("IterateAll(...): unexpected error %v", err) + } + if count != 0 { + t.Fatalf("unexpected count: want 0 have %d", count) + } + }) }) t.Run("error after close", func(t *testing.T) {