From 898260c177f29553cec2e357fd82815584f06883 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 11 Oct 2020 12:40:43 +1100 Subject: [PATCH] Tidy up some storage close handling --- client_test.go | 5 ++--- storage/piece_resource.go | 14 +++++++++----- test/transfer_test.go | 8 +++++++- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/client_test.go b/client_test.go index 777c5962..dcb6d0be 100644 --- a/client_test.go +++ b/client_test.go @@ -151,7 +151,7 @@ func TestAddDropManyTorrents(t *testing.T) { } } -func fileCachePieceResourceStorage(fc *filecache.Cache) storage.ClientImplCloser { +func fileCachePieceResourceStorage(fc *filecache.Cache) storage.ClientImpl { return storage.NewResourcePieces(fc.AsResourceProvider()) } @@ -367,7 +367,7 @@ func writeTorrentData(ts *storage.Torrent, info metainfo.Info, b []byte) { } } -func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf func(*filecache.Cache) storage.ClientImplCloser) { +func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf func(*filecache.Cache) storage.ClientImpl) { fileCacheDir, err := ioutil.TempDir("", "") require.NoError(t, err) defer os.RemoveAll(fileCacheDir) @@ -376,7 +376,6 @@ func testAddTorrentPriorPieceCompletion(t *testing.T, alreadyCompleted bool, csf greetingDataTempDir, greetingMetainfo := testutil.GreetingTestTorrent() defer os.RemoveAll(greetingDataTempDir) filePieceStore := csf(fileCache) - defer filePieceStore.Close() info, err := greetingMetainfo.UnmarshalInfo() require.NoError(t, err) ih := greetingMetainfo.HashInfoBytes() diff --git a/storage/piece_resource.go b/storage/piece_resource.go index 1ef0e602..201d3f37 100644 --- a/storage/piece_resource.go +++ b/storage/piece_resource.go @@ -16,21 +16,25 @@ type piecePerResource struct { p resource.Provider } -func NewResourcePieces(p resource.Provider) ClientImplCloser { +func NewResourcePieces(p resource.Provider) ClientImpl { return &piecePerResource{ p: p, } } -func (s *piecePerResource) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) { - return s, nil +type piecePerResourceTorrentImpl struct { + piecePerResource } -func (s *piecePerResource) Close() error { +func (piecePerResourceTorrentImpl) Close() error { return nil } -func (s *piecePerResource) Piece(p metainfo.Piece) PieceImpl { +func (s piecePerResource) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) { + return piecePerResourceTorrentImpl{s}, nil +} + +func (s piecePerResource) Piece(p metainfo.Piece) PieceImpl { return piecePerResourcePiece{ mp: p, rp: s.p, diff --git a/test/transfer_test.go b/test/transfer_test.go index 6cbc5f89..f549f913 100644 --- a/test/transfer_test.go +++ b/test/transfer_test.go @@ -221,7 +221,13 @@ func TestClientTransferRateLimitedDownload(t *testing.T) { } func fileCachePieceResourceStorage(fc *filecache.Cache) storage.ClientImplCloser { - return storage.NewResourcePieces(fc.AsResourceProvider()) + return struct { + storage.ClientImpl + io.Closer + }{ + storage.NewResourcePieces(fc.AsResourceProvider()), + ioutil.NopCloser(nil), + } } func testClientTransferSmallCache(t *testing.T, setReadahead bool, readahead int64) {