From b1e38bc6973a3b856e611dcb747f780ac99fe198 Mon Sep 17 00:00:00 2001 From: Marcin Czenko Date: Thu, 23 Oct 2025 02:01:49 +0200 Subject: [PATCH] adds a callback called before download starts (helps testing the sorting of the archives) --- communities/codex_archive_downloader.go | 17 +++++++++++++++-- communities/codex_archive_downloader_test.go | 14 +++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/communities/codex_archive_downloader.go b/communities/codex_archive_downloader.go index ecf81a1..b8a593f 100644 --- a/communities/codex_archive_downloader.go +++ b/communities/codex_archive_downloader.go @@ -43,8 +43,9 @@ type CodexArchiveDownloader struct { cancelled bool pollingInterval time.Duration // configurable polling interval for HasCid checks - // Callback for signaling archive download completion - onArchiveDownloaded func(hash string, from, to uint64) + // Callbacks + onArchiveDownloaded func(hash string, from, to uint64) + onStartingArchiveDownload func(hash string, from, to uint64) } // NewCodexArchiveDownloader creates a new archive downloader @@ -75,6 +76,13 @@ func (d *CodexArchiveDownloader) SetOnArchiveDownloaded(callback func(hash strin d.onArchiveDownloaded = callback } +// SetOnStartingArchiveDownload sets a callback function to be called before starting an archive download +// This callback is called on the main thread before launching goroutines, making it useful for testing +// the deterministic order in which archives are processed (sorted newest first) +func (d *CodexArchiveDownloader) SetOnStartingArchiveDownload(callback func(hash string, from, to uint64)) { + d.onStartingArchiveDownload = callback +} + // GetTotalArchivesCount returns the total number of archives to download func (d *CodexArchiveDownloader) GetTotalArchivesCount() int { return d.totalArchivesCount @@ -205,6 +213,11 @@ func (d *CodexArchiveDownloader) downloadAllArchives() { d.archiveDownloadCancel[archive.hash] = archiveCancelChan d.mu.Unlock() + // Call callback before starting + if d.onStartingArchiveDownload != nil { + d.onStartingArchiveDownload(archive.hash, archive.from, archive.to) + } + // Trigger archive download and track progress in a goroutine go func(archiveHash, archiveCid string, archiveFrom, archiveTo uint64, archiveCancel chan struct{}) { err := d.triggerSingleArchiveDownload(archiveHash, archiveCid, archiveCancel) diff --git a/communities/codex_archive_downloader_test.go b/communities/codex_archive_downloader_test.go index 7d2e9e4..767938e 100644 --- a/communities/codex_archive_downloader_test.go +++ b/communities/codex_archive_downloader_test.go @@ -156,7 +156,13 @@ func (suite *CodexArchiveDownloaderTestifySuite) TestMultipleArchives() { downloader := communities.NewCodexArchiveDownloader(suite.mockClient, index, communityID, existingArchiveIDs, cancelChan) downloader.SetPollingInterval(10 * time.Millisecond) - // Track completed archives + // Track the order in which archives are started (deterministic) + var startOrder []string + downloader.SetOnStartingArchiveDownload(func(hash string, from, to uint64) { + startOrder = append(startOrder, hash) + }) + + // Track completed archives (non-deterministic due to concurrency) completedArchives := make(map[string]bool) downloader.SetOnArchiveDownloaded(func(hash string, from, to uint64) { completedArchives[hash] = true @@ -185,8 +191,14 @@ func (suite *CodexArchiveDownloaderTestifySuite) TestMultipleArchives() { assert.Contains(suite.T(), completedArchives, "archive-2", "Should have completed archive-2") assert.Contains(suite.T(), completedArchives, "archive-3", "Should have completed archive-3") + // Verify sorting: archives should be started in most-recent-first order (deterministic) + // This tests the internal sorting logic before concurrency begins + expectedStartOrder := []string{"archive-3", "archive-2", "archive-1"} + assert.Equal(suite.T(), expectedStartOrder, startOrder, "Archives should be started in most-recent-first order") + suite.T().Log("✅ Multiple archives test passed") suite.T().Logf(" - Completed %d out of %d archives", len(completedArchives), 3) + suite.T().Logf(" - Start order (sorted): %v", startOrder) } // Run the test suite