From 6e399e8f507669ffc8f16a596320e73ab634055d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 12 Jul 2016 16:42:54 +1000 Subject: [PATCH] Add test for issue #97 --- issue97_test.go | 26 ++++++++++++++++++++++++++ torrent.go | 22 +++++++++++++--------- 2 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 issue97_test.go diff --git a/issue97_test.go b/issue97_test.go new file mode 100644 index 00000000..3f4e16d2 --- /dev/null +++ b/issue97_test.go @@ -0,0 +1,26 @@ +package torrent + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/anacrolix/torrent/internal/testutil" + "github.com/anacrolix/torrent/storage" +) + +func TestHashPieceAfterStorageClosed(t *testing.T) { + td, err := ioutil.TempDir("", "") + require.NoError(t, err) + defer os.RemoveAll(td) + cs := storage.NewFile(td) + tt := &Torrent{} + tt.info = &testutil.GreetingMetaInfo().Info + tt.makePieces() + tt.storage, err = cs.OpenTorrent(tt.info) + require.NoError(t, err) + require.NoError(t, tt.storage.Close()) + tt.hashPiece(0) +} diff --git a/torrent.go b/torrent.go index cb17f49b..216ebbc1 100644 --- a/torrent.go +++ b/torrent.go @@ -205,6 +205,18 @@ func infoPieceHashes(info *metainfo.Info) (ret []string) { return } +func (t *Torrent) makePieces() { + hashes := infoPieceHashes(&t.info.Info) + t.pieces = make([]piece, len(hashes)) + for i, hash := range hashes { + piece := &t.pieces[i] + piece.t = t + piece.index = i + piece.noPendingWrites.L = &piece.pendingWritesMutex + missinggo.CopyExact(piece.Hash[:], hash) + } +} + // Called when metadata for a torrent becomes available. func (t *Torrent) setInfoBytes(b []byte) error { if t.haveInfo() { @@ -237,15 +249,7 @@ func (t *Torrent) setInfoBytes(b []byte) error { } t.metadataBytes = b t.metadataCompletedChunks = nil - hashes := infoPieceHashes(&t.info.Info) - t.pieces = make([]piece, len(hashes)) - for i, hash := range hashes { - piece := &t.pieces[i] - piece.t = t - piece.index = i - piece.noPendingWrites.L = &piece.pendingWritesMutex - missinggo.CopyExact(piece.Hash[:], hash) - } + t.makePieces() for _, conn := range t.conns { if err := conn.setNumPieces(t.numPieces()); err != nil { log.Printf("closing connection: %s", err)