From 1d30609b0eb1ca7c5200a5b51aec10b7dd695f43 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 27 Jun 2024 14:32:32 +1000 Subject: [PATCH] Make Torrent.Complete a method and make it return a read-only interface --- test/leecher-storage.go | 8 ++++---- tests/issue-930/server.go | 2 +- torrent.go | 9 +++++++-- torrent_test.go | 2 +- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/test/leecher-storage.go b/test/leecher-storage.go index a60f077a..8a5f9819 100644 --- a/test/leecher-storage.go +++ b/test/leecher-storage.go @@ -137,7 +137,7 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) { defer seeder.Close() // Adding a torrent and setting the info should trigger piece checks for everything // automatically. Wait until the seed Torrent agrees that everything is available. - <-seederTorrent.Complete.On() + <-seederTorrent.Complete().On() // Create leecher and a Torrent. leecherDataDir := t.TempDir() cfg = torrent.TestingConfig(t) @@ -174,7 +174,7 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) { return }()) require.NoError(t, err) - assert.False(t, leecherTorrent.Complete.Bool()) + assert.False(t, leecherTorrent.Complete().Bool()) assert.True(t, new) //// This was used when observing coalescing of piece state changes. @@ -210,9 +210,9 @@ func testClientTransfer(t *testing.T, ps testClientTransferParams) { go leecherTorrent.VerifyData() } if canComplete { - <-leecherTorrent.Complete.On() + <-leecherTorrent.Complete().On() } else { - <-leecherTorrent.Complete.Off() + <-leecherTorrent.Complete().Off() } assert.NotEmpty(t, seederTorrent.PeerConns()) leecherPeerConns := leecherTorrent.PeerConns() diff --git a/tests/issue-930/server.go b/tests/issue-930/server.go index 69473953..5507f53d 100644 --- a/tests/issue-930/server.go +++ b/tests/issue-930/server.go @@ -12,7 +12,7 @@ func server() { go func() { for range time.Tick(time.Second * 5) { for _, torrent := range client.Torrents() { - if torrent.Complete.Bool() { + if torrent.Complete().Bool() { fmt.Println("Dropping torrent", torrent.InfoHash().HexString()) torrent.Drop() } diff --git a/torrent.go b/torrent.go index 3dd785cf..440fc891 100644 --- a/torrent.go +++ b/torrent.go @@ -163,7 +163,7 @@ type Torrent struct { pex pexState // Is On when all pieces are complete. - Complete chansync.Flag + complete chansync.Flag // Torrent sources in use keyed by the source string. activeSources sync.Map @@ -2930,7 +2930,7 @@ func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex { } func (t *Torrent) updateComplete() { - t.Complete.SetBool(t.haveAllPieces()) + t.complete.SetBool(t.haveAllPieces()) } func (t *Torrent) cancelRequest(r RequestIndex) *Peer { @@ -3257,3 +3257,8 @@ file: } return } + +// Is On when all pieces are complete. +func (t *Torrent) Complete() chansync.ReadOnlyFlag { + return &t.complete +} diff --git a/torrent_test.go b/torrent_test.go index 15c2a4fc..ab3f4361 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -135,7 +135,7 @@ func testEmptyFilesAndZeroPieceLength(t *testing.T, cfg *ClientConfig) { defer tt.Drop() tt.DownloadAll() require.True(t, cl.WaitAll()) - assert.True(t, tt.Complete.Bool()) + assert.True(t, tt.Complete().Bool()) assert.True(t, missinggo.FilePathExists(fp)) } -- 2.48.1