Add test for issue #97

This commit is contained in:
Matt Joiner 2016-07-12 16:42:54 +10:00
parent fbe0ded844
commit 6e399e8f50
2 changed files with 39 additions and 9 deletions

26
issue97_test.go Normal file
View File

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

View File

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