From 045d42cb1f49dc1a45c83d8b1a5e6e739f41d4ad Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 9 Apr 2025 12:24:45 +1000 Subject: [PATCH] Switch client_test.go to go-quicktest/qt and add errTorrentClosed --- client_test.go | 24 ++++++++++-------------- reader.go | 2 +- torrent.go | 4 +++- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/client_test.go b/client_test.go index 49bd8102..c71a8c43 100644 --- a/client_test.go +++ b/client_test.go @@ -19,8 +19,7 @@ import ( "github.com/anacrolix/log" "github.com/anacrolix/missinggo/v2" "github.com/anacrolix/missinggo/v2/filecache" - "github.com/frankban/quicktest" - qt "github.com/frankban/quicktest" + "github.com/go-quicktest/qt" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -195,7 +194,7 @@ func TestCompletedPieceWrongSize(t *testing.T) { assert.True(t, new) r := tt.NewReader() defer r.Close() - qt.Check(t, iotest.TestReader(r, []byte(testutil.GreetingFileContents)), qt.IsNil) + qt.Check(t, qt.IsNil(iotest.TestReader(r, []byte(testutil.GreetingFileContents)))) } func BenchmarkAddLargeTorrent(b *testing.B) { @@ -337,12 +336,11 @@ func TestTorrentDroppedDuringResponsiveRead(t *testing.T) { require.NoError(t, err) leecherTorrent.Drop() n, err := reader.Read(b) - assert.EqualError(t, err, "torrent closed") + qt.Assert(t, qt.Equals(err, errTorrentClosed)) assert.EqualValues(t, 0, n) } func TestDhtInheritBlocklist(t *testing.T) { - c := qt.New(t) ipl := iplist.New(nil) require.NotNil(t, ipl) cfg := TestingConfig(t) @@ -357,7 +355,7 @@ func TestDhtInheritBlocklist(t *testing.T) { assert.Equal(t, ipl, s.(AnacrolixDhtServerWrapper).Server.IPBlocklist()) numServers++ }) - c.Assert(numServers, qt.Not(qt.Equals), 0) + qt.Assert(t, qt.Not(qt.Equals(numServers, 0))) } // Check that stuff is merged in subsequent AddTorrentSpec for the same @@ -441,7 +439,7 @@ func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf assert.Equal(t, alreadyCompleted, psrs[0].Complete) if alreadyCompleted { r := tt.NewReader() - quicktest.Check(t, iotest.TestReader(r, []byte(testutil.GreetingFileContents)), quicktest.IsNil) + qt.Check(t, qt.IsNil(iotest.TestReader(r, []byte(testutil.GreetingFileContents)))) } } @@ -590,7 +588,6 @@ func TestPeerInvalidHave(t *testing.T) { } func TestPieceCompletedInStorageButNotClient(t *testing.T) { - c := qt.New(t) greetingTempDir, greetingMetainfo := testutil.GreetingTestTorrent() defer os.RemoveAll(greetingTempDir) cfg := TestingConfig(t) @@ -602,8 +599,8 @@ func TestPieceCompletedInStorageButNotClient(t *testing.T) { InfoBytes: greetingMetainfo.InfoBytes, InfoHash: greetingMetainfo.HashInfoBytes(), }) - c.Check(err, qt.IsNil) - c.Check(new, qt.IsTrue) + qt.Check(t, qt.IsNil(err)) + qt.Check(t, qt.IsTrue(new)) } // Check that when the listen port is 0, all the protocols listened on have @@ -907,11 +904,10 @@ func TestBadPeerIpPort(t *testing.T) { func TestClientConfigSetHandlerNotIgnored(t *testing.T) { cfg := TestingConfig(t) cfg.Logger.SetHandlers(log.DiscardHandler) - c := qt.New(t) cl, err := NewClient(cfg) - c.Assert(err, qt.IsNil) + qt.Assert(t, qt.IsNil(err)) defer cl.Close() - c.Assert(cl.logger.Handlers, qt.HasLen, 1) + qt.Assert(t, qt.HasLen(cl.logger.Handlers, 1)) h := cl.logger.Handlers[0].(log.StreamHandler) - c.Check(h.W, qt.Equals, io.Discard) + qt.Check(t, qt.Equals(h.W, io.Discard)) } diff --git a/reader.go b/reader.go index 039fd99c..31d4682c 100644 --- a/reader.go +++ b/reader.go @@ -202,7 +202,7 @@ func (r *reader) waitAvailable(ctx context.Context, pos, wanted int64, wait bool } select { case <-r.t.closed.Done(): - err = errors.New("torrent closed") + err = errTorrentClosed return case <-ctx.Done(): err = ctx.Err() diff --git a/torrent.go b/torrent.go index 8f7e41fa..3154662d 100644 --- a/torrent.go +++ b/torrent.go @@ -52,6 +52,8 @@ import ( "github.com/anacrolix/torrent/webtorrent" ) +var errTorrentClosed = errors.New("torrent closed") + // Maintains state of torrent within a Client. Many methods should not be called before the info is // available, see .Info and .GotInfo. type Torrent struct { @@ -2260,7 +2262,7 @@ func (t *Torrent) addPeerConn(c *PeerConn) (err error) { } }() if t.closed.IsSet() { - return errors.New("torrent closed") + return errTorrentClosed } for c0 := range t.conns { if c.PeerID != c0.PeerID { -- 2.48.1