adds a callback called before download starts (helps testing the sorting of the archives)

This commit is contained in:
Marcin Czenko 2025-10-23 02:01:49 +02:00
parent e703d06366
commit b1e38bc697
No known key found for this signature in database
GPG Key ID: A0449219BDBA98AE
2 changed files with 28 additions and 3 deletions

View File

@ -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)

View File

@ -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